<!-- 检查内容设置-->
<template>
	<view class="page-wrap">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules"
					ref="wForm" errorType="border-bottom">
					<u-form-item required label="检查范围" prop="CHECKOBJECT_DESCRIPTION" borderBottom
						@click="handleShowSheet({title: '检查范围', name: 'CHECKOBJECT'})">
						<u--input disabledColor="#fff" v-model="model.CHECKOBJECT_DESCRIPTION" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>
					<u-form-item required label="检查内容" prop="CHECKCONTENT" borderBottom>
						<u--textarea autoHeight v-model="model.CHECKCONTENT" placeholder="检查内容"
							border="surround"></u--textarea>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>

		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<view class="sub-form-btns">
						<view class="sub-form-btn" @click="handleAdd">
							<u-icon class="icon" name="plus-circle" color="#3c9cff" size="21"></u-icon>
							<u--text type="primary" text="新增"></u--text>
						</view>
					</view>

					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<uni-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for="(item, index) in model.Nav_ListCheckQuestion">
								<uni-collapse-item :title="item.NUM + '. ' + item.DESCREPTION" name="1">
									<view slot="value" class="row-action">
										<text @click.stop="DetailDel(item)" class="enableTrue">
											<u--text type="primary" class="btnCheck" text="删除"></u--text>
										</text>
									</view>

									<u-form-item class="subRequired" required label="检查问题描述" prop="DESCREPTION"
										borderBottom>
										<u--textarea autoHeight v-model="item.DESCREPTION" placeholder="检查问题描述"
											border="surround"></u--textarea>
									</u-form-item>

									<u-form-item class="subRequired" required label="整改建议与措施" prop="DEMAND"
										borderBottom>
										<u--textarea autoHeight v-model="item.DEMAND" placeholder="整改建议与措施"
											border="surround"></u--textarea>
									</u-form-item>
									<u-form-item class="subRequired" required label="问题等级" prop="QUESTION_LEVEL_SHOW"
										borderBottom
										@click="handleShowSheet({title: '问题等级', name: 'QUESTION_LEVEL',itemData:item})">
										<u--input disabledColor="#ffffff" v-model="item.QUESTION_LEVEL_SHOW"
											border="none" inputAlign="right"></u--input>
										<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
									</u-form-item>
								</uni-collapse-item>
							</uni-card>
						</uni-collapse>
					</u--form>

				</view>
			</view>
		</u-sticky>

		<u-modal :show="showDelModalIndex >= 0" @confirm="confirmDel" title="确认删除？"></u-modal>

		<query-selector :multiple="selectorInfo.isMultiple" :show="selectorInfo.showPopup"
			:defaultChecked="selectorInfo.defaultChecked" :title="selectorInfo.title" :lists="selectorInfo.dataLists"
			@search="handleSearch" @select="handleSelected" @close="selectorInfo.showPopup=false" />

		<view class="bottom-button">
			<button type="primary" @click="FullCheckUpdate">提交</button>
		</view>

	</view>
</template>

<script>
	import {
		guid,
		initFilter
	} from '../../../../utils/common'

	import {
		FullUpdateContents
	} from '../../../../services/apply/subPages/BS/safeCheck'

	import {
		getEnum
	} from '../../../../services/common'

	import {
		listBSQuestionLevelEnum,
		listBSQuestionLevelEnumVal
	} from '../../../../utils/enums'

	export default {
		data() {
			return {
				indexShow: 10, //懒加载信息
				total: 0,
				tableKey: '0',
				TaskID: '',
				listPropUpload: ['SAFE_CHECK_ID'],
				listPropValUpload: [],
				AUDIT_OPINION: '',
				ORG_ID: '',
				modelEdit: {},
				model: {
					ID: '',
					ORG_ID: '',
					CHECKOBJECT_DESCRIPTION: '',
					CHECKOBJECT: '',
					CHECKCONTENT: '',
					//操作需要同时操作 Nav_ListSafeCheckDetailShow
					Nav_ListCheckQuestion: [{
						ORG_ID: '',
						CHECK_CONTENTS_ID: '',
						DESCREPTION: '',
						DEMAND: '',
						QUESTION_LEVEL: '',
						QUESTION_LEVEL_SHOW: '',
						NUM: '1',
					}] //子表
				},
				isAudit: true,
				lists: [],
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
					value: ''
				},
				selectorInfo: {
					isSubShow: false,
					isMultiple: false,
					showPopup: false,
					title: '检查人员',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},

				showDelModalIndex: undefined,
				rules: {
					'CHECKOBJECT_DESCRIPTION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'CHECKCONTENT': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
			}

		},
		onLoad(option) {
			this.model.ID = guid()

			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey
			this.ORG_ID = uni.getStorageSync('orgId')
			this.model.ORG_ID = uni.getStorageSync('orgId')
		},
		methods: {
			handleAdd() {
				if (this.model.Nav_ListCheckQuestion == null) {
					this.model.Nav_ListCheckQuestion = []
				}

				var NUM = this.model.Nav_ListCheckQuestion.length
				this.model.Nav_ListCheckQuestion.unshift({
					ID: guid(),
					ORG_ID: this.ORG_ID,
					NUM: NUM + 1,
					CHECK_CONTENTS_ID: this.model.ID,
					DESCREPTION: '',
					DEMAND: '',
					QUESTION_LEVEL: '',
					QUESTION_LEVEL_SHOW: '',
				})
			},

			//手动删除信息 编号修改
			DetailDel(item) {
				//不要直接删除 比如已经保存过，直接删除提交的时候不操作数据库
				item.IS_DELETED = true
				let indexNotDel = 1
				// this.ListApplyDetail = []
				this.model.Nav_ListCheckQuestion.forEach((e, i) => {
					if (!e.IS_DELETED) {
						e.ROW_NO = indexNotDel
						// this.ListApplyDetail.push(e)
						indexNotDel++
					}
				})
				
			},
			confirmDel() {
				this.model.Nav_ListCheckQuestion.splice(this.showDelModalIndex, 1)
				this.showDelModalIndex = undefined
			},
			handleTab(t) {
				this.currentTab = t
			},
			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					if (this.model.Nav_ListCheckQuestion == null || this.model.Nav_ListCheckQuestion.length < 1) {
						uni.$showErrorInfo('检查问题不能为空');
					} else {

						let index = 0
						var isOK = true
						this.model.Nav_ListCheckQuestion.forEach(item => {
							if (item.ORG_ID == null||item.ORG_ID =='')
								item.ORG_ID = this.ORG_ID
								
							item.CHECK_CONTENTS_ID = this.model.ID
							item.SERIOUS_RISK = (item.QUESTION_LEVEL == 10 ? 1 : 0)

							index++
							if (item.DESCREPTION == null || item.DESCREPTION == '') {
								isOK = false
								uni.$showErrorInfo('第【' + index + '】项检查问题描述不能为空');
							}
							if (isOK && (item.DEMAND == null || item.DEMAND == '')) {
								isOK = false
								uni.$showErrorInfo('第【' + index + '】项整改建议与措施不能为空');
							}
							if (isOK && (item.QUESTION_LEVEL == null || item.QUESTION_LEVEL == '')) {
								isOK = false
								uni.$showErrorInfo('请选择第【' + index + '】项的问题等级');
							}
						})

						if (isOK) {
							FullUpdateContents(this.model).then(res => {
								uni.$showMsgFunc('操作成功!', () => {
										uni.navigateBack()
								}, 'success', 1000)
							})
						}
					}
				}).catch(err => {
					uni.$showErrorInfo('校验失败');
				})
			},

			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				let dataSelect = []
				var defaultChecked = []

				if (p.name == 'CHECKOBJECT') {
					this.selectorInfo.isMultiple = false
					var dataParm = {
						'name': "BSMineTypeEnum"
					}
					dataSelect = await getEnum(dataParm).then(res => {
						if (res != undefined && res.length > 0) {
							for (let i = 0; i < res.length; i++) {
								res[i].name = res[i].NAME
								// res[i].code = res[i].ID
							}
						}
						return res
					})
				} else if (p.name == 'QUESTION_LEVEL') {
					this.selectorInfo.isMultiple = false
					dataSelect = []
					for (let i = 0; i < listBSQuestionLevelEnum.length; i++) {
						dataSelect.push({
							name: listBSQuestionLevelEnum[i],
							ID: listBSQuestionLevelEnumVal[i]
						})
					}
				}

				if (defaultChecked == null)
					defaultChecked = []
				if (dataSelect.length) {
					this.selectorInfo = {
						itemData: p.itemData,
						isMultiple: this.selectorInfo.isMultiple,
						showPopup: true,
						title: p.title,
						name: p.name,
						dataLists: dataSelect,
						defaultChecked: defaultChecked
					}
				} else {
					// 暂无数据
				}
			},
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == "CHECKOBJECT") {
					this.model.CHECKOBJECT_DESCRIPTION = e.NAME
					this.model.CHECKOBJECT = e.ID
				} else if (this.selectorInfo.name == 'QUESTION_LEVEL') {
					this.selectorInfo.itemData.QUESTION_LEVEL = e.ID
					this.selectorInfo.itemData.QUESTION_LEVEL_SHOW = e.name
				}
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			//数据加载
			handleSearch(val) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val
				}
				this.handleShowSheet(p)
			},
		},
		computed: {},
		onReachBottom() {
			if (this.indexShow < this.total) {
				var end = this.indexShow + 10
				if (end >= this.total)
					end = this.total

				var tampAdd = this.model.Nav_ListCheckQuestion.slice(this.indexShow, end)
				this.Nav_ListSafeCheckDetailShow = this.Nav_ListSafeCheckDetailShow.concat(tampAdd)
				// this.$forceUpdate()
				this.indexShow = end
			}
		},
	}
</script>


<style scoped>
	@import url("../../../../style/css/editTemplate.css");
</style>