<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="130px">
      <el-form-item label="采购单号" prop="contractNo">
        <el-input v-model="queryParams.contractNo" placeholder="采购单号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="项目名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="子项目名称" prop="projectSubName">
        <el-input v-model="queryParams.projectSubName" 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="goodType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择采购单类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PART_PURCHASE_ORDER_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="documentStatus">
        <el-select v-model="queryParams.status" placeholder="请选择单据状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_DOCUMENT_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left:15px">
        <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>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
      <el-table-column label="序号" type="index" align="center" fixed min-width="70px" />
      <el-table-column label="采购单号" align="center" prop="contractNo" min-width="180" fixed>
        <template #default="scope">
          <el-button text type="primary" @click="openForm('detail', scope.row.id)">
            {{ scope.row.contractNo }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="采购单类型" align="center" prop="goodsType" min-width="180px" >
        <template #default="scope">
          {{
            getIntDictOptions(DICT_TYPE.HELI_PART_PURCHASE_ORDER_TYPE).find(
              (dict) => dict.value === scope.row.goodsType
            )?.label || '未知状态'
          }}
        </template>
      </el-table-column>
      <el-table-column label="客户简码" align="center" prop="brief" min-width="180px" />
      <el-table-column label="项目名称" align="center" prop="projectName" min-width="180px" />
      <el-table-column label="项目简码" align="center" prop="projectNameSim" min-width="180px" />
      <el-table-column label="单据状态" align="center" prop="status" min-width="180px" >
        <template #default="scope">
          {{
            getIntDictOptions(DICT_TYPE.HELI_DOCUMENT_STATUS).find(
              (dict) => dict.value === scope.row.status
            )?.label || '未知状态'
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="180">
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 1"
            link
            type="primary"
            @click="openForm('check', scope.row.id)"
          >
            去审核
          </el-button>
          <el-button link type="primary" @click="openForm('detail', scope.row.id)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

</el-card>

</template>

<script setup lang="ts">
import {DICT_TYPE, getIntDictOptions} from '@/utils/dict'
import { dateFormatter1} from '@/utils/formatTime'
import * as MaterialPlanApi from '@/api/heli/materialplan'
import * as PartPurchaseOrderApi from "@/api/heli/partpurchaseorder";
import {getPartCheckPage} from "@/api/heli/partpurchaseorder";


defineOptions({ name: 'MaterialPlan' })
const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  contractNo: undefined,
  projectId: undefined,
  projectPlanId: undefined,
  submitUserId: undefined,
  projectName: undefined,
  projectSubName: undefined,
  supplierName:undefined,
  submitTime: [],
  auditor: undefined,
  auditTime: [],
  goodsType: 2,
  description: undefined,
  createTime: [],
  matType:0,
  status:1
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PartPurchaseOrderApi.getPartCheckPage(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()
}

/** 添加/修改操作 */

const openForm = (type: string, id?: number) => {

  switch (type) {
    case 'create':
      router.push({ path: '/purchase/materialplanadd', query: { id: id } })
      break;
    case 'update':
      router.push({ path: '/purchase/materialplanedit', query: { id: id } })
      break;
    case 'detail':
      router.push({ path: '/purchase/PartPurchaseCheckDetail', query: { id: id , type: type} })
      break;
    case 'check':
      router.push({ path: '/purchase/PartPurchaseCheckDetail', query: { id: id , type: type} })
      break;
    default:
      break;
  }

}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MaterialPlanApi.delMaterialPlan(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}


const userInit = ref()
/** 初始化 **/
onMounted(async () => {
  //用户列表数据
  // userInit.value = await UserApi.getSimpleUserList()
  getList()
})
</script>
