<!-- 设备证书管理 -->
<script lang="ts" setup name="CertificateList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getCertificateList } from '@/api/eqpt/device/certificate'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  approvalStatus: '',
  certificateName: '',
  certificateNo: '',
  certificateValidEnd: '',
  certificateValidStart: '',
  checkOrganization: '',
  equipmentName: '',
  equipmentNo: '',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '证书编号',
    value: 'certificateNo',
    align: 'center',
  },
  {
    text: '证书名称',
    value: 'certificateName',
    align: 'center',
  },
  {
    text: '统一编号',
    value: 'equipmentNo',
    align: 'center',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '校准(检定)单位',
    value: 'checkOrganization',
    align: 'center',
  },
  {
    text: '校准(检定)日期',
    value: 'checkDate',
    align: 'center',
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getCertificateList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/certificate/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
const range = ref()
watch(() => range.value, (newVal) => {
  listQuery.certificateValidStart = ''
  listQuery.certificateValidEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.certificateValidStart = `${newVal[0]} 00:00:00`
      listQuery.certificateValidEnd = `${newVal[1]} 23:59:59`
    }
  }
}, {
  deep: true,
})
const reset = () => {
  range.value = []
  listQuery.approvalStatus = ''
  listQuery.certificateName = ''
  listQuery.certificateNo = ''
  listQuery.certificateValidEnd = ''
  listQuery.certificateValidStart = ''
  listQuery.checkOrganization = ''
  listQuery.equipmentName = ''
  listQuery.equipmentNo = ''
  listQuery.offset = 20
  listQuery.limit = 1
  search()
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.certificateNo" placeholder="证书编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.certificateName" placeholder="证书名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.equipmentNo" placeholder="统一编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.checkOrganization" placeholder="校准(检定)单位" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="range" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="证书有效期开始时间" end-placeholder="证书有效期结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                查看
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
