 <template>
  <div class="pageBody">
    <div class="topPart">
      <div class="title">
        <div class="tltleLeft">编辑岗位</div>
        <!-- <div class="isUsed">
          是否启用
          <el-radio
            v-model="FormData.isUsed"
            v-if="FormData.isUsed == 1 || form.isUsed == 3"
            :label="1"
            >是</el-radio
          >
          <el-radio v-model="FormData.isUsed" v-if="FormData.isUsed == 2" :label="2"
            >是</el-radio
          >
          <el-radio v-model="FormData.isUsed" :label="3">否</el-radio>
        </div> -->
        <div>
          <!-- <button type="submmit" class="delBtn" @click="delate">删除</button> -->
          <button type="submmit" class="saveBtn" @click="saveJob()">
            保存
          </button>
        </div>
      </div>
      <div class="titleContent">
        <el-form ref="form" :model="FormData" label-width="80px">
          <el-row :gutter="40">
            <el-col :span="5">
              <el-form-item label="岗位名称" prop="name">
                <el-input
                  v-model="FormData.JobName"
                  maxlength="32"
                  placeholder="请输入岗位名称"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="描述" prop="diesc">
                <el-input
                  v-model="FormData.JobDesc"
                  maxlength="128"
                  placeholder="描述内容"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="content">
      <el-row :gutter="25">
        <el-col :span="24" v-if="systemSet.UserOfficerMulti != 0">
          <div class="part">
            <Organization
              title="所属组织"
              :data="dataList"
              :defaultProps="defaultProps"
              :nodekey="'Organid'"
              @getCheckedData="getOrganTreeData"
              :checkedKeys="checkedIdList"
            />
          </div>
        </el-col>
        <el-col :span="8" v-if="systemSet.UserOfficerMulti === 0">
          <div class="part">
            <Organization
              title="所属组织"
              :data="originData"
              :defaultProps="defaultProps"
              :nodekey="'Organid'"
              :checkedKeys="checkedIdList"
            />
          </div>
        </el-col>
        <el-col
          :span="8"
          v-if="systemSet.OpenRole === 0 && systemSet.UserOfficerMulti === 0"
        >
          <div class="part">
            <Permissiontree
              @getTreeData="getAuthTreeData"
              :checkedKeys="checkedAuthKeys"
              :queryType="queryType"
              :queryId="originId"
              title="权限树"
            />
          </div>
        </el-col>
        <el-col
          :span="8"
          v-if="systemSet.OpenRole === 0 && systemSet.UserOfficerMulti === 0"
        >
          <div class="part">
            <Rulesofcompetency title="权限规则" />
          </div>
        </el-col>
        <el-col
          :span="8"
          v-if="systemSet.OpenRole != 0 && systemSet.UserOfficerMulti === 0"
        >
          <div class="part">
            <rolelist
              title="角色列表"
              @checkChange="checkRoleChange"
              :dataList="originRoleList"
              :checkBoxList="checkedRoleKeys"
              @checkClick="roleClick"
              :number="8"
            />
          </div>
        </el-col>
        <el-col
          :span="8"
          v-if="systemSet.OpenRole != 0 && systemSet.UserOfficerMulti === 0"
        >
          <div class="part">
            <permissionlist
              title="权限列表"
              :RoleList="powerList"
              :check="true"
              class="hucPower"
              @Competen="Competen"
            />
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import Permissiontree from "@/components/permissiontree/index.vue";
import Rulesofcompetency from "@/components/rulesofcompetency/index.vue";
import Organization from "@/components/organization/index.vue";
import rolelist from "@/components/rolelist/index.vue";
import permissionlist from "@/components/permissionlist/index.vue";

import treeData from "../minixs/treeData";
import roleData from "../minixs/roleData";

import { GetJobDetails, EditJob } from "@/api/jobApi";
import { newOrganall } from "@/api/postInterface";
import { RoleAuths } from "@/api/apiAuthority";

export default {
  data() {
    return {
      queryType: "origin",
      JobId: 0, //当前岗位ID
      systemSet: null, //系统设置
      FormData: {
        JobName: "",
        AuthList: [],
        JobDesc: "",
        JobId: 0,
        JobName: "",
        OrganId: 0,
        RoleList: [],
      },
      rules: {
        JobName: [
          { required: true, message: "请输入岗位名称", trigger: "blur" },
          {
            min: 1,
            max: 16,
            message: "长度在 1 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
      defaultProps: {
        children: "children",
        label: "OrganName",
      },
      listData: [], //上级组织树
      originId: "-1", //所属组织ID
      originData: [], //所属组织详情
      checkedIdList: [], //选中组织树
      AuthList: [], //选中权限（包含规则组）
      RoleList: [], //选中角色组
      checkedAuthKeys: [],
      checkedRoleKeys: [],
      originRoleList: [], //所属组织角色列表
      powerList:[],//角色权限列表
    };
  },
  mixins: [treeData, roleData],
  watch: {
    arrs: {
      handler(arr) {
        arr.forEach((item) => {
          item.name = item.RoleName;
          if (
            item.IsSelected == 1 &&
            _.indexOf(this.originRoleList, item) == -1
          ) {
            this.originRoleList.push(item);
          }
        });
        this.checkedRoleKeys = [];
        this.RoleList.forEach((item) => {
          this.originRoleList.forEach((value, index) => {
            if (item.RoleId == value.RoleId) {
              this.checkedRoleKeys.push(index);
            }
          });
        });
      },
      deep: true,
    },
  },
  created: function () {
    const systemData = this.$store.state.app.systemSet;
    if (typeof systemData == "string") {
      this.systemSet = JSON.parse(systemData);
    } else {
      this.systemSet = systemData;
    }
    this.JobId = this.$route.query.JobId;
    this.getJobDetails();
  },
  components: {
    Organization,
    Permissiontree,
    Rulesofcompetency,
    rolelist,
    permissionlist,
  },
  methods: {
    //获取岗位详情
    getJobDetails() {
      try {
        let data = {
          JobId: this.JobId,
        };
        GetJobDetails(data).then((result) => {
          if (result.code === 0) {
            this.FormData = _.cloneDeep(result.returnData);
            this.originId = result.returnData.OrganId+"";
            this.AuthList = _.cloneDeep(result.returnData.AuthList);
            this.$store.state.auth.authList = _.cloneDeep(
              result.returnData.AuthList
            );
            let list = _.cloneDeep(result.returnData.RoleList);
            if(list){
              list.forEach((item, index) => {
              if (item.IsSelected == 1) {
                this.RoleList.push(item);
              }
            });
            }
            if (this.AuthList) {
              this.AuthList.forEach((item) => {
                this.checkedAuthKeys.push(item.AuthId);
              });
            }
            this.checkedIdList.push(result.returnData.OrganId);
            this.getOrganDetails();
            this.getRoleByOrgan(this.originId);
          }
        });
      } catch (error) {
        console.log("GetJobDetails");
        console.log(error);
      }
    },
    //获取所属组织详情
    getOrganDetails() {
      try {
        let data = {
          OrganId: this.originId,
        };
        newOrganall(data).then((result) => {
          if (result.code == 0) {
            this.originData = [];
            this.originData.push(result.returnData);
          }
        });
      } catch (error) {
        console.log("newOrganall");
        console.log(error);
      }
    },
    //获取当权选中权限树
    getAuthTreeData(data) {
      this.checkedAuthTreeList = _.cloneDeep(data);
    },
    //获取组织树点击
    getOrganTreeData(data) {
      console.log(data);
    },
    //角色选中后
    checkRoleChange(data) {
      this.RoleList = [];
      data.forEach((item) => {
        this.originRoleList[item].IsSelected = 1;
        this.RoleList.push(this.originRoleList[item]);
      });
    },
    //点击角色后
    roleClick(data) {
      try {
        let params = {
          RoleId: data.RoleId,
        };
        RoleAuths(params).then((result) => {
          this.powerList = result.returnData;
        });
      } catch (error) {
        console.log("RoleAuths");
        console.log(error);
      }
    },
    //保存
    saveJob() {
      try {
        let arr = _.cloneDeep(this.checkedAuthTreeList);
        let authArrs = _.cloneDeep(this.$store.state.auth.authArrs);
        if (arr) {
          this.AuthList = [];
          arr.forEach((item) => {
            this.AuthList.push(item.AuthList);
          });
          this.AuthList.forEach((item, index) => {
            authArrs.forEach((item2) => {
              if (item.AuthId == item2.AuthId) {
                this.AuthList[index] = item2.AuthList;
              }
            });
          });
        }
        this.FormData.AuthList = this.AuthList;
        this.FormData.RoleList = this.RoleList;
        let data = _.cloneDeep(this.FormData);
        EditJob(data).then((result) => {
          if (result.code == 0) {
            this.$message.success(result.message);
            this.$router.push("/jobs");
          }
        });
      } catch (error) {
        console.log("EditJob");
        console.log(error);
      }
    },
    //查看权限列表明细
    Competen(data) {
      this.$emit("Competen", data);
    },
    //获取指定数据
    decompose(data, id) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].GroupId == id) {
          this.dataObj = data[i];
          delete this.dataObj.children;
          this.dataObj = [this.dataObj];
        } else if (data[i].children && data[i].children.length > 0) {
          this.decompose(data[i].children, id);
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.pageBody {
  width: 100%;
  height: calc(100vh - 178px);
  padding: 0 64px;
  padding-top: 32px;
  .topPart {
    box-sizing: border-box;
    width: 100%;
    height: 184px;
    padding: 32px;
    background: #ffffff;
    box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
    border-radius: 16px;
    .title {
      display: flex;
      justify-content: space-between;
      flex-direction: row;
      position: relative;
      .isUsed {
        position: absolute;
        left: 194px;
        top: 6px;
      }
      .tltleLeft {
        font-size: 24px;
        font-weight: bold;
        color: #303133;
      }
      .saveBtn {
        width: 80px;
        height: 40px;
        background: linear-gradient(0deg, #777dba, #6983be);
        border-radius: 6px;
        color: #fff;
        border: none;
      }
      .delBtn {
        width: 80px;
        height: 40px;
        background: #f56c6c;
        border-radius: 6px;
        color: #fff;
        border: none;
        margin-right: 24px;
      }
    }
    .titleContent {
      width: 100%;
      padding-top: 35px;
      box-sizing: border-box;
      .resetPass {
        background: #6e81bc;
        color: #fff;
        margin-top: 4px;
        margin-left: -25px;
      }
    }
  }
  .content {
    width: 100%;
    height: calc(100% - 184px);
    padding-top: 24px;
    box-sizing: border-box;
    .part {
      width: 100%;
      height: 720px;
      background: #ffffff;
      box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
      border-radius: 16px;
    }
  }
}
</style>
