
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align: left" @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formModel.name" placeholder="请输入名称" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formModel.address" placeholder="请输入地址" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="区域" prop="areaId">
          <el-select-tree size="mediumn" style="width: 300px" :props="props" :options="queryAreaResult" v-model="formModel.areaId" height="200"></el-select-tree>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="formModel.linkman" placeholder="请输入联系人" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formModel.phone" placeholder="请输入电话" maxlength="11" show-word-limit oninput="value=value.replace(/[^\d]/g,'')" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import warehouseApi from "@/api/base/warehouse";
import areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        areaId: [{ required: true, message: "区域不能为空", trigger: "blur" }],
        linkman: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "电话不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      props: {
        value: "id",
        label: "name",
        children: "children"
      },
      queryAreaResult: [],
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return warehouseApi.add(self.formModel);
            } else {
              return warehouseApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    var formData = new FormData();
    areaApi.queryList(formData).then(response=>{
      var jsonData = response.data;
      this.queryAreaResult = jsonData;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return warehouseApi.create();
      } else {
        return warehouseApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>