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


Page({
  data: {
    servant: {
      gohome: '0',
      walk: '1',
      walkDate: '', //可接单时间
      address: '',
      goHomeDate: '', //返乡时间
      goBackDate: '', //返程时间
      date: [ {
				date: '2月6日',
        lunar: '廿七',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
      }, {
				date: '2月7日',
        lunar: '廿八',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
      }, {
      	date: '2月8日',
        lunar: '廿九',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
      }, {
				date: '2月9日',
        lunar: '除夕',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
      }, {
				date: '2月10日',
        lunar: '春节',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
      }, {
				date: '2月11日',
        lunar: '初二',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			},{
				date: '2月12日',
        lunar: '初三',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			},{
				date: '2月13日',
        lunar: '初四',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			},{
				date: '2月14日',
        lunar: '初五',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			},{
				date: '2月15日',
        lunar: '初六',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			},{
				date: '2月16日',
        lunar: '初七',
        type: [{
          title: '全天',
          checked: true
        }, {
          title: '白班',
          checked: false
        }, {
          title: '休息',
          checked: false
        }]
			}, 
			
			
		],

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {} else {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
  },
  saveNewYearReport() {
    let that = this;
    if (this.valid() == false) return;
    console.info(that.data.servant) 
    wx.showModal({
      title: '提示',
      content: '请核对提交内容，确定提交吗？提交后无法修改',
      success: function (sm) {
        if (sm.confirm) {
          wx.showLoading({
            title: '提交中',
          });

          util.request(api.NewYearReport, that.data.servant, "POST").then(res => {
            wx.hideLoading();
            wx.showToast({
              title: '报备成功',
              duration: 2000
            });
            setTimeout(
              function () {
                wx.switchTab({
                  url: '/pages/upgrade/my/my',
                })
              }, 3000
            );
          }).catch(res => {
            wx.hideLoading();
            wx.showToast({
              title: res.data.data,
              duration: 3000
            });
          })

        }
      }
    });
  },
  valid() {
    if (this.data.servant.gohome == '1' && (this.data.servant.address == '' || this.data.servant.goHomeDate == '' || this.data.servant.goBackDate == '' || this.data.servant.walkDate == "")) {
      wx.showToast({
        title: '请填写返乡居住地及时间',
        duration: 3000
      });
      return false;
    }
    if (this.data.servant.gohome == '1' && this.data.servant.goBackDate < this.data.servant.goHomeDate) {
      wx.showToast({
        title: '【返城】时间不能小于【返乡】时间',
        duration: 3000
      });
      return false;
    }
    return true;
  },
  radioChangeGoHome(e) {
    this.setData({
      'servant.gohome': e.detail.value
    })
    if (e.detail.value) {
      this.setData({
        'servant.walk': 1
      })
    }
    console.info(this.data.servant)
  },
  radioChangeWalk(e) {
    this.setData({
      'servant.walk': e.detail.value
    })
    console.info(this.data.servant)
  },
  bindAddress(e) {
    this.setData({
      'servant.address': e.detail.value
    })
  },
  bindGoHomeChange(e) {
    this.setData({
      'servant.goHomeDate': e.detail.value
    })
  },
  bindGoBackChange(e) {
    this.setData({
      'servant.goBackDate': e.detail.value
    })
  },
  bindWalkDateChange(e) {
    this.setData({
      'servant.walkDate': e.detail.value
    })
    //小于选择的接单日期，全部设置为休息
    // var dates = this.data.servant.date;
    // var selected = this.data.servant.walkDate.substring(5).replace("-", "月") + "日";
    // if (selected.substring(0, 1) == "0")
    //   selected = selected.substring(1, 5);
    // //console.info(selected)
    // dates.forEach(item => {
    //   if (item.date <= selected)
    //     item.type[2].checked = true;
    // })
    // this.setData({
    //   'servant.date': dates
    // });
  },
  radioDateChange(e) {
    let date = e.currentTarget.dataset.id; 
    let title = e.detail.value; 
    var dates = this.data.servant.date;
    dates.forEach(item => {
      if (item.date == date) {
        item.type.forEach(t => { 
            t.checked = (t.title == title);  
        })
      }

    });
    this.setData({
      'servant.date': dates
    })

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})