<template>
	<view class="questionnaire">
		<form @submit="formSubmit" >
			<template v-for="(item,index) in list">
				<view v-if="item.type == 'radio'" class="margin-top padding sict-label"  :key="item.value">
					<view>
						<span style="color:red;display: inline-block; vertical-align:inherit;margin-right:4upx" v-if="item.required">*</span>
						{{index+1}}、 {{item.label}}
					</view>
					<view class="text-right  sict-value">
						<radio-group>
							<view>
								<view class="label" v-for="(opt, optIndex) in item.opts" :key="opt.label" @click="radioChange(opt.value,item.value)">
									<radio :class="form[item.value]== opt.value ?'checked':''" :checked="form[item.value]== opt.value?true:false"
									 :value="opt.value"></radio>
									<view style="display: inline-block;" class="right">{{opt.label}}</view>
								</view>
							</view>
						</radio-group>
					</view>
				</view>
				
				<template  v-else-if="item.type == 'picker'">
					<view class="cu-form-group margin-top padding-rl" :key="item.value">
						<view :class="item.titleClass"><span style="color:red;display: inline-block; vertical-align:inherit;margin-right:4upx" v-if="item.required">*</span>{{index+1}}、{{item.label}}</view>
						<picker @change="updatePickerVal($event,item.value)" :value="form[item.value]" :range="item.opts">
							<view class="picker">
								{{form[item.value]>-1?item.opts[form[item.value]]:'请选择'}}
							</view>
						</picker>
					</view>
				</template>
				
				<template  v-else-if="item.type == 'input'" >
					<view class="cu-form-group margin-top padding-rl" :key="item.value">
						<view :class="item.titleClass">
							<span style="color:red;display: inline-block; vertical-align:inherit;margin-right:4upx" v-if="item.required">*</span>
						{{index+1}}、{{item.label}}</view>
						<input  class="text-right" :placeholder="item.placeholder" :type="item.controType" :value="form[item.value]" name="input" @input="updateInputVal($event,item.value)"></input>
					</view>
				</template>
				
				<view class="margin-top-xs" v-show="item.child && childItem.show" v-for="(childItem,childIndex) in item.child" :key="childItem.value">
						<view class="margin-top-sm padding-rl sict-label cu-form-group">
							<view>
								<span style="color:red;display: inline-block; vertical-align:inherit;margin-right:4upx" v-if="childItem.required">*</span>
								{{childItem.title}}
							</view>
							<checkbox  v-if="childItem.type == 'radio'" :class="form[childItem.value]?'checked':''" :checked="form[childItem.value]?true:false" :value="childItem.value"></checkbox>
							
							<picker v-else-if="childItem.type == 'picker'" range-key="label"  @change="updatePickerVal($event,childItem.value)" :value="form[childItem.value]" :range="childItem.opts">
								<view class="picker">
									{{form[childItem.value]>-1?childItem.opts[form[childItem.value]].label:'请选择'}}
								</view>
							</picker>
							
							<input  class="text-right" v-else-if="childItem.type == 'input'" :type="childItem.controType" :placeholder="childItem.placeholder" :value="form[childItem.value]" name="input" @input="updateInputVal($event,childItem.value)"></input>
						</view>
				</view>
			</template>
			
			<view class="padding">
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg">提 交（ 3 / 3 ）</button>
			</view>
			
		</form>
	</view>
</template>

<script>
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	import validate from '@/utils/validate.js'
	import {
		getSampleId
	} from '@/utils/index.js'
	export default {
		components: {
			uniCollapse,
			uniCollapseItem
		},
		data() {
			return {
				id:'',
				radio:['食管癌','胃癌','结直肠肿瘤','炎症性肠病','其它器官恶性肿瘤（如乳腺、卵巢等其它器官）'],
				list:[
					{
						label:'一级亲属（父母、子女、兄弟姐妹）有无罹患胃癌情况',
						type:'radio',
						required:true,
						value:'relativesGastric',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						],
						child:[
							{
								title:'数量',
								show:false,
								required:true,
								value:'relativesGastricCount',
								titleClass:'title',
								type:'picker',
								opts:[{value:'a',label:'一位'},{value:'b',label:'多位'}]
							},
							{
								title:'诊断时最小年龄',
								show:false,
								required:true,
								controType:'number',
								value:'relativesGastricAge',
								titleClass:'title',
								placeholder:'请输入年龄',
								type:'input',
							}
						]
					},
					{
						label:'一级亲属罹患其它恶性肿瘤情况',
						type:'radio',
						required:true,
						value:'relativesCancer',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						],
						child:[
							{
								title:'数量',
								show:false,
								required:true,
								value:'relativesCancerCount',
								titleClass:'title',
								type:'picker',
								opts:[{value:'a',label:'一位'},{value:'b',label:'多位'}]
							},
							{
								title:'肿瘤名称',
								show:false,
								value:'relativesCancerName',
								required:true,
								titleClass:'title',
								placeholder:'请输入肿瘤名称',
								type:'input',
							}
						]
					},
					{
						label:'是否有萎缩性胃炎、胃溃疡、胃息肉、手术后残胃、肥厚性胃炎、恶性贫血等胃病史',
						titleClass:'title',
						required:true,
						type:'radio',
						value:'gaisticDiseaseHistory',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有幽门螺杆菌感染史',
						titleClass:'title',
						required:true,
						type:'radio',
						value:'hpInfection',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有高盐、腌制食物、吸烟、重度饮酒等不良饮食习惯',
						titleClass:'title',
						required:true,
						type:'radio',
						value:'habit',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
				],
				form:{
					"relativesGastric":"",
					"relativesGastricCount":-1,
					"relativesGastricAge":"",
					"relativesCancer":"",
					"relativesCancerCount":-1,
					"relativesCancerName":"",
					"gaisticDiseaseHistory":"",
					"hpInfection":"",
					"habit":""
				}
			}
		},
		
		onLoad(option) {
			this.id = option.id || ''
			if(option.id){
				this.getInfo(option.id)
			}
		},
		
		
		methods: {
			
			// 获取信息
			getInfo(id){
				this.$http.get(`/gastric-cancer-data/report/selectReportBySampleId?id=${id}`).then(res => {
					const d = res.data
					if(d.code == 1){
						this.form =  Object.assign(this.form,d.object.reportContent.data)    
						this.isShowChild()
					}
				}).catch(err => {
					console.log(err)
				})
			},
			
			radioChange(val,prop){
				this.$set(this.form,prop,val)
				this.isShowChild()
			},
			
			radioChange2(index, checkList, val) {
				for (var i = 0, lenI = checkList.length; i < lenI; ++i) {
					if (index == i) {
						this.$set(checkList[i], 'checked', !checkList[i].checked)
					}else{
						this.$set(checkList[i], 'checked', false)
					}
				}
			},
			
			isShowChild(){
				this.list[0].child[0].show = this.form['relativesGastric'] == 'a'
				this.list[0].child[1].show = this.form['relativesGastric'] == 'a'
				this.list[1].child[0].show = this.form['relativesCancer'] == 'a'
				this.list[1].child[1].show = this.form['relativesCancer'] == 'a'
			},
			
			Switch(e,prop){
				this.form[prop] = e.detail.value
			},
			// 更新输入框绑定的表单内容
			updateInputVal(event, props) {
				this.form[props] = event.target.value
			},
			// 更新下拉框绑定的表单内容
			updatePickerVal(event,props){
				this.$set(this.form, props, event.detail.value)
			},
			// 提交信息
			formSubmit(){
				let flag = true
				let msg = ''
				for (let i = 0; i < this.list.length; i++) {
					if(this.list[i].required){
						if(!this.form[this.list[i].value]){
							flag = false
							uni.showToast({
								icon: 'none',
								title: "第"+(i+1)+"项有必填数据未填写"
							})
							break
						}
						if(this.form[this.list[i].value] == 'a' && this.list[i].child && this.list[i].child.length>0){
							for (let j = 0; j < this.list[i].child.length; j++) {
								if(this.list[i].child[j].required && !this.form[this.list[i].child[j].value]){
									flag = false
									uni.showToast({
										icon: 'none',
										title: "第"+(i+1)+"大项的第"+ (j+1) +"子项有必填数据未填写"
									})
									break
								}
							}
						}
						if(!flag){
							break
						}
					}
				}
				
				if(!flag){
					return false
				}else{
					this.form.checkTime = getSampleId("","YYYY-MM-DD")
					let obj = {
						"id":this.id,
						"auditStatus": -3,
						"data":{
							"data": this.form,
							"diseaseType": this.$store.getters.userInfo.diseaseType,
							"isPrivate": this.$store.getters.userInfo.isPrivate
						}
					}
					
					this.$http.post(`/gastric-cancer-data/wechat/report`,obj).then(res => {
						const d = res.data
						if(d.code == 1){
							uni.showToast({
								icon: 'none',
								title: '信息提交后不可更改，同时请留意通知公告。'
							})
							// #ifdef MP-WEIXIN
							setTimeout(function() {
								wx.switchTab({
									url: '/pages/home/home',
								});
							}, 1000);
							// #endif
						}else{
							uni.showToast({
								icon: 'none',
								title: d.message
							})
						}
					}).catch(err => {
						console.log(err)
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sict-title {
		padding: 20upx 0;
		padding-right: 20upx;
		word-break: break-all;
		text-align: justify
	}

	.sict-label {
		background-color: white;
		font-size: 30upx;
		text-align: justify;
	}

	.sict-value {
		margin-top: 15upx;
		.right {
			margin-left: 10upx;
			vertical-align: bottom;
		}
		.label {
			// display: inline-block;
			text-align: left;
			padding: 20upx 0;
			padding-left: 30upx;
			border-bottom: 2upx solid #d3d3d3;
			&:last-child {
				border-bottom: 0;
			}
		}
	}

	uni-radio-group {
		border: 2upx solid #d3d3d3;
		border-radius: 8upx;
		margin-left: 20upx;
		display: block;
	}

	radio-group {
		border: 2upx solid #d3d3d3;
		border-radius: 8upx;
		margin-left: 20upx;
		display: block;
	}

	.icon-right {
		font-family: cuIcon;
		font-size: 34upx;
		color: #8799a3;
		line-height: 100upx;
		width: 60upx;
		text-align: right;
		margin: auto;
	}

	.uni-list-cell {
		.list-item {
			border-bottom: 2upx solid #d3d3d3;
			padding: 20upx 0;
			padding-left: 30upx;
		}

		&:last-child {
			.list-item {
				border-bottom: 0;
			}
		}
	}

	.uni-list {
		margin-left: 20upx;
		border: 2upx solid #d3d3d3;
		border-radius: 8upx;
	}
</style>

<style>
	.questionnaire .uni-collapse-cell__title:after {
		border-bottom: 0 !important;
	}
</style>
