<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="150px">
        <el-form-item label="委外验收单号" prop="outsourceNo">
          <el-input
v-model="queryParams.outsourceNo" placeholder="请输入委外验收单号" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="单据日期" prop="createTime">
          <el-date-picker
class="!w-240px" v-model="queryParams.createTime" type="date" value-format="YYYY-MM-DD"
            placeholder="单据日期" />
        </el-form-item>
        <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="deliver">
          <el-input
v-model="queryParams.deliver" placeholder="请输入送检人" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="收检人" prop="receiver">
          <el-input
v-model="queryParams.receiver" placeholder="请输入收检人" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="单据状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="下拉选择" clearable class="!w-240px">
            <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.HELI_OUTSOURCE_STOCK_STATUS)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </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-button type="primary" plain @click="openForm('create')" v-hasPermi="['heli:outsource-stock:create']">
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <!-- <el-button
type="success" plain @click="handleExport" :loading="exportLoading"
            v-hasPermi="['heli:outsource-stock:export']">
            <Icon icon="ep:download" 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="outsourceNo" fixed width="180px"/>
      <el-table-column label="单据日期" align="center" prop="createTime" :formatter="dateFormatter" width="130px" >
        <template #default="scope">
            {{
              formatDate(scope.row.createTime, 'YYYY-MM-DD')
            }}
         </template>
      </el-table-column>
      <el-table-column label="采购单号" align="center" prop="purchaseNo" min-width="140"/>
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <!-- <el-table-column label="采购物类型" align="center" prop="goodsType">
        <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="是否自动入库 0 否 1 是" align="center" prop="autoStockIn" />
      <el-table-column label="是否自动出库 0 否 1 是" align="center" prop="autoStockOut" /> -->
      <el-table-column label="送检人" align="center" prop="deliver" />
      <el-table-column label="收检人" align="center" prop="receiver" />
      <el-table-column label="到货日期" align="center" prop="arrivalDate" :formatter="dateFormatter" width="130px" >
        <template #default="scope">
            {{
              formatDate(scope.row.arrivalDate, 'YYYY-MM-DD')
            }}
         </template>
      </el-table-column>
      <el-table-column label="检验日期" align="center" prop="inspectionDate" :formatter="dateFormatter" width="130px" >
        <template #default="scope">
            {{
              formatDate(scope.row.inspectionDate, 'YYYY-MM-DD')
            }}
         </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="单据状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_OUTSOURCE_STOCK_STATUS" :value="scope.row.status" min-width="140" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="100">
        <template #default="scope">
          <el-button
link type="primary" @click="openForm('edit', scope.row.id)"
            v-hasPermi="['heli:outsource-stock:update']">
            编辑
          </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>

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

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

defineOptions({ name: 'OutsourceStock' })

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,
  outsourceNo: undefined,
  purchaseId: undefined,
  supplierId: undefined,
  goodsType: undefined,
  autoStockIn: undefined,
  autoStockOut: undefined,
  deliver: undefined,
  receiver: undefined,
  arrivalDate: [],
  inspectionDate: [],
  remark: undefined,
  status: undefined,
  submitter: undefined,
  submitTime: [],
  createTime: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OutsourceStockApi.getOutsourceStockPage(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 formRef = ref()
const openForm = (type: string, id?: number) => {
  switch (type) {
    case 'create':
      router.push({ path: '/outsource/outsourcestockadd', query: { id: id } })
      break
    case 'edit':
      router.push({ path: '/outsource/outsourcestockedit', query: { id: id } })
      break
    case 'detail':
      router.push({ path: '/outsource/outsourcestockdetail', query: { id: id } })
      break
    default:
      break
  }
  //formRef.value.open(type, id)
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OutsourceStockApi.exportOutsourceStock(queryParams)
    download.excel(data, '委外验收入库单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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