const app = getApp()
const util = require("../../../utils/util");
const api = require('../../../api/api.js');
// const update = require('../../../api/update.js');
const user = require('../../../utils/user.js');
import moment from 'moment'



Page({
  data: {
		showjz:false,
    interviewInfo: [],
    interviewShow: false,
    userInfo: {},
    labelName: [],
    homeInfo: {
      onPositionYear: '',
      salary: '',
      total: '',
      unReadSize: "",
      workerInfo: {},
      workerNotice: [],
      stationAdminName: '',
      stationAdminTel: '',
    },
    salary: {
      salary: '',
      total: ''
    },
    hasLogin: true,
    currentDate: moment(new Date()).format('YYYY-MM-DD'),
    currentWeek: '',
    myRebuyInfo: {
      looseOrderRebuy: 0,
      looseOrder: 0,
      cycleOrderRebuy: 0,
      cycleOrder: 0,
      looseOrderRebuyRate: 0,
      cycleOrderRebuyRate: 0
    },
    animate: false,
    msgList: [

    ],
    modalShow: false,
    unReadSize: "",
    workerInfo: {},
    workerNotice: [],
    workerNoticeAlert: [],
    alertNoticeInfo: {

    },
    isUat: false

  },
  goFeedback() {
    if (this.data.alertNoticeInfo.isFeedback == 1) {
      // 跳转到回执页面
      // app.globalData.alertNoticeInfo=this.data.alertNoticeInfo;
      wx.navigateTo({
        url: '/pages/upgrade/msgReceipt/msgReceipt?id=' + this.data.alertNoticeInfo.id
      });
    } else if (this.data.alertNoticeInfo.isFeedback != 1 && this.data.alertNoticeInfo.pagePath == '') {
      // 调已读接口
      util.request(api.readNotice, {
        id: this.data.alertNoticeInfo.id
      }, 'GET').then(res => {
        if (res.errno === 0) {

          this.getMsgCenter();
        }


      });

    } else {

      // 调已读接口
      util.request(api.readNotice, {
        id: this.data.alertNoticeInfo.id
      }, 'GET').then(res => {
        if (res.errno === 0) {
          // 跳转其他页面
          wx.navigateTo({
            url: this.data.alertNoticeInfo.pagePath
          });
        } 

      }); 

    }

  },
  goJoinCp() {
    wx.navigateTo({
      url: '/pages/upgrade/cp/join',
    })
	},
	toDxkOrder(){
		wx.navigateTo({
      url: '/pages/mall/goods/goods?id=288',
    })
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // val(this.messageScroll,2000);
    // let week=util.getWeek(this.data.currentDate);
    // this.setData({
    //   currentWeek:week
    // });
    // this.getRebuyInfo();
    this.isNewYearReport();
    // this.getHomeInfo()
    // wx.hideTabBar(); 
  },
  isNewYearReport() { //是否新年报备
    util.request(api.NewYearReported).then(res => {
      if (res.data) {
        wx.showModal({
          title: '返乡调查',
          content: '新年返乡调查已开始，是否现在去报备？',
          success: function (sm) {
            if (sm.confirm) {
              wx.navigateTo({
                url: '/pages/servant/gohome/report',
              })

            }
          }
        });
      }
    })

  },
  lastWeek() {
    let current = moment(new Date((new Date(this.data.currentDate)).getTime() - 7 * 24 * 60 * 60 * 1000)).format('YYYY-MM-DD');
    console.log(current);
    let week = util.getWeek(current);
    this.setData({
      currentDate: current,
      currentWeek: week
    });
    this.getRebuyInfo();
  },
  nextWeek() {
    let current = moment(new Date((new Date(this.data.currentDate)).getTime() + 7 * 24 * 60 * 60 * 1000)).format('YYYY-MM-DD');
    console.log(current);
    let week = util.getWeek(current);
    this.setData({
      currentDate: current,
      currentWeek: week
    });
    this.getRebuyInfo();
  },
  getRebuyInfo() {
    util.request(api.myRebuyInfo, {
      date: this.data.currentDate,

    }, 'GET').then(res => {
      if (res.errno === 0) {
        this.setData({
          myRebuyInfo: res.data
        });
      }


    });
  },
  messageScroll() {
    this.setData({
      animate: true
    });
    setTimeout(() => {
      let list = this.data.msgList;
      list.push(list[0]);
      this.setData({
        msgList: list
      });
      list.shift();
      this.setData({
        msgList: list,
        animate: false
      });

    }, 3000);
  },
  makeCall(e) {
    let mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  exitLogin: function () {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function (res) {
        if (!res.confirm) {
          return;
        }

        util.request(api.AuthLogout, {}, 'POST');
        app.globalData.hasLogin = false;
        wx.removeStorageSync('token');
        wx.removeStorageSync('userInfo');
        wx.redirectTo({
          url: '/pages/index/index'
        });
      }
    })

  },
  getInterview() {
    util.request(api.interview, {
      // workerNo:this.data.userInfo.workerNo,
      // status:1,
    }, 'GET').then(res => {
      console.log(res);
      if (res.errno == 0) {
        //  if(res.data&&res.data!=null){
        //  if(res.data.length>=1){
        this.setData({
          interviewInfo: res.data
        });
        //  }
        //  }

      }


    });
  },
  operateInterview(e) {
    let type = e.currentTarget.dataset.type;
    let id = e.currentTarget.dataset.id;
    let content = (type == 1 ? '确定接受邀请么？' : '确定拒绝邀请么？')
    wx.showModal({
      title: '',
      content: content,
      success: res => {
        if (res.confirm) {
          util.request(api.acceptOrRefuseInterview, {
            interviewId: id,
            isAccept: type,
          }, 'GET').then(res => {
            console.log(res);
            if (res.errno == 0) {

              this.getInterview();
            }


          });

        }
      }
    });


  },
  getHomeInfo() {
    util.request(api.homeInfo, {}, 'GET').then(res => {
      console.log(res);
      if (res.errno === 0) {
        console.log(res.data.workerInfo.labelNames);
        console.log(res.data.workerInfo.labelNames.split(','));
        this.setData({
          homeInfo: res.data,
          labelName: res.data.workerInfo.labelNames == "" ? [] : res.data.workerInfo.labelNames.split(','),
          // 'homeInfo.unReadSize':res.data.unReadSize>99?99:res.data.unReadSize


        });
      }


    });
  },
  goLeave() { //请假
    wx.navigateTo({
     url: '/pages/servant/leave/leave',
    }); 
    // wx.showModal({
    //   title: '提醒',
    //   content: '暂未开通，敬请期待',
    //   showCancel: false
    // });
  },
  goHouse() {

    wx.showModal({
      title: '提醒',
      content: '暂未开通，敬请期待',
      showCancel: false
    })
  },
  getPerfect() {
    // wx.navigateTo({
    //   url: '/pages/servant/perfect/perfectWorkerInfo'
    // });
    wx.navigateTo({
      url: '/pages/upgrade/completeInfo/completeInfo'
    });
  },
  goBookIndex(e) {
    if (this.data.hasLogin) {
      let tab = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      try {
        wx.setStorageSync('tab', tab);
      } catch (e) {

      }
      wx.navigateTo({
        url: route,

      })
    } else {
      wx.navigateTo({
        url: "/pages/index/index"
      });
    };
  },
  // goNucleicList() {
    // wx.navigateTo({
    //   url: '/pages/servant/nucleic/list'
		// });
		// wx.navigateTo({
		// 	url: '/pages/servant/gohome/report',
		// })
  // },
  goMessage() {
    wx.navigateTo({
      url: '/pages/upgrade/messageList/messageList'
    })
  },

  goSalaryDetail(e) { //查询工资明细
    // const {
    //   year,
    //   month
    // } = calendar.getCurrentYM();
    //   let m = '11';
    //   if (m < 10) m = '0' + String(m);
    //  let year='2022-';
    let month = moment(new Date()).format('YYYY-MM');

    let type = e.currentTarget.dataset.type;
    let total = (type == 1 ? this.data.homeInfo.salary : this.data.homeInfo.total);
    //servantType 表示服务者身份99是临时服务者 明细页可以显示提现按钮
    wx.navigateTo({
      url: '/pages/servant/salary/detail/detail?month=' + month + '&total=' + total + '&type=' + type + '&servantType=' + '' + '&blance=' + this.data.homeInfo.salary
    });
  },
  goNucleicList() {
    // wx.navigateTo({
    //   url: '/pages/servant/nucleic/list'
		// })
				wx.navigateTo({
			url: '/pages/servant/gohome/report',
		})
  },
  copyNum() {

    wx.setClipboardData({
      data: this.data.homeInfo.workerShareCode,
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '复制成功',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
  goLearnCenter() {
    wx.navigateTo({
      url: '/pages/upgrade/learnCenter/learnCenter',
    })
  },
  closeModal() {
    this.setData({
      modalShow: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let week = util.getWeek(this.data.currentDate);
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      currentWeek: week,
			userInfo: userInfo,
			showjz: userInfo.ofJz == 293
    });
    if (userInfo && userInfo.id < 0) {
      // 未入职状态
      this.setData({
        interviewShow: true
      });
      this.getInterview();
      this.getHomeInfo();
      return;
    }
    // setInterval(this.messageScroll,2000);

    this.getRebuyInfo();
    this.isNewYearReport();
    this.getHomeInfo();
    this.getMsgCenter();

    //是否测试环境
    // if(api.WxApiRoot.indexOf("uat")){
    //   this.setData({
    //     isUat:true
    //   })
    // } 
    this.setData({
      isUat: wx.getStorageSync('isUat')
    })
  },
  getMsgCenter() {
		if(!this.data.showjz){
			return;//非鲸致全职不展示通知
		}
    let userInfo = wx.getStorageSync('userInfo');
    util.request(api.getMsgCenter, {
      workerNo: userInfo.workerNo,

    }, 'GET').then(res => {
      if (res.errno === 0) {
        this.setData({

          unReadSize: res.data.unReadSize > 99 ? 99 : res.data.unReadSize,
          workerNotice: res.data.workerNotice,
          workerNoticeAlert: res.data.workerNoticeAlert,
          modalShow: res.data.workerNoticeAlert.length > 0 ? true : false,
          alertNoticeInfo: res.data.workerNoticeAlert.length > 0 ? res.data.workerNoticeAlert[0] : {}
        });
      }


    });
	},
	goMaster(){
		// util.request(api.transferIntroductionQrCode, {
		
		// }, 'POST').then(res=> {
		// 	if (res.errno === 0) {
		// 		let url=res.data;
		// 		wx.previewImage({
		// 			current: url, // 当前显示图片的http链接
		// 			urls: [url] // 需要预览的图片http链接列表
		// 		})
			
		// 	}else{
		// 		wx.showToast({
		// 			title: res.errmsg,
		// 			icon:'none'
		// 		})
		// 	}
		// });
	//  meetZzy 1有资格 0没资格
	// 	pauseZzy 1暂停 0没有暂停
	let meetZzy=this.data.homeInfo.workerInfo.meetZzy;
	let pauseZzy=this.data.homeInfo.workerInfo.pauseZzy;
	if(meetZzy==0){
		wx.navigateTo({
			url: '/pages/master/joinGain/joinGain',
		});
	}else if(meetZzy==1&&pauseZzy==1){
			// 已暂停
			wx.navigateTo({
				url: '/pages/master/pauseMaster/pauseMaster',
			})
	}else{
		// wx.navigateTo({
		// 	url: '/pages/master/shareMaster/shareMaster?rid='+this.data.userInfo.id,
    // });
    wx.navigateTo({
			url: '/pages/master/shareIndex/shareIndex?rid='+this.data.userInfo.id,
		});
	}
    
	},
	goIm(){
    wx.navigateTo({
      url: '/pages/chat/im/im' 
    }); 
  },
  onHide: function () {

  },

  onUnload: function () {

  },


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

  },

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

  },

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

  },

})