var app = getApp();
var that;
import 'dingtalk-jsapi/entry/mobile';
import openLink from 'dingtalk-jsapi/api/biz/util/openLink';
Page({
  data: {
    showIndex: 1,
    thisweek: {
      end_day: '',
      start_day: ''
    },
    employee_id: '',
    isUrl: true,
    start: '',//查看周开始时间
    allDate:'',
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "积分周报" });
    if (e.employee_id) {
      this.setData({
        employee_id: e.employee_id
      })
    }
    if (e.start) {
      this.setData({
        isUrl: e.start,
      })
    }
    if (e.isUrl) {
      this.setData({
        isUrl: false,
      })
    }
    this.setData({
        allDate: app.globalData.allDate,
    })
    this.setDate();
    dd.showLoading({
      content: '加载中...',
    });
    setTimeout(() => {
      dd.hideLoading();
    }, 1000)
  },
  onShareAppMessage() {
    let userId = this.data.employee_id ? this.data.employee_id : app.globalData.userData.id
    return {
      title: '积分周报',
      desc: that.data.result.employee.name + '的积分周报分享',
      path: 'pages/reportBox/report/report?employee_id=' + userId + '&isUrl=123&start' + that.data.result.date.start
    };
  },
  onShow() {
    this.opneWebSocket()
  },
  //跳转
  openView(e) {
    let index = e.target.dataset.index
    if (!this.data.isUrl && index != 6) {
      return false
    }
    if (index == 1) {
      dd.navigateTo({
        url: '../../statistics/my_task/my_task'
      })
    } else if (index == 2) {
      dd.navigateTo({
        url: '../../statistics/my_issue/my_issue'
      })
    } else if (index == 3) {
      dd.navigateTo({
        url: '../../workbench/prize_buckle/prize_buckle'
      })
    } else if (index == 4) {
      let data = {
        type: 'week_statistics',
        employee_id: this.data.employee_id ? this.data.employee_id : app.globalData.userData.id,
        start_date: this.data.start ? this.data.start : this.data.thisweek.start_day,
        method: 'ruleList',
        page_size: 1000,
        rule_item_id: e.target.dataset.item.rule_item_id
      }
      dd.navigateTo({
        url: '../moreE/moreE?data=' + JSON.stringify(data)
      })
    } else if (index == 5) {
      dd.navigateTo({
        url: '../noUser/noUser?list=' + JSON.stringify(this.data.result.rank.free_man)
      })
    } else if (index == 6) {
      openLink({
        url: 'https://h5.dingtalk.com/open-market/share.html?shareGoodsCode=D34E5A30A9AC7FC6327AB5E6D2D145418457E59F16C3FC969F1A50AEF2E5E320588012470F1AB045&token=d37751d45abad15178adf60f936d7c28&shareUid=C5EB7B5C85793F3D909B2AC4771E12D7'
      })
    } else if (index == 7) {
      let data = {
        type: 'week_statistics',
        employee_id: this.data.employee_id ? this.data.employee_id : app.globalData.userData.id,
        start_date: this.data.start ? this.data.start : this.data.thisweek.start_day,
        method: 'myTaskList',
        page_size: 1000,
        range: 'all'
      }
      dd.navigateTo({
        url: '../newspaperTask/newspaperTask?data=' + JSON.stringify(data)
      })
    } else if (index == 8) {
      let data = {
        type: 'week_statistics',
        employee_id: this.data.employee_id ? this.data.employee_id : app.globalData.userData.id,
        start_date: this.data.start ? this.data.start : this.data.thisweek.start_day,
        method: 'taskList',
        page_size: 1000,
        range: 'all'
      }
      dd.navigateTo({
        url: '../newspaperTask/newspaperTask?data=' + JSON.stringify(data)
      })
    }
  },
  setDate() {
    var thisweek = {};
    var date = new Date();
    // 本周一的日期
    date.setDate(date.getDate() - date.getDay() + 1);
    let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
    let getDate = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()
    thisweek.start_day = date.getFullYear() + "-" + month + "-" + getDate;

    // 本周日的日期
    date.setDate(date.getDate() + 6);
    let month2 = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
    let getDate2 = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()
    thisweek.end_day = date.getFullYear() + "-" + month2 + "-" + getDate2;
    this.setData({
      thisweek: thisweek
    })
  },
  // 页面被关闭
  onUnload() {
    app.globalData.socketApi.closewebsocket();
  },
  opneWebSocket() {
    let data = {
      type: 'week_statistics',
      employee_id: this.data.employee_id ? this.data.employee_id : app.globalData.userData.id,
      start_date: this.data.start ? this.data.start : this.data.thisweek.start_day
    }
    app.globalData.socketApi.sendData(data, this.onmessageWS);
  },
  onmessageWS(e) {
    // 因为作用域问题，这个函数不能使用this
    if (e.type == 'week_statistics' && e.msg == 'all') {
      let result = e.result
      that.setData({
        result: result
      })
      // app.globalData.socketApi.closewebsocket();
    }
  }
});
