<script setup lang="ts">

import type IStackerLocationTableData from "@/model/table/IStackerLocationTableData.ts";
import {ref} from "vue";
import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StackerLocationApi from "@/api/stackerLocation.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type IStackerLocationCreateParams from "@/interface/page/stackerLocation/IStackerLocationCreateParams.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {modelValue.value = false;};
const oneLocation = ref<IStackerLocationTableData>({
  locationType: 0,
  laneId: 1,
  machineId: 1,
  lRow: 1,
  lLine: 1,
  lLayer: 1,
  lDepth: 1,
}); // 单个添加的数据信息
const manyLocation = ref<IStackerLocationCreateParams>({
  locationType: 0,
  startString: '',
  laneId: 1,
  machineId: 1,
  lRow: 1,
  lLineTotal: 1,
  lLineOffset: 1,
  lDepthTotal: 1,
  lLayerTotal: 1,
  endString: '',
}); // 批量添加的货位信息

// 添加单个货位
const addOne = () => {
  if(StringUtils.isNullOrEmpty(oneLocation.value.locationId)) {
    MessageUtils.warningMessageBox("货位编号不能为空");
    return;
  }
  MessageUtils.confirmMessageBox('确定添加上述数据？','添加确认').then(()=>{
    const loadingInstance = MessageUtils.loading();
    StackerLocationApi.addOneStackerLocation(oneLocation.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{});
};
// 添加批量货位
const addMany = () => {
  MessageUtils.confirmMessageBox('确定批量添加配置的数据？','添加确认').then(()=>{
    const loadingInstance = MessageUtils.loading();
    StackerLocationApi.addStackerLocationBatch(manyLocation.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{});
};



</script>

<template>
  <el-dialog :model-value="modelValue" title="添加货位信息" @close="close" :close-on-click-modal="false">
      <el-row>
        <el-col :span="11" style="border: #e0e0e0 solid 1px; padding: 10px; border-radius: 5px">
          <el-scrollbar style="height:50vh">
          <h4 style="font-weight: bolder; margin-bottom: 10px">单个货位位添加：</h4>
          <el-row>
            <el-form label-position="right" :model="oneLocation" label-width="100px" require-asterisk-position="right" style="width: 100%; margin-right: 10px">
              <el-form-item label="货位编号：" required>
                <el-input v-model="oneLocation.locationId" placeholder="请输入货位编号"></el-input>
              </el-form-item>
              <el-form-item label="业务货位：" required>
                <el-input v-model="oneLocation.businessLocation" placeholder="请输入业务货位"></el-input>
              </el-form-item>
              <el-form-item label="货位类型：" required>
                <el-select v-model="oneLocation.locationType" placeholder="请选择货位类型">
                  <el-option label="普通" :value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="巷道号：" required>
                <el-input-number v-model="oneLocation.laneId" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="设备号：" required>
                <el-input-number v-model="oneLocation.machineId" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="排号：" required>
                <el-input-number v-model="oneLocation.lRow" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="列号：" required>
                <el-input-number v-model="oneLocation.lLine" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="层号：" required>
                <el-input-number v-model="oneLocation.lLayer" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="深度：" required>
                <el-input-number v-model="oneLocation.lDepth" :min="1" :max="999"></el-input-number>
              </el-form-item>
              <el-form-item label="货位标签：">
                <el-input v-model="oneLocation.locationTag"></el-input>
              </el-form-item>
              <el-form-item label="备注：">
                <el-input v-model="oneLocation.remark"></el-input>
              </el-form-item>
            </el-form>
          </el-row>
          <el-row>
            <el-button type="success" @click="addOne">添加一个</el-button>
          </el-row>
          </el-scrollbar>
        </el-col>
        <el-col :span="12" :offset="1" style="border: #e0e0e0 solid 1px; padding: 10px; border-radius: 5px">
          <el-scrollbar style="height:50vh">
          <h4 style="font-weight: bolder; margin-bottom: 10px">批量货位添加：</h4>
            <el-row style="margin-bottom: 10px">
              <el-text type="primary" style="font-size: 12px">仅支持批量添加指定设备的某一排的货位信息</el-text>
              <el-text type="warning" style="font-size: 12px">该操作会自动清理指定设备的排的货位信息，请谨慎操作！</el-text>
            </el-row>
            <el-row>
              <el-form label-position="right" :model="manyLocation" label-width="120px" require-asterisk-position="right" style="width: 100%; margin-right: 10px">
                <el-form-item label="货位类型：" required>
                  <el-select v-model="manyLocation.locationType" placeholder="请选择货位类型">
                    <el-option label="普通" :value="0"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="起始字符串：">
                  <el-input v-model="manyLocation.startString"></el-input>
                </el-form-item>
                <el-form-item label="设备号：" required>
                  <el-input-number v-model="manyLocation.machineId" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="巷道号：" required>
                  <el-input-number v-model="manyLocation.laneId" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="排号：" required>
                  <el-input-number v-model="manyLocation.lRow" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="总列数：" required>
                  <el-input-number v-model="manyLocation.lLineTotal" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="列偏移量：" required>
                  <el-input-number v-model="manyLocation.lLineOffset" :min="0" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="忽略的列号：">
                  <el-input v-model="manyLocation.lLineSkip"></el-input>
                  <el-text type="warning" style="font-size: 12px">以货架列为准，若有多个列号用用英文逗号隔开</el-text>
                </el-form-item>
                <el-form-item label="总层数：" required>
                  <el-input-number v-model="manyLocation.lLayerTotal" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="忽略的层号：">
                  <el-input v-model="manyLocation.lLayerSkip"></el-input>
                  <el-text type="warning" style="font-size: 12px">若有多个层号用用英文逗号隔开</el-text>
                </el-form-item>
                <el-form-item label="总深度：" required>
                  <el-input-number v-model="manyLocation.lDepthTotal" :min="1" :max="999"></el-input-number>
                </el-form-item>
                <el-form-item label="结束字符串：">
                  <el-input v-model="manyLocation.endString"></el-input>
                </el-form-item>
                <el-form-item label="货位标签：">
                  <el-input v-model="manyLocation.locationTag"></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                  <el-input v-model="manyLocation.remark"></el-input>
                </el-form-item>
              </el-form>
            </el-row>
            <el-row>
              <el-button type="success" @click="addMany">批量添加</el-button>
            </el-row>
          </el-scrollbar>
        </el-col>
      </el-row>
    <template #footer>
      <el-button type="danger" @click="close">关闭窗口</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>