<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="productionDate">
        <el-date-picker clearable
          v-model="queryParams.productionDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="失效日期" prop="expirationDate">
        <el-date-picker clearable
          v-model="queryParams.expirationDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择失效日期">
        </el-date-picker>
      </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="unqualifiedQuantity">
        <el-input
          v-model="queryParams.unqualifiedQuantity"
          placeholder="请输入不合格数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存储库位" prop="storageLocation">
        <el-input
          v-model="queryParams.storageLocation"
          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:wmsInboundOrderItem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsInboundOrderItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsInboundOrderItem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsInboundOrderItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsInboundOrderItemList" @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="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效日期" align="center" prop="expirationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检状态" align="center" prop="qualityStatus" />
      <el-table-column label="质检数量" align="center" prop="checkQuantity" />
      <el-table-column label="不合格数量" align="center" prop="unqualifiedQuantity" />
      <el-table-column label="不合格原因" align="center" prop="unqualifiedReason" />
      <el-table-column label="存储库位" align="center" prop="storageLocation" />
      <el-table-column label="上架状态" align="center" prop="putawayStatus" />
      <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:wmsInboundOrderItem:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsInboundOrderItem: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="wmsInboundOrderItemRef" :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="productionDate">
          <el-date-picker clearable
            v-model="form.productionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效日期" prop="expirationDate">
          <el-date-picker clearable
            v-model="form.expirationDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择失效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="质检数量" prop="checkQuantity">
          <el-input v-model="form.checkQuantity" placeholder="请输入质检数量" />
        </el-form-item>
        <el-form-item label="不合格数量" prop="unqualifiedQuantity">
          <el-input v-model="form.unqualifiedQuantity" placeholder="请输入不合格数量" />
        </el-form-item>
        <el-form-item label="不合格原因" prop="unqualifiedReason">
          <el-input v-model="form.unqualifiedReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="存储库位" prop="storageLocation">
          <el-input v-model="form.storageLocation" 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="WmsInboundOrderItem">
import { listWmsInboundOrderItem, getWmsInboundOrderItem, delWmsInboundOrderItem, addWmsInboundOrderItem, updateWmsInboundOrderItem } from "@/api/ware/wmsInboundOrderItem"

const { proxy } = getCurrentInstance()

const wmsInboundOrderItemList = 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,
    productionDate: null,
    expirationDate: null,
    qualityStatus: null,
    checkQuantity: null,
    unqualifiedQuantity: null,
    unqualifiedReason: null,
    storageLocation: null,
    putawayStatus: 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
  listWmsInboundOrderItem(queryParams.value).then(response => {
    wmsInboundOrderItemList.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,
    productionDate: null,
    expirationDate: null,
    qualityStatus: null,
    checkQuantity: null,
    unqualifiedQuantity: null,
    unqualifiedReason: null,
    storageLocation: null,
    putawayStatus: null,
    remark: null
  }
  proxy.resetForm("wmsInboundOrderItemRef")
}

/** 搜索按钮操作 */
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
  getWmsInboundOrderItem(_itemId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改入库单明细"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wmsInboundOrderItemRef"].validate(valid => {
    if (valid) {
      if (form.value.itemId != null) {
        updateWmsInboundOrderItem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWmsInboundOrderItem(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 delWmsInboundOrderItem(_itemIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

getList()
</script>
