<template>
	<view class="content">
	<view class="zdyNav">
	  <view class="status_bar" :style="{height: iStatusBarHeight + 'px'}"></view>
	  <view class="nav">
		  <view><image src="../../static/img/baiheiback.png" mode="" class="baiheiback" @click="back"></image></view>
		  <view >录入报告</view>
		  <view style="width:70px;"></view>
	  </view>
	</view>
	<view class="status_bar" :style="{height: iStatusBarHeight + 'px'}"></view>
    <view style="height: 44px;"></view>
	<view class="paddingBox">
		<view class="top">
			<view class="topLine">
				<view class="topLineTitle">访问日期</view>
				<view class="topLineRIght">
					<view class="topTime">
						 <picker mode="date" :value="date" :end="nowDate" @change="bindTimeChange">
						       <view class="uni-picker">{{date}}</view>
						  </picker>
					</view>
					<image src="../../static/img/jiantou.png" mode="" class="topJiantou"></image>
				</view>
			</view>
			<view class="topLine">
				<view class="topLineTitle">访问人</view>
				<view class="topLineRIght">
					<span class="topSpan">{{userInfo.name}}</span>
				</view>
			</view>
			<view class="topLine" style="border: none;">
				<view class="topLineTitle">访问门店</view>
				<view class="topLineRIght">
					<span class="topSpan">{{ShopName}}</span>
				</view>
			</view>
		</view>
		
		<view class="kbox" v-for="(v,i) in list" >
			<view class="kboxTitle">{{v.categoryName}}</view>
			<view class="kboxLineCont">
				<view class="kline" v-for="(item,index) in v.sectionList" @click="goTx(item,i,index)">
					<image src="../../static/img/icon_luru@2x.png" mode="" class="klineImg"></image>
					<view class="klineName"> <span class="isRequired" v-if="item.isRequired==1">*</span> {{item.checkName}}</view>
				    <image v-if="item.check" src="../../static/img/icon_luruhou@2x.png" mode="" class="klineXzImg"></image>
				</view>
			</view>
		</view>
		
	</view>
	
	  <view style="height: 150rpx;"></view>
	  <view class="bottom">
		  <view class="bottomBtn" @click="goNext">完成，填写下一步行动建议</view>
	  </view>
	   
	</view>
</template>

<script>
	
	export default {
		
		data() {
			return {
				userInfo:'',
				date:'',
				nowDate:'',
				shopId:'',
				ShopName:'',
				list:'',
				iStatusBarHeight:'',
				packageID:'',
				packageName:'',
				sheetID:'',
				type:'',
				sheetID:'',
				editType:'',
				Contactor:'',
			}
		},
		onShow() {
			if(this.$store.state.categoryList){
				this.list=this.$store.state.categoryList;
				this.list.splice(1,0)
				console.log(this.list)
			}
		},
		onLoad(opt) {
          //页面加载初始化生命周期函数
		   this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
		   this.userInfo=uni.getStorageSync("userInfo");
		   this.ShopName=opt.ShopName;
		   this.Contactor=opt.Contactor;
		   this.type=opt.type;
		   if(this.type){
			   this.sheetID=opt.sheetID
		   }else{
			    this.shopId=opt.shopId;
			    this.getCheckPackageInfo();
		   }
		  
		   this.getTime();
		  
		   
		},
		methods: {
			back(){
				var that=this;
				uni.showModal({
				    title: '提示',
				    content: '返回此次填写的内容将不保留，是否返回',
				    success: function (res) {
				        if (res.confirm) {
				          uni.navigateBack({
				          	delta:1
				          })
				        } else if (res.cancel) {
				           
				        }
				    }
				});
				
				
			},
			goTx(item,i,index){
				uni.navigateTo({
					url:'entryredItem?name='+item.checkName+'&index1='+i+'&index2='+index
				})
				
			},
			getCheckPackageInfo(){
				uni.showLoading({ });
				this.$http('accompany/SuperCheckSheet/queryCheckPackageInfo', {
					shopID:this.shopId
				}, 'GET').then(res => {
					this.list=res.data.categoryList;
					this.packageName=res.data.packageName;
					this.packageID=res.data.id;
					this.$store.commit('mutationsCategoryList',this.list)
				})
			},
			bindTimeChange(e){
				console.log(e);
				this.date=e.detail.value
			},
			getTime(){
				var date = new Date(),
				year = date.getFullYear(),
				month = date.getMonth() + 1,
				day = date.getDate(),
				hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
				minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
				second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				month >= 1 && month <= 9 ? (month = "0" + month) : "";
				day >= 0 && day <= 9 ? (day = "0" + day) : "";
				var timer = year + '-' + month + '-' + day //+ ' ' + hour + ':' + minute + ':' + second;
				this.date=timer;
				this.nowDate=timer;
				return timer;
			},
			goNext(){
				if(this.type==3){
					this.goEdit()
				}else if(this.editType==2){
					this.gonewEdit()
				}else{
					var go=true;
					var name='';
					var categoryList=[]
					console.log(this.list)
					
					this.list.forEach(item=>{
						var obj={
							categoryID:item.categoryID,
							categoryName:item.categoryName,
							sectionList:[]
						}
						item.sectionList.forEach(v=>{
							if(v.check){
								var isCheck=1
							}else{
								var isCheck=0
							}
							var sectionListobj={
								sectionID:v.sectionID,
								checkName:v.checkName,
								checkCycle:v.checkCycle,
								isCheck:isCheck,
								isRequired:v.isRequired,
								itemList:[]
							}
							
							
							v.itemList.forEach(i=>{
								//if()
								var itemObj={
									itemName:i.itemName,
									sectionItemID:i.sectionItemID,
									itemValue:i.itemValue,
									describe:i.describe,
									keyType:i.keyType,
									keyValue:i.keyValue,
								}
								if(!itemObj.itemValue&&itemObj.itemValue!==0){
									itemObj.itemValue=''
								}
								sectionListobj.itemList.push(itemObj)
							})
							obj.sectionList.push(sectionListobj)
						})
							categoryList.push(obj);
						
					})
					var a = JSON.parse(JSON.stringify(this.list));
					a.reverse();
					console.log(a)
				    a.forEach(item=>{
						var b=JSON.parse(JSON.stringify(item.sectionList))
						b.reverse();
						b.forEach(v=>{
							if(v.isRequired==1){
								if(!v.check){
									go=false
									name=v.checkName
								}
							}
						})
					})
					//return false;
					console.log('categoryList')
					console.log(categoryList)
					if(!go){
						uni.showToast({
						    title: '请填写'+name,
							 icon:'none',
						    duration: 3000,
						});
						return false;
					}
					uni.showLoading({ });
					var checkTime=this.date;//+' 00:00:00'
					this.$http('accompany/SuperCheckSheet/addSuperCheckSheet', {
						shopID:this.shopId,
						shopName:this.ShopName,
						checkTime:checkTime,
						packageID:this.packageID,
						packageName:this.packageName,
						categoryList:JSON.stringify(categoryList)
					}, 'POST').then(res => {
						this.sheetID=res.data;
						this.editType=2;
						uni.navigateTo({
							url:'opinion?sheetID='+this.sheetID+'&shopName='+this.ShopName+'&Contactor='+this.Contactor
						})
					})
				}
				
				
				
			},
			goEdit(){
				
				var go=true;
				var name='';
				var categoryList=[]
				console.log(this.list)
				this.list.forEach(item=>{
					var obj={
						categoryID:item.categoryID,
						categoryName:item.categoryName,
						id:item.id,
						sectionList:[]
					}
					item.sectionList.forEach(v=>{
						if(v.check){
							var isCheck=1
						}else{
							var isCheck=0
						}
						var sectionListobj={
							sectionID:v.sectionID,
							checkName:v.checkName,
							checkCycle:v.checkCycle,
							isCheck:isCheck,
							isRequired:v.isRequired,
							id:v.id,
							itemList:[]
						}
						/* if(v.isRequired==1){
							if(!v.check){
								go=false
								name=v.checkName
							}
						} */
						
						v.itemList.forEach(i=>{
							var itemObj={
								itemName:i.itemName,
								sectionItemID:i.sectionItemID,
								itemValue:i.itemValue,
								describe:i.describe,
								keyType:i.keyType,
								keyValue:i.keyValue,
								id:i.id,
							}
							if(!itemObj.itemValue&&itemObj.itemValue!==0){
								itemObj.itemValue=''
							}
							sectionListobj.itemList.push(itemObj)
						})
						obj.sectionList.push(sectionListobj)
					})
						categoryList.push(obj);
					
				})
							
				console.log('categoryList')
				console.log(categoryList);
				var a = JSON.parse(JSON.stringify(this.list));
				a.reverse();
				console.log(a)
				a.forEach(item=>{
					var b=JSON.parse(JSON.stringify(item.sectionList))
					b.reverse();
					b.forEach(v=>{
						if(v.isRequired==1){
							if(!v.check){
								go=false
								name=v.checkName
							}
						}
					})
				})
				if(!go){
					uni.showToast({
					    title: '请填写'+name,
						 icon:'none',
					    duration: 3000,
					});
					return false;
				}
				uni.showLoading({ });
				var checkTime=this.date; //+' 00:00:00'
				this.$http('accompany/SuperCheckSheet/editSuperCheckSheet', {
					sheetID:this.sheetID,
					checkTime:checkTime,
					categoryList:JSON.stringify(categoryList)
				}, 'POST').then(res => {
					this.sheetID=res.data;
					console.log(this.sheetID);
					uni.navigateTo({
						url:'opinion?sheetID='+this.sheetID+'&type='+this.type+'&shopName='+this.ShopName+'&Contactor='+this.Contactor
					})
				})
			},
			gonewEdit(){
				var that=this;
				uni.getLocation({
					success(res) {
						that.lng = res.longitude
						that.lat = res.latitude
						that.getSuperCheckSheetInfo()
					},
					fail() {
					
					}
				})
				//#ifdef H5
				
				that.lng='117.06533'
				that.lat='36.68013'
				that.getSuperCheckSheetInfo()
				 // #endif
			},
			getSuperCheckSheetInfo(){
				uni.showLoading({
					title: '加载中'
				})
				let url = 'accompany/SuperCheckSheet/querySuperCheckSheetInfo',
					params = {
						sheetID: this.sheetID,
						lat:this.lat,
						lng:this.lng
					}
				this.$http(url, params, 'GET').then(res => {
					this.info = res.data;
					this.editBgdata()
				})
			},
			editBgdata(){
				this.info.categoryList.forEach(item=>{
					item.sectionList.forEach(v=>{
						v.check=v.isCheck;
						v.itemList.forEach(i=>{
							if(i.keyType==1){
								
								i.keyValue=i.keyValue.replace(/，/ig,',')
								var array= i.keyValue.split(",")
								var array2=i.itemValue.split(",")
								var arr=[];
								console.log(array2)
								array.forEach(name=>{
									var index = array2.indexOf(name);
									console.log(index)
									if(index!=-1){
										var obj={
											name:name,
											check:true
										}
									}else{
										var obj={
											name:name,
											check:false
										}
									}
									
									arr.push(obj)
								})
								i.array=arr
							}else if(i.keyType==4){
							   var arr=[];
								for(var j=0;j<Number(i.keyValue) +1;j++){
									  arr.push(j)
								}
							   i.array=arr.reverse();
							   i.storeIndex=null;
							
							}else if(i.keyType==3){
								i.imgArr=i.itemValue.split(",")
							}
						})
					})
				})
				//console.log(this.info.categoryList)
				this.list=this.info.categoryList;
				this.goEdit()
			}
		},
		onUnload(){
			
		  /* uni.showModal({
		       title: '提示',
		       content: '这是一个模态弹窗',
		       success: function (res) {
		           if (res.confirm) {
		               console.log('用户点击确定');
		           } else if (res.cancel) {
		               console.log('用户点击取消');
		           }
		       }
		   }); */
		}
	}
</script>

<style scoped>
	.content{
		 background: #F4F5F7;
		 min-height: 100vh;
		
	}
	/* #ifdef H5 */
		.content{
			 background: #F4F5F7;
			 min-height:calc(100vh - 44px);
			
		}
	/* #endif */
	.paddingBox{
		padding: 20rpx 24rpx;
	}
	.top{
		background: #FFFFFF;
		border-radius: 10rpx;
	}
	.topLine{
		font-size: 28rpx;
		padding: 28rpx 20rpx;
		border-bottom: 1px solid #EEEEEE;
		display: flex;
	}
	.topLineTitle{
		width: 190rpx;
	}
	.topSpan{
		color: #999999;
	}
	.topJiantou{
		width: 14rpx;
		height: 26rpx;
		margin-top: 10rpx;
	}
	.topLineRIght{
	  display: flex;
	  justify-content: space-between;
	  width:470rpx ;
	}
	.topTime{
		width: 400rpx;
	}
	.klineImg{
		width: 70rpx;height: 70rpx;
	}
	.kbox{
		background: #FFFFFF;
		margin-top: 20rpx;
	}
	.kboxTitle{
		font-size:32rpx;
		font-weight: 600;
		color: #3C3C3C;
		padding: 30rpx 20rpx;
	}
	.kboxLineCont{
		display: flex;
		flex-wrap: wrap;
		padding-bottom: 20rpx;
	}
	.kline{
		width: 25%;
		text-align: center;
		padding: 20rpx 0;
		position: relative;
	}
	.klineXzImg{
		width: 28rpx;
		height: 28rpx;
		position: absolute;
		top: 70rpx;
		right: 40rpx;
	}
	.klineName{
		color: #3C3C3C;
		line-height: 36rpx;
	}
	.bottom{
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		box-shadow: 0px -4px 8px 0px rgba(153,153,153,0.08);
		position: fixed;
		left: 0;
		bottom: 0;
	}
	.bottomBtn{
		width: 702rpx;
		height: 74rpx;
		background: linear-gradient(124deg, #FF8700 0%, #FF4F00 100%);
		border-radius: 37rpx;
		text-align: center;
		line-height: 74rpx;
		color: #FFFFFF;
		font-size:30rpx;
		margin-top: 24rpx;
		margin-left: 24rpx;
	}
	.isRequired{
		color: #FF4F00;
	}
	.status_bar{
		background: #FFFFFF;
	}
	.nav{
		width: 100vw;
		height: 44px;
		background: #FFFFFF;
		display: flex;
		justify-content: space-between;
		font-size: 34rpx;
		line-height: 44px;
	}
	.baiheiback{
		width: 70px;
		height: 44px;
	}
	.zdyNav{
		position: fixed;
		left: 0;
		top: 0;
		background: #FFFFFF;
		z-index: 11;
	}
</style>
