<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入入库单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联单号" prop="relatedOrderNo">
        <el-input
          v-model="queryParams.relatedOrderNo"
          placeholder="请输入关联单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入总数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applicantId">
        <el-input
          v-model="queryParams.applicantId"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计到货日期" prop="expectedArrivalDate">
        <el-date-picker clearable
          v-model="queryParams.expectedArrivalDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预计到货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际到货日期" prop="actualArrivalDate">
        <el-date-picker clearable
          v-model="queryParams.actualArrivalDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择实际到货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="质检员" prop="qualityCheckerId">
        <el-input
          v-model="queryParams.qualityCheckerId"
          placeholder="请输入质检员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="质检时间" prop="qualityCheckTime">
        <el-date-picker clearable
          v-model="queryParams.qualityCheckTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择质检时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上架员" prop="putawayPersonId">
        <el-input
          v-model="queryParams.putawayPersonId"
          placeholder="请输入上架员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架时间" prop="putawayTime">
        <el-date-picker clearable
          v-model="queryParams.putawayTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择上架时间">
        </el-date-picker>
      </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:wmsInboundOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsInboundOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsInboundOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsInboundOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsInboundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库单ID" align="center" prop="orderId" />
      <el-table-column label="入库单号" align="center" prop="orderNo" />
      <el-table-column label="入库类型" align="center" prop="orderType" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="供应商ID" align="center" prop="supplierId" />
      <el-table-column label="关联单号" align="center" prop="relatedOrderNo" />
      <el-table-column label="总数量" align="center" prop="totalQuantity" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="单据状态" align="center" prop="orderStatus" />
      <el-table-column label="申请人" align="center" prop="applicantId" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计到货日期" align="center" prop="expectedArrivalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expectedArrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际到货日期" align="center" prop="actualArrivalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.actualArrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检员" align="center" prop="qualityCheckerId" />
      <el-table-column label="质检时间" align="center" prop="qualityCheckTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.qualityCheckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检状态" align="center" prop="qualityStatus" />
      <el-table-column label="上架员" align="center" prop="putawayPersonId" />
      <el-table-column label="上架时间" align="center" prop="putawayTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.putawayTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="approvalStatus" />
      <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:wmsInboundOrder:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsInboundOrder: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="wmsInboundOrderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入入库单号" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="关联单号" prop="relatedOrderNo">
          <el-input v-model="form.relatedOrderNo" placeholder="请输入关联单号" />
        </el-form-item>
        <el-form-item label="总数量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总数量" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="申请人" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计到货日期" prop="expectedArrivalDate">
          <el-date-picker clearable
            v-model="form.expectedArrivalDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择预计到货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际到货日期" prop="actualArrivalDate">
          <el-date-picker clearable
            v-model="form.actualArrivalDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择实际到货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="质检员" prop="qualityCheckerId">
          <el-input v-model="form.qualityCheckerId" placeholder="请输入质检员" />
        </el-form-item>
        <el-form-item label="质检时间" prop="qualityCheckTime">
          <el-date-picker clearable
            v-model="form.qualityCheckTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择质检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上架员" prop="putawayPersonId">
          <el-input v-model="form.putawayPersonId" placeholder="请输入上架员" />
        </el-form-item>
        <el-form-item label="上架时间" prop="putawayTime">
          <el-date-picker clearable
            v-model="form.putawayTime"
            type="date"
            value-format="YYYY-MM-DD"
            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>
      <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="WmsInboundOrder">
import { listWmsInboundOrder, getWmsInboundOrder, delWmsInboundOrder, addWmsInboundOrder, updateWmsInboundOrder } from "@/api/ware/wmsInboundOrder"

const { proxy } = getCurrentInstance()

const wmsInboundOrderList = 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,
    orderNo: null,
    orderType: null,
    warehouseId: null,
    supplierId: null,
    relatedOrderNo: null,
    totalQuantity: null,
    totalAmount: null,
    orderStatus: null,
    applicantId: null,
    applyTime: null,
    expectedArrivalDate: null,
    actualArrivalDate: null,
    qualityCheckerId: null,
    qualityCheckTime: null,
    qualityStatus: null,
    putawayPersonId: null,
    putawayTime: null,
    approvalStatus: null,
  },
  rules: {
    orderNo: [
      { required: true, message: "入库单号不能为空", trigger: "blur" }
    ],
    orderType: [
      { required: true, message: "入库类型不能为空", trigger: "change" }
    ],
    warehouseId: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询入库单主列表 */
function getList() {
  loading.value = true
  listWmsInboundOrder(queryParams.value).then(response => {
    wmsInboundOrderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderNo: null,
    orderType: null,
    warehouseId: null,
    supplierId: null,
    relatedOrderNo: null,
    totalQuantity: null,
    totalAmount: null,
    orderStatus: null,
    applicantId: null,
    applyTime: null,
    expectedArrivalDate: null,
    actualArrivalDate: null,
    qualityCheckerId: null,
    qualityCheckTime: null,
    qualityStatus: null,
    putawayPersonId: null,
    putawayTime: null,
    approvalStatus: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("wmsInboundOrderRef")
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _orderId = row.orderId || ids.value
  getWmsInboundOrder(_orderId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改入库单主"
  })
}

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

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

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

getList()
</script>
