
<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"
            size=""
            height="200"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="机构" prop="companyStructureId">
          <el-select-tree
            :props="companyStructureProps"
            :options="companyStructureResult"
            v-model="formModel.companyStructureId"
            height="200"
            size=""
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="关联场景" prop="sceneId">
          <el-select v-model="sceneId" filterable placeholder="请选择">
            <el-option
              v-for="result in sceneData"
              :key="result.id"
              :label="result.sceneName"
              :value="result.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务公司" prop="serviceCompanyId" v-if="sceneId == '4'">
          <el-select-tree
            :props="props"
            :options="companyResult"
            v-model="formModel.serviceCompanyId"
            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-select
            v-model="formModel.showIconId"
            filterable
            multiple
            placeholder="请选择"
            style="width: 280px"
          >
            <el-option
              v-for="icon in iconResult"
              :key="icon.iconId"
              :label="icon.iconName"
              :value="icon.iconId"
            ></el-option>
          </el-select>
        </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 companyStructureInfoApi from "@/api/base/companyStructureInfo";
import iconInfoApi from "@/api/base/iconInfo";
import sceneInfoApi from "@/api/base/sceneInfo";
import SelectTree from "@/components/SelectTree";
import { Breadcrumb } from "element-ui";

export default {
  components: {
    "el-select-tree": SelectTree,
  },
  props: ["businessKey", "title", "personId"],
  data() {
    return {
      formModel: { companyId: "" },
      ruleValidate: {
        companyId: [
          { required: true, message: "对应公司不能为空", trigger: "blur" },
        ],
        personRoleId: [
          { required: true, message: "对应角色不能为空", trigger: "blur" },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      companyResult: [],
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      personRoleResult: [],
      iconResult: [],
      companyStructureResult: [],
      companyStructureProps: {
        // 配置项（必选）
        value: "id",
        label: "structureName",
        children: "children",
      },
      sceneData: [],
      sceneId: "",
      oldPersonRoleId:"",
    };
  },
  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;
        }
      }
    });

    sceneInfoApi.list().then((response) => {
      var jsonData = response.data;
      this.sceneData = 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;
    //     }
    //   }
    // });
  },
  watch: {
    "formModel.companyId": function (val, oldval) {
      if (val != null && val != "") {
        var self = this;
        if(self.oldPersonRoleId != ""){
          self.formModel.personRoleId = self.oldPersonRoleId;
        }else{
          self.formModel.personRoleId = "";
          self.oldPersonRoleId = "";
        }
        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;
            }
          }
        });

        companyStructureInfoApi.list(formData).then(function (response) {
          var jsonData = response.data;
          if (jsonData.result) {
            if (jsonData.data != null && jsonData.data != "") {
              self.companyStructureResult = jsonData.data;
            }
          }
        });

        companyInfoApi.edit(val).then((response) => {
          var jsonData = response.data;
          if (jsonData.result) {
            self.sceneId = jsonData.data.sceneId;
          }
        });
      }
    },
    "sceneId": function (val, oldval) {
      if (val != null && val != "") {
        var self = this;
        var formData = new FormData();
        formData.append("sceneId", val);
      
        personRoleInfoApi.findBySceneId(formData).then(function (response) {
          var jsonData = response.data;
          if (jsonData.result) {
            if (jsonData.data != null && jsonData.data != "") {
              if(self.oldPersonRoleId != ""){
                self.formModel.personRoleId = self.oldPersonRoleId;
              }else{
                self.formModel.personRoleId = "";
                self.oldPersonRoleId = "";
              }
              self.personRoleResult = jsonData.data;
            }
          }
        });
      }
    },
    // "formModel.personRoleId": function (val, oldval) {
    //   if (val != null && val != "") {
    //     var self = this;
    //     self.formModel.showIconId = "";
    //     var formData = new FormData();
    //     formData.append("roleId", val);

    //     iconInfoApi.listByRoleId(formData).then(function (response) {
    //       var jsonData = response.data;
    //       if (jsonData.result) {
    //         if (jsonData.data != null && jsonData.data != "") {
    //           self.iconResult = jsonData.data;
    //         }
    //       }
    //     });
    //   }
    // },
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;
      self.formModel.personId = self.personId;
      // if (
      //   self.formModel.showIconId != null &&
      //   self.formModel.showIconId != ""
      // ) {
      //   var showIconId = self.formModel.showIconId.join(",");
      //   self.formModel.showIconId = showIconId;
      // } else {
      //   self.formModel.showIconId = "";
      // }

      if (
        self.formModel.companyStructureId != null &&
        self.formModel.companyStructureId == "0"
      ) {
        self.$message({
          message: "请选择下级机构",
          type: "warning",
        });
        self.formModel.companyStructureId == null;
      } else {
        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.formModel.companyStructureId == null;
                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;
          self.oldPersonRoleId = self.formModel.personRoleId;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>