<template>
  <el-dialog
    :title="!!formId ? '修改库位' : '新增库位'"
    :visible.sync="isShow"
    width="40%"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form
      ref="areaForm"
      style="width: 70%"
      :model="form"
      :rules="rules"
      size="small"
      label-width="100px"
    >
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select
          v-model="form.warehouseId"
          filterable
          style="width: 100%"
          placeholder="请选择所属仓库"
        >
          <el-option
            v-for="item in warehouse_list"
            :key="item.id"
            :label="item.warehouseName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="库区名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入库区名称"></el-input>
      </el-form-item>
      <el-form-item label="库区编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入库区编码"></el-input>
      </el-form-item>
      <el-form-item label="库区类型" prop="type">
        <el-select
          v-model="form.type"
          style="width: 100%"
          placeholder="请选择库区类型"
        >
          <el-option
            v-for="item in type_options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="长" prop="length">
        <el-input-number
          v-model="form.length"
          style="width: calc(100% - 39px)"
          placeholder="请输入长"
          :min="0"
        ></el-input-number>
        <el-tag style="margin-left: 5px">米</el-tag>
      </el-form-item>
      <el-form-item label="宽" prop="width">
        <el-input-number
          v-model="form.width"
          style="width: calc(100% - 39px)"
          placeholder="请输入宽"
          :min="0"
        ></el-input-number>
        <el-tag style="margin-left: 5px">米</el-tag>
      </el-form-item>
      <el-form-item label="高" prop="height">
        <el-input-number
          v-model="form.height"
          style="width: calc(100% - 39px)"
          placeholder="请输入高"
          :min="0"
        ></el-input-number>
        <el-tag style="margin-left: 5px">米</el-tag>
      </el-form-item>
      <el-form-item label="库位限重" prop="weight">
        <el-input-number
          v-model="form.weight"
          style="width: calc(100% - 45px)"
          placeholder="请输入库位限重"
          :min="0"
        ></el-input-number>
        <el-tag style="margin-left: 5px">KG</el-tag>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getAllWarehouse,
    addReservoir,
    getReservoirInfo,
    updateReservoir,
  } from "@/api/Stock";
  export default {
    name: "WarehouseAreaAdd",
    props: {
      isShow: {
        type: Boolean,
        default: false,
      },
      formId: {
        type: [String, Number],
        default: "",
      },
    },
    data() {
      return {
        warehouse_list: [],
        type_options: [
          {
            label: "普通货位",
            value: 1,
          },
          {
            label: "入库区",
            value: 2,
          },
          {
            label: "大货区",
            value: 3,
          },
          {
            label: "散货区",
            value: 4,
          },
          {
            label: "残次品区",
            value: 5,
          },
          {
            label: "退货区",
            value: 6,
          },
          {
            label: "出库区",
            value: 7,
          },
          {
            label: "分拣区",
            value: 8,
          },
        ],
        form: {
          storageLocationId: "",
          name: "",
          code: "",
          length: 0,
          width: 0,
          height: 0,
          weight: 0,
          type: 1,
        },
        rules: {
          name: [
            { required: true, message: "请输入库区名称", trigger: "blur" },
          ],
          code: [
            { required: true, message: "请输入库区编码", trigger: "blur" },
          ],
          type: [
            { required: true, message: "请选择库区类型", trigger: "change" },
          ],
          warehouseId: [
            { required: true, message: "请选择仓库", trigger: "change" },
          ],
        },
      };
    },
    watch: {
      formId(val) {
        if (val) {
          this.getReservoirInfo();
        } else {
          this.form = {
            warehouseId: "",
            name: "",
            length: 0,
            width: 0,
            height: 0,
            weight: 0,
            type: 1,
          };
        }
      },
    },
    created() {
      this.getAllWarehouse();
    },
    mounted() {
      if (this.formId) {
        this.getReservoirInfo();
      }
    },
    methods: {
      cancel() {
        this.$emit("cancel");
      },
      confirm() {
        if (!this.form.code.trim() || !this.form.name.trim()) {
          this.$message.warning("必填项不能为空");
          return;
        }
        this.$refs["areaForm"].validate(async (valid) => {
          if (valid) {
            if (this.formId) {
              await this.updateReservoir();
            } else {
              await this.addReservoir();
            }
            this.cancel();
            this.$emit("confirm");
          }
        });
      },
      //新增
      async addReservoir() {
        const data = await addReservoir(this.form);
        this.$message.success("新增成功");
      },
      //编辑
      async updateReservoir() {
        const data = await updateReservoir(this.formId, this.form);
        this.$message.success("修改成功");
      },
      //获取详情
      async getReservoirInfo() {
        const { data } = await getReservoirInfo(this.formId);
        this.form = {
          warehouseId: data.warehouseId,
          name: data.name,
          length: data.length,
          width: data.width,
          height: data.height,
          weight: data.weight,
          type: data.type,
          code: data.code,
        };
      },
      //  请求仓库列表
      async getAllWarehouse() {
        const data = await getAllWarehouse({
          page: 1,
          pageSize: 999,
        });

        this.warehouse_list = data.data;
      },
    },
  };
</script>

<style scoped></style>
