var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    dateIndex: "1",
    year: app.globalData.year,//年
    month: app.globalData.month,//月
    jdYear: app.globalData.year,//季度年
    jdji: 0,//季度季
    jdArr: ['一季度', '二季度', '三季度', '四季度'],
    yearIndex:0,
    yearArr:app.globalData.yearArr,
    jdYearIndex:0,
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm: (data) => console.log(data),
    dateObj:{
      type:'3',
      date:app.globalData.month
    },
  },
  didMount() {
    this.setData({
      isShow: this.props.isShow,
    })
    if(this.props.dateObj.type){
      this.setData({
        dateIndex:this.props.dateObj.type,
      })
      if(this.props.dateObj.type==1){
          this.data.yearArr.forEach((item,index)=>{
            if(this.props.dateObj.date==item){
                this.setData({
                  yearIndex:index,
                })
            }
          })
      }else if(this.props.dateObj.type==2){
          var data=String(this.props.dateObj.date);
          this.data.yearArr.forEach((item,index)=>{
            if(data.substr(0,4)==item){
                this.setData({
                  jdYearIndex:index,
                })
            }
          })
          this.setData({
            jdji:data.substr(data.length-1,1)-1
          })
      }else{
          this.setData({
            month:this.props.dateObj.date,
          })
      }
    }
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    selectYear2(e) {
      this.setData({
        jdYearIndex: e.detail.value,
      });
    },
    selectYear(e) {
      this.setData({
        yearIndex: e.detail.value,
      });
    },
    setjdji(e) {
      this.setData({
        jdji: e.detail.value,
      });
    },
    onConfirm() {
      var obj;
      switch (this.data.dateIndex) {
        case "1":
          obj = {
            type: this.data.dateIndex,
            date: this.data.yearArr[this.data.yearIndex]
          }
          break
        case "2":
          var year=this.data.yearArr[this.data.jdYearIndex];
          var num=this.data.jdji;
          num++;
          let arrNew=new Array()
          arrNew.push(year)
          arrNew.push(num)
          let strNew=arrNew.join('')
          obj = {
            type: this.data.dateIndex,
            date:parseInt(strNew)
          }
          break
        case "3":
          obj = {
            type: this.data.dateIndex,
            date: this.data.month
          }
          break
      };
      this.props.onConfirm(obj);
      this.props.onClose();

    },
    onClose() {
      this.props.onClose();
    },
    selectDay() {
      dd.datePicker({
        format: 'yyyy',
        currentDate: this.data.jdYear,
        success: (res) => {
          this.setData({
            jdYear: res.date
          })
        },
      });
    },
    selectDay2() {

    },
    selectDate() {
      dd.datePicker({
        format: 'yyyy-MM',
        currentDate: this.data.month,
        success: (res) => {
          this.setData({
            month: res.date
          })
        },
      });
    },
    activeDate(e) {
      this.setData({ dateIndex: e.target.dataset.index })
    },

  },
});
