
<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="parentId">
          <el-select-tree
            :props="props"
            :options="queryAreaResult"
            v-model="formModel.parentId"
            size="mediumn"
            style="width: 300px"
          ></el-select-tree>
        </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 areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "codeKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [{ required: true, message: "区域名称不能为空", trigger: "blur" },],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      queryAreaResult: [],
      props: {
        value: "id",
        label: "name"
      }
    };
  },
  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 areaApi.add(self.formModel);
            } else {
              return areaApi.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;

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

      if (jsonData.result) {
        self.formModel = jsonData.data;

        var formData = new FormData();
        formData.append("containCode", self.codeKey);
        areaApi.queryList(formData).then(response=>{
          var jsonData = response.data;
          self.queryAreaResult = jsonData;
        });
      } else {
        self.$message.error(jsonData.message + "");
      }
    })
    .catch((error) => {
      self.$message.error(error + "");
    });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>