<!--
 * @Author: your name
 * @Date: 2021-11-29 09:27:43
 * @LastEditTime: 2022-03-21 03:28:09
 * @LastEditors: your name
 * @Description: 权限管理
 * @FilePath: \Foshan4A2.0\src\views\authorityManagement\components\home.vue
-->
<template>
  <div class="authority-home">
    <!--状态和搜索-->
    <div class="authority-head flex">
      <div class="status flex-wrap">
        <div class="status1"><span class="icon"></span>集团内</div>
        <div class="status2"><span class="icon"></span>集团外</div>
      </div>
      <Search @getSearchData="getSearchData" @clearSearchData="clearSearchData" />
    </div>
    <!--权限树-->
    <div class="authority-content">
      <!-- <OrgTree :dataList="dataList" :renderContent="renderContent" /> -->
      <el-row :gutter="24" class="bodyContent">
        <el-col :span="6" class="bodyPart">
          <div class="grid-content">
            <div class="title">组织树</div>
            <div class="contentTree">
              <el-tree ref="tree" :data="dataListTree" :props="defaultProps" node-key="OrganId" :filter-node-method="filterNode" highlight-current :default-expanded-keys="expandedKeys" :current-node-key="currentKey" @node-expand="handleExpand" @node-collapse="handleCollapse" @node-click="handleNodeClick">
              </el-tree>
            </div>
          </div>
        </el-col>
        <el-col :span="18" class="bodyPart">
          <div class="grid-contentTop">
            <div class="title">当前组织</div>
            <div class="content">
              <el-empty :image-size="100" style="padding:0" v-if="currDataArr.length<1"></el-empty>
              <el-row>
                <el-col :span="8" v-for="data in currDataArr" :key="data.OrganId">
                  <div class="itemBox">
                    <div class="status" v-if="data.OrganType == 1"></div>
                    <div class="status2" v-if="data.OrganType != 1"></div>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <div class="itemTitle">{{ data.OrganName }}</div>
                      </el-col>
                      <el-col :span="12">
                        <div class="flex info">
                          <div class="icon">
                            <el-tooltip v-if="doesJobShow" v-is="['organtree_btn_add_job']" effect="dark" content="新增职务" placement="bottom">
                              <span class="cap cap-job" @click="renderJob(data)" v-if="data.OrganId != -1"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_add_officer']" effect="dark" content="新增职员" placement="bottom">
                              <span v-if="data.OrganId != -1" class="cap cap-member" @click="renderMember(data)"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_update_organ']" effect="dark" content="编辑组织" placement="bottom">
                              <span class="cap cap-edit" @click="renderEdit(data)" v-if="data.OrganId != -1"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_add_organ']" effect="dark" content="新增组织" placement="bottom">
                              <span class="cap cap-sub" @click="renderAdd(data)"></span>
                            </el-tooltip>
                          </div>
                        </div>
                      </el-col>
                      <template v-if="doesJobShow">
                        <el-col :span="8" v-if="data.OrganId != -1">
                          <span @click="toJob(data)" class="cursor">
                            <span class="msg">职务：</span>
                            <span>{{ data.JobCount }}</span>
                          </span>
                        </el-col>
                        <el-col :span="8" v-if="data.OrganId != -1">
                          <span @click="toOfficer(data)" class="cursor">
                            <span class="msg">职员：</span>
                            <span>{{ data.OfficerCount }}</span>
                          </span>
                        </el-col>
                      </template>
                      <template v-else>
                        <el-col :span="24" v-if="data.OrganId != -1">
                          <span @click="toOfficer(data)" class="cursor">
                            <span class="msg">职员：</span>
                            <span>{{ data.OfficerCount }}</span>
                          </span>
                        </el-col>
                      </template>
                      <!-- <el-col :span="8" v-if="data.OrganId != -1">
                        <span class="msg">权限：</span>
                        <span>{{ data.AuthCount }}</span>
                      </el-col> -->
                      <el-col :span="12" v-if="data.OrganId != -1">
                        <span class="msg">状态：</span>
                        <span class="use" v-if="data.Status == '1'">启用</span>
                        <span class="unUse" v-if="data.Status != '1'">禁用</span>
                      </el-col>
                      <el-col v-is="['organtree_btn_state_swatch']" :span="12" v-if="data.OrganId != -1">
                        <el-switch active-color="#6F81BC" v-model="data.flag" @change="renderChange(data)"></el-switch>
                      </el-col>
                    </el-row>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
          <div class="grid-contentBottom">
            <div class="title">下级组织</div>
            <div class="content">
              <el-empty :image-size="150" v-if="childrenData.length<1"></el-empty>
              <el-row :gutter="24">
                <el-col :span="8" v-for="(data, index) in childrenData" :key="data.OrganId">
                  <div v-is="['organtree_btn_del_organ']" class="info-close" @click="onNodeClick(data,index)">
                    <i class="el-icon-close"></i>
                  </div>
                  <div class="itemBox" @click.stop="childrenClick(data)">
                    <div class="status" v-if="data.OrganType == 1"></div>
                    <div class="status2" v-if="data.OrganType != 1"></div>
                    <el-row>
                      <el-col :span="12">
                        <div class="itemTitle">{{ data.OrganName }}</div>
                      </el-col>
                      <el-col :span="12">
                        <div class="flex info">
                          <div class="icon">
                            <el-tooltip v-if="doesJobShow" v-is="['organtree_btn_add_job']" effect="dark" content="新增职务" placement="bottom">
                              <span class="cap cap-job" @click.stop="renderJob(data)" v-if="data.OrganId != -1"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_add_officer']" effect="dark" content="新增职员" placement="bottom">
                              <span v-if="data.OrganId != -1" class="cap cap-member" @click.stop="renderMember(data)"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_update_organ']" effect="dark" content="编辑组织" placement="bottom">
                              <span class="cap cap-edit" @click.stop="renderEdit(data)" v-if="data.OrganId != -1"></span>
                            </el-tooltip>

                            <el-tooltip v-is="['organtree_btn_add_organ']" effect="dark" content="新增组织" placement="bottom">
                              <span class="cap cap-sub" @click.stop="renderAdd(data)"></span>
                            </el-tooltip>
                          </div>
                        </div>
                      </el-col>
                      <template v-if="doesJobShow">
                        <el-col :span="8" v-if="data.OrganId != -1">
                          <span @click="toJob(data)" class="cursor">
                            <span class="msg">职务：</span>
                            <span>{{ data.JobCount }}</span>
                          </span>
                        </el-col>
                        <el-col :span="8" v-if="data.OrganId != -1">
                          <span @click="toOfficer(data)" class="cursor">
                            <span class="msg">职员：</span>
                            <span>{{ data.OfficerCount }}</span>
                          </span>
                        </el-col>
                      </template>
                      <template v-else>
                        <el-col :span="24" v-if="data.OrganId != -1">
                          <span @click="toOfficer(data)" class="cursor">
                            <span class="msg">职员：</span>
                            <span>{{ data.OfficerCount }}</span>
                          </span>
                        </el-col>
                      </template>
                      <!-- <el-col :span="8" v-if="data.OrganId != -1">
                        <span class="msg">权限：</span>
                        <span>{{ data.AuthCount }}</span>
                      </el-col> -->
                      <el-col :span="12" v-if="data.OrganId != -1">
                        <span class="msg">状态：</span>
                        <span class="use" v-if="data.Status == '1'">启用</span>
                        <span class="unUse" v-if="data.Status != '1'">禁用</span>
                      </el-col>
                      <el-col v-is="['organtree_btn_state_swatch']" :span="12" v-if="data.OrganId != -1">
                        <el-switch active-color="#6F81BC" v-model="data.flag" @click.stop.native @change="childrenRenderChange(data, index)"></el-switch>
                      </el-col>
                    </el-row>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <!--删除弹框-->
    <Dialog :flag="flag">
      <div class="airportInfoDialog">
        <div class="title">删除组织</div>
        <div class="content">是否确认删除{{ title }}？</div>
        <div class="foot right t30">
          <el-button size="medium" class="r24" @click="remove" type="danger">删除</el-button>
          <el-button size="medium" @click="flag = false">取消</el-button>
        </div>
      </div>
    </Dialog>
  </div>
</template>

<script>
// import OrgTree from '@/layout/components/OrgTree'
import Search from "@/layout/components/Search";
import Dialog from "@/layout/components/Dialog";
import { organele, tissueTreeStart } from "@/api/postInterface";
import treeData from "../minixs/treeData";
export default {
  name: "Authority",
  components: { Search, Dialog },
  mixins: [treeData],
  data () {
    return {
      dataIndex: 1,
      dataNum: 0,
      type: null,
      title: "", //弹框title
      flag: false, //弹框开关
      dataId: null, //tree数据id
      defaultProps: {
        children: "children",
        label: "OrganName",
      },
      currDataArr: [],
      currDataArrId: 0,
      childrenData: [],
      dataListTree: [],
      currentKey: -1,
      expandedKeys: [-1],
      delIndex: 0
    };
  },
  created () {
    let keyWords = "";
    const treeEx = sessionStorage.getItem('orgTreeDatas');
    if (this.$route.query.keyWords) {
      keyWords = this.$route.query.keyWords;
    }
    if (treeEx) {
      this.expandedKeys = JSON.parse(treeEx);
    }
    this.getOrganTree(keyWords);
  },
  mounted () {
    setTimeout(() => {
      this.$refs.tree.setCurrentKey(this.expandedKeys[this.expandedKeys.length - 1]);
    }, 500);
  },
  watch: {
    dataList: {
      handler: function (val) {
        this.dataListTree = [];
        this.dataListTree.push(_.cloneDeep(val));
      },
      deep: true,
    },
    currentKey: {
      handler (val) {
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(val)
        })
      }
    }
  },
  methods: {
    filterNode (value, data) {
      if (!value) return true;
      return data.OrganName.indexOf(value) !== -1;
    },
    // 添加组织
    renderAdd (data) {
      this.$router.push({
        path: "/organization/organizationAdd",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId },
      });
    },
    // 新增职务
    renderJob (data) {
      this.$router.push({
        path: "/jobs/add",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId },
      });
    },
    // 新增职员
    renderMember (data) {
      this.$router.push({
        path: "/staff/staffAdd",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId, orgType: 'org' },
      });
    },
    // 修改组织
    renderEdit (data) {
      this.$router.push({
        path: "/organization/organizationEdit",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId },
      });
    },
    // 新增组织
    renderSub (data) {
      this.$router.push({
        path: "/organization/organizationAdd",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId },
      });
    },
    // 关闭开启
    renderChange (data) {
      const { OrganId, flag } = data;
      this.handleChange(OrganId, flag);
    },
    //下级关闭
    childrenRenderChange (data, index) {
      const { OrganId, flag } = data;
      const Status = flag ? 1 : 0;
      try {
        tissueTreeStart({
          OrganId: OrganId,
          Status: Status,
        }).then((res) => {
          if (res.code === 0) {
            this.$message.success(res.message);
            this.childrenData[index].Status = Status;
            // this.getOrganTree();
          } else {
            this.$message.error(res.message);
            this.childrenData[index].flag = !flag;
          }
        });
      } catch (error) {
        console.log("出错了", error);
        this.childrenData[index].flag = !flag;
      }
    },
    //删除组织
    remove () {
      this.deleteOrg(this.dataId);
    },
    //查询
    getSearchData (val) {
      if (val) {
        this.$router.replace({ path: '/organization', query: { keyWords: val } })
      } else if (this.$route.query) {
        this.$router.replace('/organization')
      }
      this.$refs.tree.filter(val);
    },
    //清除查询
    clearSearchData () {
      this.$router.replace('/organization')
      this.getOrganTree();
    },
    //节点关闭按钮点击
    onNodeClick (data, index) {
      this.delIndex = index
      this.flag = true;
      this.dataId = data.OrganId;
      this.title = data.OrganName;
    },
    //获取指定数据
    decompose (value, key, id) {
      let data = _.cloneDeep(value);
      let arr = [];
      for (let i = 0; i < data.length; i++) {
        if (data[i][key] == id) {
          arr.push(data[i]);
        }
      }
      return arr;
    },
    //渲染节点

    //-----------获取数据------------
    //应用状态变更
    async handleChange (id, flag) {
      const Status = flag ? 1 : 0
      try {
        const res = await tissueTreeStart({
          OrganId: id,
          Status: Status
        });
        if (res.code === 0) {
          this.$message.success(res.message);
          this.currDataArr[0].Status = Status
          // this.getOrganTree();
        } else {
          this.$message.error(res.message);
          this.currDataArr[0].flag = !flag;
        }
      } catch (error) {
        console.log("出错了", error);
        this.currDataArr[0].flag = !flag;
      }
    },
    //组织删除
    async deleteOrg (id) {
      try {
        const res = await organele({
          OrganId: id,
        });
        if (res.code === 0) {
          this.$message.success(res.message);
          this.flag = false;
          this.getOrganTree();
          this.childrenData.splice(this.delIndex, 1);
        } else {
          this.flag = false;
          this.$message.error(res.message);
        }
      } catch (error) {
        this.flag = false;
      }
    },
    //树点击
    handleNodeClick (data) {
      const dataNew = _.cloneDeep(data);
      this.currentKey = dataNew.OrganId;
      this.currDataArr = [];
      this.childrenData = [];
      this.currDataArr = this.decompose(
        this.dataArr,
        "OrganId",
        this.currentKey
      );
      this.childrenData = this.decompose(
        this.dataArr,
        "OrganUpid",
        this.currentKey
      );
    },
    //下级卡片点击
    childrenClick (data) {
      const dataNew = _.cloneDeep(data);
      this.currentKey = dataNew.OrganId;
      this.currDataArr = [];
      this.childrenData = [];
      this.currDataArr = this.decompose(
        this.dataArr,
        "OrganId",
        this.currentKey
      );
      this.childrenData = this.decompose(
        this.dataArr,
        "OrganUpid",
        this.currentKey
      );
      if (!this.expandedKeys.includes(this.currentKey)) {
        this.expandedKeys.push(this.currentKey)
      }
    },
    // 节点展开
    handleExpand (obj) {
      this.expandedKeys.push(obj.OrganId)
      sessionStorage.setItem('orgTreeDatas', JSON.stringify(this.expandedKeys))
    },
    // 节点收起
    handleCollapse (obj) {
      const { OrganId, children } = obj
      children.length && this.collapseChilren(children)
      this.expandedKeys = this.expandedKeys.filter(key => key !== OrganId)
      sessionStorage.setItem('orgTreeDatas', JSON.stringify(this.expandedKeys))
    },
    // 收起子节点
    collapseChilren (children) {
      children.forEach(child => {
        const { OrganId, children } = child
        children.length && this.collapseChilren(children)
        this.expandedKeys = this.expandedKeys.filter(key => key !== OrganId)
      })
    },
    //岗位点击
    toJob (data) {
      this.$router.push({
        path: "/jobs",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId, orgType: 'org' },
      });
    },
    //职员点击
    toOfficer (data) {
      this.$router.push({
        path: "/staff",
        query: { OrganUpid: data.OrganUpid, OrganId: data.OrganId, orgType: 'org' },
      });
    }
  },
};
</script>

<style lang="scss" scoped>
.authority-home {
  padding: 0 64px;
  overflow-x: auto;
  .authority-head {
    position: fixed;
    width: 100%;
    padding: 30px 64px;
    padding-left: 156px;
    left: 0;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    background: #f5f7fa;
    z-index: 100;
    .status {
      & > div {
        margin-right: 28px;
        .icon {
          width: 16px;
          height: 16px;
          background: #6f81bc;
          border-radius: 2px;
          display: inline-block;
          vertical-align: middle;
          margin-right: 10px;
          position: relative;
          top: -2px;
        }
        &:last-child {
          margin-right: 0;
        }
      }
      .status2 {
        .icon {
          background: #de4799;
        }
      }
    }
  }
  .authority-content {
    padding-top: 110px;
    height: calc(100vh - 166px);
    box-sizing: border-box;
    .bodyContent {
      height: 100%;
      .bodyPart {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      }
    }
    .grid-content {
      width: 100%;
      height: 100%;
      background: #ffffff;
      border-radius: 16px;

      .title {
        font-size: 18px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #303133;
        height: 40px;
        padding: 30px;
      }
      .contentTree {
        height: calc(100% - 60px);
        overflow: auto;
        padding: 20px;
      }
    }
    .grid-contentTop {
      width: 100%;
      height: 240px;
      background: #ffffff;
      border-radius: 16px;

      .title {
        font-size: 18px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #303133;
        height: 40px;
        padding: 30px;
      }
      .content {
        width: 100%;
        height: calc(100% - 60px);
        padding: 0 20px;
      }
    }

    .grid-contentBottom {
      width: 100%;
      height: calc(100% - 240px - 24px);
      background: #ffffff;
      border-radius: 16px;
      .title {
        font-size: 18px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #303133;
        height: 40px;
        padding: 30px;
      }
      .content {
        width: 100%;
        height: calc(100% - 60px);
        padding: 20px 20px;
        overflow-y: auto;
      }
    }
    .el-col {
      position: relative;
      .info-close {
        position: absolute;
        width: 24px;
        height: 24px;
        line-height: 24px;
        text-align: center;
        background: #000000;
        border-radius: 50%;
        top: -10px;
        right: 0;
        z-index: 15;
        color: #fff;
        cursor: pointer;
        .icon {
          color: #fff;
          font-weight: 600;
        }
      }
    }
    .itemBox {
      width: 100%;
      padding: 24px;
      position: relative;
      background: #f5f7fa;
      box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
      border-radius: 16px;
      padding: 10px 20px;
      overflow: hidden;
      min-height: 100px;
      margin-bottom: 24px;
      .status {
        width: 100%;
        height: 4px;
        background: #6f80bc;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        border-radius: 80px;
      }
      .status2 {
        width: 100%;
        height: 4px;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        background: #de4799;
      }
      .el-col {
        padding: 10px 0;
      }
      .itemTitle {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #303133;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      .info {
        line-height: normal;
        justify-content: flex-end;
        .name {
          font-size: 16px;
          font-weight: bold;
        }
        .cap {
          width: 24px;
          height: 24px;
          display: inline-block;
          background-repeat: no-repeat;
          background-size: cover;
          transition: all 0.3s;
          margin-left: 10px;
          &:first-child {
            margin-right: 0;
          }
        }
        .cap-plus {
          background-image: url("../../../assets/status/ic_plus.png");
          &:hover {
            background-image: url("../../../assets/status/ic_plus_hovar.png");
          }
        }
        .cap-edit {
          background-image: url("../../../assets/status/ic_edit.png");
          &:hover {
            background-image: url("../../../assets/status/ic_edit_hovar.png");
          }
        }
        .cap-sub {
          background-image: url("../../../assets/status/ic_subordinate.png");
          &:hover {
            background-image: url("../../../assets/status/ic_subordinate_hovar.png");
          }
        }
        .cap-job {
          background-image: url("../../../assets/status/ic_jobs.png");
          &:hover {
            background-image: url("../../../assets/status/ic_jobs_hovar.png");
          }
        }
        .cap-member {
          background-image: url("../../../assets/status/ic_member.png");
          &:hover {
            background-image: url("../../../assets/status/ic_member_hovar.png");
          }
        }
      }
      span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #909399;
        display: inline-block;
      }
      .msg {
        color: #303133;
      }
      .use {
        color: #6f80bc;
      }
      .unUse {
        color: #f56c6c;
      }
      .el-switch {
        float: right;
      }
    }
  }
}
::-webkit-scrollbar-track-piece {
  background: #d3dce6;
}

::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-thumb {
  background: #99a9bf;
  border-radius: 20px;
}
</style>
