<template>
  <el-dialog append-to-body :title="title" :visible.sync="innerVisible">
    <el-row>
      <el-col :span="24">
        <div style="float: right">
          <el-button size="mini" type="primary" @click="handleAdd">新建联系人</el-button>
          <el-dropdown style="margin-left: 10px" trigger="click">
            <el-button icon="el-icon-more" size="mini" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-upload2">导入</el-dropdown-item>
              <el-dropdown-item icon="el-icon-download">导出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-input
          v-model="queryForm.keyword"
          clearable
          placeholder="客户名称/手机/电话"
          style="width: 30%; margin-right: 10px"
          suffix-icon="el-icon-search" />
        <!--        <span>显示：</span>-->
        <!--        <el-radio-group v-model="queryForm.type">-->
        <!--          <el-radio-button label="全部客户" />-->
        <!--          <el-radio-button label="我负责的客户" />-->
        <!--        </el-radio-group>-->
        <table-tool :check-list.sync="checkList" :columns="columns" style="float: right" />
      </el-col>
    </el-row>
    <el-table ref="contactTable" v-loading="listLoading" :data="list" @selection-change="setSelectRows">
      <el-table-column align="center" type="selection" />
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <span v-if="item.prop === 'custStatus'">
            {{ row.custStatus == 10 ? '正常' : '异常' }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <span slot="footer">
      <el-button size="mini" type="primary" @click="save">保存</el-button>
      <el-button size="mini" @click="innerVisible = false">取消</el-button>
    </span>
    <!-- 新建联系人弹窗 -->
    <customer-contact ref="contact" @contactSave="fetchData" />
  </el-dialog>
</template>

<script>
  import customerApi from '@/api/customer/index'
  import TableTool from '@/components/table/TableTool'
  import CustomerContact from '@/views/customer/components/Contact'

  export default {
    name: 'SelectContact',
    components: {
      TableTool,
      CustomerContact,
    },
    props: {
      title: {
        type: String,
        default: '选择客户联系人',
      },
      multiple: Boolean,
      // 示例{ custId: id, custName: custName}
      defaultCustomer: {
        type: Object,
        default() {
          return {}
        },
      },
      queryParams: {
        type: Object,
        default() {
          return {}
        },
      },
    },
    data() {
      return {
        innerVisible: false,
        queryForm: {
          keyword: '',
          type: '全部客户',
          pageNum: 1,
          pageSize: 10,
        },
        checkList: [],
        columns: [
          {
            label: '联系人姓名',
            width: 'auto',
            prop: 'cuctName',
            // sortable: true,
            disableCheck: true,
          },
          {
            label: '客户名称',
            width: 'auto',
            prop: 'custName',
          },
          {
            label: '手机号码',
            width: 'auto',
            prop: 'telephone',
          },
          {
            label: '微信',
            width: 'auto',
            prop: 'wechat',
          },
          {
            label: '邮箱',
            width: 'auto',
            prop: 'email',
          },
          {
            label: '职务',
            width: 'auto',
            prop: 'postion',
          },
          {
            label: '关键决策人',
            width: 'auto',
            prop: 'policy',
          },
          {
            label: '性别',
            width: 'auto',
            prop: 'cuctGender',
            // sortable: true,
          },
        ],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    mounted() {},
    methods: {
      open() {
        this.innerVisible = true
        this.fetchData()
      },
      save() {
        this.innerVisible = false
        console.log(this.selectRows)
        this.$emit('save', this.selectRows)
      },
      handleAdd() {
        this.$refs.contact.contactForm.custId = this.defaultCustomer.custId
        this.$refs.contact.contactForm.custName = this.defaultCustomer.custName
        this.$refs.contact.contactVisible = true
      },
      async fetchData() {
        this.listLoading = true
        let query = Object.assign(this.queryForm, this.queryParams)
        const {
          data: { list, total },
        } = await customerApi.getContact(query)
        this.list = list
        this.total = total
        this.listLoading = false
      },
      setSelectRows(val) {
        if (!this.multiple && val.length === this.list.length && val.length > 1) {
          // 返回单条数据情况下-控制全选情况下单选第一条数据
          this.$refs.contactTable.clearSelection()
          if (this.selectRows.length === 1) {
            return
          }
          this.$refs.contactTable.toggleRowSelection(val.shift(), true)
        } else if (!this.multiple && val.length > 1) {
          // 返回单条数据情况下-控制选择当前点击数据
          this.$refs.contactTable.clearSelection()
          this.$refs.contactTable.toggleRowSelection(val.pop(), true)
        } else {
          this.selectRows = val
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
    },
  }
</script>

<style scoped></style>
