<script setup lang="ts" name="SelectCustomer">
import { ref } from 'vue'
import type { Ref } from 'vue'
import { ElMessage } 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'

defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['changeVisible', 'confirmCheckout'])

// 查询条件
const listQuery: Ref<ICustomerQuery> = ref({
  businessSize: '', // 业务规模
  customerName: '', // 公司名称
  customerNo: '', // 客户编号
  grade: '', // 履约评级
  offset: 1,
  limit: 20,
})
interface dictType {
  id: string
  name: string
  value: string
}

const businessSizeList = ref<dictType[]>([]) // 业务规模列表
const gradeList = ref<dictType[]>([]) // 履约评级列表
// 表头
const columns = ref<TableColumn[]>([
  { text: '客户编号', value: 'customerNo', width: '160', align: 'center', fixed: true },
  { text: '客户名称', value: 'customerName', align: 'center', fixed: true },
  { text: '公司规模', value: 'companySizeName', width: '110', align: 'center' },
  { text: '业务规模', value: 'businessSizeName', width: '130', align: 'center' },
  { text: '履约评级', value: 'gradeName', width: '90', align: 'center' },
  { text: '整体评价', value: 'evaluationName', width: '110', align: 'center' },
  { text: '公司地址', value: 'fullAddress', 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 getDict() {
  // 获取业务规模
  getDictByCode('businessSize').then((response) => {
    businessSizeList.value = response.data
  })
  // 获取履约评级
  getDictByCode('grade').then((response) => {
    gradeList.value = response.data
  })
}
getDict()

// 表格数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getCustomerList(listQuery.value).then((response) => {
    // 模拟数据
    list.value = response.data.rows.map((item: ICustomer) => {
      if (item.addressProvinceName && item.addressCityName) {
        item.fullAddress = `${item.addressProvinceName}/${item.addressCityName}`
      }
      else if (item.addressProvinceName || item.addressCityName) {
        item.fullAddress = item.addressProvinceName || item.addressCityName
      }
      else if (item.addressCountryName) {
        item.fullAddress = item.addressCountryName
      }
      return item
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    businessSize: '', // 业务规模
    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 = () => {
  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.businessSize" 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 #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
      <template #footer>
        <el-button type="primary" @click="clickConfirm">
          确定
        </el-button>
        <el-button @click="cancle">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
