<!-- 选择现场检测编号 -->
<script lang="ts" setup name="SelectApproveList">
import { ElMessage } from 'element-plus'
import type { 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 { getFieldTestApprovalList } from '@/api/business/fieldTest/approve'
import type { IListQuery } from '@/views/business/fieldTest/approve/approve-interface'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  approvalStatus: '4', // 审批状态 4已通过
  customerName: '', // 委托方名称
  applyName: '', // 负责人
  formId: SCHEDULE.FIELD_TEST_APPROVAL, // 表单id
  siteExecutiveNo: '', // 现场检测编号
  applyTimeEnd: '', // 申请时间结束
  applyTimeStart: '', // 申请时间开始
  limit: 20,
  offset: 1,
})
// 多选选中的内容
const checkoutList = ref([])
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const columns = ref<TableColumn[]>([
  { text: '审批编号', value: 'siteExecutiveNo', align: 'center', width: '160' },
  { text: '审批名称', value: 'siteExecutiveName', align: 'center' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '申请人', value: 'applyName', align: 'center' },
  { text: '申请时间', value: 'applyTime', align: 'center', width: '180' },
])
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  // 模拟数据
  getFieldTestApprovalList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 重置
const reset = () => {
  listQuery.value = {
    approvalStatus: '4', // 审批状态 4已通过
    customerName: '', // 委托方名称
    applyName: '', // 负责人
    formId: SCHEDULE.FIELD_TEST_APPROVAL, // 表单id
    siteExecutiveNo: '', // 现场检测编号
    applyTimeEnd: '', // 申请时间结束
    applyTimeStart: '', // 申请时间开始
    limit: 20,
    offset: 1,
  }
  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('add', checkoutList.value)
    dialogFormVisible.value = false
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  fetchData()
}

watch(dateRange, (val) => {
  if (val) {
    listQuery.value.applyTimeStart = `${val[0]}`
    listQuery.value.applyTimeEnd = `${val[1]}`
  }
  else {
    listQuery.value.applyTimeStart = ''
    listQuery.value.applyTimeEnd = ''
  }
})

defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择现场测试、校准或检定审批单编号" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.siteExecutiveNo" placeholder="审批编号" class="short-input" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.customerName" placeholder="委托方名称" class="short-input" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.applyName" 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="[20]"
        @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>
