<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="关联采购单号" prop="orderSn">
              <el-input v-model="queryParams.orderSn" placeholder="请输入关联采购单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库ID" prop="wid">
              <el-input v-model="queryParams.wid" placeholder="请输入仓库ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="仓库名称" prop="wareHouseName">
              <el-input v-model="queryParams.wareHouseName" placeholder="请输入仓库名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="采购计划号" prop="planSn">
              <el-input v-model="queryParams.planSn" placeholder="请输入采购计划号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="本地产品ID" prop="productId">
              <el-input v-model="queryParams.productId" placeholder="请输入本地产品ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="品名" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入品名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="SKU" prop="sku">
              <el-input v-model="queryParams.sku" placeholder="请输入SKU" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="FNSKU" prop="fnsku">
              <el-input v-model="queryParams.fnsku" placeholder="请输入FNSKU" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="店铺ID" prop="sid">
              <el-input v-model="queryParams.sid" placeholder="请输入店铺ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="型号" prop="model">
              <el-input v-model="queryParams.model" placeholder="请输入型号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="含税单价" prop="price">
              <el-input v-model="queryParams.price" placeholder="请输入含税单价" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="价税合计" prop="amount">
              <el-input v-model="queryParams.amount" placeholder="请输入价税合计" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="计划采购量" prop="quantityPlan">
              <el-input v-model="queryParams.quantityPlan" placeholder="请输入计划采购量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="实际采购量" prop="quantityReal">
              <el-input v-model="queryParams.quantityReal" placeholder="请输入实际采购量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="到货入库量" prop="quantityEntry">
              <el-input v-model="queryParams.quantityEntry" placeholder="请输入到货入库量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="待到货量" prop="quantityReceive">
              <el-input v-model="queryParams.quantityReceive" placeholder="请输入待到货量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="退货数" prop="quantityReturn">
              <el-input v-model="queryParams.quantityReturn" placeholder="请输入退货数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="换货量" prop="quantityExchange">
              <el-input v-model="queryParams.quantityExchange" placeholder="请输入换货量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="质检量" prop="quantityQc">
              <el-input v-model="queryParams.quantityQc" placeholder="请输入质检量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="待质检量" prop="quantityQcPrepare">
              <el-input v-model="queryParams.quantityQcPrepare" placeholder="请输入待质检量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="期待到货时间" prop="expectArriveTime">
              <el-date-picker clearable
                v-model="queryParams.expectArriveTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择期待到货时间"
              />
            </el-form-item>
            <el-form-item label="箱数" prop="casesNum">
              <el-input v-model="queryParams.casesNum" placeholder="请输入箱数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单箱数量" prop="quantityPerCase">
              <el-input v-model="queryParams.quantityPerCase" placeholder="请输入单箱数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否删除：0 否，1 是" prop="isDelete">
              <el-input v-model="queryParams.isDelete" placeholder="请输入是否删除：0 否，1 是" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="税率" prop="taxRate">
              <el-input v-model="queryParams.taxRate" placeholder="请输入税率" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="SPU" prop="spu">
              <el-input v-model="queryParams.spu" placeholder="请输入SPU" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="款名" prop="spuName">
              <el-input v-model="queryParams.spuName" placeholder="请输入款名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['amz:purchaseOrderItem:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['amz:purchaseOrderItem:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['amz:purchaseOrderItem:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['amz:purchaseOrderItem:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="purchaseOrderItemList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="采购单子项ID" align="center" prop="id" />
        <el-table-column label="关联采购单号" align="center" prop="orderSn" />
        <el-table-column label="仓库ID" align="center" prop="wid" />
        <el-table-column label="仓库名称" align="center" prop="wareHouseName" />
        <el-table-column label="采购计划号" align="center" prop="planSn" />
        <el-table-column label="本地产品ID" align="center" prop="productId" />
        <el-table-column label="品名" align="center" prop="productName" />
        <el-table-column label="SKU" align="center" prop="sku" />
        <el-table-column label="FNSKU" align="center" prop="fnsku" />
        <el-table-column label="店铺ID" align="center" prop="sid" />
        <el-table-column label="型号" align="center" prop="model" />
        <el-table-column label="含税单价" align="center" prop="price" />
        <el-table-column label="价税合计" align="center" prop="amount" />
        <el-table-column label="计划采购量" align="center" prop="quantityPlan" />
        <el-table-column label="实际采购量" align="center" prop="quantityReal" />
        <el-table-column label="到货入库量" align="center" prop="quantityEntry" />
        <el-table-column label="待到货量" align="center" prop="quantityReceive" />
        <el-table-column label="退货数" align="center" prop="quantityReturn" />
        <el-table-column label="换货量" align="center" prop="quantityExchange" />
        <el-table-column label="质检量" align="center" prop="quantityQc" />
        <el-table-column label="待质检量" align="center" prop="quantityQcPrepare" />
        <el-table-column label="期待到货时间" align="center" prop="expectArriveTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.expectArriveTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="箱数" align="center" prop="casesNum" />
        <el-table-column label="单箱数量" align="center" prop="quantityPerCase" />
        <el-table-column label="是否删除：0 否，1 是" align="center" prop="isDelete" />
        <el-table-column label="税率" align="center" prop="taxRate" />
        <el-table-column label="SPU" align="center" prop="spu" />
        <el-table-column label="款名" align="center" prop="spuName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['amz:purchaseOrderItem:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['amz:purchaseOrderItem:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改供应商订单产品明细对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="purchaseOrderItemFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购单子项ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入采购单子项ID" />
        </el-form-item>
        <el-form-item label="关联采购单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入关联采购单号" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="wid">
          <el-input v-model="form.wid" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="wareHouseName">
          <el-input v-model="form.wareHouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="采购计划号" prop="planSn">
          <el-input v-model="form.planSn" placeholder="请输入采购计划号" />
        </el-form-item>
        <el-form-item label="本地产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入本地产品ID" />
        </el-form-item>
        <el-form-item label="品名" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入品名" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item label="FNSKU" prop="fnsku">
          <el-input v-model="form.fnsku" placeholder="请输入FNSKU" />
        </el-form-item>
        <el-form-item label="店铺ID" prop="sid">
          <el-input v-model="form.sid" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="含税单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入含税单价" />
        </el-form-item>
        <el-form-item label="价税合计" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入价税合计" />
        </el-form-item>
        <el-form-item label="计划采购量" prop="quantityPlan">
          <el-input v-model="form.quantityPlan" placeholder="请输入计划采购量" />
        </el-form-item>
        <el-form-item label="实际采购量" prop="quantityReal">
          <el-input v-model="form.quantityReal" placeholder="请输入实际采购量" />
        </el-form-item>
        <el-form-item label="到货入库量" prop="quantityEntry">
          <el-input v-model="form.quantityEntry" placeholder="请输入到货入库量" />
        </el-form-item>
        <el-form-item label="待到货量" prop="quantityReceive">
          <el-input v-model="form.quantityReceive" placeholder="请输入待到货量" />
        </el-form-item>
        <el-form-item label="退货数" prop="quantityReturn">
          <el-input v-model="form.quantityReturn" placeholder="请输入退货数" />
        </el-form-item>
        <el-form-item label="换货量" prop="quantityExchange">
          <el-input v-model="form.quantityExchange" placeholder="请输入换货量" />
        </el-form-item>
        <el-form-item label="质检量" prop="quantityQc">
          <el-input v-model="form.quantityQc" placeholder="请输入质检量" />
        </el-form-item>
        <el-form-item label="待质检量" prop="quantityQcPrepare">
          <el-input v-model="form.quantityQcPrepare" placeholder="请输入待质检量" />
        </el-form-item>
        <el-form-item label="期待到货时间" prop="expectArriveTime">
          <el-date-picker clearable
            v-model="form.expectArriveTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择期待到货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="箱数" prop="casesNum">
          <el-input v-model="form.casesNum" placeholder="请输入箱数" />
        </el-form-item>
        <el-form-item label="单箱数量" prop="quantityPerCase">
          <el-input v-model="form.quantityPerCase" placeholder="请输入单箱数量" />
        </el-form-item>
        <el-form-item label="是否删除：0 否，1 是" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除：0 否，1 是" />
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="form.taxRate" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="SPU" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入SPU" />
        </el-form-item>
        <el-form-item label="款名" prop="spuName">
          <el-input v-model="form.spuName" placeholder="请输入款名" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PurchaseOrderItem" lang="ts">
import { listPurchaseOrderItem, getPurchaseOrderItem, delPurchaseOrderItem, addPurchaseOrderItem, updatePurchaseOrderItem } from '@/api/amz/purchaseOrderItem';
import { PurchaseOrderItemVO, PurchaseOrderItemQuery, PurchaseOrderItemForm } from '@/api/amz/purchaseOrderItem/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const purchaseOrderItemList = ref<PurchaseOrderItemVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const purchaseOrderItemFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: PurchaseOrderItemForm = {
  id: undefined,
  orderSn: undefined,
  wid: undefined,
  wareHouseName: undefined,
  planSn: undefined,
  productId: undefined,
  productName: undefined,
  sku: undefined,
  fnsku: undefined,
  sid: undefined,
  model: undefined,
  price: undefined,
  amount: undefined,
  quantityPlan: undefined,
  quantityReal: undefined,
  quantityEntry: undefined,
  quantityReceive: undefined,
  quantityReturn: undefined,
  quantityExchange: undefined,
  quantityQc: undefined,
  quantityQcPrepare: undefined,
  expectArriveTime: undefined,
  remark: undefined,
  casesNum: undefined,
  quantityPerCase: undefined,
  isDelete: undefined,
  taxRate: undefined,
  spu: undefined,
  spuName: undefined,
}
const data = reactive<PageData<PurchaseOrderItemForm, PurchaseOrderItemQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderSn: undefined,
    wid: undefined,
    wareHouseName: undefined,
    planSn: undefined,
    productId: undefined,
    productName: undefined,
    sku: undefined,
    fnsku: undefined,
    sid: undefined,
    model: undefined,
    price: undefined,
    amount: undefined,
    quantityPlan: undefined,
    quantityReal: undefined,
    quantityEntry: undefined,
    quantityReceive: undefined,
    quantityReturn: undefined,
    quantityExchange: undefined,
    quantityQc: undefined,
    quantityQcPrepare: undefined,
    expectArriveTime: undefined,
    casesNum: undefined,
    quantityPerCase: undefined,
    isDelete: undefined,
    taxRate: undefined,
    spu: undefined,
    spuName: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "采购单子项ID不能为空", trigger: "blur" }
    ],
    orderSn: [
      { required: true, message: "关联采购单号不能为空", trigger: "blur" }
    ],
    wid: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
    wareHouseName: [
      { required: true, message: "仓库名称不能为空", trigger: "blur" }
    ],
    planSn: [
      { required: true, message: "采购计划号不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "本地产品ID不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "品名不能为空", trigger: "blur" }
    ],
    sku: [
      { required: true, message: "SKU不能为空", trigger: "blur" }
    ],
    fnsku: [
      { required: true, message: "FNSKU不能为空", trigger: "blur" }
    ],
    sid: [
      { required: true, message: "店铺ID不能为空", trigger: "blur" }
    ],
    model: [
      { required: true, message: "型号不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "含税单价不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "价税合计不能为空", trigger: "blur" }
    ],
    quantityPlan: [
      { required: true, message: "计划采购量不能为空", trigger: "blur" }
    ],
    quantityReal: [
      { required: true, message: "实际采购量不能为空", trigger: "blur" }
    ],
    quantityEntry: [
      { required: true, message: "到货入库量不能为空", trigger: "blur" }
    ],
    quantityReceive: [
      { required: true, message: "待到货量不能为空", trigger: "blur" }
    ],
    quantityReturn: [
      { required: true, message: "退货数不能为空", trigger: "blur" }
    ],
    quantityExchange: [
      { required: true, message: "换货量不能为空", trigger: "blur" }
    ],
    quantityQc: [
      { required: true, message: "质检量不能为空", trigger: "blur" }
    ],
    quantityQcPrepare: [
      { required: true, message: "待质检量不能为空", trigger: "blur" }
    ],
    expectArriveTime: [
      { required: true, message: "期待到货时间不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    casesNum: [
      { required: true, message: "箱数不能为空", trigger: "blur" }
    ],
    quantityPerCase: [
      { required: true, message: "单箱数量不能为空", trigger: "blur" }
    ],
    isDelete: [
      { required: true, message: "是否删除：0 否，1 是不能为空", trigger: "blur" }
    ],
    taxRate: [
      { required: true, message: "税率不能为空", trigger: "blur" }
    ],
    spu: [
      { required: true, message: "SPU不能为空", trigger: "blur" }
    ],
    spuName: [
      { required: true, message: "款名不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询供应商订单产品明细列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPurchaseOrderItem(queryParams.value);
  purchaseOrderItemList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  purchaseOrderItemFormRef.value?.resetFields();
}

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: PurchaseOrderItemVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加供应商订单产品明细";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: PurchaseOrderItemVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getPurchaseOrderItem(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改供应商订单产品明细";
}

/** 提交按钮 */
const submitForm = () => {
  purchaseOrderItemFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updatePurchaseOrderItem(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addPurchaseOrderItem(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: PurchaseOrderItemVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除供应商订单产品明细编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delPurchaseOrderItem(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('amz/purchaseOrderItem/export', {
    ...queryParams.value
  }, `purchaseOrderItem_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
