<template>
	<view class="page">
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期一 · {{minTenAndZero(week[0])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[0].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[0].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[0].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期二 · {{minTenAndZero(week[1])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[1].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[1].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[1].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期三 · {{minTenAndZero(week[2])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[2].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[2].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[2].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期四 · {{minTenAndZero(week[3])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[3].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[3].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[3].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期五 · {{minTenAndZero(week[4])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[4].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[4].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[4].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期六 · {{minTenAndZero(week[5])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[5].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[5].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[5].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="example-box">
			<view class="item" style="margin-top: 22upx;">
				<view class="itemname">星期天 · {{minTenAndZero(week[6])}}</view>
			</view>
			<view class="item">
				<view class="itemname">必做事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入必做事项" v-model="itemList[6].mustContent" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="itemname">待办事项</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入待办事项" v-model="itemList[6].waitContet" @change='infoChange'/>
					</view>
				</view>
			</view>
			<view class="item item_line">
				<view class="itemname">需求资源</view>
				<view class="itemtext">
					<view>
						<input class="iteminput" placeholder="请输入需求资源" v-model="itemList[6].needSource" @change='infoChange'/>
					</view>
				</view>
			</view>
		</view>
		<view class="fixMargin"></view>
		<view class="goHome" @click='goMain("weekInfo")'>
			<img src="../../static/img/gohome.png">
		</view>
		<view class="fixBottom">
			<view class="uni-flex uni-row">
				<button type="primary"  style="width: 100%;"  @click="submit">确认提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		config
	} from '../../request/js/config.js'
	export default{
		data(){
			return {
				config:config,
				itemList:[
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期一',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期二',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期三',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期四',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期五',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期六',
					},
					{
						mustContent:'',
						waitContet:'',
						needSource:'',
						week:'星期天',
					}
				],
				week:[],
			}
		},
		onLoad(option) {
		    var _this = this;
		   //如果不是，是新增，获取session中的数据。
		   uni.getStorage({
				key: 'weekInfo',
				success: function (res) {
					// console.log(res);
					var session = res.data;
					if(session.itemList && session.itemList.length>0){
						_this.itemList = session.itemList
						_this.itemList.forEach(function(val,index){
							_this.week.push(_this.minTenAndZero(_this.changeTime(val.reportDate)));
						})
					}else{
						//初始化展示当前周的日期。
						_this.week = _this.getNextWeek();
						_this.itemList.forEach(function(val,index){
							val.reportDate = _this.minTenAndZero(_this.week[index]);
						})
					}
				},
				fail:function(){
					_this.week = _this.getNextWeek();
					_this.itemList.forEach(function(val,index){
						val.reportDate = _this.minTenAndZero(_this.week[index]);
					})
				}
		   });
		},
		methods:{
			changeTime(info){
				var st = [];
				if(info){
					st = info.split(" ");
				}
				return st[0];
			},
			minTenAndZero(value){
				var array = value.split('-');
				var year = array[0];
				var month = parseInt(array[1]);
				var day =parseInt( array[2]);
				
				var date = year+'-'+(month<10?('0'+month):month)+'-'+(day<10?('0'+day):day)
			    return date
			},
			infoChange(){
				//保存当前修改信息
				var weekInfo =  uni.getStorageSync('weekInfo');
				if(!weekInfo){
					weekInfo = {}
				}
				weekInfo.itemList = this.itemList;
				uni.setStorage({
				    key: 'weekInfo',
				    data: weekInfo,
				    success: function () {

				    }
				});
			},
			getNextWeek() {
				var currentDate = new Date();
			    var timesStamp = currentDate.getTime();
			    var currenDay = currentDate.getDay();
			    var dates = [];
			    for (var i = 0; i < 7; i++) {
			        dates.push(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)+ 24*60*60*1000*7).toLocaleDateString().replace(/\//g, '-'));
			    }
			    return dates
			},
			submit(){
				// console.log(this.itemList);
				uni.navigateTo({
				    url: "addWeek"
				});
			}
		}
	}
</script>