
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="true"
    append-to-body
    :close-on-click-modal="false"
    style="text-align: left"
    width="80%"
    @close="closeDialog"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'150px'"
        inline
      >
        <el-form-item label="公司名称" prop="companyId">
          <el-select-tree
            size="mini"
            :props="companyProps"
            :options="companyResult"
            v-model="formModel.companyId"
            height="200"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="结构名称" prop="structureName">
          <el-input
            v-model="formModel.structureName"
            placeholder="请输入结构名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="位置名称"
          prop="positionName"
        >
          <el-input
            v-model="formModel.positionName"
            placeholder="请输入位置名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级" prop="parentId">
          <el-select-tree
            size="mini"
            :props="parentProps"
            :options="parentResult"
            v-model="formModel.parentId"
            height="200"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="formModel.remark"
            placeholder="请输入备注信息"
            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 companyStructureInfoApi from "@/api/base/companyStructureInfo";
import companyInfoApi from "@/api/base/companyInfo";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        companyId: [
          { required: true, message: "公司id不能为空", trigger: "blur" },
        ],
        structureName: [
          { required: true, message: "结构名称不能为空", trigger: "blur" },
        ],
        positionName: [
          {
            required: true,
            message: "位置名称（如：楼栋、单元、部门、工号）不能为空",
            trigger: "blur",
          },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      companyResult: [],
      companyProps: {
        value: "id",
        label: "name",
        children: "children"
      },
      parentResult: [],
      parentProps: {
        value: "id",
        label: "structureName"
      },
    };
  },
  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 companyStructureInfoApi.add(self.formModel);
            } else {
              return companyStructureInfoApi.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);
            }
          });
        }
      });
    },
  },
  created() {
    var self = this;

    companyInfoApi.list().then(function(response) {
      var jsonData = response.data;
      if (jsonData.result) {
        self.companyResult = jsonData.data;
      }
    });

    companyStructureInfoApi.query().then(function(response) {
      var jsonData = response.data;
      if (jsonData.result) {
        self.parentResult = jsonData.data;
      }
    });

    // this.changePage(1);
    // this.loadTree();
  },
  mounted: function () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return companyStructureInfoApi.create();
      } else {
        return companyStructureInfoApi.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>