<template name="times">
	<view>
		<view class="cu-timeline" v-for="(v,k) in fetch.times" :key="v.id">
			<view class="cu-time">{{v.name}}</view>
			<view class="cu-item" :class="v1.id == times ? 'text-blue' : ''" v-for="(v1,k1) in v.child" :key="v1.id" :style="{backgroundImage: v1.icon}">
				<view class="content" :style="{color:v1.color,backgroundColor:v1.bgcolor}" @click="openTime(k, k1)">
					<view class="cu-capsule radius">
						<view class="cu-tag bg-cyan">{{v1.name}}</view>
						<view class="cu-tag line-white">
							<!--
							<picker mode="date" :value="fetch.user_times[v1.id] && fetch.user_times[v1.id].day != 0 ? fetch.user_times[v1.id].day : v1.year + '-' + month + '-' + day" :start="v1.year+`-01-01`" :end="(v1.end_year ? v1.end_year : v1.year) +`-12-31`" :fields="`day`" @change="selectDay" :data-year="v1.year" :data-name="v1.name" :data-id="v1.id">
								<view class="uni-input">{{fetch.user_times[v1.id] && fetch.user_times[v1.id].day != 0 ? fetch.user_times[v1.id].day : '主序时间'}}</view>
							</picker>
							-->
							<view class="uni-input">{{fetch.user_times[v1.id] && fetch.user_times[v1.id].day != 0 ? fetch.user_times[v1.id].day : '主序时间'}}</view>
						</view>
					</view>
					<view @click="go(v1)">
						<view class="margin-top" v-if="v1.desc&& !v1.pic">{{v1.desc}}</view>
						<view class="margin-top" v-if="v1.pic && !v1.desc"><image :src="v1.pic" mode="widthFix" class="slide-image" style="height:auto"></image></view>
					</view>
				</view>
				
				<w-picker
					:visible.sync="v1.visible"
					mode="date" 
					:startYear="v1.year" 
					:endYear="(v1.end_year ? v1.end_year : v1.year)"
					:value="fetch.user_times[v1.id] && fetch.user_times[v1.id].day != 0 ? fetch.user_times[v1.id].day : v1.year + '-' + month + '-' + day"
					:current="false"
					fields="day"
					@confirm="selectDay($event,v1.id, v1.year, v1.name)"
					:disabled-after="false"
					:ref="'date'+v1.id" 
				></w-picker>
			</view>
		</view>
	</view>
</template>

<script>
import wPicker from "@/lib/w-picker/w-picker.vue";
export default {
	name: "times",
	props: {
		content_id : {
			type : String,
			value : null
		},
		width : {
			type : String,
			default : '100%'
		},
		param : {},
		index : 0,
		times : 0,
		set : 0,
	},
	data() {
		return {
			timeVisible:false,
			fetch : {},
			current : '',
			year : '',
			month : '',
			day : '',
		}
	},
	components:{
		wPicker
	},
	mounted() {
		this.fetch = this.param;
		this.getDate();
	},
	methods:{
		openTime : function(k, k1) {
			this.fetch.times[k].child[k1].visible = true;
		},
		getDate : function(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			this.year = year;
			this.month = month > 9 ? month : '0' + month;;
			this.day = day > 9 ? day : '0' + day;
			this.current = this.year + '-' + this.month + '-' + this.day;
		},
		getData : function() {
			this.$emit('getTimes');
		},
		getInfo : function(t) {
			//触底刷新
		},
		go : function(times) {
			var self = this;
			this.Dever.alert('正在进入' + times.name);
			// 要拿到最新的page_id
			if (this.fetch.user_times[times.id]) {
				var day = this.fetch.user_times[times.id].day;
			} else {
				var day = '主序时间';
			}
			if (day == '主序时间') {
				day = '';
			}
			var code = this.Dever.config.code;
			
			this.Dever.post('app/collection/?l=api.getPageId', {set:this.set,code:code,times_id:times.id,day:day}, function(t) {
				if (t.code) {
					self.Dever.location('dream/view?code=' + t.code);
				} else {
					self.Dever.alert('这是您当前选择的时间');
				}
			})
			
		},
		selectDay : function(res, id, year, name) {
			var times = {};
			times.id = id;
			times.year = year;
			times.name = name;
			//times = res.currentTarget.dataset;
			var value = res.value;
			if (!this.fetch.user_times[times.id]) {
				this.fetch.user_times[times.id] = {};
				this.fetch.user_times[times.id].day = '主序时间';
			}
			var old = this.fetch.user_times[times.id].day;
			this.fetch.user_times[times.id].day = value;
			var current = times.year + '-' + this.month + '-' + this.day;
			if (this.fetch.user_times[times.id].day == current) {
				this.fetch.user_times[times.id].day = '主序时间';
			}
			if (old != value) {
				this.go(times);
			}
		},
	},
}
</script>

<style>
.slide-image {
	
}
</style>
