<!-- 选择计量数据 -->
<script lang="ts" setup name="EnvironmentalDialog">
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import type { IEnvironmental, IListQuery, IRoomNumber } from '@/views/business/lab/environmental/environmentalList_interface'
import { getEnvironmentRecordList } from '@/api/business/lab/environmentRecord'
import { getTypeSelect } from '@/api/system/price'
import { SCHEDULE } from '@/utils/scheduleDict'
import { getReportList } from '@/api/business/lab/report'

const emits = defineEmits(['confirm'])
const dialogFormVisible = ref(false) // 控制弹窗显隐
// 查询参数
const listQuery = ref({
  approvalStatus: '4', // 审批状态 4已通过
  certificateReportCode: '', //		证书编号
  certificateReportName: '', //		证书名称
  customerName: '', //		委托方名称
  formId: SCHEDULE.BUSINESS_REPORT_ON_CREDENTIALS,
  measureCategory: '', //	检校类别
  orderCode: '', //	委托单编号
  sampleName: '', //	样品名称
  sampleNo: '', //	样品编号
  offset: 1, // 当前页
  limit: 5, // 每页多少条
})
const loadingTable = ref(false) // loading
const list = ref([]) // 表格参数
const checkoutList = ref([]) // 多选表格
const total = ref(0)
const columns = ref<TableColumn[]>([
  { text: '证书编号', value: 'certificateReportCode', width: '160', align: 'center' },
  { text: '证书名称', value: 'certificateReportName', align: 'center' },
  { text: '样品编号', value: 'sampleNo', align: 'center', width: '160' },
  { text: '样品名称', value: 'sampleName', align: 'center' },
  { text: '型号', value: 'sampleModel', align: 'center' },
  { text: '委托单编号', value: 'orderCode', align: 'center', width: '160' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '检定员', value: 'measurePersonName', align: 'center' },
  { text: '证书附件', value: 'certificateReportFile', align: 'center', width: '220' },
  { text: '检校日期', value: 'calibrationTime', align: 'center', width: '120' },
])

// 查询列表
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getReportList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: { calibrationTime: string; orderCode: string; certificateReportFile: string }) => {
      return {
        ...item,
        calibrationTime: item.calibrationTime ? dayjs(item.calibrationTime).format('YYYY-MM-DD') : item.calibrationTime,
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
fetchData(true)

// 搜索
const search = () => {
  fetchData(true)
}
// 重置
const clearList = () => {
  listQuery.value = {
    approvalStatus: '4', // 审批状态 4已通过
    certificateReportCode: '', //		证书编号
    certificateReportName: '', //		证书名称
    customerName: '', //		委托方名称
    formId: SCHEDULE.BUSINESS_REPORT_ON_CREDENTIALS,
    measureCategory: '', //	检校类别
    orderCode: '', //	委托单编号
    sampleName: '', //	样品名称
    sampleNo: '', //	样品编号
    offset: 1, // 当前页
    limit: 5, // 每页多少条
  }
  fetchData(true)
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 submitForm = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选择原证书')
  }
  else {
    emits('confirm', checkoutList.value[0])
    dialogFormVisible.value = false
  }
}

// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  clearList()
}

// 多选选中
const handleSelectionChange = (val: any) => {
  checkoutList.value = val
}

// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  fetchData(true)
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择原证书" width="60%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <search-item>
        <el-input
          v-model.trim="listQuery.certificateReportCode"
          placeholder="证书编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.certificateReportName"
          placeholder="证书名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.customerName"
          placeholder="委托方名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleName"
          placeholder="样品名称"
          clearable
        />
      </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"
        :page-sizes="[5]"
        :is-multi="false" @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="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

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