<!-- 测试、校准或检定工作分包通知书 -->
<script lang="ts" setup name="SubcontractingNotice">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getListPage } from '@/api/eqpt/MeasurementBusiness/notice'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  applicantName: '',
  applicantTimeEnd: '',
  applicantTimeStart: '',
  approvalStatus: '',
  customerName: '',
  formId: '',
  noticeNo: '',
  outsourcerName: '',
  offset: 1,
  limit: 20,
})
// 证书有效期开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.applicantTimeStart = ''
  listQuery.applicantTimeEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.applicantTimeStart = `${newVal[0]} 00:00:00`
      listQuery.applicantTimeEnd = `${newVal[1]} 23:59:59`
    }
  }
})
const columns = ref([
  {
    text: '通知书编号',
    value: 'noticeNo',
    align: 'center',
  },
  {
    text: '通知书名称',
    value: 'noticeName',
    align: 'center',
  },
  {
    text: '受检单位名称',
    value: 'customerName',
    align: 'center',
  },
  {
    text: '分包方名称',
    value: 'outsourcerName',
    align: 'center',
  },
  {
    text: '申请人',
    value: 'createUserName',
    align: 'center',
  },

  {
    text: '申请时间',
    value: 'createTime',
    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
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
    // list.value = [
    //   {
    //     approvalStatus: '',
    //     approvalStatusName: '',
    //     contacts: '张三',
    //     createTime: '2023-09-08',
    //     createUserId: 0,
    //     createUserName: '创建人',
    //     customerId: 0,
    //     customerName: '受检单位',
    //     decisionItem: 0,
    //     deviceName: '测试设备',
    //     groupCode: '',
    //     groupCodeName: '分组1',
    //     id: 0,
    //     labCode: '',
    //     labCodeName: '',
    //     mobile: '110',
    //     noticeDate: '',
    //     noticeName: '通知书1',
    //     noticeNo: '123',
    //     noticeUserId: '',
    //     noticeUserName: '受检单位',
    //     outsourcerName: '分包方1',
    //     processId: '',
    //     taskId: '',
    //   },
    // ]
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.applicantName = ''
  listQuery.applicantTimeEnd = ''
  listQuery.applicantTimeStart = ''
  listQuery.approvalStatus = ''
  listQuery.customerName = ''
  listQuery.formId = ''
  listQuery.noticeNo = ''
  listQuery.outsourcerName = ''
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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: `/bnoticepage/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.noticeNo" 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.outsourcerName" placeholder="分包方名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.applicantName" placeholder="申请人" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" 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="160" 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>
