
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :append-to-body="true"
    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="companyId">
          <el-select-tree
            :props="props"
            :options="companyResult"
            v-model="formModel.companyId"
            height="200"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="角色" prop="personRoleId">
          <el-select
            v-model="formModel.personRoleId"
            filterable
            placeholder="请选择"
            style="width: 280px"
          >
            <el-option
              v-for="role in personRoleResult"
              :key="role.id"
              :label="role.personRoleName"
              :value="role.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示的图标" prop="showIconId">
          <el-input
            v-model="formModel.showIconId"
            placeholder="请输入展示的图标id"
            style="width: 300px"
          ></el-input>
        </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 personPopedomApi from "@/api/base/personPopedom";
import personRoleInfoApi from "@/api/base/personRoleInfo";
import companyInfoApi from "@/api/base/companyInfo";
import deviceInfoApi from "@/api/base/deviceInfo";
import SelectTree from "@/components/SelectTree";

export default {
  components: {
    "el-select-tree": SelectTree,
  },
  props: ["businessKey", "title", "personId"],
  data() {
    return {
      formModel: { companyId: "" },
      ruleValidate: {
        companyId: [
          { required: true, message: "对应公司id不能为空", trigger: "blur" },
        ],
        personRoleId: [
          { required: true, message: "对应角色id不能为空", trigger: "blur" },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      companyResult: [],
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      personRoleResult: [],
    };
  },
  created() {
    var self = this;
    companyInfoApi.list().then(function (response) {
      var jsonData = response.data;
      if (jsonData.result) {
        if (jsonData.data != null && jsonData.data != "") {
          self.companyResult = jsonData.data;
        }
      }
    });

    // var formData = new FormData();
    // formData.append("companyId", "c52b03cb-7526-4e8b-8b90-4f7a6a37b586");
    // personRoleInfoApi.listByCompanyId(formData).then(function (response) {
    //   var jsonData = response.data;
    //   if (jsonData.result) {
    //     if (jsonData.data != null && jsonData.data != "") {
    //       self.personRoleResult = jsonData.data;
    //     }
    //   }
    // });

    this.loadTree();
  },
  watch: {
    "formModel.companyId": function (val, oldval) {
      if(val != null && val != ""){
        var self = this;
        self.formModel.personRoleId = "";
        var formData = new FormData();
        formData.append("companyId", val);
        personRoleInfoApi.listByCompanyId(formData).then(function (response) {
          var jsonData = response.data;
          if (jsonData.result) {
            if (jsonData.data != null && jsonData.data != "") {
              self.personRoleResult = jsonData.data;
            }
          }
        });
      }
    },
  },
  methods: {
    loadTree() {
      var formData = new FormData();
      companyInfoApi.loadChildren(formData).then((resp) => {
        var jsonData = resp.data;

        if (jsonData.result) {
          this.treeData = jsonData.data;
        } else {
          this.$message.error(jsonData.message + "");
        }
      });
    },
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;
      self.formModel.personId = self.personId;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return personPopedomApi.add(self.formModel);
            } else {
              return personPopedomApi.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 personPopedomApi.create();
      } else {
        return personPopedomApi.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 + "");
      });
  },
};
</script>