<!-- 选择样品库下的样品 -->
<script lang="ts" setup name="SelectSampleList">
import { ElMessage } from 'element-plus'
import type { DateModelType } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getSampleList } from '@/api/customer/sampleList'

const emits = defineEmits(['confirm'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  sampleNo: '', // 样品编号
  sampleName: '', // 样品名称
  sampleModel: '', // 型号
  customerNo: '', // 委托方代码
  customerName: '', // 委托方名称
  startTime: '', // 检定开始时间
  endTime: '', // 检定结束时间
  overtimeStatus: '', // 样品超期状态 1已超期、0未超期、空字符串 已超期+未超期
  manufacturer: '', // 生产厂家
  manufacturingNo: '', // 出厂编号
  offset: 1,
  limit: 20,
})
const timeRange = ref<[DateModelType, DateModelType]>(['', ''])
const checkoutList = ref([]) // 多选选中的内容
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const columns = ref<TableColumn[]>([
  { text: '样品编号', value: 'sampleNo', width: '160', align: 'center' },
  { text: '样品名称', value: 'sampleName', width: '120', align: 'center' },
  { text: '型号', value: 'sampleModel', align: 'center' },
  { text: '出厂编号', value: 'manufacturingNo', align: 'center' },
  { text: '生产厂家', value: 'manufacturer', align: 'center' },
  { text: '委托方代码', value: 'customerNo', align: 'center', width: '160' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '检定周期(月)', value: 'measurePeriod', align: 'center' },
  { text: '上次检定时间', value: 'measureLastTime', align: 'center', width: '180px' },
  { text: '样品状态', value: 'sampleStatusName', align: 'center', width: '80px' },
  {
    text: '证书过期时间',
    value: 'validDeadline',
    align: 'center',
    width: '180px',
    styleFilter: (row: any) => { return new Date(row.validDeadline as string).getTime() < new Date().getTime() ? 'color: red' : '' },
  },
])

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  // 模拟数据
  getSampleList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 重置
const reset = () => {
  listQuery.value = {
    sampleNo: '', // 样品编号
    sampleName: '', // 样品名称
    sampleModel: '', // 型号
    customerNo: '', // 委托方代码
    customerName: '', // 委托方名称
    startTime: '', // 检定开始时间
    endTime: '', // 检定结束时间
    overtimeStatus: '', // 样品超期状态 1已超期、0未超期、空字符串 已超期+未超期
    manufacturer: '', // 生产厂家
    manufacturingNo: '', // 出厂编号
    offset: 1,
    limit: 20,
  }
  fetchData()
}
// 搜索
const searchList = () => {
  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
  }
  searchList()
}
// 点击确定
const confirm = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选择样品')
  }
  else {
    emits('confirm', checkoutList.value)
    dialogFormVisible.value = false
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  fetchData()
}

// 时间变更
watch(timeRange, (val) => {
  if (val) {
    listQuery.value.startTime = `${val[0]}`
    listQuery.value.endTime = `${val[1]}`
  }
  else {
    listQuery.value.startTime = ''
    listQuery.value.endTime = ''
  }
})
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" class="select-sample-dialog" title="选择样品(样品来源于样品库)" width="65%">
    <search-area
      :need-clear="true"
      @search="searchList" @clear="reset"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleNo"
          placeholder="样品编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleName"
          placeholder="样品名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleModel"
          placeholder="型号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.manufacturingNo"
          placeholder="出厂编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.manufacturer"
          placeholder="生产厂家"
          clearable
        />
      </search-item>
      <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>
        <search-item>
          <el-date-picker
            v-model="timeRange"
            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-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"
        @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>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="confirm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss">
.select-sample-dialog {
  .el-radio__label {
    display: none !important;
  }
}
</style>
