//index.js

const app = getApp()

Page({
  data: {
    userInfo:{},
    deptId:'',
    height:"auto",
    scale:'10',
    initialLon:'108.922306',
    initialLat:'34.191848',
    markers: [],
    value1:'省',
    value2:'市/区',
    value3:'公司',
    show1: false,
    show2: false,
    show3: false,
    sheng: [],
    citys:{},
    companys:{},
    cityArea: [],
    company:[],
    projectData:[],
    environProjectData:[],
    projectCount:'0',
    ifSonCompany:'',
    ifDeptId:false,
    monitoringCount:'0',
    videoNum:'0',
    mechanicsNum:'0',
    environCount:'0',
    logCompanyName:'',
    popUp1:false,
    popUp2:false,
    popUp3:false,
    companyId:'',
    paramPost:'',
    loadShow:false,
    //省、市、区
    provinceId:'',
    cityId:'',
    areaId:'',
    temDeptId:'',
    //*******新版本*****/
    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:'',
  },

  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);
    this.selectEarly(companyId,'',provinceId,cityId,areaId);
    this.getEnvironCount(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 });
    }
  },

  onCloseTest() {
    this.setData({ showTest: false });
  },

  
  onChangeSheng(event) {
    //加载蒙版
    this.onClickShow();
    var province =   event.detail.value;
    this.setData({
      value1: province.text ,
      value2:"市/区",
      value3:"公司",
      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"});
    app.globalData.paramDeptId = province.id,
    this.setData({ show1: false });
    this.getDeptSubordinate(province.id);
    this.selectProjectByCompanyId(this.data.userInfo.deptId,province.id,'','');
    this.selectEarly(this.data.userInfo.deptId,'',province.id,'','');
    this.getEnvironCount(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 });

    let deptId = this.data.userInfo.deptId;
    this.selectProjectByCompanyId(deptId,'','',event.detail.value[1].id);
    //this.selectEarly(deptId,'','','',event.detail.value[1].id);
    //this.getEnvironCount(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 });
  },

  //页面初始化
  onLoad: function (option) {
    var that = this
    //加载蒙版
    that.onClickShow();
    //调用wx.getSystemInfo接口，然后动态绑定组件高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight,
        })
      }
    }),
    //获取缓存中数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        console.log(res.data);
        that.setData({
          userInfo:res.data
        })
        app.globalData.deptId = res.data.deptId,
        //加载用户组织架构信息
        that.getFilstDept(res.data.deptId,res.data.userId);
      }
    })
  },

  /**
   * 加载用户组织架构信息
   * @param {*} deptId 
   * @param {*} userId 
   */
  getFilstDept:function(deptId,userId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/selectDeptAll',
      data:{
        "deptId":deptId,
        "userId":userId,
        "minRoleId":that.data.userInfo?.minRoleId
      },
      method:"GET",
      success:function(res){
        console.log(res.data);
        res = res.data;
        //判断登录是否项目人员
        if(res.data.projectId != undefined){ 
          var markers = [{id:0,projectId:res.data.projectId,longitude:res.data.longitude,latitude:res.data.latitude,projectName:res.data.projectName}];
          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,towerCount:res.data.towerCount,monitoringCount:res.data.monitoringCount}]
          that.setData({
            value1:res.data.shengName,
            value2:res.data.shiName+'/'+res.data.quName,
            value3:res.data.companyName,
            markers:markers,
            scale:15,
            initialLon:res.data.longitude,
            initialLat:res.data.latitude,
            projectCount:1,
            projectData:projectData,
            logCompanyName:res.data.companyName,
            popUp1:true,
            popUp2:true,
            popUp3:true,
          })
          //that.selectEarly(res.data.deptId,res.data.projectId,'','','');
          //that.getEnvironCount(res.data.deptId,res.data.projectId,'','','');
          that.onClickHide();
        }else{
          var columns = [];
          columns.push(res.data.province_list);
          that.setData({
            value1:res.data.shengName,
            value2:res.data.shiName+'/'+res.data.quName,
            logCompanyName:res.data.logCompanyName,
            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')
              });
            }
          }
          //that.selectEarly(res.data.deptId,'','','','');
          //that.getEnvironCount(res.data.deptId,'','','','');
          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'),'','','');
          }
          that.onClickHide();
        }
      }
    })
  },

  /**
   * 获取市区信息
   * @param {*} provinceId  省id
   */
  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){
        that.onClickHide();
        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":that.data.userInfo.userId,
        "deptId":deptId,
        "provinceId":provinceId,
        "cityId":cityId,
        "areaId":areaId,
        "minRoleId":that.data.userInfo.minRoleId
      },
      method:"GET",
      success:function(res){
        //关闭蒙版
        that.onClickHide();
        if(res.data.length >0){
          that.setData({
            markers:res.data,
            projectCount:res.data.length,
            projectData:res.data,
            scale:5,
            initialLon:res.data[0].longitude,
            initialLat:res.data[0].latitude,
          })
          app.globalData.projectInfoList =  res.data;
        }else{
          that.setData({
            markers:res.data,
            projectCount:res.data.length,
            projectData:res.data,
            scale:5,
          }) 
        }     
      }
    })
  },

  /**
   * 根据组织架构查询出公司信息
   * @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){
        that.onClickHide();
        if(state == "SGS"){
          that.setData({
            shengGs:res.data
          })
        }else{
          that.setData({
            ziGs:res.data
          })
        }       
      }
    })
  },

  /**
   * 预警总个数
   * @param {*} deptId 
   * @param {*} projectId 
   * @param {*} provinceId 
   * @param {*} cityId 
   * @param {*} areaId 
   */
  selectEarly:function(deptId,projectId,provinceId,cityId,areaId){
      var that = this;
      wx.request({
        url: app.globalData.reqUrl+'/weixin/applets/selectYangchenCount',
        method:"GET",
        data:{
          "deptId":deptId,
          "projectId":projectId,
          "provinceId":provinceId,
          "cityId":cityId,
          "areaId":areaId,
        },
        success:function(res){
          that.onClickHide();
          that.setData({
            monitoringCount:res.data.monitoringCount,
            videoNum:res.data.videoNum,
            mechanicsNum:res.data.machineEnvirCount
          })
        }
      })
  },

  /**
   * 预警项目信息
   * @param {*} deptId 
   * @param {*} projectId 
   * @param {*} provinceId 
   * @param {*} cityId 
   * @param {*} areaId 
   */
  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.onClickHide();
        that.setData({
          environProjectData:res.data,
          environCount:res.data.length
        })
      }
    })
  },

  /**
   * 个人定位点
   */
  selectUserLocation:function(){
    let markers = this.data.markers;
    markers.push(app.globalData.markers[0]);
    this.setData({
      scale:5,
      initialLon:app.globalData.initialLon,
      initialLat:app.globalData.latitude,
      markers:markers,
    })
  },


  //点击项目点跳转到项目概况页面
  getProjectListById:function(even){
    if(even.markerId != '-1'){
      var projectData= this.data.projectData;
      var value1 = "";
      if(this.data.value1 != ""){
        value1 = this.data.value1;
      }else{
        value1 = '省';
      }
      var value2 = "";
      if(this.data.value2 != ""){
        value2 = this.data.value2;
      }else{
        value2 = '市/区';
      }
      var value3 = "";
      if(this.data.value3 != ""){
        value3= this.data.value3;
      }else{
        value3 = '公司';
      }
      //赋值到公共参数
      app.globalData.projectId = this.data.markers[even.markerId].projectId;
      app.globalData.projectName = this.data.markers[even.markerId].projectName;
      //app.globalData.projectInfoList = JSON.stringify(projectData);
      app.globalData.type = 1;
      app.globalData.value1 = value1;
      app.globalData.value2 = value2;
      app.globalData.value3 = value3;
      wx.redirectTo({
        url: '../xiangmugaikuang/xiangmugaikuang'
      })
    }else{
      app.toast("本定位点为个人定位");
    }
  },

  //查看项目详情
  checkDetailsSY:function(even){
    var projectData= this.data.projectData;
    var value1 = "";
    if(this.data.value1 != ""){
      value1 = this.data.value1;
    }else{
      value1 = '省';
    }
    var value2 = "";
    if(this.data.value2 != ""){
      value2 = this.data.value2;
    }else{
      value2 = '市/区';
    }
    var value3 = "";
    if(this.data.value3 != ""){
      value3= this.data.value3;
    }else{
      value3 = '公司';
    }
    //添加数据到app.js文件固定参数
    //app.globalData.projectInfoList = JSON.stringify(projectData);
    app.globalData.type = 1;
    app.globalData.value1 = value1;
    app.globalData.value2 = value2;
    app.globalData.value3 = value3;
    wx.redirectTo({
      url: '../gongchengliebiao/gongchengliebiao'
    })
  },

  //查看预警详情
  checkDetailsYJ:function(even){
    var environProjectData = this.data.environProjectData;
    var value1 = "";
    if(this.data.value1 != ""){
      value1 = this.data.value1;
    }else{
      value1 = '省';
    }
    var value2 = "";
    if(this.data.value2 != ""){
      value2 = this.data.value2;
    }else{
      value2 = '市/区';
    }
    var value3 = "";
    if(this.data.value3 != ""){
      value3= this.data.value3;
    }else{
      value3 = '公司';
    }
    //添加数据到app.js文件固定参数
    //app.globalData.projectInfoList = JSON.stringify(environProjectData);
    app.globalData.type = 2;
    app.globalData.value1 = value1;
    app.globalData.value2 = value2;
    app.globalData.value3 = value3;
    wx.redirectTo({
      url: '../gongchengliebiao/gongchengliebiao',
    })
  },

})
