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

const commonUtil = require("../../utils/commonUtil.js");
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mamberList: [],
    isEdit: true,
    tabs: [{ title: "全部" }, { title: "分组1" }, { title: "分组2" }, { title: "分组3" }, { title: "分组4" }, { title: "分组5" }, { title: "分组6" }],

    pagination: {
      pageNum: 1,
      pageSize: 8,
      total: null, //分页配置
      status: 0,
      keys: null,
      // searchTag: 1,
    },
    userInfo: app.globalData.loginUserInfo,
    creator: {},
    agency: "",
    agencyList: [
    ],
    toastShow: false,
    memberGroupList: [],
    unsetGroupList: [], // 未分配列表
    groupVal: '', // 部门名称输入
    drawerShow: false, //底部抽屉显隐
    creatorVal: '', // 创建人字段
    allowTrans: false
  },

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    console.log("onshow");
    let that = this;
    if (this.data.toastShow) {
      wx.showToast({
        title: "已添加，等待成员同意",
        icon: "none",
        duration: 2000,
        success: () => { },
      });
    }
    console.log(this.data.allowTrans);
    if (this.data.allowTrans) {
      console.log('允许转让');
      this.setData({
        newCreatorShow: true
      })
    }
    this.setData({
      userInfo: app.globalData.loginUserInfo,
      // agency: this.data.agencyList[0].name,
      toastShow: false,
    });
    // this.getMemberList();
    this.getGroupInfo()
    this.getMemberGroupList();
  },
  onTabClick (e) {
    const index = e.detail.index;
    console.log(index);

    let status = -1;
    if (index == 1) {
      status = 10;
    } else if (index == 2) {
      status = 20;
    } else if (index == 3) {
      status = 25;
    } else if (index == 4) {
      status = 30;
    } else if (index == 5) {
      status = 40;
    } else if (index == 6) {
      status = 999;
    } else {
      status = -1;
    }
    this.data.searchParam.pageNum = 1;
    this.data.searchParam.status = status;
    this.setData({
      //  navlist: e.mark.ind,
      activeTab: index,
      searchParam: this.data.searchParam,
    });
  },
  getMemberList () {
    let that = this;
    wx.showLoading({
      title: "加载中...",
      mask: true,
    });
    customRequest("/yishoudan/user/getAgencyCorpUsers", { header: 'headers', method: 'GET', data: {} }).then((res) => {
      if (res.data.status == 200) {
        try {
          res.data.data.recordList.forEach((item) => {
            item.name = commonUtil.getAgencyRoleTypeById(item.agencyRole);
          });
          res.data.data.recordList.sort((a, b) => {
            return a.agencyRole - b.agencyRole;
          });
          wx.hideLoading();
          that.setData({
            mamberList: res.data.data.recordList,
          });
        } catch (error) {
          wx.hideLoading();
          console.log(error);
        }
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/user/getAgencyCorpUsers",
    //   method: "GET",
    //   header: app.globalData.headers,
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       try {
    //         res.data.data.recordList.forEach((item) => {
    //           item.name = commonUtil.getAgencyRoleTypeById(item.agencyRole);
    //         });
    //         res.data.data.recordList.sort((a, b) => {
    //           return a.agencyRole - b.agencyRole;
    //         });
    //         wx.hideLoading();
    //         that.setData({
    //           mamberList: res.data.data.recordList,
    //         });
    //       } catch (error) {
    //         wx.hideLoading();
    //         console.log(error);
    //       }
    //     }
    //   },
    // });
  },
  /**
  * 获取未分配分组列表
  */
  getGroupInfo () {
    let that = this
    wx.showLoading({
      title: '加载中...',
    })
    customRequest("/yishoudan/user/getAgencyCorpUsers", { header: 'headers', method: 'post', data: { ...that.data.pagination, agencyTeamIds: -1 } }).then(({data}) => {
      if (data.status == 200) {
        that.setData({
          unsetGroupList: 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: -1 },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       that.setData({
    //         unsetGroupList: data.data.recordList
    //       })
    //     }
    //     wx.hideLoading()

    //   }
    // })
  },

  getMemberGroupList () {
    let that = this
    customRequest("/yishoudan/agency/team/list", { header: 'headers', method: 'GET', data: {} }).then(({ data }) => {
      let unset = data.data.list.findIndex((item) => {
        return item.id == -1
      })
      if (unset != -1) {
        data.data.list.splice(unset, 1)
      }
      console.log(unset);
      if (data.status == 200) {
        that.setData({
          memberGroupList: data.data.list
        })
        wx.setStorageSync('MEMBERGROUPLIST', that.data.memberGroupList)
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/agency/team/list",
    //   method: "GET",
    //   header: app.globalData.headers,
    //   success: function ({ data }) {
    //     console.log(data);
    //     let unset = data.data.list.findIndex((item) => {
    //       return item.id == -1
    //     })
    //     if (unset != -1) {
    //       data.data.list.splice(unset, 1)
    //     }
    //     console.log(unset);
    //     if (data.status == 200) {
    //       that.setData({
    //         memberGroupList: data.data.list
    //       })
    //       wx.setStorageSync('MEMBERGROUPLIST', that.data.memberGroupList)
    //     }

    //   }
    // })
  },

  changeEditType () {
    this.setData({
      isEdit: !this.data.isEdit,
    });
  },
  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) {

    //   wx.request({x
    //     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)}`,
    });
  },
  toGroupList (e) {
    wx.navigateTo({
      url: `../memberGroupList/index?info=${JSON.stringify(e.currentTarget.dataset.info)}`,
    });
  },
  addMember () {
    wx.navigateTo({
      url: `../addOrEditMember/index`,
    });
  },
  showDelModal (e) {
    let that = this;
    wx.showModal({
      title: "删除成员",
      content: "确定将该成员删除吗?",
      confirmColor: "#1890ff",
      success (res) {
        if (res.confirm) {
          console.log("用户点击确定");
          that.deleteMember(e.currentTarget.dataset.id);
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  /**
   * 监听要转让的手机号
   */
  updateCreatorVal (e) {
    console.log(e);
    this.setData({
      creatorVal: e.detail.value
    })
  },
  /**
  * 转让创建人
  */
  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'
    //       })
    //     }
    //   }
    // })
  },
  closegroupDialog () {
    this.setData({
      groupDialog: false,
      groupVal: '',
      newCreatorShow: false,
    })
  },
  showgroupDialog () {
    this.setData({
      groupDialog: true
    })
  },
  deleteMember (id) {
    let that = this;
    wx.showLoading({
      title: "删除中...",
      mask: true,
    });
    customRequest("/yishoudan/user/delAgencyCorpUser", { header: 'headers', method: 'POST', data: { id, } }).then((res) => {
      wx.hideLoading();
      if (res.data.status == 200) {
        wx.showToast({
          title: "已删除",
          icon: "success",
          duration: 1000,
        });
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/user/delAgencyCorpUser",
    //   method: "POST",
    //   header: app.globalData.headers,
    //   data: {
    //     id,
    //   },
    //   success: function (res) {
    //     console.log(res);
    //     wx.hideLoading();
    //     if (res.data.status == 200) {
    //       wx.showToast({
    //         title: "已删除",
    //         icon: "success",
    //         duration: 1000,
    //       });
    //     }
    //     // setTimeout(() => {
    //     //   that.getMemberList();
    //     // }, 500);
    //   },
    // });
  },
  changeAgency (e) {
    console.log(e);
    this.setData({
      agency: this.data.agencyList[e.detail.value].name,
    });
  },
  /**
  * 添加新部门
  */
  addGroup () {
    let that = this
    console.log(this.data.groupVal);
    if (this.data.groupVal.trim() == '') {
      return
    }
    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()
        that.getMemberGroupList()
      }
    })
    // 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()
    //       that.getMemberGroupList()
    //     }

    //   }
    // })
  },
  /**
  * 添加部门输入事件
  */
  updateGroup (e) {
    console.log(e);
    this.setData({
      groupVal: e.detail.value
    })
  },
  /**
  * 底部抽屉显示
  */
  showDrawer () {
    this.setData({
      drawerShow: true
    })
  },
  /**
  * 底部抽屉隐藏
  */
  hideDrawer () {
    this.setData({
      drawerShow: false
    })
  },
  /**
  * 底部抽屉隐藏
  */
  hidedrawershow (e) {
    console.log(e);
    this.setData({
      [e.detail.type]: false
    })
    // console.log(this.data.drawerShow);
  },
  /**
* 阻止滑动穿透
*
*
*/
  modalMove () {
    return false;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {
    this.data.allowTrans = false // 重置允许转让的判断变量

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
    // wx.switchTab({
    // 	url: "../mine/index",
    // });
  },

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

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
});
