<template>
  <el-card class="hl-card">
    <template #header>
      <span>委外验收进度查询</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="120px">
        <el-form-item label="采购单号" prop="purchaseNo">
          <el-input
v-model="queryParams.purchaseNo" placeholder="请输入采购单号" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input
v-model="queryParams.supplierName" placeholder="请输入供应商" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="采购物名称" prop="materialName">
          <el-input
v-model="queryParams.materialName" placeholder="请输入采购物名称" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="规格/型号" prop="spec">
          <el-input
v-model="queryParams.spec" placeholder="请输入规格/型号" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> 重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
  </el-card>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
      <el-table-column fixed label="序号" type="index" align="center" width="60" />
      <el-table-column label="采购单号" align="center" prop="purchaseNo" fixed width="180px" />
      <el-table-column label="供应商" align="center" prop="supplierName" fixed width="140px"/>
      <el-table-column label="采购物料类型" align="center" prop="goodsType" width="140px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_PROJECT_PURCHASE_GOODS_TYPE" :value="scope.row.goodsType" min-width="140" />
        </template>
      </el-table-column>
      <el-table-column label="采购物编号" align="center" prop="materialCode" width="120px" />
      <el-table-column label="采购物名称" align="center" prop="materialName" width="120px" />
      <el-table-column label="规格型号" align="center" prop="spec" width="100px"/>
      <el-table-column label="材质" align="center" prop="composition" width="100px"/>
      <!-- <el-table-column label="系统单位" align="center" prop="unit" width="100px"/> -->
      <el-table-column label="系统单位" align="center" prop="unit" width="140px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_MATERIAL_UNIT" :value="scope.row.unit" min-width="140" />
        </template>
      </el-table-column>
      <el-table-column label="采购数量" align="center" prop="purchaseAmount" width="100px"/>
      <el-table-column label="预计到货日期" align="center" prop="arrivalTime" :formatter="dateFormatter" width="150px">
        <template #default="scope">
          {{
            formatDate(scope.row.arrivalTime, 'YYYY-MM-DD')
          }}
        </template>
      </el-table-column>
      <el-table-column label="累计到货数量" align="center" prop="sumCurrentCount" width="140px"/>
      <el-table-column label="累计不合格数量" align="center" prop="sumUnqualifiedCount" width="150px" />
      <el-table-column label="实际到货日期（最晚）" align="center" prop="arrivalDate" :formatter="dateFormatter" width="220px">
        <template #default="scope">
          {{
            formatDate(scope.row.arrivalDate, 'YYYY-MM-DD')
          }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OutsourceStockForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import * as OutsourceStockApi from '@/api/heli/outsourcestock'
import OutsourceStockForm from './OutsourceStockForm.vue'
import { dateFormatter, formatDate } from '@/utils/formatTime'

defineOptions({ name: 'OutsourceStock' })

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purchaseNo: undefined,
  purchaseId: undefined,
  supplierId: undefined,
  supplierName: undefined,
  materialName: undefined,
  spec: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OutsourceStockApi.getOutsourceStockProgressQueryPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
