<template>
  <div class="MTCompanyUser">
    <div>组织管理</div>
    <el-row type="flex" justify="start">
      <el-col :span="10">
        <el-tree
        :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
          empty-text="暂无数据"
          :expand-on-click-node="false"
        ></el-tree>
      </el-col>
      <el-col :span="12" v-if="MTCompanyUserBtn.MTCompanyUserBtn27">
        <el-table :data="tableData">
          <el-table-column label="头像" min-width="80" align="center">
            <template slot-scope="rows">
              <div class="ImgWidth">
                <img :src="rows.row.avatar" alt="" >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            min-width="80"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="角色"
            min-width="80"
            align="center"
            prop="role"
          >
          </el-table-column>
          <el-table-column
            prop="mobile"
            label="联系方式"
            min-width="80"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { setTimeout } from "timers";
import { filter } from "minimatch";
export default {
  components: {},
  name: "MTCompanyUser",
  data() {
    return {
      data: [],
      defaultProps: {
        children: "organizations",
        label: "areaName",
        isLeaf: "leaf"
      },
      ARRoNE: [],
      ARRTWO: [],
      ARRThree: [],
      tableData: [],
      MTCompanyUserBtn:{
        MTCompanyUserBtn27:0
      }

    };
  },
  created() {
    this.getOrganztionList();

    var MTCompanyUserBtn = {}
    this.getButton(26).then(result => {
      if (result.data.statusCode==="1") {
        MTCompanyUserBtn = result.data.data
        MTCompanyUserBtn.map((item)=>{
          if(item.id==27){
            this.MTCompanyUserBtn.MTCompanyUserBtn27 = item.checked?1:0 
          }
        })
      }
    });
  },
  mounted() {
  },
  methods: {
    // 获取列表
    getOrganztionList(){
        var url="enterprise/organization/list";
        var param={
        mtCompanyId:JSON.parse(sessionStorage.getItem("companyID")).id,
      }
      this.axios.post(url,param).then(res=>{
        if(res.statusCode==="1"){
          // var a = JSON.stringify(res.data.areas).replace(/province/g, "label");
          // var b = JSON.parse(a);
          var a=res.data.map(item=>{
           item.areaName=item.areaName?item.areaName+`(所辖城市数量:${item.organizations?item.organizations.length:0})`:"未知"+`(所辖城市数量:${item.organizations?item.organizations.length:0})`;
           if(!item.organizations){
             return item;
           }
           item.organizations.map(item2=>{
              item2.areaName=item2.areaName+`(辖行政区数量:${item2.organizations?item2.organizations.length:0})`;
               if(!item2.organizations){
                  return item2;
                }
               item2.organizations.map(item3=>{
                 var a3=item3;
                 item3.areaName=item3.areaName+`(所辖项目数量:${item3.projects?item3.projects.length:0})`;
                 if(!item3.projects){
                  return a3;
                }
                 var b4=item3.projects.map(item4=>{
                   var a4={
                     areaName:item4.projectName+`(所辖电梯数量:${item4.num})`,
                     level:4,
                      id:item4.id
                   };
                   return a4
                 })
                 a3.organizations=b4;
                 return a3
              })
              return item2
            })
            return item;
          })
          this.data=res.data;
        }else if(res.statusCode==="0"){
            this.$message(res.message)
        }
      })
    },
    handleNodeClick(...age) {
      var url="enterprise/organization/user";
      if(parseInt(age[0].level)<4) return ;
      var param={
        id:age[0].id
      };
      this.axios.post(url,param).then(res=>{
        if(res.statusCode==="1"){
          this.tableData=res.data;
        }else{
          this.$message({
            type: "warning",
            message:res.message
          })
          this.tableData=[];
        }
      })
      // var param={id:age[0].projects[0].id}
      // this.axios.post(url,param).then(res=>{
      // })
    },
    /*
    loadNode(node, resolve) {
      var _this=this;
      // var bbb = [
      //   {
      //     Level: 1,
      //     id: "230000",
      //     name: "黑龙江省(所辖城市数量:1)",
      //     open: false,
      //     pId: "0"
      //   },
      //   {
      //     Level: 1,
      //     id: "420000",
      //     name: "湖北省(所辖城市数量:1)",
      //     open: false,
      //     pId: "0"
      //   },
      //   {
      //     Level: 2,
      //     id: "230200",
      //     name: "齐齐哈尔市(所辖行政区数量:2)",
      //     open: false,
      //     pId: "230000"
      //   },
      //   {
      //     Level: 3,
      //     id: "230206",
      //     name: "富拉尔基区(所辖项目数量:1)",
      //     open: false,
      //     pId: "230200"
      //   },
      //   {
      //     Level: 3,
      //     id: "230207",
      //     name: "碾子山区(所辖项目数量:1)",
      //     open: false,
      //     pId: "230200"
      //   },
      //   {
      //     Level: 2,
      //     id: "420100",
      //     name: "武汉市(所辖行政区数量:1)",
      //     open: false,
      //     pId: "420000"
      //   },
      //   {
      //     Level: 2,
      //     id: "420105",
      //     name: " 潜江市(所辖行政区数量:1)",
      //     open: false,
      //     pId: "420000"
      //   },
      //   {
      //     Level: 3,
      //     id: "420114",
      //     name: "蔡甸区(所辖项目数量:13)",
      //     open: false,
      //     pId: "420100"
      //   }
      // ];
      var ARR_filter = function(A, B) {
        console.log(_this.data);
        return _this.data.filter((item, index, value) => {
          var a=item;
          if(item.Level === A){
            a.areaName=item.areaName+"(所辖项目数量"+item.organizations.length+")"
          }
          console.log(a);
          return a;
        });
      };
      switch (node.level) {
        case 0:
          let ARRoNE = ARR_filter(1, "0");

          return resolve(ARRoNE);
          break;
        case 1:
          let ARRTWO = ARR_filter(2, node.data.id);
          return resolve(ARRTWO);
          break;
        case 2:
          let ARRThree = ARR_filter(3, node.data.id);
          return resolve(ARRThree);
          break;
        case 3:
          
          return resolve(this.MoreNode(node.data));
          break;
      }
    },*/
    MoreNode() {
      return [
        {
          ID: 100110410000037,
          click: "GetProjectUser('100110410000037')",
          name: "项目333(系统电梯台量:0)",
          leaf: true
        }
      ];
    },
    GetProjectUser(a) {
      return [
        {
          AvatarUrl:
            "https://ty-picture.oss-cn-hangzhou.aliyuncs.com/tx123.png",
          Mobile: "15927827433",
          Name: "廖子豪",
          UserRole: 12
        },
        {
          AvatarUrl:
            "https://ty-picture.oss-cn-hangzhou.aliyuncs.com/tx123.png",
          Mobile: "15927431528",
          Name: "龚小钰",
          UserRole: 16
        }
      ];
    }
  }
};
</script>
<style lang="stylus"></style>

