<!-- 手动检查登记 -->
<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="RiskAreaNAME"
						@click="handleShowSheet({title: '检查区域', name: 'RiskAreaNAME'})" borderBottom>
						<u--input disabledColor="#fff" v-model="model.RiskAreaNAME" 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" color="#2979ff" size="14"></u-icon>
							<u--text type="primary" text="检查信息"></u--text>
						</view>
					</view>
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm"><!-- :model="subDataModel" -->
						<uni-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for=" item  in model.Nav_ListCheckDetail">
								<uni-collapse-item :title="item.ROW_NO+ '. ' + item.Nav_CheckProject.NAME">
									<u-form-item class="subRequired" required label="检查项目" prop="Nav_CheckProject.NAME"
										@click="handleShowSheet({title: '检查项目', name: 'CheckProjectNAME',itemData:item})"
										borderBottom>
										<u--input disabledColor="#fff" v-model="item['Nav_CheckProject'].NAME"
											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 class="subRequired" required label="检查项目分类"
										prop="Nav_CheckProjectCategory.NAME"
										@click="handleShowSheet({title: '检查项目分类', name: 'CheckProjectCategoryNAME',itemData:item})"
										borderBottom>
										<u--input disabledColor="#fff" v-model="item['Nav_CheckProjectCategory'].NAME"
											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 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>

									<u-form-item class="subRequired" required label="隐患地点" prop="ADDRESS" borderBottom>
										<u--input disabledColor="#fff" placeholder='隐患地点' v-model="item.ADDRESS"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="隐患原因" prop="listCheckDetailReason" borderBottom
										@click="handleShowSheet({title: '问题等级', name: 'listCheckDetailReason',itemData:item})">
										<!-- @click="handleChange(item)" -->
										<u--input disabledColor="#fff" v-model="item.listCheckDetailReason"
											border="none" inputAlign="right"></u--input>
										<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
									</u-form-item>
									<view class="upload-title">附件</view>
									<full-upload v-model="item.Nav_Files" :isShowBtn='true'
										:listProp='listPropSubUpload'
										:listPropVal='item.listPropValSubUpload'></full-upload>
								</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="handleShowSheet" @select="handleSelected" @close="selectorInfo.showPopup=false" />

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

	</view>
</template>

<script>
	import {
		guid,
		initFilter,
		extendRule,
		extendInclude,
		extendGroupRule,
		initFilterGroup,
		extendFilterGroup,
		extendFilterGroupGroupRules
	} from '../../../../utils/common'

	import {
		AddCheckRecord,
		CheckTypeOrderPaged,
		CheckTypeLevelOrderPaged,
		OrderPagedAreaMineType,
		CheckProjectOrderPaged,
		OrderPagedProjectCategory,
		OrderPagedSafeCheckDetail,
		OrderPagedSuitAll,
		orderPagedRiskReason
	} from '../../../../services/apply/subPages/BS/safeCheck'

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

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

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				listPropSubUpload: ['SAFE_CHECK_ID', 'SAFE_CHECK_DETAIL_ID'],
				listPropValUpload: [],
				AUDIT_OPINION: '',

				model: {
					ID: '',
					ORG_ID: '',
					SAFE_CHECK_ID: '',
					DEPARTMENT_ID: '',
					CHECKOBJECT: '',
					CHECKOBJECT_DESCRIPTION: '',
					RISK_AREA_ID: '',
					RiskAreaNAME: '',
					CHECK_TYPE_ID: '',
					CHECK_TYPE_LEVEL_ID: '',
					ROW_NO: '',
					CHECKCONTENT: '',
					Nav_ListUserCheck: [],
					Nav_ListCheckDetail: [{
						DESCREPTION: '',
						DEMAND: '',

						ID: '',
						ORG_ID: '',
						SAFE_CHECK_ID: '',
						QUESTION_LEVEL: '',
						ROW_NO: '1',
						CHECK_PROJECT_ID: '',
						Nav_CheckProject: {
							NAME: ''
						},
						CHECK_PROJECT_CATEGORY_ID: '',
						Nav_CheckProjectCategory: {
							NAME: ''
						},
						ADDRESS: '',
						Nav_ListCheckDetailReason: [],
						Nav_Files: [],
						listPropValSubUpload: []
					}],
				},
				isAudit: true,
				lists: [],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},

				selectorInfo: {
					showPopup: false,
					isMultiple: false,
					title: '隐患原因',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},

				showCheckDate: false,
				showPopup: false,
				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 = option.ID
			this.model.ORG_ID = uni.getStorageSync('orgId')
			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey

			this.model.ROW_NO = option.ROW_NO
			this.model.SAFE_CHECK_ID = option.SAFE_CHECK_ID
			this.model.CHECKOBJECT = option.CHECKOBJECT
			this.model.CHECK_TYPE_ID = option.CHECK_TYPE_ID
			this.model.DEPARTMENT_ID = option.DEPARTMENT_ID
			this.model.CHECK_TYPE_LEVEL_ID = option.CHECK_TYPE_LEVEL_ID
			this.model.Nav_ListUserCheck = JSON.parse(decodeURIComponent(option.Nav_ListUserCheck))

			this.model.Nav_ListCheckDetail[0].ROW_NO = parseInt(option.ROW_NO) + 1
			this.model.Nav_ListCheckDetail[0].SAFE_CHECK_ID = this.model.SAFE_CHECK_ID
			this.model.Nav_ListCheckDetail[0].ID = guid()
			this.model.Nav_ListCheckDetail[0].ORG_ID = this.model.ORG_ID

			this.model.Nav_ListCheckDetail[0].listPropValSubUpload.push(this.model.ID)
			this.model.Nav_ListCheckDetail[0].listPropValSubUpload.push(this.model.Nav_ListCheckDetail[0].ID)

		},
		methods: {
			handleOkRow() {
				console.log('ok row')
			},
			handleDelRowBefore(index) {
				this.showDelModalIndex = index
			},
			confirmDel() {
				this.model.Nav_ListSafeCheckDetail.splice(this.showDelModalIndex, 1)
				this.showDelModalIndex = undefined
			},
			handleAdd() {
				if (this.model.Nav_ListCheckDetail == null) {
					this.model.Nav_ListCheckDetail = []
				}
				var NUM = parseInt(this.model.ROW_NO) + this.model.Nav_ListCheckDetail.length + 1

				var detailIDAdd = guid()
				var detailTemp = {
					ID: detailIDAdd,
					ORG_ID: this.model.ORG_ID,
					SAFE_CHECK_ID: this.model.SAFE_CHECK_ID,
					QUESTION_LEVEL: '',
					ROW_NO: NUM,
					CHECK_PROJECT_ID: '',
					Nav_CheckProject: {
						NAME: ''
					},
					CHECK_PROJECT_CATEGORY_ID: '',
					Nav_CheckProjectCategory: {
						NAME: ''
					},
					ADDRESS: '',
					Nav_ListCheckDetailReason: [],
					Nav_Files: [],
					listPropValSubUpload: [],
				}
				detailTemp.listPropValSubUpload.push(this.model.ID)
				detailTemp.listPropValSubUpload.push(detailIDAdd)

				this.model.Nav_ListCheckDetail.unshift(detailTemp)
			},
			handleTab(t) {
				this.currentTab = t
			},
			FullCheckUpdate() {
				const ele = this.$refs
				if (this.model.Nav_ListCheckDetail == null || this.model.Nav_ListCheckDetail.length < 1) {
					uni.$showErrorInfo('请新增检查信息后再提交');
				} else if (this.model.Nav_ListCheckDetail[0].DESCREPTION == '' || this.model.Nav_ListCheckDetail[0]
					.DEMAND == '') {
					uni.$showErrorInfo('请完善检查信息');
				} else {
					ele['wForm'].validate().then(res => {
						this.model.Nav_ListCheckDetail.forEach(e => {
							e.Nav_CheckProject = null;
							e.Nav_CheckProjectCategory = null
							if (e.Nav_ListCheckDetailReason != null && e.Nav_ListCheckDetailReason.length >
								0) {
								e.Nav_ListCheckDetailReason.forEach(ec => {
									ec.Nav_RiskReason = null
								})
							}
						})
						AddCheckRecord(this.model).then(res => {

							uni.$showMsgFunc('操作成功!', () => {
								var linkToUrl = 'safeCheckSign?tableKey=' + this.tableKey +
									'&ID=' + this.model.ID + '&taskID=' + this.TaskID //Url 传参
								uni.navigateTo({
									url: linkToUrl
								})
							}, 'success', 1000)
						})
					}).catch(err => {
						console.log('校验失败', err)
					})
				}
			},

			// 下拉数据加载
			async handleShowSheet(p) {
				if (p.name == undefined) {
					// 搜索
					var val = p
					p = this.selectorInfo
					p.val = val
				}

				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				let dataSelect = []
				var defaultChecked = []

				if (p.name == 'RiskAreaNAME') {
					this.selectorInfo.isMultiple = false
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					// this.selectorInfo.isMultiple = true
					json.FilterGroup.Groups.push({
						'IsAnd': false,
						'Rules': [{
							'Field': "CHECKOBJECT",
							'Operate': 1,
							'Value': this.model.CHECKOBJECT
						}]
					})
					json.Sort = 'NAME'
					// extendRule(json, 'CHECKOBJECT', 1, this.model.CHECKOBJECT);
					dataSelect = await OrderPagedAreaMineType(json).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].CODE
							}
						}
						return res
					})
				} else if (p.name == 'CheckProjectNAME') {
					this.selectorInfo.isMultiple = false
					extendRule(json, 'ENABLE_STATUS', 1, 0);
					extendRule(json, 'IS_DELETED', 1, 'false');
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					dataSelect = await CheckProjectOrderPaged(json).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].CODE
							}
						}
						return res
					})
				} else if (p.name == 'CheckProjectCategoryNAME') {
					if (this.model.CHECKOBJECT == null || this.model.CHECKOBJECT.length < 1) {
						uni.$showErrorInfo('请先选择检查范围！')
						return false
					} else if (this.model.CHECK_TYPE_ID == null || this.model.CHECK_TYPE_ID.length < 1) {
						uni.$showErrorInfo('请先选择检查类型！')
						return false
					} else {
						this.selectorInfo.isMultiple = false
						extendRule(json, 'Nav_ListCategoryType.CHECK_TYPE_ID', 1, this.model.CHECK_TYPE_ID);
						extendRule(json, 'CHECKOBJECT', 1, this.model.CHECKOBJECT);
						if (p.val) {
							extendRule(json, 'NAME', 9, p.val);
						}
						dataSelect = await OrderPagedProjectCategory(json).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].CODE
								}
							}
							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]
						})
					}
				} else if (p.name == 'listCheckDetailReason') {
					this.selectorInfo.isMultiple = true
					dataSelect = await orderPagedRiskReason(json).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].CODE
							}
						}
						return res
					})
				}

				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 {
					// 暂无数据
				}
			},

			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			//选择信息
			handleSelected(e) {
				this.selectorInfo.showPopup = false

				switch (this.selectorInfo.name) {
					case 'QUESTION_LEVEL':
						this.selectorInfo.itemData.QUESTION_LEVEL = e.ID
						this.selectorInfo.itemData.QUESTION_LEVEL_SHOW = e.name
						break;
					case 'RiskAreaNAME':
						this.model.RISK_AREA_ID = e.ID
						this.model.RiskAreaNAME = e.name
						break;
					case 'CheckProjectNAME':
						this.selectorInfo.itemData.CHECK_PROJECT_ID = e.ID
						this.selectorInfo.itemData.Nav_CheckProject = e
						break;
					case 'CheckProjectCategoryNAME':
						this.selectorInfo.itemData.CHECK_PROJECT_CATEGORY_ID = e.ID
						this.selectorInfo.itemData.Nav_CheckProjectCategory = e
						break;
					case "listCheckDetailReason":
						let listReason = []
						let reasonShow = ''
						if (e.length > 0) {
							e.forEach((item, i) => {
								if (i > 0) {
									reasonShow += ' '
								}
								listReason.push({
									ID: guid(),
									CHECK_DETAIL_ID: this.selectorInfo.itemData.ID,
									RISK_REASON_ID: item.ID,
									ORG_ID: item.ORG_ID,
									Nav_RiskReason: e[i]
								})
								reasonShow += item.NAME
							})
						}
						this.selectorInfo.itemData.Nav_ListCheckDetailReason = listReason
						this.selectorInfo.itemData.listCheckDetailReason = reasonShow
						break;
					default:
						break;
				}
			}
		},
		computed: {},
	}
</script>


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