<template>
  <div class="app-container">
    <addressdny @getAdress="getAdress" ref="address"></addressdny>
    <div class="filter-container">

      <el-input size="mini" @input="searchData()" v-model="search.keyWord" placeholder="关键字" style="width: 200px;"
                class="filter-item" @keyup.enter.native="searchData"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        size="mini"
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="$router.push({path:'/doc/createCustomer'})">
        增加客户
      </el-button>
      <el-checkbox v-if="!isChoose" size="mini" @change="checkDisable" v-model="disableLock" label="已禁用"
                   border></el-checkbox>
      <el-button type="primary" size="mini" icon="el-icon-refresh" @click="getTableInfo">刷新</el-button>
    </div>
    <el-table
      size="mini"
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      tooltip-effect="light"
      :size="!webType || isChoose ? 'mini':''"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" :width="webType&&!isChoose?'150':'100'"
                       class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
            <template v-if="!isChoose">
              <el-button
                @click="$router.push({path:'/doc/editCustomer/'+row.id,query:{id:row.id,clientName:row.clientName}})"
                type="primary" size="mini">
                编辑
              </el-button>
              <el-button @click="editStatus(row.id,'0')" type="danger" v-if="row.enable==1" size="mini">
                禁用
              </el-button>
              <el-button @click="editStatus(row.id,'1')" v-else-if="row.enable==0" size="mini">
                启用
              </el-button>
<!--              <el-button size="mini" @click="carEditOrderAddr(row.id,row.clientName)" type="primary">配置收货地址-->
<!--              </el-button>-->
            </template>
            <template v-else>
              <el-button @click="sourceType == 1? chooseThisTo(row):chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </template>
      </el-table-column>
      <template v-if="!isChoose || !webType">
        <el-table-column align="center" label="状态" class-name="status-col" width="100">
          <template slot-scope="{row}">
            <el-tag v-if="row.enable==1" type="success">
              启用
            </el-tag>
            <el-tag v-else-if="row.enable==0" type="danger">
              禁用
            </el-tag>
          </template>
        </el-table-column>
      </template>
      <el-table-column
        width="150px"
        prop="clientName"
        show-overflow-tooltip
        label="客户名称">
        <template slot-scope="{row}">
          <span class="ellipsis"> {{ row.clientName }}</span>
        </template>
      </el-table-column>
      <template>
        <el-table-column
          width="90px"
          prop="contactMan"
          show-overflow-tooltip
          label="联系人">
          <template slot-scope="{row}">
            <span class="ellipsis"> {{ row.contactMan }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="150"
          prop="contactPhone"
          label="联系电话">
        </el-table-column>
        <el-table-column
          align="left"
          class-name="status-col"
          width="388px"
          show-overflow-tooltip
          label="联系地址">
          <template slot-scope="{row}">
            <span class="ellipsis"><span v-if="row.province">{{ row.province }}</span><span
              v-if="row.city">{{ row.city }}</span><span v-if="row.area">{{ row.area }}</span><span v-if="row.address">{{ row.address }}</span></span>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <pagination
      v-show="listData.total>0"
      :total="listData.total"
      :page.sync="listData.page"
      :limit.sync="listData.pageSize"
      @pagination="getTableInfo"/>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>

  </div>
</template>

<script>
import { getInfo } from '@/api/doc/client/client.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import Addressdny from '@/components/Dialog/Addressdny.vue'
import 'vue-area-linkage/dist/index.css'
import { pca, pcaa } from 'area-data'
import { CodeToText, regionData } from 'element-china-area-data'

export default {
  name: 'ClientList',
  components: {
    Cancledialog, Pagination, Addressdny
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      invoicePhone: '',
      invoiceAddress: '',
      bankName: '',
      taxNum: '',
      billId: '',
      billTitle: '新增发票抬头',
      dialogStatusBill: 1,//1增加，2修改
      invoiceTitle: '',
      innerVisible: false,
      idClientRow: '',
      gridData: [],//发票抬头列表
      billDialog: false,
      selectedOptions: [],
      options: regionData, // 地址地区信息
      webType: true,
      cityshow: false,
      selected: [], // 省市区
      pca: pca,
      pcaa: pcaa,
      byId: '',
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      disableLock: false,
      search: {
        keyWord: ''
      },
      ruleForm: {},
      paytypeList: [],
      industryList: [],
      levelList: [],
      rules: {
        industryName: [
          { required: true, message: '请选择行业信息', trigger: 'change' }
        ],
        levelName: [
          { required: true, message: '请选择客户等级', trigger: 'change' }
        ],
        payMode: [
          { required: true, message: '请选择付款方式', trigger: 'change' }
        ],
        clientName: [
          { required: true, message: '请填写客户名称', trigger: 'blur' }
        ],
        contactMan: [
          { required: true, message: '请填写联系人', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请填写联系电话', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      listData1: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: ''
    }
  },
  mounted() {
    this.getTableInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {

    handleChange(value) {
      this.adrValue = CodeToText[value[0]] + '/' + CodeToText[value[1]] + '/' + CodeToText[value[2]]
      // console.log('adrValue', this.adrValue)
    },
    iscityShow() {
      this.cityshow = false
      this.selected = []
      var t = setTimeout(() => {
        this.cityshow = true
      }, 0)
    },
    checkDisable(val) {
      // console.log(val);
      this.listData.page = 1;
      this.getTableInfo()
    },

    // 获取列表
    getTableInfo() {
      const _that = this
      _that.listLoading = true

      if (_that.disableLock) {
        _that.search.enable = 0;
      } else {
        _that.search.enable = 1;
      }

      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }

      getInfo(data).then(response => {
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },

    // 启用禁用
    editStatus(id, status) {
      var _that = this
      var data = {
        id: id,
        enable: status
      }
      var params = {
        'clientInfo': JSON.stringify(data)
      }

      var url = '/doc/enableClientInfo'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    // 刷新表格
    tableRefresh() {
      // 刷新变革
      this.getTableInfo()
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getTableInfo()
    },
    carEditOrderAddr(id, name) {
      this.$refs.address.outerVisible = true;
      this.$refs.address.idClient = id;
      this.$refs.address.clientName = name;
      var pageNum = 1;
      this.$refs.address.getInfo(pageNum, '');
      this.$refs.address.searchVal = '';
    },
    getAdress(adr) {
      // console.log(adr);
      // this.defaultAddress = adr;
      // // console.log(this.defaultAddress);
      // this.$forceUpdate();//重绘
    },
    indexMethod(index) {
      return index + 1;
    },

    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('chooseClient', row)
    },
    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.clientName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      row.addressList = []
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>
<style>
.area-select .area-selected-trigger {
  padding: 0px 20px 7px 12px !important;
}

.area-select {
  height: 40px !important;
}
</style>

<style lang="scss" scoped>
.area-select {
  height: 40px;
}

.filter-container {
  margin-bottom: 15px;
}

</style>
