<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" label-width="100px">
            <el-form-item label="采购单号" prop="orderSn">
              <el-input v-model="queryParams.orderSn" placeholder="请输入采购单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="店铺名称" prop="storeName">
              <el-input v-model="queryParams.storeName" placeholder="请输入采购店铺名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="SKU" prop="sku">
              <el-input v-model="queryParams.sku" placeholder="请输入产品唯一标识码" 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="发货数量" prop="quantitySend">-->
            <!--              <el-input v-model="queryParams.quantitySend" placeholder="请输入发货数量" clearable @keyup.enter="handleQuery" />-->
            <!--            </el-form-item>-->
            <el-form-item label="发货方" prop="outSupplierName">
              <el-input v-model="queryParams.outSupplierName" placeholder="请输入发货供应商名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!--            <el-form-item label="发货方ID" prop="outSupplierId">-->
            <!--              <el-input v-model="queryParams.outSupplierId" placeholder="请输入发货供应商系统ID" clearable @keyup.enter="handleQuery" />-->
            <!--            </el-form-item>-->
            <el-form-item label="收货方" prop="inSupplierName">
              <el-input v-model="queryParams.inSupplierName" placeholder="请输入收货供应商名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!--            <el-form-item label="收货方ID" prop="inSupplierId">-->
            <!--              <el-input v-model="queryParams.inSupplierId" placeholder="请输入收货供应商系统ID" clearable @keyup.enter="handleQuery" />-->
            <!--            </el-form-item>-->
            <el-form-item label="跟单姓名" prop="trackerName">
              <el-input v-model="queryParams.trackerName" placeholder="请输入跟单人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!--            <el-form-item label="跟单ID" prop="trackerId">-->
            <!--              <el-input v-model="queryParams.trackerId" placeholder="请输入跟单id" clearable @keyup.enter="handleQuery" />-->
            <!--            </el-form-item>-->
            <el-form-item label="发货时间" prop="sendTime">
              <el-date-picker clearable v-model="queryParams.sendTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择发货时间" />
            </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:shipForward:add']">新增 </el-button>-->
          <!--                </el-col>-->
          <!--                <el-col :span="1.5">-->
          <!--                  <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['amz:shipForward:edit']"-->
          <!--                    >修改-->
          <!--                  </el-button>-->
          <!--                </el-col>-->
          <!--                <el-col :span="1.5">-->
          <!--                  <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['amz:shipForward:remove']"-->
          <!--                    >删除-->
          <!--                  </el-button>-->
          <!--                </el-col>-->
          <!--                <el-col :span="1.5">-->
          <!--                  <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['amz:shipForward:export']">导出 </el-button>-->
          <!--                </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="shipForwardList" @selection-change="handleSelectionChange">
        <!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="转发单号" align="center" prop="id" />
        <el-table-column label="采购单号" align="center" prop="orderSn" />
        <el-table-column label="采购店铺名称" align="center" prop="storeName" />
        <el-table-column label="SKU" align="center" prop="sku" />
        <el-table-column label="产品全称" align="center" prop="productName" />
        <el-table-column label="计划发货数量" align="center" prop="quantityPlan" />

        <el-table-column label="发货数量" align="center" prop="quantitySend" />
        <el-table-column label="发货箱数" align="center" prop="boxNum" />
        <el-table-column label="发货方" align="center" prop="outSupplierName" />
        <el-table-column label="总金额" align="center" prop="totalPrice" />
        <!--        <el-table-column label="发货供应商系统ID" align="center" prop="outSupplierId" />-->
        <el-table-column label="收货方" align="center" prop="inSupplierName" />
        <!--        <el-table-column label="收货供应商系统ID" align="center" prop="inSupplierId" />-->
        <!--        <el-table-column label="计划发货起始时间" align="center" prop="shipmentStart" width="180">-->
        <!--          <template #default="scope">-->
        <!--            <span>{{ parseTime(scope.row.shipmentStart, '{y}-{m}-{d}') }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="计划发货截止时间" align="center" prop="shipmentEnd" width="180">-->
        <!--          <template #default="scope">-->
        <!--            <span>{{ parseTime(scope.row.shipmentEnd, '{y}-{m}-{d}') }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="跟单人" align="center" prop="trackerName" />
        <!--        <el-table-column label="跟单id" align="center" prop="trackerId" />-->
        <el-table-column label="转发状态" align="center" prop="fwdStatus">
          <template #default="scope">
            <dict-tag :options="forward_status" :value="scope.row.fwdStatus" />
          </template>
        </el-table-column>
        <el-table-column label="发货时间" align="center" prop="sendTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发货箱数" align="center" prop="boxNum" />
        <el-table-column label="预计到达" align="center" prop="expDeliveryDate" width="180" />

        <!--        <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:shipForward:edit']"></el-button>-->
        <!--            </el-tooltip>-->
        <!--            <el-tooltip content="删除" placement="top">-->
        <!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['amz:shipForward: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="800px" append-to-body>
      <el-form ref="shipForwardFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="采购单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入采购单号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入采购店铺名称" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入产品唯一标识码" />
        </el-form-item>
        <el-form-item label="产品全称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品全称" />
        </el-form-item>
        <el-form-item label="发货数量" prop="quantitySend">
          <el-input v-model="form.quantitySend" placeholder="请输入发货数量" />
        </el-form-item>
        <el-form-item label="发货箱数" prop="quantitySend">
          <el-input v-model="form.boxNum" placeholder="请输入发货箱数" disabled />
        </el-form-item>
        <el-form-item label="发货方" prop="outSupplierName">
          <el-input v-model="form.outSupplierName" placeholder="请输入发货供应商名称" />
        </el-form-item>
        <!--        <el-form-item label="发货方ID" prop="outSupplierId">-->
        <!--          <el-input v-model="form.outSupplierId" placeholder="请输入发货供应商系统ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="收货方" prop="inSupplierName">
          <el-input v-model="form.inSupplierName" placeholder="请输入收货供应商名称" />
        </el-form-item>
        <!--        <el-form-item label="收货方ID" prop="inSupplierId">-->
        <!--          <el-input v-model="form.inSupplierId" placeholder="请输入收货供应商系统ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="发货区间" prop="shipmentStart">
          <el-date-picker
            v-model="intervalDate"
            type="daterange"
            range-separator="到"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :size="'small'"
            @change="intervalAction"
          />
        </el-form-item>
        <el-form-item label="跟单人" prop="trackerName">
          <el-input v-model="form.trackerName" placeholder="请输入跟单人" />
        </el-form-item>
        <!--        <el-form-item label="跟单id" prop="trackerId">-->
        <!--          <el-input v-model="form.trackerId" placeholder="请输入跟单id" />-->
        <!--        </el-form-item>-->
        <el-form-item label="发货时间" prop="sendTime">
          <el-date-picker clearable v-model="form.sendTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择发货时间">
          </el-date-picker>
        </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>

    <el-dialog :title="orderDialog.title" v-model="orderDialog.visible" width="1500px" append-to-body>
      <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:purchaseOrder:add']"> 新增 </el-button>-->
        <!--            </el-col>-->
        <!--            <el-col :span="1.5">-->
        <!--              <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['amz:purchaseOrder:edit']"-->
        <!--                >修改-->
        <!--              </el-button>-->
        <!--            </el-col>-->
        <!--            <el-col :span="1.5">-->
        <!--              <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['amz:purchaseOrder:remove']"-->
        <!--                >删除-->
        <!--              </el-button>-->
        <!--            </el-col>-->
        <!--            <el-col :span="1.5">-->
        <!--              <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['amz:purchaseOrder:export']">导出 </el-button>-->
        <!--            </el-col>-->
        <!--            <right-toolbar v-model:showSearch="showSearch" @queryTable="getOrderList"></right-toolbar>-->
        <!--          </el-row>-->
        <!--        </template>-->

        <el-table v-loading="orderLoading" :data="purchaseOrderList" @selection-change="handleSelectionChange">
          <!--        <el-table-column type="selection" width="55" align="center" />-->
          <el-table-column label="采购单号" align="center" prop="orderSn" />
          <!--        <el-table-column label="自定义单号" align="center" prop="customOrderSn" />-->
          <!--        <el-table-column label="供应商ID" align="center" prop="supplierId" />-->
          <el-table-column label="供应商名称" align="center" prop="supplierName" />
          <el-table-column label="店铺名称" align="center" prop="storeName" />
          <!--        <el-table-column label="采购员ID" align="center" prop="optUid" />-->
          <!--        <el-table-column label="审核人姓名" align="center" prop="auditorRealname" />-->
          <!--        <el-table-column label="操作人姓名" align="center" prop="optRealname" />-->
          <!--        <el-table-column label="最后操作人姓名" align="center" prop="lastRealname" />-->
          <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <!--        <el-table-column label="应付货款" align="center" prop="payment" />-->
          <!--        <el-table-column label="审核人员ID" align="center" prop="auditorUid" />-->
          <!--        <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">-->
          <!--          <template #default="scope">-->
          <!--            <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>-->
          <!--          </template>-->
          <!--        </el-table-column>-->
          <!--        <el-table-column label="最后操作人员ID" align="center" prop="lastUid" />-->
          <!--        <el-table-column label="最后操作时间" align="center" prop="lastTime" width="180">-->
          <!--          <template #default="scope">-->
          <!--            <span>{{ parseTime(scope.row.lastTime, '{y}-{m}-{d}') }}</span>-->
          <!--          </template>-->
          <!--        </el-table-column>-->
          <!--        <el-table-column label="作废原因" align="center" prop="reason" />-->
          <!--        <el-table-column label="是否含税" align="center" prop="isTax" />-->
          <!--        <el-table-column label="采购单状态" align="center" prop="status" />-->
          <!--        <el-table-column label="状态说明" align="center" prop="statusText" />-->
          <!--        <el-table-column label="支付状态说明" align="center" prop="payStatusText" />-->
          <!--        <el-table-column label="到货状态" align="center" prop="statusShipped" />-->
          <!--        <el-table-column label="到货状态说明" align="center" prop="statusShippedText" />-->
          <!--        <el-table-column label="货物总价" align="center" prop="amountTotal" />-->
          <!--        <el-table-column label="总金额" align="center" prop="totalPrice" />-->
          <!--        <el-table-column label="币种符号" align="center" prop="icon" />-->
          <!--        <el-table-column label="付款状态" align="center" prop="payStatus" />-->

          <!--        <el-table-column label="其他费用" align="center" prop="otherFee" />-->
          <!--        <el-table-column label="其他费用币种" align="center" prop="otherCurrency" />-->
          <!--        <el-table-column label="费用分摊方式" align="center" prop="feePartType" />-->
          <!--        <el-table-column label="运费" align="center" prop="shippingPrice" />-->
          <!--        <el-table-column label="运费币种" align="center" prop="shippingCurrency" />-->
          <!--        <el-table-column label="采购币种" align="center" prop="purchaseCurrency" />-->
          <!--        <el-table-column label="采购汇率" align="center" prop="purchaseRate" />-->
          <!--        <el-table-column label="采购总量" align="center" prop="quantityTotal" />-->
          <!--        <el-table-column label="仓库ID" align="center" prop="wid" />-->
          <!--        <el-table-column label="仓库名称" align="center" prop="wareHouseName" />-->
          <!--        <el-table-column label="仓库名(备份)" align="center" prop="wareHouseBakName" />-->
          <!--        <el-table-column label="入库量" align="center" prop="quantityEntry" />-->
          <el-table-column label="实际采购量" align="center" prop="quantityReal" />
          <!--        <el-table-column label="待到货量" align="center" prop="quantityReceive" />-->
          <!--        <el-table-column label="采购方ID" align="center" prop="purchaserId" />-->
          <!--        <el-table-column label="联系人" align="center" prop="contactPerson" />-->
          <!--        <el-table-column label="联系方式" align="center" prop="contactNumber" />-->
          <!--        <el-table-column label="结算方式" align="center" prop="settlementMethod" />-->
          <!--        <el-table-column label="结算描述" align="center" prop="settlementDescription" />-->
          <!--        <el-table-column label="支付方式" align="center" prop="paymentMethod" />-->
          <el-table-column label="交货天数" align="center" prop="estDlvDays" />
          <!--          <el-table-column label="订单状态" align="center" prop="orderStatus">-->
          <!--            <template #default="scope">-->
          <!--              <dict-tag :options="vendor_order_status" :value="scope.row.orderStatus" />-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="交货时间" align="center" prop="deliveryTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="订单交付变动天数" align="center" prop="dlvTimeVar" />
          <el-table-column label="备注" align="center" prop="remark" />
          <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:purchaseOrder:edit']"></el-button>
              </el-tooltip>

              <!--              <el-button size="small" link type="primary" @click="handleConfirm(scope.row)" v-hasPermi="['amz:purchaseOrder:remove']">-->
              <!--                确认-->
              <!--              </el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="orderTotal > 0"
          :total="orderTotal"
          v-model:page="orderQueryParams.pageNum"
          v-model:limit="orderQueryParams.pageSize"
          @pagination="getOrderList"
        />
      </el-card>
      <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="ShipForward" lang="ts">
import { listShipForward, getShipForward, delShipForward, addShipForward, updateShipForward } from '@/api/amz/shipForward';
import { ShipForwardVO, ShipForwardQuery, ShipForwardForm } from '@/api/amz/shipForward/types';
import { listPurchaseOrder } from '@/api/amz/purchaseOrder';
import { PurchaseOrderVO } from '@/api/amz/purchaseOrder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { forward_status } = toRefs<any>(proxy?.useDict('forward_status'));

const shipForwardList = ref<ShipForwardVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const orderLoading = 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 orderTotal = ref(0);

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

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

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

const initFormData: ShipForwardForm = {
  id: undefined,
  orderSn: undefined,
  storeName: undefined,
  sku: undefined,
  productName: undefined,
  quantitySend: undefined,
  outSupplierName: undefined,
  outSupplierId: undefined,
  inSupplierName: undefined,
  inSupplierId: undefined,
  shipmentStart: undefined,
  shipmentEnd: undefined,
  trackerName: undefined,
  trackerId: undefined,
  fwdStatus: undefined,
  sendTime: undefined
};
const data = reactive<PageData<ShipForwardForm, ShipForwardQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderSn: undefined,
    storeName: undefined,
    sku: undefined,
    productName: undefined,
    quantitySend: undefined,
    outSupplierName: undefined,
    outSupplierId: undefined,
    inSupplierName: undefined,
    inSupplierId: undefined,
    shipmentStart: undefined,
    shipmentEnd: undefined,
    trackerName: undefined,
    trackerId: undefined,
    fwdStatus: undefined,
    sendTime: undefined,
    type: 'shipForward',
    params: {}
  },
  rules: {
    id: [{ required: true, message: '$comment不能为空', trigger: 'blur' }],
    orderSn: [{ required: true, message: '采购单号不能为空', trigger: 'blur' }],
    storeName: [{ required: true, message: '采购店铺名称不能为空', trigger: 'blur' }],
    sku: [{ required: true, message: '产品唯一标识码不能为空', trigger: 'blur' }],
    productName: [{ required: true, message: '产品全称不能为空', trigger: 'blur' }],
    quantitySend: [{ required: true, message: '发货数量不能为空', trigger: 'blur' }],
    outSupplierName: [{ required: true, message: '发货供应商名称不能为空', trigger: 'blur' }],
    outSupplierId: [{ required: true, message: '发货供应商系统ID不能为空', trigger: 'blur' }],
    inSupplierName: [{ required: true, message: '收货供应商名称不能为空', trigger: 'blur' }],
    inSupplierId: [{ required: true, message: '收货供应商系统ID不能为空', trigger: 'blur' }],
    shipmentStart: [{ required: true, message: '计划发货起始时间不能为空', trigger: 'blur' }],
    shipmentEnd: [{ required: true, message: '计划发货截止时间不能为空', trigger: 'blur' }],
    trackerName: [{ required: true, message: '跟单人不能为空', trigger: 'blur' }],
    trackerId: [{ required: true, message: '跟单id不能为空', trigger: 'blur' }],
    fwdStatus: [{ required: true, message: '转发状态不能为空', trigger: 'change' }],
    sendTime: [{ required: true, message: '发货时间不能为空', trigger: 'blur' }]
  }
});

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

const orderQueryParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderSn: undefined,
  customOrderSn: undefined,
  supplierId: undefined,
  supplierName: undefined,
  optUid: undefined,
  auditorRealname: undefined,
  optRealname: undefined,
  lastRealname: undefined,
  orderTime: undefined,
  payment: undefined,
  auditorUid: undefined,
  auditorTime: undefined,
  lastUid: undefined,
  lastTime: undefined,
  reason: undefined,
  isTax: undefined,
  status: undefined,
  statusText: undefined,
  payStatusText: undefined,
  statusShipped: undefined,
  statusShippedText: undefined,
  amountTotal: undefined,
  totalPrice: undefined,
  icon: undefined,
  payStatus: undefined,
  otherFee: undefined,
  otherCurrency: undefined,
  feePartType: undefined,
  shippingPrice: undefined,
  shippingCurrency: undefined,
  purchaseCurrency: undefined,
  purchaseRate: undefined,
  quantityTotal: undefined,
  wid: undefined,
  wareHouseName: undefined,
  wareHouseBakName: undefined,
  quantityEntry: undefined,
  quantityReal: undefined,
  quantityReceive: undefined,
  purchaserId: undefined,
  contactPerson: undefined,
  contactNumber: undefined,
  settlementMethod: undefined,
  settlementDescription: undefined,
  paymentMethod: undefined,
  estDlvDays: undefined,
  orderStatus: 'ord_confirmed',
  deliveryTime: undefined,
  dlvTimeVar: undefined,
  params: {}
});

/** 查询货件转发列表 */
const getList = async () => {
  loading.value = true;
  const res = await listShipForward(queryParams.value);
  shipForwardList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const purchaseOrderList = ref<PurchaseOrderVO[]>([]);

const getOrderList = async () => {
  orderLoading.value = true;
  const res = await listPurchaseOrder(orderQueryParams.value);
  purchaseOrderList.value = res.rows;
  orderTotal.value = res.total;
  orderLoading.value = false;
};

const intervalDate = ref<any>();

const intervalAction = () => {
  console.log('intervalDate', intervalDate);
  queryParams.value.shipmentStart = getDateToString(intervalDate.value[0]);
  queryParams.value.shipmentEnd = getDateToString(intervalDate.value[1]);
};

const getDateToString = (date: Date): string => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需加 1
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

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

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

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '订单列表';

  // getOrderList();
  // orderDialog.visible = true;
  // orderDialog.title = '订单列表';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ShipForwardVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getShipForward(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改货件转发';
};

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

/** 删除按钮操作 */
const handleDelete = async (row?: ShipForwardVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除货件转发编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delShipForward(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

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