<!-- 现场检测记录列表 -->
<script lang="ts" setup name="FieldTestRecordList">
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { IList, IListQuery } from './record-interface'
import { printJSON } from '@/utils/printUtils'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import type { dictType } from '@/global'
import type { IMenu } from '@/components/buttonBox/buttonBox'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import ButtonBox from '@/components/buttonBox/buttonBox.vue'
import { exportFile } from '@/utils/exportUtils'
import { delFieldTestRecordList, exportRecordDetail, getFieldTestRecordList } from '@/api/business/fieldTest/record'
import { SCHEDULE } from '@/utils/scheduleDict'
import { cancelApproval } from '@/api/approval'
const approvalDialog = ref() // 审批对话ref
const $route = useRoute()
const $router = useRouter()
const { proxy } = getCurrentInstance() as any
const TabActiveButton = 'FieldTestRecordList'
const loadingTable = ref(false)
const menu = ref<IMenu[]>([]) // 审批状态按钮组合
const active = ref('') // 右上方按钮
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  logNo: '', // 记录表编号
  createUserName: '', // 创建人
  customerName: '', // 委托方名称
  siteExecutiveNo: '', //  现场测试、校准 或检定审批单编号
  limit: 20,
  offset: 1,
})

// 表头
const columns = ref<TableColumn[]>([
  { text: '记录表编号', value: 'logNo', align: 'center', width: '160' },
  { text: '记录表名称', value: 'logName', align: 'center' },
  { text: '现场测试、校准 或检定审批单编号', value: 'siteExecutiveNo', align: 'center', width: '160' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '创建人', value: 'createUserName', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180' },
])
const list = ref<IList[]>([]) // 列表
const total = ref(0) // 数据总条数
// 选中的内容
const checkoutList = ref<string[]>([])

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

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getFieldTestRecordList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 清除条件
const clearList = () => {
  listQuery.value = {
    logNo: '', // 记录表编号
    createUserName: '', // 创建人
    customerName: '', // 委托方名称
    siteExecutiveNo: '', //  现场测试、校准 或检定审批单编号
    limit: 20,
    offset: 1,
  }
  fetchData()
}
// 搜索
const searchList = () => {
  fetchData(true)
}

// 新建
const add = () => {
  $router.push({
    path: '/fieldTest/record/add',
  })
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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, val: string) => {
  switch (val) {
    case 'delete':
      ElMessageBox.confirm(
        '确认删除吗？',
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        },
      )
        .then(() => {
          delFieldTestRecordList({ id: row.id }).then((res) => {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            fetchData(true)
          })
        })
      break
    case 'detail':
      $router.push({
        path: `/fieldTest/recordDoc/detail/${row.id}`,
      })
      break
    case 'edit':
      $router.push({
        path: `/fieldTest/record/${val}/${row.id}`,
      })
      break
  }
}
onMounted(async () => {
  fetchData(false)
})
</script>

<template>
  <div>
    <!-- 布局 -->
    <app-container>
      <search-area :need-clear="true" @search="searchList" @clear="clearList">
        <search-item>
          <el-input v-model.trim="listQuery.logNo" placeholder="记录表编号" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.createUserName" placeholder="创建人" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.customerName" placeholder="委托方名称" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.siteExecutiveNo" placeholder=" 现场测试、校准 或检定审批单编号" style="width: 240px;" clearable />
        </search-item>
      </search-area>
      <table-container>
        <template #btns-right>
          <icon-button icon="icon-add" title="新建" type="primary" @click="add" />
        </template>
        <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="120"
            >
              <template #default="{ row }">
                <el-button
                  size="small"
                  link
                  type="primary"
                  @click="handleEdit(row, 'edit')"
                >
                  编辑
                </el-button>
                <el-button
                  size="small"
                  type="primary"
                  link
                  @click="handleEdit(row, 'detail')"
                >
                  详情
                </el-button>
                <el-button
                  size="small"
                  type="danger"
                  link
                  @click="handleEdit(row, 'delete')"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>
