<!-- 选择分包方编号 -->
<script lang="ts" setup name="SelectOutsourcerNoDialog">
import { ElMessage } from 'element-plus'
import type { Ref } from 'vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import type { DateModelType } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { SCHEDULE } from '@/utils/scheduleDict'
import { getDirectoriesList } from '@/api/business/subpackage/directories'
const emits = defineEmits(['confirm'])
const dialogFormVisible = ref(false) // 控制对话框显隐
// 查询条件
const listQuery = ref({
  approvalStatus: '0', // 审批状态 0全部
  director: '', // 机构负责人
  outsourcerName: '', // 单位名称
  outsourcerNo: '', // 分包方编号
  formId: SCHEDULE.SUBPACKAGE_DIRECTORIES_APPROVAL, // 表单id
  createTimeStart: '', // 创建时间(开始)
  createTimeEnd: '', // 创建时间(结束)
  offset: 1,
  limit: 5,
})
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const checkoutList = ref([]) // 多选选中的内容
const loadingTable = ref(false) // 表格loading
const list = ref([]) as any // 表格数据
const total = ref(0) // 数组总数
const columns = ref<TableColumn[]>([
  { text: '分包方编号', value: 'outsourcerNo', align: 'center', width: '180' },
  { text: '单位名称', value: 'outsourcerName', align: 'center' },
  { text: '机构负责人', value: 'director', align: 'center' },
  { text: '联系电话', value: 'phone', align: 'center' },
  { text: '测试能力', value: 'businessScope', align: 'center' },
  { text: '地址', value: 'fullAddress', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180' },
])

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getDirectoriesList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: { estimateSignDate: string; agreementAmount: number; measureValidDate: string }) => {
      return {
        ...item,
        measureValidDate: item.measureValidDate ? dayjs(item.measureValidDate).format('YYYY-MM-DD') : item.measureValidDate,
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
// 重置
const clearList = () => {
  listQuery.value = {
    approvalStatus: '0', // 审批状态 0全部
    director: '', // 机构负责人
    outsourcerName: '', // 单位名称
    outsourcerNo: '', // 分包方编号
    formId: SCHEDULE.SUBPACKAGE_DIRECTORIES_APPROVAL, // 表单id
    createTimeStart: '', // 创建时间(开始)
    createTimeEnd: '', // 创建时间(结束)
    offset: 1,
    limit: 5,
  }
  dateRange.value = ['', '']
  fetchData()
}
// 搜索
const search = () => {
  fetchData(true)
}
// 多选选中
const handleSelectionChange = (val: any) => {
  checkoutList.value = val
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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
  }
  search()
}
// 点击确定
const confirmSelect = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选择分包方')
  }
  else {
    emits('confirm', checkoutList.value)
    dialogFormVisible.value = false
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  clearList()
}
/**
 * 初始化
 * @param isMultiParam 是否多选
 */
const initDialog = () => {
  dialogFormVisible.value = true
  fetchData()
}
// ----------------------------------------------钩子------------------------------------------------------
watch(dateRange, (val) => {
  if (val) {
    listQuery.value.createTimeStart = `${val[0]}`
    listQuery.value.createTimeEnd = `${val[1]}`
  }
  else {
    listQuery.value.createTimeStart = ''
    listQuery.value.createTimeEnd = ''
  }
})
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择委托方" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <search-item>
        <el-input v-model.trim="listQuery.outsourcerNo" placeholder="分包方编号" class="short-input" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.outsourcerName" placeholder="单位名称" class="short-input" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.director" placeholder="机构负责人" class="short-input" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="dateRange"
          class="short-input"
          type="datetimerange"
          range-separator="至"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="创建时间(开始)"
          end-placeholder="创建时间(结束)"
        />
      </search-item>
    </search-area>

    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        is-showmulti-select
        :list-loading="loadingTable"
        :is-multi="false"
        :height="260"
        :page-sizes="[5]"
        @change="changePage"
        @multi-select="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>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>
