<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="areaId">
        <el-select
          v-model="form.areaId"
          filterable
          style="width: 100%"
          placeholder="请选择所属库区"
        >
          <el-option
            v-for="item in warehouse_list"
            :key="item.id"
            :label="item.name"
            :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>
    <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 {
    getListReservoir,
    getStorageLocationInfo,
    addStorageLocation,
    updateStorageLocation,
  } from "@/api/Stock";
  export default {
    name: "WarehouseAreaAdd",
    props: {
      isShow: {
        type: Boolean,
        default: false,
      },
      formId: {
        type: [String, Number],
        default: "",
      },
    },
    data() {
      return {
        warehouse_list: [],
        form: {
          name: "",
          code: "",
        },
        rules: {
          name: [
            { required: true, message: "请输入库位名称", trigger: "blur" },
          ],
          code: [
            { required: true, message: "请输入库位编码", trigger: "blur" },
          ],
          areaId: [
            { required: true, message: "请选择库区", trigger: "change" },
          ],
        },
      };
    },
    watch: {
      formId(val) {
        if (val) {
          this.getStorageLocationInfo();
        } else {
          this.form = {
            areaId: "",
            name: "",
            code: "",
          };
        }
      },
    },
    created() {
      this.getListReservoir();
    },
    mounted() {
      if (this.formId) {
        this.getStorageLocationInfo();
      }
    },
    methods: {
      cancel() {
        this.$emit("cancel");
      },
      confirm() {
        if (!this.form.code || !this.form.name.trim()) {
          this.$message.warning("必填项不能为空");
          return;
        }
        this.$refs["areaForm"].validate(async (valid) => {
          if (valid) {
            if (this.formId) {
              await this.updateStorageLocation();
            } else {
              await this.addStorageLocation();
            }
            this.cancel();
            this.$emit("confirm");
          }
        });
      },
      //新增
      async addStorageLocation() {
        const data = await addStorageLocation(this.form);
        this.$message.success("新增成功");
      },
      //编辑
      async updateStorageLocation() {
        const data = await updateStorageLocation(this.formId, this.form);
        this.$message.success("修改成功");
      },
      //获取详情
      async getStorageLocationInfo() {
        const { data } = await getStorageLocationInfo(this.formId);
        this.form = {
          areaId: data.areaId,
          name: data.name,
          code: data.code,
        };
      },
      //  请求库区列表
      async getListReservoir() {
        const data = await getListReservoir();
        this.warehouse_list = data.data;
      },
    },
  };
</script>

<style scoped></style>
