<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:'',
				radio:['食管癌','胃癌','结直肠肿瘤','炎症性肠病','其它器官恶性肿瘤（如乳腺、卵巢等其它器官）'],
				list:[
					{
						label:'是否在上消化道癌发病率较高地区常住',
						type:'radio',
						required:true,
						value:'highIncidenceArea',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否有心、脑、肺、肝、肾等重要脏器疾病',
						type:'radio',
						required:true,
						value:'seriousIllness',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						]
					},
					{
						label:'是否正在服用抗凝血药物',
						type:'radio',
						required:true,
						value:'takeAnticoagulants',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						],
						child:[
							{
								title:'停药一周后凝血情况',
								show:false,
								required:true,
								value:'tempOneWeekCruor',
								titleClass:'title',
								type:'picker',
								opts:[
									{value:'a',label:'正常'},
									{value:'b',label:'异常'}
								]
							}
						]
					},
					{
						label:'是否具有上消化道内镜检查禁忌症',
						type:'radio',
						required:true,
						value:'haveGastricContraindication',
						opts:[
							{value:'a',label:'是'},
							{value:'b',label:'否'},
						],
						child:[
							{
								title:'上消化道内镜检查禁忌症(可多选)',
								show:false,
								required:true,
								value:'gastricContraindication',
								titleClass:'title',
								type:'morePicker',
								opts:[
									{
										value: 'a',
										label: '严重心脏病，心力衰竭'
									},
									{
										value: 'b',
										label: '重症呼吸道疾病，呼吸困难哮喘持续状态'
									},
									{
										value: 'c',
										label: '咽后壁脓肿，严重脊柱畸形，主动脉瘤患者'
									},
									{
										value: 'd',
										label: '身体虚弱不能耐受内镜检查者，难以镇静自控者'
									},
									{
										value: 'e',
										label: '上消化道腐蚀性炎症急性期，疑似上消化道穿孔者'
									},
									{
										value: 'f',
										label: '大量腹水，严重腹胀，重度食管曲张'
									},
									{
										value: 'g',
										label: '妊娠期妇女'
									},
									{
										value: 'h',
										label: '有碘过敏史者'
									},
									{
										value: 'i',
										label: '有出血倾向者（凝血功能不正常）'
									},
									{
										value: 'j',
										label: '有严重脑部疾病者'
									},
								]
							}
						]
					},
				],
				form:{
					"highIncidenceArea":"",
					"seriousIllness":'',
					"takeAnticoagulants":"",
					"oneWeekCruor":-1,
					"tempOneWeekCruor":-1,
					"haveGastricContraindication":'',
					"gastricContraindication":[],
				}
			}
		},
		
		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[2].child[0].show = this.form['takeAnticoagulants'] == 'a'
				
				this.list[3].child[0].show = this.form['haveGastricContraindication'] == 'a'
			},
			
			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(){
				this.form['oneWeekCruor'] = this.form['tempOneWeekCruor'] == 0 ? 'a' :this.form['tempOneWeekCruor'] == 1 ? 'b' :''
				this.form['gastricContraindication'] = []
				for (let i = 0, lenI = this.list[3].child[0].opts.length; i < lenI; ++i) {
					if (this.list[3].child[0].opts[i].checked) {
						this.form['gastricContraindication'].push(this.list[3].child[0].opts[i].value)
					}
				}
				
				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{
					if(this.form['haveGastricContraindication'] == 'b'){
						this.form['gastricContraindication'] = []
					}
					
					if(this.form['takeAnticoagulants'] == 'b'){
						this.form['oneWeekCruor'] = ''
					}
					
					if( this.form['seriousIllness'] == 'a' ||  this.form['highIncidenceArea'] == 'b' ||  this.form['oneWeekCruor'] == 'b' || this.form['gastricContraindication'].length>0){
						this.modalName = "DialogModal1"
						this.msg = "对不起，您不符合筛查标准，感谢您的参与！"
						this.saveData(-1,[])
					}else{
						this.saveData(-4,[])
					}
				}
			},
			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/standardSignUp/standardQuestionnaire/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>
