<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 v-if="childItem.type != 'morePicker'">
								<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>
							
							<view v-else-if="childItem.type == 'morePicker'">
								<view class="margin-top">
									<uni-collapse>
										<uni-collapse-item :title="childItem.title" :open="true" :required="childItem.required">
											<view>
												<view class="uni-list">
													<checkbox-group>
														<label class="uni-list-cell uni-list-cell-pd" v-for="(itemCheck,indexCheck) in childItem.opts" :key="itemCheck.value"
														 @click="checkboxChange(indexCheck,childItem.opts,itemCheck,itemCheck.value)">
															<view class="list-item">
																<view class="fl">
																	<checkbox :value="itemCheck.value" :checked="itemCheck.checked" />
																</view>
																<view style="padding-left:60upx">{{itemCheck.label}}</view>
															</view>
														</label>
													</checkbox-group>
												</view>
											</view>
										</uni-collapse-item>
									</uni-collapse>
								</view>
							</view>
							
							<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">提 交（ 2 / 3 ）</button>
			</view>
		</form>
		<view class="cu-modal" :class="modalName=='DialogModal1'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">提示消息</view>
					<view class="action" @tap="modalName = ''">
						<text class="cuIcon-close text-black"></text>
					</view>
				</view>
				<view class="padding-xl">
					{{msg}}
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-gray" @tap="modalName = ''">取消</button>
						<button class="cu-btn line-blue text-blue margin-left" @tap="hideModal">确定</button>
					</view>
				</view>
			</view>
		</view>
	</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'
	export default {
		components: {
			uniCollapse,
			uniCollapseItem
		},
		data() {
			return {
				id:'',
				modalName:'',
				msg:'',
				list:[
					{
						label:'距上次内镜检查间隔时间一年内',
						type:'radio',
						required:true,
						value:'withinOneYear',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有胃大部切除术手术史',
						type:'radio',
						required:true,
						value:'subtotalGastrectomy',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'两周内服用PPI（埃索美拉唑、泮托拉唑、雷贝拉唑、兰索拉唑等）或H2受体拮抗剂（法莫替丁等)',
						type:'radio',
						required:true,
						value:'ppi',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						],
					},
					{
						label:'有下列一种或多种症状:1.6个月内缺铁性贫血  2.12个月内消化道出血（黑便、血便等）  3.体重减轻（非减肥原因6个月内体重减轻≥4.5Kg）  4.经常性腹泻  5.吞咽困难或哽噎感  6.腹部肿块',
						type:'radio',
						required:true,
						value:'symptom',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有以下任何一种疾病史：食管癌、胃癌、结直肠肿瘤、息肉病、炎症性肠病、其它器官恶性肿瘤（乳腺、卵巢、子宫及泌尿系统等）',
						type:'radio',
						required:true,
						value:'diseasesHistory',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有影像、肿瘤标志物等检查高度怀疑罹患肿瘤可能',
						type:'radio',
						required:true,
						value:'possibilityOfCancer',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有严重心、肺、肝、肾功能不全，或严重精神疾患',
						type:'radio',
						required:true,
						value:'seriousIllness',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
				],
				form:{
					"withinOneYear":-1,
					"subtotalGastrectomy":-1,
					"ppi":-1,
					"symptom":-1,
					"diseasesHistory":-1,
					"possibilityOfCancer":-1,
					"seriousIllness":-1
				}
			}
		},
		
		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(){
				
			},
			
			Switch(e,prop){
				this.form[prop] = e.detail.value
			},
			// 更新输入框绑定的表单内容
			updateInputVal(event, props) {
				this.form[props] = event.target.value
			},
			// 更新复选框绑定的表单内容
			checkboxChange(index, checkList, obj, prop) {
				for (var i = 0, lenI = checkList.length; i < lenI; ++i) {
					if (index == i) {
						this.$set(checkList[i], 'checked', !checkList[i].checked)
					}
				}
			},
			// 更新下拉框绑定的表单内容
			updatePickerVal(event,props){
				this.$set(this.form, props, event.detail.value)
			},
			hideModal(){
				this.modalName = ''
			},
			// 提交信息
			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{
					let flagSubmit = true
					for (const key in this.form) {
						if(this.form[key] == 'a' && key != 'agree'){
							flagSubmit = false
							break
						}
					}
					if(flagSubmit){
						this.saveData(-4,[])
					}else{
						this.modalName = "DialogModal1"
						this.msg = "对不起，您不符合筛查标准，感谢您的参与！"
						this.saveData(-1,[])
					}
				}
			},
			saveData(code,illegalReason){
				let obj = {
					"id":this.id,
					"auditStatus": code,
					"illegalReason":illegalReason,
					"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 && code != -1){
						uni.navigateTo({
							url: `/personModule/vanguardSignUp/vanguardQuestionnaire/index?id=${this.id}`
						})
					}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>
