/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：一览界面日期下拉组件
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		admin		  	2021-5-5			1.00		   	  新建
 *******************************************************************************/
const util = require('../../../utils/util.js')
const Constant = require('../../../utils/Constants.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //默认值
    defaultValue: {
      type: String,
      default: '-1',
    },
    // 是否有
    blankFlag: {
      type: Boolean,
      default: true
    },
    // 该组件的编码名称（唯一）
    code:{
      type:String,
      default:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    active: null,
    dateDefault: {},
    dateTypeList: [],
    dateSearch: [new Date().getTime() + 100, new Date().getTime() + 100],
    // 往组件外返回值
    dataDefaultSearch: [],
    createTimeType: { text: '时间', value: -1 },
  },

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

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      let dateTypeList = []
      if (this.data.blankFlag) {
        dateTypeList.push(this.data.createTimeType)
      }
      dateTypeList = dateTypeList.concat(Constant.searchDateSource);
      let dateDefault = {}
      let date = []
      // 默认值
      if (dateTypeList != null) {
        dateTypeList.forEach(item => {
          if (item.value + '' === this.data.defaultValue + '') {
            dateDefault.value = item.value
            dateDefault.text = item.text
            console.log('item', item)
            if (item.value + '' == '1') {
              date = [util.formatTime(new Date()).substring(0, 10), util.formatTime(new Date()).substring(0, 10)]
            }
            else if (item.value + '' == '2') {
              date = [util.formatTime(util.getCurrentWeek(0)).substring(0, 10), util.formatTime(util.getCurrentWeek(-6)).substring(0, 10)]
            }
            else if (item.value + '' == '3') {
              date = util.getCurrentMonth();
            }
            else if (item.value + '' == '6') {
              date = util.timeForMat(7)
            }
            else if (item.value + '' == '5') {
              date = util.timeForMat(30)
            }
            else if (item.value + '' == '-2') {
              date = [util.formatTime(new Date(this.data.dateSearch[0])).substring(0, 10),
              util.formatTime(new Date(this.data.dateSearch[1])).substring(0, 10)]
            }
          }
        })
      }
      this.setData({
        dateTypeList: dateTypeList,
        dateDefault: dateDefault,
        dataDefaultSearch: date,
        dateSearch: [new Date(this.data.dateSearch[0]).getTime(), new Date(this.data.dateSearch[1]).getTime()]
      }) 
      // 如果有默认值 ，需要传递出去  
      if(this.data.defaultValue){
        this.chooseDefault();
      }
     },
    /**
     * 日期变化 查询 
     * @param {*} e 
     */
    choose(e) {
      let _this = this
      let item = e.currentTarget.dataset.item

      if (item.value == -2) {
        _this.setData({
          show: true
        })
        setTimeout(function () {
          _this.setData({
            active: 0
          })
        }, 100)
      } else {
        _this.setData({
          dateDefault: item
        })

        let date = []
        if (item.value == 1) {
          date = [util.formatDataTime(new Date()).substring(0, 10), util.formatDataTime(new Date()).substring(0, 10)]
        }
        else if (item.value == 2) {
          date = [util.formatDataTime(util.getCurrentWeek(0)).substring(0, 10), util.formatDataTime(util.getCurrentWeek(-6)).substring(0, 10)]
        }
        else if (item.value == 3) {
          date = util.getCurrentMonth();

        } else if (item.value == 4) {
          let startYear = util.getYear("s", 0)
          let endYear = util.getYear("e", 0)
          //本年
          date = [util.formatDataTime(new Date(startYear)).substring(0, 10),
          util.formatDataTime(new Date(endYear)).substring(0, 10)]
        }
        else if (item.value == 6) {
          //近30天
          date = util.timeForMat(7)
        }
        else if (item.value == 5) {
          //近30天
          date = util.timeForMat(30)
        }
        this.setData({
          dataDefaultSearch: date
        })
        this.triggerEvent('dateCommit', {code:this.data.code, date: date,  dateDefault: this.data.dateDefault })
      }
    },
    /**
     * @desc   : 设置默认值
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    chooseDefault(){
      let defVal = this.data.defaultValue
      if (defVal != -2) {
        let date = []
        if (defVal == 1) {
          date = [util.formatTime(new Date()).substring(0, 10), util.formatTime(new Date()).substring(0, 10)]
        }
        else if (defVal == 2) {
          date = [util.formatTime(util.getCurrentWeek(0)).substring(0, 10), util.formatTime(util.getCurrentWeek(-6)).substring(0, 10)]
        }
        else if (defVal == 3) {
          date = util.getCurrentMonth();

        } else if (defVal == 4) {
          let startYear = util.getYear("s", 0)
          let endYear = util.getYear("e", 0)
          //本年
          date = [util.formatTime(new Date(startYear)).substring(0, 10),
          util.formatTime(new Date(endYear)).substring(0, 10)]
        }
        else if (defVal == 6) {
          //近7天
          date = util.timeForMat(7)
        }
        else if (defVal == 5) {
          //近30天
          date = util.timeForMat(30)
        }
        let filters = Constant.searchDateSource.filter(it=>it.value == defVal);
        if(filters && filters.length > 0){
          this.setData({
            dateDefault:filters[0]
          })
          this.triggerEvent('dateCommit', {code:this.data.code, date: date,  dateDefault: this.data.dateDefault,initFlag:true })
        }
      }
    },
    /**
     * 日期变化 查询数据 
     * @param {*} e 
     */
    changeDate(e) {
      let dateSearch = this.data.dateSearch;
      dateSearch[e.currentTarget.dataset.index] = e.detail
      this.setData({
        dateSearch: dateSearch
      })
    },

    /**
     * 日期变化 查询数据 
     * @param {*} e 
     */
    onCloseChooseDate() {
      this.setData({
        show: false
      })
      this.setData({
        dateSearch: [new Date().getTime(), new Date().getTime()],
      })
    },
    /**
     * 日期变化 查询 
     * @param {*} e 
     */
    onConfirmChooseDate() {
      this.setData({
        show: false,
        dateDefault: this.data.dateTypeList[this.data.dateTypeList.length - 1]
      })
      let date = [util.formatTime(new Date(this.data.dateSearch[0])).substring(0, 10),
      util.formatTime(new Date(this.data.dateSearch[1])).substring(0, 10)]
      this.setData({
        dataDefaultSearch: date
      })
      this.triggerEvent('dateCommit', {code:this.data.code,  date: date, dateDefault: this.data.dateDefault })
    },
  }
})
