<template>
  <div>
		<u-calendar 
		:show="show" 
		@confirm="onConfirm"
		@close="show=false"
		:maxDate="maxDate" 
		:defaultDate="defaultDate"
		:formatter='formatter'
		ref="calendar2"
		></u-calendar>
  </div>
</template>

<script>
  export default {
    props: ["dateList"],
    watch: {
    	dateList:{
    		handler(newValue, oldValue){
    			if(newValue&&newValue.length>0){
    				this.maxDate=newValue[newValue.length-1].startTime.substr(0,10)
    			}
    		},
    		deep:true,
    		immediate:true
    	}
    },
    data() {
      return {
        date: '', //选择的日期
        show: false, //控制日历显示隐藏
        defaultDate:'', //默认日期
        maxDate:'',//最大日期
      }
    },
		created() { //默认为第一天
			this.$refs.calendar2.setFormatter(this.formatter)
		},
    methods: {
			//---年月日转化
      formatDate(date) {
        let year = date.getFullYear()
        let month = date.getMonth() + 1;
        if (month < 10) {
          month = '0' + month
        }

        let days = date.getDate();
        if (days < 10) {
          days = '0' + days
        }
      	 let dates = year + "-" + month + "-" + days
        return dates
      },
			//---日期确认事件
      onConfirm(date) { 
        this.show = false;
        this.$emit("dateConfig", date[0])
      },
			//---时间格式转换
      formatter(day) { 
        let year = day.date.getFullYear()
        let month = day.date.getMonth() + 1;
        if (month < 10) {
          month = '0' + month
        }
        let days = day.date.getDate();
        if (days < 10) {
          days = '0' + days
        }
        let dates = year + "-" + month + "-" + days
				//真机调试手极端报错，暂时注释
				// this.dateList.forEach((item, index) => {
				// 	if (item.startTime.substr(0, 10) == dates) {
				// 		day.bottomInfo = "¥" + item.sellingPrice
				// 		day.dot = true
				// 	}
				// })
        // if (!day.bottomInfo) {
        //   day.type = "disabled"
        // }
        return day
      },
			 //---设置默认日期方法
      setDefaultDate(val) {
        let dates = val.split("-")
        let year = parseInt(dates[0])
        let month = parseInt(dates[1]) - 1
        let day = parseInt(dates[2])
        this.defaultDate = new Date(year, month, day)
      },
    }
  }
</script>

<style scoped="scoped">

</style>
