// pages/gongchengliebiao/gongchengliebiao.js\
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    title:'工程列表',
    ids: "",
    value1:'省',
    value2:'市/区',
    value3:'公司',
    show1: false,
    show2: false,
    show3: false,
    sheng:[],
    citys:{},
    companys:{},
    cityArea: [],
    company: [],
    proCount:'0',
    projectInfoList:{},
    projectData:[],
    ifSonCompany:'',
    ifDeptId:false,
    deptId:'',
    type:'',
    popUp1:false,
    popUp2:false,
    popUp3:false,
    companyId:'',
    paramPost:'',
    loadShow:false,
    //省、市、区
    provinceId:'',
    cityId:'',
    areaId:'',
    //////新版本/////
    showTest:false,
    tabActive:0,
    jt:[],
    shengGs:[],
    ziGs:[],
    nav1: "0",
    nav2: "-1",
    nav3: "-1",
    classA:"",
    classB:"",
    classC:"",
    flag1:true,
    flag2:false,
    flag3:false,
    Highlight1:"active",
    Highlight2:"",
    Highlight3:"",
    title2:false,
    title3:false,
    dataclass1:0,
    dataclass2:0,
    dataclass3:0,
    overall1:"",
    overall2:"",
    overall3:"",
    newCompanyId:'',
    choiceShow:false,
    //模糊查询参数
    FuzzyPorjectId:"",
    FuzzyDeptId:"",
    FuzzyCompanyId:"",
    FuzzyType:"",
    FuzzyifSonCompany:"",
    Fuzzylist:[], 
  },

  onclassA(event){
    this.setData({nav1: event.target.dataset.index});
    this.setData({dataclass1: 1});
    if(this.data.dataclass1 == 1){
      this.setData({title2: true});
    }
    this.setData({classA: event.target.dataset.name});
    this.setData({overall1: event.target.dataset.name,newCompanyId:event.target.dataset.id});
    this.setData({value3:event.target.dataset.name})
    //获取子公司信息
    this.setData({showTest:false})
    wx.setStorageSync('select_dept_id', event.target.dataset.id)
    wx.setStorageSync('select_dept_name', event.target.dataset.name)
    wx.setStorageSync('select_dept_index', event.target.dataset.index)
    this.selectProjectByCompanyId(event.target.dataset.id,'','','');
    //this.selectCompanyJG(event.target.dataset.id,'SGS');
  },  
  onclassB(event){
    this.setData({nav2: event.target.dataset.index});
    this.setData({dataclass2: 1});
    if(this.data.dataclass2 == 1){
      this.setData({title3: true});
    }
    this.setData({classB:  " / " + event.target.dataset.name });
    this.setData({overall2:  event.target.dataset.name,newCompanyId:event.target.dataset.id });
    this.setData({value3:this.data.overall1 + " / "+ event.target.dataset.name})
    //获取子公司信息
    this.selectCompanyJG(event.target.dataset.id,'GS');
  },

  onclassC(event){
    this.setData({nav3: event.target.dataset.index});
    this.setData({dataclass3: 1});
    this.setData({classC:  " / " + event.target.dataset.name });
    this.setData({overall3:  event.target.dataset.name ,newCompanyId:event.target.dataset.id});
    this.setData({value3:  this.data.overall1 + " / " +  this.data.overall2 + " / " + event.target.dataset.name});
  },

  queren(event){
    //加载蒙版
    this.onClickShow();
    this.setData({ showTest: false});
    this.setData({ show3: false });
    if(this.data.overall1 == ""){
      this.setData({value3:""});
    }else if(this.data.overall2 == ""){
      this.setData({value3:  this.data.overall1 + " / "});
    }else if(this.data.overall3 == ""){
      this.setData({value3:  this.data.overall1 + " / " +  this.data.overall2 + " / " });  
    }else{
      this.setData({value3:  this.data.overall1 + " / " +  this.data.overall2 + " / " +  this.data.overall3});
    }
    
    let companyId = this.data.newCompanyId;
    let provinceId = this.data.provinceId;
    let cityId = this.data.cityId;
    let areaId = this.data.areaId;
    this.selectProjectByCompanyId(companyId,provinceId,cityId,areaId);
  },


  tabTltie(event){
    var id = event.currentTarget.id;
    if(id == 1){
      this.setData({flag1:true})
      this.setData({flag2:false})
      this.setData({flag3:false})
      this.setData({Highlight1:"active"})
      this.setData({Highlight2:""})
      this.setData({Highlight3:""})
      this.setData({title2:false})
      this.setData({title3:false})
      this.setData({classA:""})
      this.setData({classB:""})
      this.setData({classC:""})
      this.setData({nav2: "-1"});
      this.setData({nav3: "-1"});
      this.setData({overall1:"",overall2:"",overall3:""})
    }else if(id == 2){
      this.setData({flag1:false})
      this.setData({flag2:true})
      this.setData({flag3:false})
      this.setData({Highlight1:""})
      this.setData({Highlight2:"active"})
      this.setData({Highlight3:""})
      this.setData({title3:false})
      this.setData({classB:""})
      this.setData({classC:""})
      this.setData({nav3: "-1"});
      this.setData({overall2:"",overall3:""})
    }else{
      this.setData({flag1:false})
      this.setData({flag2:false})
      this.setData({flag3:true})
      this.setData({Highlight1:""})
      this.setData({Highlight2:""})
      this.setData({Highlight3:"active"})
      this.setData({overall3:""})
      this.setData({classC:""})
    }
  },

  showTest() {
    if(this.data.popUp3 == false){
      this.setData({ showTest: true });
    }
  },
  choice(){
    this.setData({ choiceShow: true });
  },
  onCloseTest() {
    this.setData({ showTest: false });
    this.setData({ choiceShow: false });
  },
 
  onChangeSheng(event) {
    //加载蒙版
    this.onClickShow();
    var province =   event.detail.value;
    this.setData({ 
      value1: province.text ,
      value2:"市/区",
      company: {},
      ifDeptId:true,
      provinceId:province.id,
    });
    this.setData({flag1:true})
    this.setData({flag2:false})
    this.setData({flag3:false})
    this.setData({Highlight1:"active"})
    this.setData({Highlight2:""})
    this.setData({Highlight3:""})
    this.setData({title2:false})
    this.setData({title3:false})
    this.setData({classA:""})
    this.setData({classB:""})
    this.setData({classC:""})
    this.setData({nav2: "-1"});
    this.setData({nav3: "-1"});
    this.setData({ show1: false });
    this.getDeptSubordinate(province.id);
    this.selectProjectByCompanyId(this.data.userInfo.deptId,province.id,'','');
  },

  onChangeShiQu(event) {
    //加载蒙版
    this.onClickShow();
    var city =   event.detail.value[0];
    var area =   event.detail.value[1].text;
    var sq = city+"/"+area
    this.setData({ 
      value2: sq ,
      value3:"公司",
      company: {},
      ifDeptId:true,
      cityId:area == '全部'?event.detail.value[1].id:'',
      areaId:area == '全部'?'':event.detail.value[1].id
    });
    this.setData({flag1:true})
    this.setData({flag2:false})
    this.setData({flag3:false})
    this.setData({Highlight1:"active"})
    this.setData({Highlight2:""})
    this.setData({Highlight3:""})
    this.setData({title2:false})
    this.setData({title3:false})
    this.setData({classA:""})
    this.setData({classB:""})
    this.setData({classC:""})
    this.setData({nav2: "-1"});
    this.setData({nav3: "-1"});
    this.setData({ show2: false });
    this.selectProjectByCompanyId(deptId,'','',event.detail.value[1].id);
  },

  onChange1(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.citys[value[0]]);
  },
  onChange2(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.companys[value[0]]);
  },

  showPopup1() {
    if(this.data.popUp1 == false){
      this.setData({ show1: true });
    }
  },
  showPopup2() {
    if(this.data.popUp2 == false){
      this.setData({ show2: true });
    }
  },

  onCancel() {
    this.setData({ show1: false });
    this.setData({ show2: false });
    this.setData({ show3: false });
  },

  onClose() {
    this.setData({ show1: false });
    this.setData({ show2: false });
    this.setData({ show3: false });
  },

  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },
  onLogout(){
    wx.clearStorageSync();
    wx.setStorageSync('isReload', "1")
    wx.redirectTo({
      url: '../login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取缓存中数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({
          userInfo:res.data,
          value1:app.globalData.value1,
          value2:app.globalData.value2,
          value3:app.globalData.value3    
        })
        // let _dataList = app.globalData.projectInfoList;
        // debugger
        // if(app.globalData.type == "1"){
        //   that.setData({
        //     "type":'1',
        //     title:"工程列表",
        //     proCount:_dataList.length,
        //     projectInfoList:_dataList,
        //   })      
        // }
      // else{
      //       that.setData({
      //         projectInfoList:_dataList,
      //         proCount:_dataList.length,
      //         title:"预警列表",
      //         "type":'2',
      //       })
      //   }
      //加载用户组织架构信息
      that.getFilstDept(res.data.deptId,res.data.userId);
      }
    })
  },

   /**
    * 加载用户组织架构信息
    * @param {*} even 
    * @param {*} even1 
    */
   getFilstDept:function(even,even1){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/selectDeptAll',
      data:{
        "deptId":even,
        "userId":even1,
        "minRoleId":that.data.userInfo?.minRoleId
      },
      method:"GET",
      success:function(res){
        console.log(res.data);
        res = res.data;
         //判断登录是否项目人员
        if(res.data.projectId != undefined){ 
          var projectData = [{projectId:res.data.projectId,projectName:res.data.projectName,projectAddress:res.data.projectAddress,companyName:res.data.companyName,streamNumber:res.data.streamNumber,projectCount:res.data.projectCount,videoNum:res.data.videoNum,monitoringCount:res.data.monitoringCount}]
          that.setData({
            value1:res.data.shengName,
            value2:res.data.shiName+'/'+res.data.quName,
            value3:res.data.companyName,
            projectData:projectData,
            popUp1:true,
            popUp2:true,
            popUp3:true,
          })
        }else{
          var columns = [];
          columns.push(res.data.province_list);
          that.setData({
            value1:res.data.shengName,
            value2:res.data.shiName+'/'+res.data.quName,
            value3:res.data.logCompanyName,
            companyId:res.data.companyId,
            paramPost:res.data.postId,
            company:columns,
            companys:columns,
            jt:res.data.province_list,
            popUp1:true,
            popUp2:true
          })
          if(res.data.province_list.length==1){
            that.setData({
              popUp3:true
            });
          }else{
            if(wx.getStorageSync('select_dept_index')!="" && wx.getStorageSync('select_dept_name')!=""){
              that.setData({
                nav1:wx.getStorageSync('select_dept_index'),
                value3:wx.getStorageSync('select_dept_name')
              });
            }
          }
          if(wx.getStorageSync('select_dept_id')==""){
            if(res.data.province_list.length==1){
              that.selectProjectByCompanyId(res.data.deptId,'','','');
            }else{
              that.selectProjectByCompanyId("0",'','','');
            }
          }else{
            that.selectProjectByCompanyId(wx.getStorageSync('select_dept_id'),'','','');
          }
        }
      }
    })
  },

  /**
   * 获取市区信息
   * @param {*} provinceId 
   */
  getDeptSubordinate:function(provinceId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/project/selectDeptSubordinate',
      data:{
        "userId":this.data.userInfo.userId,
        "provinceId":provinceId,
        "deptId":this.data.userInfo.deptId
      },
      method:"GET",
      success:function(res){
        let citys = res.data;
        let cityArea = [
          {
            values: Object.keys(citys),
            className: 'column1',
          },
          {
            values: citys[Object.keys(citys)[0]],
            className: 'column2',
            defaultIndex: 2,
          },
        ]  
        that.setData({
          cityArea: cityArea,
          citys:citys
        })
      }
    })
  },

  /**
   * 根据公司筛选项目信息
   * @param {*} deptId 
   * @param {*} provinceId 
   * @param {*} cityId 
   * @param {*} areaId 
   */
  selectProjectByCompanyId:function(deptId,provinceId,cityId,areaId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/project/selectProjectByCompanyId',
      data:{
        "userId":this.data.userInfo.userId,
        "deptId":deptId,
        "provinceId":provinceId,
        "cityId":cityId,
        "areaId":areaId,
        "minRoleId":that.data.userInfo.minRoleId
      },
      method:"GET",
      success:function(res){
        that.setData({
          proCount:res.data.length,
          projectData:res.data,
          projectInfoList:res.data
        })
        app.globalData.projectInfoList =  res.data; 
        //if(that.data.type == 1){
        //  that.setData({
        //    proCount:res.data.length,
        //    projectInfoList:res.data,
        //  }) 
        //}else{
          //that.getEnvironCount(deptId,'',provinceId,cityId,areaId);
        //} 
        //关闭蒙版
        that.onClickHide();
      }
    })
  },

  /**
   * 根据组织架构查询出公司信息
   * @param {*} deptId 
   * @param {*} state 
   */
  selectCompanyJG:function(deptId,state){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/project/selectCompanyJG',
      data:{
        "deptId":deptId
      },
      method:"GET",
      success:function(res){
        if(state=="SGS"){
          that.setData({
            shengGs:res.data
          })
        }else{
          that.setData({
            ziGs:res.data
          })
        }
      }
    })
  },

  //获取当前预警信息
  getEnvironCount:function(deptId,projectId,provinceId,cityId,areaId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/applets/getEnvironCount',
      method:'GET',
      data:{
        "deptId":deptId,
        "projectId":projectId,
        "provinceId":provinceId,
        "cityId":cityId,
        "areaId":areaId,
      },
      success:function(res){
        that.setData({
          projectInfoList:res.data,
          proCount:res.data.length
        })
      }
    })
  },

  //返回到地图页面
  returnMap:function(){
    wx.redirectTo({
      url: '../map/map',
    })
  },

   //模糊查询项目信息
   actqu:function(event){
    var that = this;
    that.setData({Fuzzylist:[]});
    if(event.detail.value != ""){
      wx.request({
        url: app.globalData.reqUrl+'/wechat/project/selectProjectInfoByName',
        data:{
          "projectName":event.detail.value,
          "userId":that.data.userInfo.userId,
          "deptId":that.data.jt.length==1?that.data.userInfo.deptId:(wx.getStorageSync('select_dept_id')==""?"0":wx.getStorageSync('select_dept_id')),
          "minRoleId":that.data.userInfo.minRoleId
        },
        method:"GET",
        success:function(res){
          res = res.data;
          that.setData({
            Fuzzylist:res.data
          })
        }
      })
    }else{
      that.setData({Fuzzylist:[]});
    }
  },

  //点击数据
  checkProjetName:function(event){
    var newList = [];
    var list = event.currentTarget.dataset.list;
    var projectName = event.currentTarget.dataset.name;
    for(var i =0;i<list.length;i++ ){
        if(list[i].projectName == projectName){
          newList.push(list[i]);
        }
    }
    this.setData({
      projectInfoList:newList,
      proCount:newList.length,
      choiceShow:false,
    })
  },

  //跳转到项目概况
  checkProject:function(even){
    //赋值到公共参数
    app.globalData.projectId = even.currentTarget.dataset['id'];
    app.globalData.projectName = even.currentTarget.dataset['name'];
    //app.globalData.projectInfoList = JSON.stringify(this.data.projectInfoList);
    app.globalData.type = this.data.type;
    app.globalData.value1 = this.data.value1;
    app.globalData.value2 = this.data.value2;
    app.globalData.value3 = this.data.value3;
    wx.redirectTo({
      url: '../xiangmugaikuang/xiangmugaikuang',
    })

  }

})