let app = getApp();
//替换成开发者后台设置的安全域名
let domain = app.globalData.domain;
let getDepartmentUrl = domain + "/dingding/getDepartment";
let addInSignUrl = domain + "/dingding/addInSign";
let getInSignUrl = domain +"/dingding/getInSign";
let getStaffUrl = domain + "/dingding/getStaff";
Page({

  data: {
    departmentList: [
      // { name: "工程部", id: 0, employeeList: [{ name: "张三", checked: false }, { name: "李四", checked: false }],isColpand:false ,checked:false},
      // { name: "市场部", id: 1, employeeList: [{ name: "张三", checked: false }, { name: "李四", checked: false }], isColpand: false, checked: false},
      // { name: "销售部", id: 2, employeeList: [{ name: "张三", checked: false }, { name: "李四", checked: false }], isColpand: false, checked: false }
    ],
    selectall:{
      checked:false,
      color:"#ccc",

       text:"全选"
    },
     mid:null,
     cnum:null ,
     inputValue: ''
  },

//搜索框文本内容显示
  searchbuild(e){
   // event.detail = {value: value};
 //  con
   var userId = app.globalData.userId;
    // this.setData({
    //   inputValue:,
    // })
    console.log('searchbuild-----' + this.data.inputValue);
 
    var _this = this;
    
      dd.httpRequest({
        url: getStaffUrl,
        data: {
          "keyword": e.detail.value,
          "userId":userId
        },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      success: function(res){
        console.log('res----', res.data)
          
        var departmentList = _this.data.departmentList;
          departmentList = res.data.result;
          for (var i = 0; i<departmentList.length;i++){
            if(departmentList.length < 3){
             departmentList[i]['isColpand']=true;
            }else{
               departmentList[i]['isColpand']=false;
            }
            departmentList[i]['checked'] = false;
            for (var j = 0; j < departmentList[i].uList.length;j++){
              departmentList[i].uList[j]['checked']=false;
            }
          }
          
          _this.setData({
            departmentList
          })
        
          // var data = res.data.departmentList;
    
          // that.setData({
          //   departmentList: data
          // })
        
      },
       fail: function(res) {
        console.log("httpRequestFail---", res)
       
      },
      complete: function(res) {
        dd.hideLoading();
      }
      })

   },


  search:function(e){
      //event.detail = {value: value}

  },



  select_all:function(e){
    
    if (!this.data.selectall.checked){
      var departmentList = this.data.departmentList;
      for (var i = 0; i < departmentList.length; i++) {
        departmentList[i].checked = true;
        for (var j = 0; j < departmentList[i].uList.length; j++) {
          departmentList[i].uList[j].checked = true;
        }
      }
      this.setData({
        "selectall.text": "取消全选",
        "selectall.color": "#156EF3",
        "selectall.checked": true,
        departmentList
      });
    }else {
  
      var departmentList = this.data.departmentList;
      for (var i = 0; i < departmentList.length; i++) {
        departmentList[i].checked = false;
        for (var j = 0; j < departmentList[i].uList.length; j++) {
          departmentList[i].uList[j].checked = false;
        }
      }
      this.setData({
        "selectall.text": "全选",
        "selectall.color": "#ccc",
        "selectall.checked": false,
        departmentList
      });
    }
  },
  selected:function(e){
    const index = e.currentTarget.dataset.index;
    var departmentList= this.data.departmentList
    var  department = departmentList[index];
    if (!department.checked) {
    for (var i = 0; i < department.uList.length;i++){
        department.uList[i].checked = true;  
    }
      department.checked = true;
    } else {
      for (var i = 0; i < department.uList.length; i++) {
        department.uList[i].checked = false;
      }
      department.checked = false;
    }
    
    this.setData({
      departmentList
    })
      
  },
  single_selected:function(e){
    const dIndex = e.currentTarget.dataset.dIndex;
    const uIndex = e.currentTarget.dataset.uIndex;
    var departmentList = this.data.departmentList
    var department = departmentList[dIndex];
  
    if (!department.uList[uIndex].checked) {
      department.uList[uIndex].checked = true;
    } else {
      department.uList[uIndex].checked = false;
    }
  },
  addInSign:function(e){
    var departmentList=  this.data.departmentList;
    var uidList=[];
     for (var i=0;i<departmentList.length;i++){
       for (var j = 0; j < departmentList[i].uList.length;j++){
         if (departmentList[i].uList[j].checked==true){
           uidList.push(departmentList[i].uList[j].id);

         }
           
       }
     }
    console.log(uidList)
    var _this = this;
    if (uidList.length!=0){
    dd.httpRequest({
      url: addInSignUrl,
      method: 'POST',
      data: {
        mid: _this.data.mid,
        uidList: uidList
      },
      dataType: 'json',
      success: function(res) {
        //   console.log('success----', res)


        if (res.data.success) {
          //console.log("预定成功");
          // const index = e.currentTarget.dataset.index;
          // var employeeList = _this.data.employeeList;
          // employeeList = remove(employeeList, index);
          // _this.setData({
          //   employeeList
          // })
          dd.alert({
            title: '添加成功',
            content: '添加成功',
            buttonText: '确定',
            success: () => {
              dd.navigateTo({
                 url: '../addmeeting/addmeeting'
               });
            },
          });
        } else {
          dd.showToast({
            type: 'fail',
            content: res.data.message,
            duration: 3000,
            success: () => {
              //  dd.navigateTo({
              //    url: '../index/index'
              //  });
            },
          });
        }
      },
      fail: function(res) {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: function(res) {
        dd.hideLoading();
      }

    });
    }
  },
  // 展开折叠选择  
  changeToggle: function(e) {
    const index = e.currentTarget.dataset.index;
  //  console.log(index)

    console.log(this.data.departmentList[index]);
    if (this.data.departmentList[index].isColpand) {
      const departmentList = this.data.departmentList;
      departmentList[index].isColpand=false;
        this.setData({ departmentList });
      
    } else {
      const departmentList = this.data.departmentList;
      departmentList[index].isColpand = true;
      this.setData({ departmentList });
    }
  },
  // selectall:function(){
  //   if(this.data.departmentList[i].checked=(!this.data.selectall)){

  //   }
  // },
  onLoad: function(query) {
    // 生命周期函数--监听页面加载
    var _this = this;
    var userId = app.globalData.userId
    
    this.setData({
      mid:query.mid,
      cnum:query.cnum
    })
    dd.httpRequest({
      url: getStaffUrl,
      method: 'POST',
      data: {
        userId: userId,
         
      },
      dataType: 'json',
      success: function(res) {
        //   console.log('success----', res)


        if (res.data.success) {
          //console.log("预定成功");
          // const index = e.currentTarget.dataset.index;
          // var employeeList = _this.data.employeeList;
          // employeeList = remove(employeeList, index);
          // _this.setData({
          //   employeeList
          // })
          var departmentList = _this.data.departmentList;
          departmentList = res.data.result;
          for (var i = 0; i<departmentList.length;i++){
            departmentList[i]['isColpand']=false;
            departmentList[i]['checked'] = false;
            for (var j = 0; j < departmentList[i].uList.length;j++){
              departmentList[i].uList[j]['checked']=false;
            }
          }
          
          _this.setData({
            departmentList
          })
          console.log(departmentList);  
          dd.httpRequest({
            url: getInSignUrl,
            method: 'POST',
            data: {
              mid: _this.data.mid,

            },
            dataType: 'json',
            success: function(res) {
              //   console.log('success----', res)


              if (res.data.success) {
                //console.log("预定成功");
                // const index = e.currentTarget.dataset.index;
                // var employeeList = _this.data.employeeList;
                // employeeList = remove(employeeList, index);
                // _this.setData({
                //   employeeList
                // })
                var departmentList = _this.data.departmentList;
                var signList = res.data.result;
                for (var i = 0; i < departmentList.length; i++) {

                  for (var j = 0; j < departmentList[i].uList.length; j++) {

                    for (var k = 0; k < signList.length; k++) {
                      if (departmentList[i].uList[j].id == signList[k].uid) {
                        departmentList[i].uList[j].checked = true;
                        departmentList[i].isColpand=true;
                      }

                    }

                  }
                }

                _this.setData({
                  departmentList
                })
                console.log(departmentList);
              } else {
                dd.showToast({
                  type: 'fail',
                  content: res.data.message,
                  duration: 3000,
                  success: () => {
                    //  dd.navigateTo({
                    //    url: '../index/index'
                    //  });
                  },
                });
              }
            },
            fail: function(res) {
              console.log("httpRequestFail---", res)
              dd.alert({ content: JSON.stringify(res) });
            },
            complete: function(res) {
              dd.hideLoading();
            }

          });
        } else {
          dd.showToast({
            type: 'fail',
            content: res.data.message,
            duration: 3000,
            success: () => {
              //  dd.navigateTo({
              //    url: '../index/index'
              //  });
            },
          });
        }
      },
      fail: function(res) {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: function(res) {
        dd.hideLoading();
      }

    });
   


  },

  
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
