<script setup lang="ts">
import { batchCheckPrintPodCn, batchCheckPrintPodUs } from '@/api/podCnOrder.ts'
import { batchCheckPrintPodOrder } from '@/api/factoryOrderNew.ts'
import { InterWarehousePage } from '@/types/api/warehouse.ts'
import { ElMessage } from 'element-plus'
import { factoryWarehouseInventoryPrint } from '@/api/warehouse.ts'
import { filePath } from '@/api/axios.ts'
import { PrintData } from '@/types/api/podOrder.ts'

const showPrintDialog = ref(false)
const printData = ref<PrintData[]>([])

const apiMap: Record<number, (ids: string) => Promise<{ data: PrintData[] }>> = {
  1: batchCheckPrintPodCn,
  2: batchCheckPrintPodUs,
  3: batchCheckPrintPodOrder,
}

const open = async (type: number, ids: string) => {
  printData.value = []
  const api = apiMap[type] ?? batchCheckPrintPodCn
  const res = await api(ids)
  showPrintDialog.value = true
  printData.value = res.data
}
async function handlePrintProductTag() {
  const flag = printData.value.every(
    (el: InterWarehousePage) => el.number && el.number != '0',
  )
  if (!flag) {
    return ElMessage.warning('打印数量需大于0')
  }
  const list = printData.value.map(
    ({
       skuName = '',
       warehouseSku = '',
       supplierItemNo = '',
       number = '',
       locationName = '',
     }) => ({
      skuName,
      warehouseSku,
      supplierItemNo,
      number,
      locationName,
    }),
  )
  const res = await factoryWarehouseInventoryPrint({
    list,
    code: 'PT002',
  })
  showPrintDialog.value = false
  window.open(filePath + res.message, '_blank')
}
defineExpose({open})
</script>

<template>
  <el-dialog v-model="showPrintDialog" title="打印参数设置">
    <el-table height="400px" :data="printData" border>
      <el-table-column width="60" align="center" type="index" label="序号" />
      <el-table-column align="center" prop="skuName" label="商品名称" />
      <el-table-column align="center" prop="warehouseSku" label="库存SKU" />
      <el-table-column align="center" prop="locationName" label="库位编码" />
      <el-table-column align="center" prop="supplierItemNo" label="款号" />
      <el-table-column align="center" prop="number" label="打印数量">
        <template #default="{ row }">
          <el-input
            v-model.number="row.number"
            oninput="value=value.replace(/[^\-?\d.]/g,'')"
            placeholder="打印数量"
            clearable
          ></el-input>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-button @click="showPrintDialog = false">取消</el-button>
      <el-button type="primary" @click="handlePrintProductTag">打印</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
