<!-- 委托方任务单列表 -->
<script name="CustomerInfoOrder" lang="ts" setup>
import type { TableColumn } from '@/components/NormalTable/table_interface'
import type { IList, IListQueryOrder } from '@/views/business/manager/order/order-interface'
import { getOrderList } from '@/api/business/manager/order'

const props = defineProps({
  customerId: { type: String, default: '' },
})

const router = useRouter()

const listQuery = ref<IListQueryOrder>({
  customerId: props.customerId, // 委托方名称
  offset: 1,
  limit: 10,
  orderNo: '', // 任务单编号
  customerName: '', // 委托方名称
  createUserName: '', // 创建人
  deliverer: '', // 送样人
  createStartTime: '', // 创建开始时间
  createEndTime: '', // 创建结束时间
  receiveStatus: '', // 接收状态
})

const columns = ref<TableColumn[]>([ // 表头
  { text: '任务单编号', value: 'orderNo', align: 'center', width: '160' },
  { text: '委托方', value: 'customerName', align: 'center' },
  { text: '受检设备数量', value: 'sampleCount', align: 'center' },
  { text: '创建人', value: 'createUserName', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180' },
  { text: '送检人', value: 'deliverer', align: 'center' },
  { text: '接收状态', value: 'receiveStatusName', align: 'center', width: '100' },
])
const list = ref<IList[]>([]) // 表格数据
const total = ref(0) // 数据总数
const loadingTable = ref(false) // 表格加载状态

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }

  getOrderList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 detailOrder = (row: IList) => {
  router.push({
    path: `/manager/detail/${row.id}`,
  })
}

watch(() => props.customerId, (newVal: string) => {
  listQuery.value.customerId = newVal
  fetchData()
}, { immediate: true })
</script>

<template>
  <app-container>
    <table-container>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" @change="changePage"
      >
        <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 fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detailOrder(row)">
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
