<!-- 标准装置设备到期提醒 -->
<script name="StandardRemindList" lang="ts" setup>
import type { Ref } from 'vue'
import { getCurrentInstance, onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'
import type { IListQuery } from './remind-interface'
import type { IList } from '@/views/equipement/standard/book/book-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { SCHEDULE } from '@/utils/scheduleDict'
import { getDictByCode } from '@/api/system/dict'
import type { IMenu } from '@/components/buttonBox/buttonBox'
import useUserStore from '@/store/modules/user'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import { getStandardRemindList } from '@/api/equipment/standard/remind'
const $router = useRouter()
const operateWidth = ref('90') // 操作栏的宽度
const user = useUserStore() // 用户信息
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  deptId: '', // 部门id
  standardName: '', // 标准装置名称
  storageLocation: '', // 存放地点
  technologyFileName: '', // 依据技术文件名称
  offset: 1,
  limit: 20,
})
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading
const menu = ref<IMenu[]>([]) // 审批状态按钮组合
const approvalDialog = ref() // 审批弹窗组件ref
const delRef = ref() // 删除弹窗组件ref
// 表头
const columns = ref<TableColumn[]>([
  { text: '标准代码', value: 'standardNo', align: 'center' },
  { text: '计量标准装置名称', value: 'standardName', align: 'center' },
  { text: '存放地点', value: 'storageLocationName', align: 'center' },
  { text: '测量范围', value: 'measureRange', align: 'center' },
  { text: '不确定度或允许误差极限或准确度等级', value: 'uncertainty', align: 'center' },
  { text: '建标日期', value: 'date', align: 'center', width: '120' },
  { text: '最近复查日期', value: 'lastReviewDate', align: 'center', width: '120' },
  { text: '计量标准证书号', value: 'standardCertNo', align: 'center' },
  { text: '检定或校准项目', value: 'measureItem', align: 'center' },
])
const list = ref<IList[]>([]) // 表格数据
const checkoutList = ref<string[]>([])// 选中的内容

// -----------------------------------------字典--------------------------------------------------------------
const storageLocationList = ref({}) as any // 存放地点
const storageLocationMap = ref({}) as any // 存放地点{1: 新建}

// 查询字典
const getDict = async () => {
  // 存放地点
  const res = await getDictByCode('bizStorageLocation')
  storageLocationList.value = res.data
  res.data.forEach((item: any) => {
    storageLocationMap.value[`${item.value}`] = item.name
  })
}
// -------------------------------------------列表数据操作------------------------------------------------------------
// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getStandardRemindList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: { lastReviewDate: string; storageLocation: string }) => {
      return {
        ...item,
        storageLocationName: storageLocationMap.value[item.storageLocation],
        lastReviewDate: item.lastReviewDate ? dayjs(item.lastReviewDate).format('YYYY-MM-DD') : item.lastReviewDate, // 最近复查日期
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
// 搜索
const searchList = () => {
  fetchData(true)
}

// 重置
const clearList = () => {
  listQuery.value = {
    deptId: '', // 部门id
    standardName: '', // 标准装置名称
    storageLocation: '', // 存放地点
    technologyFileName: '', // 依据技术文件名称
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 handleEdit = (row: IList, type: string) => {
  $router.push({
    path: `/standard/${type}/${row.id}`,
    query: {
      approvalStatusName: '全部', // 审批状态名称
      standardNo: row.standardNo, // 标准装置编号
      standardName: row.standardName, // 标准装置名称
      processId: row.processId, // 流程实例id
      taskId: row.taskId, // 任务id
    },
  })
}

// ----------------------------------------------钩子------------------------------------------------------
onMounted(() => {
  getDict().then(() => {
    fetchData()
  })
})
</script>

<template>
  <app-container>
    <search-area :need-clear="true" @search="searchList" @clear="clearList">
      <search-item>
        <el-input v-model.trim="listQuery.standardName" placeholder="标准装置名称" class="short-input" clearable />
      </search-item>
      <search-item>
        <search-item>
          <el-select
            v-model="listQuery.storageLocation"
            placeholder="存放地点"
            class="short-input"
            filterable
          >
            <el-option v-for="item in storageLocationList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <!-- <search-item>
          <el-input v-model.trim="listQuery.technologyFileName" placeholder="依据的技术文件" class="short-input" clearable />
        </search-item> -->
      </search-item>
    </search-area>
    <table-container>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="loadingTable"
        is-showmulti-select @change="changePage" @multi-select="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            :width="operateWidth"
          >
            <template #default="{ row }">
              <el-button
                size="small"
                type="primary"
                link
                @click="handleEdit(row, 'detail')"
              >
                查看
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="ts" scoped>

</style>

