<template>
  <div class="max-big-box-one">
    <div v-show="newModifyPageType" class="big-max-box-one">
        <div class="left-max-box">
          <!--树结构-->
          <el-tree class="max-tree-box TabStrip" :indent="0"
                   ref="dataConfigTree"
                   default-expand-all
                   :expand-on-click-node="false"
                   node-key="id" :highlight-current="false"
                   :data="leftTopData" icon-class="none">
            <div class="tree-box" slot-scope="{ node, data }" @click="clickTree(data,node)">
              <!--<img :class="{'levelImg':node.level==1}" v-if="node.expanded&&node.childNodes[0]"-->
                   <!--src="~@/assets/images/icon_42.png">-->
              <!--<img :class="{'levelImg':node.level==1}" v-if="!node.expanded&&node.childNodes[0]"-->
                   <!--src="~@/assets/images/icon_43.png">-->
              <p class="label-p"
                 :class="{'level1':node.level==1,'level2':node.level==2,'level3':node.level==3,'levelColorA':node.checked||node.indeterminate,'levelColorB':!node.checked&&!node.indeterminate}"
              >{{ node.label }}</p>
            </div>
          </el-tree>
        </div>
        <div class="right-max-box">
          <div class="top-box">
            <div class="input-box">
            </div>
            <p class="add-button cursor_pointer" @click="clickPageType" v-if="userPermsData.indexOf('system:dept:add')!=-1">新增</p>
            <p class="del-button cursor_pointer" @click="allDel" v-if="userPermsData.indexOf('system:dept:remove')!=-1">批量删除</p>
            <p class="text-p">已选择{{tableNum}}条</p>
          </div>
          <div class="table-box">
            <el-table
              ref="multipleTable"
              :row-key="getRowKey"
              :data="tableData"
              tooltip-effect="dark"
              style="width: 100%"
              @select-all="handleSelectionChange"
              @select="handleSelectionChange">
              <el-table-column
                :reserve-selection="true"
                type="selection"
                width="64">
              </el-table-column>
              <el-table-column
                type="index"
                :index="indexMethod"
                label="序号"
                width="61">
              </el-table-column>
              <el-table-column
                prop="deptName"
                label="名称"
                width="254">
              </el-table-column>
              <el-table-column
                prop="level"
                label="级别"
                width="142">
              </el-table-column>
              <el-table-column
                prop="deptNameParent"
                label="上级"
                width="250">
                <template slot-scope="scope">{{!scope.row.deptNameParent?"最上级":scope.row.deptNameParent}}</template>
              </el-table-column>
              <el-table-column
                prop="remark"
                label="备注"
                width="188">
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="创建时间"
                width="330">
              </el-table-column>
              <el-table-column
                label="操作"
                width="200">
                <template slot-scope="scope">
                  <div class="table-min-button">
                    <div class="cursor_pointer" @click="handleEdit(scope.$index, scope.row)" v-if="userPermsData.indexOf('system:dept:edit')!=-1">
                      <img src="~@/assets/images/icon_37.png">
                      <p>编辑</p>
                    </div>
                    <div class="cursor_pointer" @click="handleDelete(scope.$index, scope.row)" v-if="userPermsData.indexOf('system:dept:remove')!=-1">
                      <img src="~@/assets/images/icon_38.png">
                      <p>删除</p>
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <el-pagination
            background
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :current-page="currentPage"
            :page-size="pagesize"
            :total="total">
          </el-pagination>
        </div>
    </div>
    <new-modify v-if="!newModifyPageType"
      :newModifyData="newModifyData"
      @newModifyLeftClick="newModifyLeftClick"
      @newModifyRightClick="newModifyRightClick"
    ></new-modify>
    <interactive-window
      v-if="windowType"
      :windowData="windowData"
      @windowLeftClick="windowLeftClick"
      @windowRightClick="windowRightClick"
    ></interactive-window>
  </div>
</template>

<script>
  import { deptTreeselect,selectDeptList,deptDel,deptAdd,deptEditor } from '@/api/userApi'
  export default {
    name: "organizeFrameworkInformationMaintenance",//组织框架信息维护
    components:{
      newModify: resolve => {require(['@/components/newModify'], resolve)},//新建-修改组件
      interactiveWindow: resolve => {require(['@/components/interactiveWindow'], resolve)},//提示弹窗
    },
    data () {
      return {
        userPermsData:localStorage.getItem('userPerms'),
        //表格限制
        pagesize: 8,
        currentPage:1,
        //列表总数
        total:0,
        //树数据
        leftTopData: [],
        /*数据*/
        tableData:[],
        //选中数据
        multipleSelection:[],
        // 弹窗状态
        windowType:false,
        //弹窗组件数据
        windowData:{},
        //勾选数量
        tableNum:0,
        // 新增-修改页面状态
        newModifyPageType:true,
        //新增-修改页面数据
        newModifyData:{}
      }
    },

    created(){

    },
    mounted() {
      this.deptTreeselect();
    },
    methods: {
      // 单独删除
      async clickDel(deptId){
        let ids = [];
        ids.push(deptId)
        const { data } = await deptDel(ids);
        if(data.code==200){
          this.windowData = {
            type:"2",
            title:"提示",
            text:"操作成功",
          };
          this.windowType = !this.windowType;
          this.$refs.multipleTable.clearSelection();
          this.deptTreeselect();
        }
      },
      //批量删除
      async clickAllDel () {
        let self = this;
        let ids = [];
        for(let i=0;i<self.multipleSelection.length;i++){
          ids.push(self.multipleSelection[i].deptId)
        }
        console.log("ids",ids);
        const { data } = await deptDel(ids);
        if(data.code==200){
          this.windowData = {
            type:"2",
            title:"提示",
            text:"操作成功",
          };
          this.windowType = !this.windowType;
          this.$refs.multipleTable.clearSelection();
          this.deptTreeselect();
        }
      },
      //数据列表
      async getListData (){
        let self = this;
        let obj={
          deptId:"",
          pageSize:self.pagesize,
          pageNum:self.currentPage,
        };
        let list = self.$refs.dataConfigTree.getCheckedNodes();
        if(list[0]){
          obj.deptId = list[0].id;
        }
        const { data } = await selectDeptList(obj);
        if(data.code==200){
          this.tableData = data.rows;
          this.total = data.total;
          this.tableNum = 0;
          this.$forceUpdate();
        }
      },
      //获取组织架构树状数据
      async deptTreeselect () {
        let self = this;
        const { data } = await deptTreeselect();
        if(data.code==200){
          this.leftTopData = data.data;
          this.getListData();
        }
      },
      //新建组件状态切换
      clickPageType(){
        let self = this;
        let newData = {
          title:"新增",
          type:"1",
          list:[
            {
              name:"名称",
              value:"",
            },
            {
              name:"备注",
              value:"",
              textArea:true,
              maxlength:40
            },
            {
              name:"是否是总仓库",
              value:"",
              radioType:true,
              modelLabel:"0",
              radioList:[
                {
                  name:"是",
                  label:"1",
                },
                {
                  name:"否",
                  label:"0",
                },
              ]
            },
            {
              name:"上级",
              treeType:true,                          //树状声明
              singleType:true,
              treeData:[],                            //树状数据
              treeList:[],                           //树状勾选数据
            }
          ]
        };
        let list = [];
        for(let i=0;i<self.leftTopData.length;i++){
          let obj = {
            value : self.leftTopData[i].id,
            label : self.leftTopData[i].label,
            children : [],
            level : "二级"
          };
          for(let k=0;k<self.leftTopData[i].children.length;k++){
            let newObj = {
            value : self.leftTopData[i].children[k].id,
            label : self.leftTopData[i].children[k].label,
            level : "三级",
            };
            obj.children.push(newObj);
          }
          list.push(obj);
        }
        newData.list[3].treeData = list;
        this.newModifyData = {...newData};
        this.newModifyPageType = !this.newModifyPageType;
      },
      // 批量删除
      allDel(){
        if(this.multipleSelection[0]){
          this.windowData = {
            type:"1",
            title:"提示",
            text:"确定要批量删除这些信息吗?",
            leftButton:"取消",
            rightButton:"确定",
            cType:"1",
          };
          this.windowType = !this.windowType;
        }else{
          this.windowData = {
            type:"3",
            title:"提示",
            text:"您没有勾选任何数据!",
          };
          this.windowType = !this.windowType;
        }
      },
      //表格绑定ID
      getRowKey (row) {
        return row.id
      },
      indexMethod(index) {
        return (this.currentPage-1)*this.pagesize+index+1;
      },
      //翻页
      handleCurrentChange(val) {
        console.log(`当前页: `,val);
        this.currentPage = val;
        this.getListData();
      },
      //编辑
      handleEdit(index, row) {
        let newData = {
          title:"编辑",
          type:"2",
          deptId:row.deptId,
          list:[
            {
              name:"名称",
              value:row.deptName,
            },
            {
              name:"备注",
              value:row.remark,
              textArea:true,
              maxlength:40
            },
            {
              name:"是否是总仓库",
              value:"",
              radioType:true,
              modelLabel:row.isMain.toString(),
              radioList:[
                {
                  name:"是",
                  label:"1",
                },
                {
                  name:"否",
                  label:"0",
                },
              ]
            },
          ]
        };
        this.newModifyData = {...newData};
        this.newModifyPageType = !this.newModifyPageType;
      },
      // 删除
      handleDelete(index, row) {
        this.windowData = {
          type:"1",
          title:"提示",
          text:"确定要删除这条信息吗?",
          leftButton:"取消",
          rightButton:"确定",
          cType:"2",
          deptId:row.deptId
        };
        this.windowType = !this.windowType;
      },
      //勾选
      handleSelectionChange(val) {
        this.multipleSelection = val;
        this.tableNum = this.multipleSelection.length
      },
      //弹窗组件左侧按钮方法-对应取消方法
      windowLeftClick(){
        this.windowType = !this.windowType;
      },
      //弹窗组件右侧按钮方法
      windowRightClick(data){
        this.windowType = false;
        if(data.cType==1){
          this.clickAllDel();
        }else if(data.cType==2){
          this.clickDel(data.deptId);
        }
      },
      //数结构点击事件
      clickTree(data,node){
        if(node.checked){
          this.$refs.dataConfigTree.setCheckedKeys([]);
        }else{
          this.$refs.dataConfigTree.setCheckedKeys([node.data.id]);
        }
        this.getListData();
      },
      //新增-修改组件内左侧按钮事件
      newModifyLeftClick(){
        this.newModifyPageType = !this.newModifyPageType;
      },
      //新增-修改组件内左侧按钮事件
      newModifyRightClick(data){
        if(!data.list[0].value){
          this.windowData = {
            type:"3",
            title:"提示",
            text:"请输入名称",
          };
          this.windowType = !this.windowType;
          return
        }
        if(!data.list[1].value){
          this.windowData = {
            type:"3",
            title:"提示",
            text:"请输入备注",
          };
          this.windowType = !this.windowType;
          return
        }
        if(data.type == "1"){
          if(!data.list[3].treeList[0]){
            this.windowData = {
              type:"3",
              title:"提示",
              text:"请选择上级",
            };
            this.windowType = !this.windowType;
            return
          }
        }
        if(data.type == 1){
          //新增
          this.deptAdd(data);
        }else if(data.type == 2){
          //编辑
          this.deptEditor(data);
        }
      },
      //新增
      async deptAdd (item) {
        let level = "";
        let id = item.list[3].treeList[item.list[3].treeList.length-1];
        for(let i=0;i<item.list[3].treeData.length;i++){
          if(id == item.list[3].treeData[i].value){
            level = item.list[3].treeData[i].level;
          }
          for(let k=0;k<item.list[3].treeData[i].children.length;k++){
            if(id == item.list[3].treeData[i].children[k].value){
              level = item.list[3].treeData[i].children[k].level;
            }
          }
        }
        let obj = {
          deptName:item.list[0].value,
          remark:item.list[1].value,
          isMain:item.list[2].modelLabel,
          parentId:id,
          level:level,
        };
        const { data } = await deptAdd(obj);
        if(data.code==200){
          this.newModifyPageType = !this.newModifyPageType;
          this.deptTreeselect();
          this.windowData = {
            type:"2",
            title:"提示",
            text:"新增成功",
          };
          this.windowType = !this.windowType;
        }
      },
      //编辑
      async deptEditor (item) {
        let obj = {
          deptId:item.deptId,
          deptName:item.list[0].value,
          remark:item.list[1].value,
          isMain:item.list[2].modelLabel,
        };
        const { data } = await deptEditor(obj);
        if(data.code==200){
          this.newModifyPageType = !this.newModifyPageType;
          this.deptTreeselect();
          this.windowData = {
            type:"2",
            title:"提示",
            text:"编辑成功",
          };
          this.windowType = !this.windowType;
        }
      },
    }
  }
</script>

<style scoped lang="stylus">
  .max-big-box-one
    display flex
    flex:1;
    .big-max-box-one
      margin:0 105px;
      flex:1;
      display flex
      .left-max-box
        width:219px;
        .max-tree-box
          z-index:1;
          overflow-x:hidden;
          overflow-y:scroll;
          white-space: nowrap;
          .tree-box
            display flex
            img
              display block
              width:20px;
              height:20px;
            p
              margin-left:8px;
      .right-max-box
        width:1490px;
        display flex
        flex-direction: column;
        .top-box
          display flex
          height:60px;
          .input-box
            width:1140px;
            display flex
          .add-button
            width:110px;
            height:30px;
            background:url("~@/assets/images/button_06_1.png");
            background-size:100% 100%;
            line-height:30px
            color:$color_ff;
            font-size:$font_12;
            padding-left:44px;
            text-align left
          .del-button
            margin-left:30px;
            width:110px;
            height:30px;
            background:url("~@/assets/images/button_05_1.png");
            background-size:100% 100%;
            line-height:30px
            color:$color_ff;
            font-size:$font_12;
            padding-left:41px;
            text-align left
          .text-p
            width:103px;
            color:$color_ff;
            line-height:30px
            font-size:$font_12;
        .table-box
          flex:1;
          width:1490px !important;
        .table-min-button
          width:140px;
          margin:0 auto;
          display flex
          div:nth-child(1)
            height:20px;
            width:60px;
            font-size:$font_14;
            color:$theme;
            display flex
            margin-right:11px;
            img
              display block
              height:20px;
              width:20px;
              margin-right:4px;
          div:nth-child(2)
            height:20px;
            width:60px;
            font-size:$font_14;
            color:$theme3;
            display flex
            img
              display block
              height:20px;
              width:20px;
              margin-right:4px;
  //修改table表格样式
  .el-table
    background rgba(6,177,240,0.2)
  .el-table /deep/ tr
  .el-table /deep/ th
    background rgba(6,177,240,0)
  .el-table::before
    height:0;
  .el-table /deep/ .cell
    max-height:46px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  .el-table /deep/ td
  .el-table /deep/ th.is-leaf
    border-bottom:0!important;
  .el-table /deep/ .el-checkbox__inner
    //background rgba(6,177,240,0.2)
    border 1px solid #216f93 !important
  .el-table /deep/ thead
    color:$color_ff;
  .el-table /deep/ th
    text-align-last: center;
  .el-table /deep/ td
    text-align-last: center;
  .el-table
    color:$color_a1
  .el-table /deep/ tbody tr:hover > td
    background rgba(6,177,240,0.2)
    color:$theme;
  .el-table /deep/ .el-checkbox
    width:14px;
    margin:0 auto;
    display block
  //修改动作条样式
  .el-pagination
    margin:20px 0 30px;
  .el-pagination.is-background /deep/ .btn-next
  .el-pagination.is-background /deep/ .btn-prev
  .el-pagination.is-background /deep/ .el-pager li
    background:url("~@/assets/images/button_13.png");
    color:$color_ff;
  .el-pagination.is-background /deep/ .el-pager li:not(.disabled).active
    background:url("~@/assets/images/button_14.png");
    color:$color_ff;
  .colorA
    color :$theme;
  ::-webkit-input-placeholder
    color: $border_1;
  ::-moz-placeholder
    color: $border_1;
  ::-ms-input-placeholder
    color: $border_1;

  //树状图
  .el-tree
    background rgba(0,0,0,0) !important
  /*多层级样式*/
  .levelImg{
    margin-top: 7px;
  }
  .level1{
    font-size:$font_17;
    color:$theme !important;
    height:34px;
    line-height 34px
  }
  .level2{
    font-size:$font_14;
    height:22px;
    line-height 22px
  }
  .level3{
    font-size:$font_12;
    height:20px;
    line-height 20px
  }
  .levelColorA{
    color:$color_ff;
  }
  .levelColorB{
    color:$color_91;
  }
  /*层级指引线*/
  .tree-container /deep/ .el-tree > .el-tree-node :after {
    border-top: none;
  }
  .el-tree /deep/ .el-tree-node .el-tree-node {
    position: relative;
    padding-left: 16px !important;
  }
  .el-tree /deep/ .el-tree-node__children {
    padding-left: 16px;
  }

  .el-tree /deep/ .el-tree-node :last-child:before {
    height: 38px;
  }

  .el-tree /deep/ .el-tree > .el-tree-node:before {
    border-left: none;
  }

  .tree-container /deep/ .el-tree > .el-tree-node:after {
    border-top: none;
  }

  .el-tree /deep/ .el-tree-node .el-tree-node:before {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  .el-tree /deep/ .el-tree-node .el-tree-node:after {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  .el-tree /deep/ .el-tree-node:before {
    border-left: 1px dashed #4386c6;
    bottom: 0px;
    height: 100%;
    top: -26px;
    width: 1px;
  }

  .el-tree /deep/ .el-tree-node:after {
    border-top: 1px dashed #4386c6;
    height: 20px;
    top: 12px;
    width: 24px;
  }

  .tree-container { //树的parent，样式自定
  }
  .el-tree /deep/ .el-tree-node__content .el-tree-node__expand-icon{
    padding:1px !important
  }
  //修改悬浮样式
  .el-tree /deep/ .el-tree-node:focus > .el-tree-node__content {
    background-color: rgba(0,0,0,0) !important;
  }
  .el-tree /deep/ .el-tree-node__content:hover {    background-color: rgba(0,0,0,0);
  }
  .el-tree /deep/ .el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
    background-color: rgba(0,0,0,0);
  }
</style>
