// pages/memberGroupList/index.js
const app = getApp();

const commonUtil = require("../../utils/commonUtil.js");
import { customRequest } from '../../utils/request.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentGroup: {},
    pagination: {
      pageNum: 1,
      pageSize: 20,
      total: null, //分页配置
      status: 0,
      keys: null,
      // searchTag: 1,
    },
    groupVal: '',
    groupType: '添加部门',
    allowTrans: false,
    newCreatorShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    console.log(JSON.parse(options.info));
    this.setData({
      currentGroup: JSON.parse(options.info)
    })
    wx.setNavigationBarTitle({
      title: this.data.currentGroup.teamName,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    this.setData({
      userInfo: app.globalData.loginUserInfo,
      // agency: this.data.agencyList[0].name,
      toastShow: false,
    });
    console.log(this.data.userInfo);
    if (this.data.allowTrans) {
      console.log('允许转让');
      this.setData({
        newCreatorShow: true
      })
    }
    this.getGroupInfo()

  },
  getGroupInfo () {
    let that = this
    wx.showLoading({
      title: '加载中...',
    })
    customRequest("/yishoudan/user/getAgencyCorpUsers", { header: 'headers', method: 'post', data: { ...that.data.pagination, agencyTeamIds: that.data.currentGroup.id } }).then(({ data }) => {
      if (data.status == 200) {
        that.setData({
          memberGroupList: data.data.recordList
        })
      }
      wx.hideLoading()

    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/user/getAgencyCorpUsers",
    //   method: "post",
    //   header: app.globalData.headers,
    //   data: { ...that.data.pagination, agencyTeamIds: that.data.currentGroup.id },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       that.setData({
    //         memberGroupList: data.data.recordList
    //       })
    //     }
    //     wx.hideLoading()

    //   }
    // })
  },
  addMember () {
    wx.navigateTo({
      url: `../addOrEditMember/index?group=` + this.data.currentGroup.id,
    });
  },
  editMember (e) {
    console.log(e);
    // 阻止成员编辑 
    if (this.data.userInfo.user.agencyRole == 3) {
      return;
    }
    if (e.currentTarget.dataset.info.agencyRole == 1 && this.data.userInfo.user.agencyRole != 1) {
      wx.showModal({
        content: "你没有权限修改团队的创建人。",
        contentColor: "#ccc",
        confirmColor: "#1890ff",
        confirmText: "我知道了",
        showCancel: false,
        success (res) { },
      });
      return;

    }
    if (e.currentTarget.dataset.info.agencyRole == 1 && this.data.userInfo.user.agencyRole == 1) {
      customRequest("/yishoudan/agency/getCreatorTel", { header: 'headers', method: 'GET', data: {} }).then(({ data }) => {
        wx.showModal({
          title: "确认要转让创建人吗？",
          content: "您将放弃创建人身份",
          contentColor: "#ccc",
          confirmColor: "#1890ff",
          confirmText: "确定",
          // showCancel: false,
          success (res) {
            // console.log(res);
            if (res.confirm) {
              wx.navigateTo({
                url: `/pages/sendCreatorCode/index?info=${JSON.stringify(data.data)}`,
              });
            }

          },
        });
      })
      // wx.request({
      //   url: app.globalData.ip + "/yishoudan/agency/getCreatorTel",
      //   method: "get",
      //   header: app.globalData.headers,
      //   success: function ({ data }) {
      //     console.log(data);
      //     wx.showModal({
      //       title: "确认要转让创建人吗？",
      //       content: "您将放弃创建人身份",
      //       contentColor: "#ccc",
      //       confirmColor: "#1890ff",
      //       confirmText: "确定",
      //       // showCancel: false,
      //       success (res) {
      //         // console.log(res);
      //         if (res.confirm) {
      //           wx.navigateTo({
      //             url: `/pages/sendCreatorCode/index?info=${JSON.stringify(data.data)}`,
      //           });
      //         }

      //       },
      //     });
      //   }
      // })

      return;
    }
    wx.navigateTo({
      url: `../addOrEditMember/index?info=${JSON.stringify(e.currentTarget.dataset.info)}`,
    });
  },
  /**
  * 关闭部门弹窗
  */
  closegroupDialog () {
    this.setData({
      groupDialog: false,
      newCreatorShow: false,
      groupVal: '',
      drawerShow: false
    })
  },
  /**
  * 打开部门弹窗
  */
  showgroupDialog (e) {
    console.log(e);
    if (e.currentTarget.dataset.type != 'add') {
      this.setData({
        groupVal: this.data.currentGroup.teamName,
        groupType: '修改当前部门名称'
      })
    } else {
      this.setData({
        groupType: '添加部门'
      })
    }
    this.setData({
      groupDialog: true
    })
  },
  /**
  * 添加新部门
  */
  addOrUpdateGroup () {
    if (this.data.groupVal.trim() == '') {
      return
    }
    console.log(this.data.groupType == '修改当前部门名称');
    if (this.data.groupType == '修改当前部门名称') {
      this.updateGroupName()
    } else {
      this.addGroupName()
    }

  },
  /**
  * 转让创建人
  */
  updateCreator () {
    console.log(this.data.creatorVal);
    let that = this
    customRequest("/yishoudan/agency/changeCreator", { header: 'headers', method: 'post', data: { tel: that.data.creatorVal } }).then(({ data }) => {
      if (data.status == 200) {
        that.setData({
          newCreatorShow: false
        })
        wx.showToast({
          title: '转让成功',
          icon: 'none',
        })
        this.getGroupInfo()

      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/agency/changeCreator",
    //   method: "post",
    //   header: app.globalData.headers,
    //   data: { tel: that.data.creatorVal },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       that.setData({
    //         newCreatorShow: false
    //       })
    //       wx.showToast({
    //         title: '转让成功',
    //         icon: 'none',
    //       })
    //       this.getGroupInfo()

    //     } else {
    //       wx.showToast({
    //         title: data.msg,
    //         icon: 'none'
    //       })
    //     }
    //   }
    // })
  },
  /**
  * 添加部门
  */
  addGroupName () {
    let that = this
    customRequest("/yishoudan/agency/team/add", { header: 'headers', method: 'post', data: { teamName: that.data.groupVal } }).then(({ data }) => {
      if (data.status == 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'none'
        })
        that.closegroupDialog()
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/agency/team/add",
    //   method: "post",
    //   header: app.globalData.headers,
    //   data: { teamName: that.data.groupVal },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       wx.showToast({
    //         title: '添加成功',
    //         icon: 'none'
    //       })
    //       that.closegroupDialog()
    //     } else {
    //       wx.showToast({
    //         title: data.msg,
    //         icon: 'none'
    //       })
    //     }

    //   }
    // })
  },
  /**
  * 更新部门名称
  */
  updateGroupName () {
    let that = this
    customRequest("/yishoudan/agency/team/update", { header: 'headers', method: 'post', data: { teamName: that.data.groupVal, recordId: this.data.currentGroup.id } }).then(({ data }) => {
      if (data.status == 200) {
        wx.showToast({
          title: '修改成功',
          icon: 'none'
        })
        wx.setNavigationBarTitle({
          title: that.data.groupVal,
        });
        that.closegroupDialog()
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }

    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/agency/team/update",
    //   method: "post",
    //   header: app.globalData.headers,
    //   data: { teamName: that.data.groupVal, recordId: this.data.currentGroup.id },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       wx.showToast({
    //         title: '修改成功',
    //         icon: 'none'
    //       })
    //       wx.setNavigationBarTitle({
    //         title: that.data.groupVal,
    //       });
    //       that.closegroupDialog()
    //     } else {
    //       wx.showToast({
    //         title: data.msg,
    //         icon: 'none'
    //       })
    //     }

    //   }
    // })
  },
  /**
  * 删除部门
  */
  delGroup () {
    let that = this

    wx.showModal({
      title: '删除部门',
      content: '确定将该部门删除吗?',
      confirmColor: '#1890ff',
      success () {
        wx.showLoading({
          title: '删除中...',
          mask: true,
        });
        customRequest("/yishoudan/agency/team/del/" + that.data.currentGroup.id, { header: 'headers', method: 'GET', data: {} }).then(({ data }) => {
          if (data.status == 200) {
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            })
            wx.navigateBack({
              delta: 1
            });
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading();
        })
        // wx.request({
        //   url: app.globalData.ip + "/yishoudan/agency/team/del/" + that.data.currentGroup.id,
        //   method: "get",
        //   header: app.globalData.headers,
        //   success: function ({ data }) {
        //     console.log(data);
        //     if (data.status == 200) {
        //       wx.showToast({
        //         title: '删除成功',
        //         icon: 'none'
        //       })
        //       wx.navigateBack({
        //         delta: 1
        //       });
        //     } else {
        //       wx.showToast({
        //         title: data.msg,
        //         icon: 'none'
        //       })
        //     }
        //     wx.hideLoading();
        //   }
        // })
      }
    })
  },
  /**
  * 添加部门输入事件
  */
  updateGroup (e) {
    console.log(e);
    this.setData({
      groupVal: e.detail.value
    })
  },
  /**
  * 监听要转让的手机号
  */
  updateCreatorVal (e) {
    console.log(e);
    this.setData({
      creatorVal: e.detail.value
    })
  },
  /**
  * 阻止滑动穿透
  *
  *
  */
  modalMove () {
    return false;
  },
  /**
  * 底部抽屉显示
  */
  showDrawer () {
    this.setData({
      drawerShow: true
    })
  },
  /**
  * 底部抽屉隐藏
  */
  hideDrawer () {
    this.setData({
      drawerShow: false
    })
  },
  /**
  * 底部抽屉隐藏
  */
  hidedrawershow (e) {
    console.log(e);
    this.setData({
      [e.detail.type]: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

    this.data.allowTrans = false // 重置允许转让的判断变量
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})