<!-- 选择我的检测 -->
<script lang="ts" setup name="EnvironmentalDialog">
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import type { DateModelType } from 'element-plus'
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'
import { getMyMeasureList } from '@/api/business/schedule/task'
import { getDictByCode } from '@/api/system/dict'
import type { dictType } from '@/global'

const emits = defineEmits(['confirm'])
const dialogFormVisible = ref(false) // 控制弹窗显隐
const orderNo = ref('')
const isMulti = ref(true)
// 查询参数
const listQuery = ref({
  sampleNo: '', // 样品编号
  sampleName: '', // 样品名称
  orderNo: '', // 委托书编号
  customerNo: '', // 委托方代码
  customerName: '', // 委托方名称
  isUrgent: '', // 是否加急
  sampleBelong: '', // 样品属性
  startTime: '', // 应检完时间-开始
  endTime: '', // 应检完时间-结束
  measureStatus: '3', // 检测状态
  remark: '', // 备注
  offset: 1,
  limit: 20,
})
const loadingTable = ref(false) // loading
const timeRange = ref<[DateModelType, DateModelType]>(['', ''])
const list = ref([]) // 表格参数
const checkoutList = ref([]) // 多选表格
const isInvalid = ref(false)
const total = ref(0)
const columns = ref<TableColumn[]>([
  { text: '样品编号', value: 'sampleNo', width: '160', align: 'center' },
  { text: '样品名称', value: 'sampleName', align: 'center' },
  { text: '型号', value: 'sampleModel', align: 'center' },
  { text: '出厂编号', value: 'manufacturingNo', align: 'center' },
  { text: '委托书编号', value: 'orderNo', align: 'center', width: '160' },
  { text: '委托方代码', value: 'customerNo', align: 'center', width: '160' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '业务员', value: 'busPersonName', align: 'center' },
  { text: '是否加急', value: 'isUrgentName', align: 'center', width: '55px', styleFilter: (row: { isUrgentName: string }) => { return row.isUrgentName == '是' ? 'color: red' : '' } },
  { text: '应检完时间', value: 'requireOverTime', align: 'center', width: '180' },
  { text: '样品属性', value: 'sampleBelongName', align: 'center' },
  { text: '当前检定环节', value: 'currentSegment', align: 'center' },
])

// --------------获取所需字典值----------------
const sampleBelongList = ref<dictType[]>([]) // 样品属性列表
function getDict() {
  // 获取样品属性
  getDictByCode('sampleBelong').then((response) => {
    sampleBelongList.value = response.data
  })
}
getDict()
// ----------------------------------------------
// 查询列表
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  listQuery.value.measureStatus = isInvalid.value ? '4' : '3'
  getMyMeasureList(listQuery.value).then((res) => {
    list.value = res.data.rows.map((item: any) => {
      item.isUrgentName = item.isUrgent == 1 ? '是' : '否'
      item.sampleFileStatus = String(item.sampleFileStatus)
      // 已经出证书的数据不可再选择
      item.isRadioDisabled = item.certificateReportId !== '' && `${item.sampleFileStatus}` === '4'
      return item
    })
    total.value = res.data.total
    loadingTable.value = false
  })
}
fetchData(true)

// 搜索
const search = () => {
  fetchData(true)
}
// 重置
const clearList = () => {
  listQuery.value = {
    sampleNo: '', // 样品编号
    sampleName: '', // 样品名称
    orderNo: '', // 委托书编号
    customerNo: '', // 委托方代码
    customerName: '', // 委托方名称
    isUrgent: '', // 是否加急
    sampleBelong: '', // 样品属性
    startTime: '', // 应检完时间-开始
    endTime: '', // 应检完时间-结束
    measureStatus: '3', // 检测状态
    remark: '', // 备注
    offset: 1,
    limit: 20,
  }
  timeRange.value = ['', '']
  listQuery.value.orderNo = orderNo.value
  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)
    dialogFormVisible.value = false
  }
}

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

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

watch(timeRange, (val) => {
  if (val) {
    listQuery.value.startTime = `${val[0]}`
    listQuery.value.endTime = `${val[1]}`
  }
  else {
    listQuery.value.startTime = ''
    listQuery.value.endTime = ''
  }
})

// 初始化
const initDialog = (orderNoParam: string, isMultiParam = true, invalid = false) => {
  dialogFormVisible.value = true
  orderNo.value = orderNoParam// 委托书编号
  listQuery.value.orderNo = orderNo.value
  isMulti.value = isMultiParam
  isInvalid.value = invalid
  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.sampleNo"
          placeholder="样品编号"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleName"
          placeholder="样品名称"
          clearable
          class="short-input"
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.orderNo"
          placeholder="委托书编号"
          clearable
          class="short-input"
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.customerNo"
          placeholder="委托方代码"
          class="short-input"
          clearable
        />
      </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>
        <el-select v-model="listQuery.isUrgent" placeholder="是否加急" style="width: 120px;" clearable>
          <el-option label="是" value="1" />
          <el-option label="否" value="0" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.sampleBelong" placeholder="样品属性" style="width: 120px;" clearable>
          <el-option v-for="item in sampleBelongList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :is-showmulti-select="isMulti"
        :list-loading="loadingTable"
        :page-sizes="[20]"
        :is-multi="isMulti" @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>
    <div style="color: red; font-size: 11px;">
      注意：样品已经过委托书的筛选
    </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>
