<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入出库单ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资ID" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物资ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划数量" prop="planQuantity">
        <el-input
          v-model="queryParams.planQuantity"
          placeholder="请输入计划数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际数量" prop="actualQuantity">
        <el-input
          v-model="queryParams.actualQuantity"
          placeholder="请输入实际数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          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="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拣货货架" prop="pickingLocation">
        <el-input
          v-model="queryParams.pickingLocation"
          placeholder="请输入拣货货架"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已拣数量" prop="pickingQuantity">
        <el-input
          v-model="queryParams.pickingQuantity"
          placeholder="请输入已拣数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="复核数量" prop="checkQuantity">
        <el-input
          v-model="queryParams.checkQuantity"
          placeholder="请输入复核数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货数量" prop="deliveryQuantity">
        <el-input
          v-model="queryParams.deliveryQuantity"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ware:wmsOutboundOrderItem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsOutboundOrderItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsOutboundOrderItem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsOutboundOrderItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsOutboundOrderItemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="明细ID" align="center" prop="itemId" />
      <el-table-column label="出库单ID" align="center" prop="orderId" />
      <el-table-column label="物资ID" align="center" prop="materialId" />
      <el-table-column label="计划数量" align="center" prop="planQuantity" />
      <el-table-column label="实际数量" align="center" prop="actualQuantity" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="批次号" align="center" prop="batchNo" />
      <el-table-column label="拣货货架" align="center" prop="pickingLocation" />
      <el-table-column label="已拣数量" align="center" prop="pickingQuantity" />
      <el-table-column label="拣货状态" align="center" prop="pickingStatus" />
      <el-table-column label="复核数量" align="center" prop="checkQuantity" />
      <el-table-column label="复核状态" align="center" prop="checkStatus" />
      <el-table-column label="发货数量" align="center" prop="deliveryQuantity" />
      <el-table-column label="发货状态" align="center" prop="deliveryStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ware:wmsOutboundOrderItem:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsOutboundOrderItem:remove']">删除</el-button>
        </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-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="wmsOutboundOrderItemRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入出库单ID" />
        </el-form-item>
        <el-form-item label="物资ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物资ID" />
        </el-form-item>
        <el-form-item label="计划数量" prop="planQuantity">
          <el-input v-model="form.planQuantity" placeholder="请输入计划数量" />
        </el-form-item>
        <el-form-item label="实际数量" prop="actualQuantity">
          <el-input v-model="form.actualQuantity" placeholder="请输入实际数量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" 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="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <el-form-item label="拣货货架" prop="pickingLocation">
          <el-input v-model="form.pickingLocation" placeholder="请输入拣货货架" />
        </el-form-item>
        <el-form-item label="已拣数量" prop="pickingQuantity">
          <el-input v-model="form.pickingQuantity" placeholder="请输入已拣数量" />
        </el-form-item>
        <el-form-item label="复核数量" prop="checkQuantity">
          <el-input v-model="form.checkQuantity" placeholder="请输入复核数量" />
        </el-form-item>
        <el-form-item label="发货数量" prop="deliveryQuantity">
          <el-input v-model="form.deliveryQuantity" placeholder="请输入发货数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WmsOutboundOrderItem">
import { listWmsOutboundOrderItem, getWmsOutboundOrderItem, delWmsOutboundOrderItem, addWmsOutboundOrderItem, updateWmsOutboundOrderItem } from "@/api/ware/wmsOutboundOrderItem"

const { proxy } = getCurrentInstance()

const wmsOutboundOrderItemList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    materialId: null,
    planQuantity: null,
    actualQuantity: null,
    unit: null,
    unitPrice: null,
    amount: null,
    batchNo: null,
    pickingLocation: null,
    pickingQuantity: null,
    pickingStatus: null,
    checkQuantity: null,
    checkStatus: null,
    deliveryQuantity: null,
    deliveryStatus: null,
  },
  rules: {
    orderId: [
      { required: true, message: "出库单ID不能为空", trigger: "blur" }
    ],
    materialId: [
      { required: true, message: "物资ID不能为空", trigger: "blur" }
    ],
    planQuantity: [
      { required: true, message: "计划数量不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询出库单明细列表 */
function getList() {
  loading.value = true
  listWmsOutboundOrderItem(queryParams.value).then(response => {
    wmsOutboundOrderItemList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    itemId: null,
    orderId: null,
    materialId: null,
    planQuantity: null,
    actualQuantity: null,
    unit: null,
    unitPrice: null,
    amount: null,
    batchNo: null,
    pickingLocation: null,
    pickingQuantity: null,
    pickingStatus: null,
    checkQuantity: null,
    checkStatus: null,
    deliveryQuantity: null,
    deliveryStatus: null,
    remark: null
  }
  proxy.resetForm("wmsOutboundOrderItemRef")
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.itemId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加出库单明细"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _itemId = row.itemId || ids.value
  getWmsOutboundOrderItem(_itemId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改出库单明细"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wmsOutboundOrderItemRef"].validate(valid => {
    if (valid) {
      if (form.value.itemId != null) {
        updateWmsOutboundOrderItem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWmsOutboundOrderItem(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _itemIds = row.itemId || ids.value
  proxy.$modal.confirm('是否确认删除出库单明细编号为"' + _itemIds + '"的数据项？').then(function() {
    return delWmsOutboundOrderItem(_itemIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ware/wmsOutboundOrderItem/export', {
    ...queryParams.value
  }, `wmsOutboundOrderItem_${new Date().getTime()}.xlsx`)
}

getList()
</script>
