<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="156px">
      <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="createTime">
        <el-date-picker
          class="!w-220px" v-model="queryParams.createTime" type="date" value-format="x"
          placeholder="单据日期" />
      </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="purchaseType">-->
<!--        <el-select v-model="queryParams.purchaseType" placeholder="下拉选择" clearable class="!w-240px">-->
<!--          <el-option-->
<!--v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PURCHASE_ORDER_TYPE)" :key="dict.value"-->
<!--            :label="dict.label" :value="dict.value" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="物料需求计划单号" prop="materialPlanNo">
        <el-input
v-model="queryParams.materialPlanNo" placeholder="物料需求计划单号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
<!--      <el-form-item label="采购物类型" prop="goodsType">-->
<!--        <el-select v-model="queryParams.goodsType" placeholder="下拉选择" clearable class="!w-220px">-->
<!--          <el-option-->
<!--v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PURCHASE_GOODS_TYPE)" :key="dict.value"-->
<!--            :label="dict.label" :value="dict.value" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="下拉选择" clearable class="!w-220px">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PURCHASE_ORDER_STATUS).filter(item=>item.value != 1)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 50px">
        <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="purchaseNo" fixed min-width="190">
          <template #default="scope">
            <el-button link type="primary" @click="openForm('detail', scope.row.id)">
              {{ scope.row.purchaseNo }}
            </el-button>
          </template>
        </el-table-column>
      <el-table-column label="单据日期" align="center" prop="createTime" :formatter="dateFormatter" min-width="180px" fixed>
        <template #default="scope">
          {{
            formatDate(scope.row.createTime, 'YYYY-MM-DD')
          }}
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplierName" min-width="180px" fixed />
      <el-table-column label="采购单类型" align="center" prop="purchaseType" min-width="220">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_PROJECT_PURCHASE_ORDER_TYPE" :value="scope.row.purchaseType" />
        </template>
      </el-table-column>
      <el-table-column label="物料需求计划单号" align="center" prop="materialPlanNo" min-width="220"/>
      <el-table-column label="采购物类型" align="center" prop="goodsType" min-width="180">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_PROJECT_PURCHASE_GOODS_TYPE" :value="scope.row.goodsType" />
        </template>
      </el-table-column>
      <el-table-column label="采购合同号" align="center" prop="contractNo" min-width="180"/>
      <el-table-column label="结算币种" align="center" prop="currencyType" min-width="180">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_CURRENCY" :value="scope.row.currencyType" />
        </template>
      </el-table-column>
      <el-table-column label="暂估单价(元)" align="center" prop="estimatedPrice" min-width="180"/>
      <el-table-column label="实际单价(元)" align="center" prop="actualPrice" min-width="180"/>
      <el-table-column label="税率" align="center" prop="taxRatio" min-width="180" />
      <el-table-column label="备注" align="center" prop="description" min-width="180" />
      <el-table-column label="单据状态" align="center" prop="status" fixed="right" min-width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_PURCHASE_ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="right" fixed="right" min-width="200">
         <template #header>
              <span style="margin-right: 30%">操作</span>
         </template>
        <template #default="scope">
          <el-button link type="primary" @click="openForm('audit', scope.row.id)" v-if="scope.row.status == 2">
            去审核
          </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 { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PurchaseOrderNoApi from '@/api/heli/purchaseorderno'
import dayjs from "dayjs";
import routeParamsCache from '@/utils/routeParamsCache'
defineOptions({ name: 'PurchaseOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  purchaseNo: undefined,
  supplierId: undefined,
  contractNo: undefined,
  purchaseType: 2,
  projectMaterialPlanId: undefined,
  goodsType: undefined,
  currencyType: undefined,
  taxRatio: undefined,
  estimatedPrice: undefined,
  actualPrice: undefined,
  status: 2,
  submitUserId: undefined,
  submitTime: [],
  auditor: undefined,
  auditTime: [],
  description: undefined,
  creator: undefined,
  createTime: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
	  const route = useRoute();

	  if(route){
		const idid = route.query.idid;
		queryParams.purchaseNo=idid
	  }

    const data = await PurchaseOrderNoApi.getPurchaseOrderNoPageByStatus(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  if (queryParams.createTime !== "Invalid Date" && queryParams.createTime !== "") {
    queryParams.createTime = formatDate(queryParams.createTime, 'YYYY-MM-DD')
  }
  getList()
}

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

/** 添加/修改操作 */
const openForm = (type: string, id?: number) => {
switch (type) {
  case 'audit':
    router.push({ path: '/purchase/auditdetail', query: { id: id } })
    break;
  case 'detail':
    router.push({ path: '/purchase/purchaseorderdetail', query: { id: id } })
    break;
  default:
    break;
}

}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurchaseOrderApi.exportPurchaseOrder(queryParams)
    download.excel(data, '采购订单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const setDefaultDate = () => {
  queryParams.createTime = dayjs().startOf('day').format('YYYY-MM-DD')

}
/** 初始化 **/
const route = useRoute()
onMounted(async () => {
  setDefaultDate()
  let params = routeParamsCache.get(route.path);
  if (params ) {
    Object.assign(queryParams, params);
  }
  await getList()
})
onBeforeUnmount(() => {
  const plainParams = JSON.parse(JSON.stringify(queryParams));
  routeParamsCache.set(route.path, plainParams);
});

window.addEventListener('beforeunload', () => {
  const plainParams = JSON.parse(JSON.stringify(queryParams));
  routeParamsCache.set(route.path, plainParams);
});
</script>
