<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库位编码" prop="locationCode">
        <el-input
          v-model="queryParams.locationCode"
          placeholder="请输入库位编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位名称" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入库位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属库区ID" prop="areaId">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入所属库区ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="X坐标" prop="xCoordinate">
        <el-input
          v-model="queryParams.xCoordinate"
          placeholder="请输入X坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Y坐标" prop="yCoordinate">
        <el-input
          v-model="queryParams.yCoordinate"
          placeholder="请输入Y坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Z坐标" prop="zCoordinate">
        <el-input
          v-model="queryParams.zCoordinate"
          placeholder="请输入Z坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大容量" prop="maxCapacity">
        <el-input
          v-model="queryParams.maxCapacity"
          placeholder="请输入最大容量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前容量" prop="currentCapacity">
        <el-input
          v-model="queryParams.currentCapacity"
          placeholder="请输入当前容量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承重限制" prop="weightLimit">
        <el-input
          v-model="queryParams.weightLimit"
          placeholder="请输入承重限制"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RFID天线ID" prop="rfidAntennaId">
        <el-input
          v-model="queryParams.rfidAntennaId"
          placeholder="请输入RFID天线ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后盘点时间" prop="lastInventoryTime">
        <el-date-picker clearable
          v-model="queryParams.lastInventoryTime"
          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:wmsLocation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ware:wmsLocation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ware:wmsLocation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ware:wmsLocation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsLocationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库位ID" align="center" prop="locationId" />
      <el-table-column label="库位编码" align="center" prop="locationCode" />
      <el-table-column label="库位名称" align="center" prop="locationName" />
      <el-table-column label="所属库区ID" align="center" prop="areaId" />
      <el-table-column label="库位类型" align="center" prop="locationType" />
      <el-table-column label="X坐标" align="center" prop="xCoordinate" />
      <el-table-column label="Y坐标" align="center" prop="yCoordinate" />
      <el-table-column label="Z坐标" align="center" prop="zCoordinate" />
      <el-table-column label="最大容量" align="center" prop="maxCapacity" />
      <el-table-column label="当前容量" align="center" prop="currentCapacity" />
      <el-table-column label="承重限制" align="center" prop="weightLimit" />
      <el-table-column label="长度" align="center" prop="length" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="高度" align="center" prop="height" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="RFID天线ID" align="center" prop="rfidAntennaId" />
      <el-table-column label="最后盘点时间" align="center" prop="lastInventoryTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastInventoryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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:wmsLocation:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ware:wmsLocation: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="wmsLocationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库位编码" prop="locationCode">
          <el-input v-model="form.locationCode" placeholder="请输入库位编码" />
        </el-form-item>
        <el-form-item label="库位名称" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入库位名称" />
        </el-form-item>
        <el-form-item label="所属库区ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属库区ID" />
        </el-form-item>
        <el-form-item label="X坐标" prop="xCoordinate">
          <el-input v-model="form.xCoordinate" placeholder="请输入X坐标" />
        </el-form-item>
        <el-form-item label="Y坐标" prop="yCoordinate">
          <el-input v-model="form.yCoordinate" placeholder="请输入Y坐标" />
        </el-form-item>
        <el-form-item label="Z坐标" prop="zCoordinate">
          <el-input v-model="form.zCoordinate" placeholder="请输入Z坐标" />
        </el-form-item>
        <el-form-item label="最大容量" prop="maxCapacity">
          <el-input v-model="form.maxCapacity" placeholder="请输入最大容量" />
        </el-form-item>
        <el-form-item label="当前容量" prop="currentCapacity">
          <el-input v-model="form.currentCapacity" placeholder="请输入当前容量" />
        </el-form-item>
        <el-form-item label="承重限制" prop="weightLimit">
          <el-input v-model="form.weightLimit" placeholder="请输入承重限制" />
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input v-model="form.length" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入高度" />
        </el-form-item>
        <el-form-item label="RFID天线ID" prop="rfidAntennaId">
          <el-input v-model="form.rfidAntennaId" placeholder="请输入RFID天线ID" />
        </el-form-item>
        <el-form-item label="最后盘点时间" prop="lastInventoryTime">
          <el-date-picker clearable
            v-model="form.lastInventoryTime"
            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="WmsLocation">
import { listWmsLocation, getWmsLocation, delWmsLocation, addWmsLocation, updateWmsLocation } from "@/api/ware/wmsLocation"

const { proxy } = getCurrentInstance()

const wmsLocationList = 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,
    locationCode: null,
    locationName: null,
    areaId: null,
    locationType: null,
    xCoordinate: null,
    yCoordinate: null,
    zCoordinate: null,
    maxCapacity: null,
    currentCapacity: null,
    weightLimit: null,
    length: null,
    width: null,
    height: null,
    status: null,
    rfidAntennaId: null,
    lastInventoryTime: null,
  },
  rules: {
    locationCode: [
      { required: true, message: "库位编码不能为空", trigger: "blur" }
    ],
    areaId: [
      { required: true, message: "所属库区ID不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询库位信息列表 */
function getList() {
  loading.value = true
  listWmsLocation(queryParams.value).then(response => {
    wmsLocationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    locationId: null,
    locationCode: null,
    locationName: null,
    areaId: null,
    locationType: null,
    xCoordinate: null,
    yCoordinate: null,
    zCoordinate: null,
    maxCapacity: null,
    currentCapacity: null,
    weightLimit: null,
    length: null,
    width: null,
    height: null,
    status: null,
    rfidAntennaId: null,
    lastInventoryTime: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("wmsLocationRef")
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _locationId = row.locationId || ids.value
  getWmsLocation(_locationId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改库位信息"
  })
}

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

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

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

getList()
</script>
