<script setup lang="ts" name="sampleDialog">
import { computed, getCurrentInstance, ref } from 'vue'
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ICustomer, ICustomerQuery } from '@/views/customer/customerInfo/customer_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getCustomerList } from '@/api/customer/customer'
import { getDictByCode } from '@/api/system/dict'
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['changeVisible', 'confirmCheckout'])

const visibleDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    emits('changeVisible', val)
  },
})

// 查询条件
const listQuery: Ref<ICustomerQuery> = ref({
  bussinessSize: '', // 业务规模
  customerName: '', // 公司名称
  customerNo: '', // 客户编号
  grade: '', // 履约评级
  offset: 1,
  limit: 20,
})
interface dictType {
  id: string
  name: string
  value: string
}
// const companySizeList = ref<dictType[]>([]) // 公司规模列表
const businessSizeList = ref<dictType[]>([]) // 业务规模列表
const gradeList = ref<dictType[]>([]) // 履约评级列表
// const evaluationList = ref<dictType[]>([]) // 整体评价列表
function getDict() {
  // // 获取公司规模
  // getDictByCode('companySize').then((response) => {
  //   companySizeList.value = response.data
  // })
  // 获取业务规模
  getDictByCode('businessSize').then((response) => {
    businessSizeList.value = response.data
  })
  // 获取履约评级
  getDictByCode('grade').then((response) => {
    gradeList.value = response.data
  })
  // // 获取整体评价
  // getDictByCode('evaluation').then((response) => {
  //   evaluationList.value = response.data
  // })
}
getDict()
// 表头
const columns = ref<TableColumn[]>([
  { text: '客户编号', value: 'customerNo', width: '160', align: 'center' },
  { text: '客户名称', value: 'customerName', width: '120', align: 'center' },
  { text: '公司规模', value: 'companySizeName', align: 'center' },
  { text: '业务规模', value: 'businessSizeName', align: 'center' },
  { text: '履约评级', value: 'gradeName', align: 'center' },
  { text: '整体评价', value: 'evaluationName', align: 'center' },
  { text: '公司地址', value: 'briefName', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180px' },
  { text: '备注', value: 'remark', align: 'center' },
])
// 表格数据
const list = ref<ICustomer[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<string[]>([])

// 数据查询
function fetchData(isNowPage = false) {
  // loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  // getCustomerList(listQuery.value).then((response) => {
  //   // 模拟数据
  //   response.data.rows = [{ bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '京东集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' }]
  //   list.value = response.data.rows.map((item: ICustomer) => {
  //     if (item.companyProvinceName && item.companyCityName) {
  //       item.briefName = `${item.companyProvinceName}/${item.companyCityName}`
  //     }
  //     else {
  //       item.briefName = item.companyProvinceName || item.companyCityName
  //     }
  //     return item
  //   })
  //   total.value = parseInt(response.data.total)
  //   loadingTable.value = false
  // })

  // 模拟数据
  const response = [{ bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '京东集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' },
    { bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '2集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' },
    { bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '3集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' }]
  list.value = response.map((item: ICustomer) => {
    if (item.companyProvinceName && item.companyCityName) {
      item.briefName = `${item.companyProvinceName}/${item.companyCityName}`
    }
    else {
      item.briefName = item.companyProvinceName || item.companyCityName
    }
    return item
  })
  total.value = 50
  loadingTable.value = false
}
// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    bussinessSize: '', // 业务规模
    customerName: '', // 公司名称
    customerNo: '', // 客户编号
    grade: '', // 履约评级
    offset: 1,
    limit: 20,
  }
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData(true)
}

// 点击取消
const cancle = () => {
  emits('changeVisible', false)
}
// 点击确定
const clickConfirm = () => {
  console.log(checkoutList.value)
  if (!checkoutList.value.length) {
    ElMessage({
      message: '请选中',
      type: 'warning',
    })
  }
  emits('confirmCheckout', checkoutList.value)
  cancle()
}

fetchData(true)
</script>

<template>
  <div class="select-kehu-dialog">
    <el-dialog v-model="visible" title="选择委托方" @close="cancle">
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input
            v-model.trim="listQuery.customerNo"
            placeholder="客户编号"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.customerName"
            placeholder="公司名称"
            clearable
          />
        </search-item>
        <search-item>
          <el-select v-model="listQuery.bussinessSize" placeholder="业务规模">
            <el-option v-for="item in businessSizeList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-select v-model="listQuery.grade" placeholder="履约评级">
            <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
      </search-area>
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :list-loading="loadingTable"
        is-showmulti-select
        :is-multi="false"
        @change="changePage"
        @multiSelect="handleSelectionChange"
      />
      <template #footer>
        <el-button type="primary" @click="clickConfirm">
          确定
        </el-button>
        <el-button @click="cancle">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
