<!-- 任务单管理表格 -->
<script lang="ts" setup name="TaskList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { cancelTask, getListPage } from '@/api/eqpt/measurementPlan/task'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  orderNo: '',
  deliverer: '',
  customerName: '',
  createUserName: '',
  createStartTime: '',
  createEndTime: '',
  offset: 1,
  limit: 20,
})
// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.createStartTime = ''
  listQuery.createEndTime = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.createStartTime = `${newVal[0]} 00:00:00`
      listQuery.createEndTime = `${newVal[1]} 23:59:59`
    }
  }
})
const columns = ref([
  {
    text: '任务单编号',
    value: 'orderNo',
    align: 'center',
  },
  {
    text: '委托方',
    value: 'customerName',
    align: 'center',
  },
  {
    text: '溯源单位',
    value: 'measureCompany',
    align: 'center',
  },
  {
    text: '创建人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '送检人',
    value: 'deliverer',
    align: 'center',
  },
  {
    text: '接收状态',
    value: 'receiveStatusName',
    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
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.orderNo = ''
  listQuery.deliverer = ''
  listQuery.customerName = ''
  listQuery.createUserName = ''
  listQuery.createStartTime = ''
  listQuery.createEndTime = ''
  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: `/meteringtask/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 取消
const cancelHandler = (row: any) => {
  ElMessageBox.confirm(
    '确认取消此任务单信息吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    cancelTask({ id: row.id }).then((res) => {
      ElMessage.success('操作成功')
      // close()
      search()
    })
  })
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.orderNo" 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.createUserName" placeholder="创建人" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.deliverer" 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>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="handler({}, 'create')" />
      </template>
      <!-- 普通表格 -->
      <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>
              <el-button link type="primary" :disabled="scope.row.receiveStatusName !== '未接收'" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button link type="danger" size="small" @click="cancelHandler(scope.row)">
                取消
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

