const imgurl = require("../../utils/imgurl");
const util = require("../../utils/util");
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activityStyle: String,
    allFlag: Boolean,
    childUrl: String,
  },
  /**
   * 组件的初始数据
   */
  data: {
    week: ['日', '一', '二', '三', '四', '五', '六'],
    dateLsit: [],
    year: new Date().getFullYear(),
    month: new Date().getMonth() + 1,
    yearFlgh: new Date().getFullYear(), //当前年
    monthFlgh: new Date().getMonth() + 1, //当前月
    dey: new Date().getDate(),
    thendata: [], //当前这一周的数组
    xiala: imgurl.concealBnt.url,
    xiangshang: imgurl.xiangshang.url,
    switchFlag: true,
    activityDateList: [],
    activiyFalg:"",
    context: -1 ,//-1表示取一周下标 1表示取一个月的下标
  },

  /**
   * 组件的方法列表
   */
  methods: {
    itiActivityLst() { //初始哈活动列表
      let tempDateLsit = this.data.dateLsit
      let tempThenData = this.data.thendata
      this.data.activityDateList.map(item0 => { //初始化当月 标记
        tempDateLsit.map(item1 => {
          if (item0.yyyy == item1.y && item0.mm == item1.m && item0.dd == item1.value) {
            item1.act = 1
          }
        })
      })
      this.data.activityDateList.map(item0 => { //初始化 当周标记
        tempThenData.map(item1 => {
          if (item0.yyyy == item1.y && item0.mm == item1.m && item0.dd == item1.value) {
            item1.act = 1
          }
        })
      })
      this.setData({
        dateLsit: tempDateLsit,
        thendata: tempThenData
      })
      this.setThenDye(0)
    },

    getDey() { //查询当月标记日期
      let tempMonth = this.data.month
      let tempDey = this.getDays(this.data.year, this.data.month) //获取每月有多少天
      if (tempMonth < 10) {
        tempMonth = '0' + (tempMonth + '')
      }
      let param = {
        startDate: this.data.year + '-' + tempMonth + "-01" + " 00:00:00",
        endDate: this.data.year + "-" + tempMonth + "-" + tempDey + " 23:59:59"
      }
      Http.get({
        url: this.data.childUrl,
        data: param
      }).then(res => {
        let {
          code,
          data
        } = res
        if (code == 200) {
          this.setData({
            activityDateList: data
          })
          this.showDate(this.data.year, this.data.month)
        }
      })
    },
    switchs() {
      let temp = this.data.switchFlag
      this.setData({
        switchFlag: !temp
      })
      if(this.data.switchFlag){
        this.setData({
          context:-1
        })
        this.setThenDye()
      }else{
        this.setData({
          context: 1
        })
        this.setThenDye()
      }
      // console.log(this.data.switchFlag)
    },
    showDate(year, month) { //刷新日历
      let thenDey = this.getThenWeek(year, month) //每个月第一天星期几
      let thenMonth = this.getDays(year, month) //获取每月有多少天
      let lastMonth = this.getLastMonth(year, month) //上月最后一天是多少天
      let tempDey = []
      let tmepArr = []
      for (let i = 0; i < 42; i++) { //初始化日历数组
        tempDey.splice(i, 0, "")
      }
      for (let i = 0; i < thenMonth; i++) { //获取当前选中月份天数
        tmepArr.splice(i, 0, {
          style: "0",
          value: i + 1,
          y: year,
          m: month
        })
      }
      tempDey.splice(thenDey, thenMonth, ...tmepArr)
      for (let i = 0; i < thenDey; i++) { //添加上一个月的天数
        tempDey.splice(i, 1, {
          style: "1",
          value: lastMonth - (thenDey - 1),
          y: year,
          m: month==1?12:month-1
        })
        lastMonth++
      }
      for (let i = 0; i < 42 - (thenDey + thenMonth); i++) { //设置下一个月
        tempDey.splice(thenDey + thenMonth + i, 1, {
          style: "2",
          value: i + 1,
          y: year,
          m: month == 12 ? 1 : month + 1
        })
      }
      this.setData({
        dateLsit: tempDey
      })
      this.itiActivityLst()
      this.setThenDye()
  
    },

    setThenDye(){//设置当天index
      if (this.data.context==-1){//-1表示一周
        console.log(this.data.thendata,this.data.dey,this.data.monthFlgh,this.data.yearFlgh,"this.data.thendata")
        let index_ = ""
        this.data.thendata.map((item,index)=>{
           if(item.value == this.data.dey && this.data.monthFlgh == item.m && item.y == this.data.yearFlgh){
             index_ =  index
             this.setData({
               activiyFalg: index_
             })
             return
           }
        })
      }else{
        let index_ = ""
        if(this.data.year==this.data.yearFlgh&&this.data.month==this.data.monthFlgh){
          this.data.dateLsit.map((item, index) => {
            if (item.value == this.data.dey && this.data.monthFlgh == item.m && item.y == this.data.yearFlgh) {
              console.log(index, "index")
              index_ = index
              this.setData({
                activiyFalg: index_
              })
              return
            }
          })
        }else{
          this.setData({
            activiyFalg: -1
          })
        }

      }
    },
    handle(e) { //点击号数
      let item = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      if (this.data.allFlag){
        this.setData({
          activiyFalg:index
        })
      }
      if (item.style==0){
        this.triggerEvent("setDate", {
          y: this.data.year,
          m: this.data.month,
          value: item.value
        })
      }else{
        return
      }
    },
    getDays(year, month) { //获取每月有多少天
      let tempDateArr = []
      let thenDate = new Date(year, month, 0).getDate()
      return thenDate
    },
    getLastMonth(year, month) { //上一个月最后一天是多少号
      if (month == 1) {
        month = 12
        year = year - 1
      } else {
        month = month - 1
      }
      let tempDateArr = []
      let thenDate = new Date(year, month, 0).getDate()
      return thenDate
    },
    getThenWeek(year, month) { //获取每个月第一天是星期几
      if (month == 1) {
        month = 12
        year = year - 1
      } else {
        month = month - 1
      }
      let date = new Date()
      date.setFullYear(year)
      date.setMonth(month)
      date.setDate(1)
      return date.getDay()
    },
    getactivityDate(year, month, mode) { //mode == 0 查询当月 mode == 1 查询当天
      let tempMonth = month
      let tempDey = this.getDays(year, month) //获取每月有多少天
      if (tempMonth < 10) {
        tempMonth = '0' + (tempMonth + '')
      }
      let param = {
        startDate: year + '-' + tempMonth + "-01" + " 00:00:00",
        endDate: year + "-" + tempMonth + "-" + tempDey + " 23:59:59"
      }
      Http.get({
        url: this.data.childUrl,
        data: param
      }).then(res => {
        let {
          code,
          data
        } = res
        if (code == 200) {
          this.setData({
            activityDateList: data
          })
        }
      })
    },
    front() { //前面
    this.setData({
      context:1
    })
      if (this.data.month == 1) {
        let temp = this.data.switchFlag
        this.setData({
          switchFlag: false
        })
        this.setData({
          year: this.data.year - 1,
          month: 12
        })
      } else {
        this.setData({
          month: this.data.month - 1
        })
      }
      this.triggerEvent("setDate", {
        y: this.data.year,
        m: this.data.month
      }) //第一个是自定义事件名，第二个是要传递的值（可以为空）
      this.getDey()
      
      
    },

    behind() { //后面
      this.setData({
        context: 1
      })
      let temp = this.data.switchFlag
      this.setData({
        switchFlag: false
      })
      if (this.data.month == 12) {
        this.setData({
          year: this.data.year + 1,
          month: 1
        })
        this.showDate(this.data.year, this.data.month)
      } else {
        this.setData({
          month: this.data.month + 1
        })
        this.showDate(this.data.year, this.data.month)
      }
      this.getDey()
      this.triggerEvent("setDate", {
        y: this.data.year,
        m: this.data.month
      })
      
    },

  },
  lifetimes: {
    attached: function() {
      this.getDey() //查询当月活动
      // 设置单周数组
      let thenDey = new Date().getDay() //今天是周几
      let thenDate = new Date().getDate() //今天是多少号
      let tempArr = []
      if (thenDey == 0) {
        for (let i = 0; i < 7; i++) {
          tempArr.splice(i, 1, {
            thenFalg: "1",
            value: thenDate,
            style: "0",
            y: this.data.year,
            m: this.data.month
          })
          thenDate++
        }
      } else {
        let temp = new Date().getDay() //今天是周几
        let tmpeDey = new Date().getDate() //今天是多少号
        for (let i = 0; i <= temp; i++) {
          tempArr.splice(i, 0, {
            style: "0",
            value: thenDate - thenDey,
            y: this.data.year,
            m: this.data.month
          })
          thenDey--
        }
        for (let i = temp + 1; i < 7; i++) {
          tmpeDey++
          tempArr.splice(i, 0, {
            style: "0",
            value: tmpeDey,
            y: this.data.year,
            m: this.data.month
          })
        }
      }
      this.setData({
        thendata: tempArr,
        context:this.data.allFlag ? -1 : 1
      })

      // 设置单周数组
      // setTimeout(() => {
      //   this.showDate(this.data.year, this.data.month)
      // }, 300)

    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})