<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>
        <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="货架编码" align="center" prop="locationCode" width="180" />
      <el-table-column label="货架名称" align="center" prop="locationName" />
      <el-table-column label="所属库区" align="center" prop="area.areaName" />
      <el-table-column label="所属仓库" align="center" prop="area.warehouse.warehouseName" />
      <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" width="80">
        <template #default="scope">
           <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <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"
    />

    <!-- 库区选择对话框组件 -->
    <AreaSelectTreeDialog
      ref="areaSelectDialogRef"
      v-model:visible="areaDialogVisible"
      title="选择库区"
      @confirm="handleAreaConfirm"
    />

    <!-- 添加或修改货架信息对话框 -->
    <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="所属库区" prop="areaId">
          <el-row :gutter="10">
            <el-col :span="18">
              <el-input v-model="form.areaId" placeholder="请选择所属库区" readonly />
            </el-col>
            <el-col :span="6">
              <el-button type="primary" @click="openAreaSelect">选择库区</el-button>
            </el-col>
          </el-row>
        </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="备注" 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"
import AreaSelectTreeDialog from "@/components/AreaSelectTreeDialog.vue"

const { proxy } = getCurrentInstance()

const {sys_normal_disable} = proxy.useDict('sys_normal_disable')

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 areaDialogVisible = ref(false)
// 库区选择对话框实例引用
const areaSelectDialogRef = ref(null)

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`)
}

// 打开库区选择对话框
function openAreaSelect() {
  areaDialogVisible.value = true
  if (areaSelectDialogRef.value) {
    areaSelectDialogRef.value.handleQuery()
  }
}

// 处理库区选择确认
function handleAreaConfirm(areas) {
  if (areas && areas.length > 0) {
    form.value.areaId = areas[0].areaId
    // 如果需要显示库区名称，可以添加一个areaName字段
  }
}

getList()
</script>
