const util = require("../../../../utils/util");
const api = require('../../../../api/api.js');
var check = require('../../../../utils/check.js');
import moment from 'moment'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentMonth:moment(new Date()).format('YYYY-MM'),
    salaryList: [],
    total: 0,
    month: '',
    type: 1,
    showAuth: false,
    servantType: 0, //servantType 表示服务者身份99是临时服务者 明细页可以显示提现按钮
    blance: 0, //可提现金额
    mobile: '',
    nid: '',
    name: '',
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      month: options.month,
      total: options.total,
      type: options.type,
      servantType: options.servantType,
      blance: options.blance
    })
    this.getSalary();
  },
  lastMonth(){

    let params=util.getYearMonth(this.data.month,'-');
    this.setData({
      month:params
    });
    this.getSalary();
  },
  nextMonth(){
    let params=util.getYearMonth(this.data.month,'+');
    this.setData({
      month:params
    });
    this.getSalary();
  },
  onChange(e) {
    console.log(e);
    this.setData({
      checked: e.detail,
    });
  },
  userProtocol() {
    wx.navigateTo({
      url: '/pages/servant/salary/protocol/protocol'
    });
  },
  getSalary() {
    let that = this
    wx.showLoading();
    util.request(api.SalaryDetail, {
      month: that.data.month,
      type: that.data.type,
      salaryType: 0
    }).then(res => {
      wx.hideLoading();
      let sumtotl=0;
      res.data.forEach(r=>{
        sumtotl=r.salary+sumtotl;
      })

      //保留两位小数
        sumtotl=sumtotl.toFixed(2);

      that.setData({
        salaryList: res.data,
        total:sumtotl,
      })
    }).catch(res => {
      wx.hideLoading();
    })
  },
  getSalaryDetail(e) { //获取项目的构成明细 比如工单计提 每单的详细情况
    let that = this
    let salaryType = e.currentTarget.dataset.salarytype;
    let salaryList = that.data.salaryList;
    let salary = null;
    salaryList.forEach(item => {
      if (item.salaryType == salaryType)
        salary = item;
    })
      salary.isShow = !salary.isShow;
    if (salary.details != null && salary.details.length > 0) {
      that.setData({
        salaryList: salaryList
      })
      return;
    }

    wx.showLoading();
    util.request(api.SalaryDetail, {
      month: that.data.month,
      type: 99,
      salaryType: salaryType
    }).then(res => {
      wx.hideLoading();
      salary.details = res.data;
      that.setData({
        salaryList: salaryList
      })
    }).catch(res => {
      wx.hideLoading();
    })

  },
  goCheckAuth() { //是否实名认证过
    let that = this
    util.request(api.ParterAuthCheck, {}).then(function (res) {
      if (res.errno === 0 && res.data) {
        that.goCashout();
      } else {
        that.setData({
          showAuth: true
        })
      }
    })
  },
  goCashout() { //提现申请
    let that = this
    wx.showModal({
      title: '提示',
      content: '提现金额为' + that.data.blance + '，平台通过授权第三方（“云账户”）向您的微信钱包打款，涉及手续费由引荐人自行承担，故最终提现金额应以实际到账金额为准。',
      success: function (sm) {
        if (sm.confirm) {
          util.request(api.ParterCashout, {
            cashout: that.data.blance
          }).then(function (res) {
            console.info(res)
            if (res.errno === 0) {
              wx.showToast({
                title: '提现申请成功',
                icon: 'none',
                duration: 2000
              });
              that.setData({
                showAuth: false,
                blance: 0
              })
            }
          });

        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goList() {
    wx.navigateTo({
      url: '/pages/servant/salary/list/list',
    })
  },
  goCancel() {
    this.setData({
      showAuth: false
    })
  },
  bindinputName(e) {
    let val = e.detail.value;
    this.setData({
      name: val
    });

  },
  bindinputNid(e) {
    let val = e.detail.value;
    this.setData({
      nid: val
    })
  },
  cashoutAuth() {
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none',
        duration: 2000
      });
      return false;

    } else if (!check.isCardCheck(this.data.nid)) {

      wx.showToast({
        title: '请输入正确的身份证号',
        icon: 'none',
        duration: 2000
      });
      return false;

    } else if (!this.data.checked) {
      wx.showToast({
        title: '请阅读《用户条款》并勾选',
        icon: 'none',
        duration: 2000
      });
      return false;

    }

    util.request(api.ParterAuth, {
      name: this.data.name,
      nid: this.data.nid
    }).then(res => {
      if (res.errno == 0) {
        this.goCashout();
      }
    });

  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})
