<template>
	<view class="todo-page">
		<uni-card margin="0" :is-shadow="true">
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" ref="wForm"
				:rules="rules">
				<u-form-item label="审核意见：" prop="AUDIT_OPINION" borderBottom v-if="model.STATUS==9">
				</u-form-item>
				<u--textarea v-html="AUDIT_OPINION" border="surround" v-if="model.STATUS==9" disabled></u--textarea>
				<u-form-item label="层级：" prop="LEVEL_SHOW" borderBottom>
					<u--input v-model="model.LEVEL_SHOW" border="none" slot="right" inputAlign="right" disabled
						disabledColor="fff"></u--input>
				</u-form-item>
				<u-form-item label="开始时间：" prop="START_TIME" borderBottom>
					<u--input v-model="model.START_TIME" border="none" slot="right" inputAlign="right" disabled
						disabledColor="fff"></u--input>
				</u-form-item>
				<u-form-item label="结束时间：" prop="END_TIME" borderBottom>
					<u--input v-model="model.END_TIME" border="none" slot="right" inputAlign="right" disabled
						disabledColor="fff"></u--input>
				</u-form-item>
				<u-form-item label="辨识区域：" prop="Areas" borderBottom>
				</u-form-item>
				<u--textarea autoHeight v-html="model.Areas" border="none" inputAlign="right" disabled
					disabledColor="#fff"></u--textarea>
				<view class="upload-title">附件：</view>
				<full-upload v-model="model.Nav_Files" :isShowBtn='true' :listProp='listPropUpload'
					:listPropVal='listPropValUpload'></full-upload>
			</u--form>
		</uni-card>
		<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="handleAddDetail">
							<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">
						<u-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for="(item, index) in model.Nav_Details.filter(i => !i.IS_DELETED)">
								<u-collapse-item :title="index + 1 + '. ' + item.Nav_Area.NAME">
									<view slot="right-icon" class="row-action"></view>
									<view slot="value" class="row-action">
										<text @click="handleDelRowBefore(index,item)">
											<u--text type="error" text="删除"></u--text>
										</text>
									</view>
									<u-form-item label="区域：" prop="Nav_Area" borderBottom
										@click="handleChange('Nav_Area',item)">
										<u--input v-model="item['Nav_Area'].NAME" placeholder="请选择区域" border="none"
											inputAlign="right" disabled disabledColor="#fff"></u--input>
										<u-icon style="margin-left: 4px;" slot="right" name="arrow-down">
										</u-icon>
									</u-form-item>
									<u-form-item label="职业危害名称：" prop="Nav_Identifying.NAME" borderBottom
										@click="handleShowSheet({title: '职业危害名称', name: 'Identifying', dataIndex: index})">
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Identifying.NAME"
											placeholder="请选择职业危害名称" 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 label="职业危害位置：" prop="HAZARD_POSITION" borderBottom>
										<u--input v-model="item.HAZARD_POSITION" border="none" inputAlign="right"
											placeholder="请填写职业危害位置"></u--input>
									</u-form-item>
									<u-form-item label="暴露时间：" prop="EXPOSURE_TIME" borderBottom>
										<u--input v-model="item.EXPOSURE_TIME" border="none" inputAlign="right"
											placeholder="请填写暴露时间"></u--input>
									</u-form-item>
									<u-form-item label="暴露限值：" prop="EXPOSURE_LIMIT_VALUE" borderBottom>
										<u--input v-model="item.EXPOSURE_LIMIT_VALUE" border="none" inputAlign="right"
											placeholder="请填写数值+单位"></u--input>
									</u-form-item>
									<u-form-item label="暴露人数：" prop="EXPOSURE_NUMBER" borderBottom>
										<u--input v-model="item.EXPOSURE_NUMBER" border="none" inputAlign="right"
											type="number" placeholder="请填写暴露人数"></u--input>
									</u-form-item>
									<u-form-item label="可能造成的职业风险：" prop="Nav_OccupationalRisk.NAME" borderBottom
										@click="handleShowSheet({title: '可能造成的职业风险', name: 'OccupationlRisk', dataIndex: index})">
										<u--input disabled disabledColor="#fff" v-model="item.Nav_OccupationalRisk.NAME"
											placeholder="请选择可能造成的职业风险" 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 label="状态：" prop="HAZARD_STATUS_SHOW" borderBottom
										@click="handleShowSheet({title: '状态', name: 'hazardStatus', dataIndex: index})">
										<u--input disabled disabledColor="#fff" v-model="item.HAZARD_STATUS_SHOW"
											placeholder="请选择状态" border="none" inputAlign="right"></u--input>
										<u-icon style="margin-left: 4px;" slot="right" name="arrow-down">
										</u-icon>
									</u-form-item>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<u-modal :show="showDelModalIndex >= 0" @confirm="confirmDel" @cancel="cancelDel" showCancelButton
			title="确认删除？"></u-modal>
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME" :defaultValue="0"></u-picker>
		<query-selector :show="showPopup" :lists="Lists" @close="handleClosePopup" @search="handleSearch"
			@select="handleSelected" />
		<view class="bottom-button">
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

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

	import {
		getAreaLists,
		getRiskRecordRefus,
		getIdentifying,
	} from '../../../../services/apply/HMServices/HMServices'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices.js'
	import '../../../../utils/showMsg.js'
	import color from '../../../../uni_modules/uview-ui/libs/config/color'
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				listPropUpload: ['RECORD_ID'],
				listPropValUpload: [],
				model: {
					Nav_LiableUser: {
						NAME: "",
					},
					Nav_Details: [],
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				rules: {
					'LEVEL': {
						type: 'string',
						required: false,
						trigger: ['blur', 'change']
					}
				},
				Lists: [],
				showPopup: false,
				TaskID: "",
				tableKey: '0',
				isLoadOK: false,
				AUDIT_OPINION: "",
				showDelModalIndex: undefined,
				ORG_ID: uni.getStorageSync('orgId'),
			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			this.tableKey = option.tableKey ? option.tableKey : '0';
			this.loadData();
		},
		methods: {
			handleDelRowBefore(index, item) {
				if (item.AREA_ID == "" || item.AREA_ID == undefined) {
					this.model.Nav_Details.splice(this.showDelModalIndex, 1)
				} else {
					this.showDelModalIndex = index
				}
			},
			cancelDel() {
				this.showDelModalIndex = undefined
			},
			confirmDel() {
				this.model.Nav_Details[this.showDelModalIndex].IS_DELETED = true;
				this.showDelModalIndex = undefined
			},

			handleClosePopup() {
				this.showPopup = false
			},
			handleAddDetail() {
				this.model.Nav_Details.unshift({
					ID: guid(),
					RECORD_ID: this.model.ID,
					Nav_User: {
						NAME: ""
					},
					Nav_Area: {
						NAME: ""
					},
					Nav_Identifying: {
						NAME: ""
					},
					Nav_OccupationalRisk: {
						NAME: ""
					},
					HAZARD_STATUS: 0,
					HAZARD_STATUS_SHOW: "有效",
					HAZARD_POSITION: "",
					AREA_ID: "",
					ORG_ID: this.ORG_ID,
					IS_DELETED: false,
					MODIFY_TYPE: 2
				})
			},
			handleChange(title, item) {
				this.currentOperateArea = item
				this.showPopup = true
				this.handleSearch('init');
			},
			handleSearch(val) {
				const json = initFilter(this.ORG_ID, "", "NAME")
				json.Limit = 20
				if (val !== 'init') {
					const tempGroup = initFilterGroup(false);
					extendGroupRule(tempGroup, 'NAME', 9, val)
					extendFilterGroup(json, tempGroup);
				}
				getAreaLists(json).then(res => {
					if (res.IsSuccessful) {
						this.Lists = res.Data.map(i => {
							return {
								...i,
								name: i.NAME,
							}
						})
					}
				})
			},
			handleSelected(val) {
				this.showPopup = false
				this.Lists = []
				this.currentOperateArea.Nav_Area = val;
				this.currentOperateArea.AREA_ID = val.ID;
			},
			loadData() {
				const json = initFilter(this.ORG_ID, "", "CODE", 1)
				if (this.listPropValUpload.length == 0) {
					this.listPropValUpload.push(this.model.ID)
				}
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, 'Nav_Files.Nav_ImgFile');
				extendInclude(json, 'Nav_Details.Nav_Area');
				extendInclude(json, 'Nav_Details.Nav_Identifying');
				extendInclude(json, 'Nav_Details.Nav_OccupationalRisk');
				extendInclude(json, 'Nav_Users.Nav_Area');
				extendInclude(json, 'Nav_Users.Nav_User');
				getRequest(json, "/HM/HazardRecord/Get").then(res => {
					this.model = res
					if (res.STATUS == 9) {
						const json = initFilter(this.ORG_ID);
						json.ID = this.model.ID;
						getRiskRecordRefus(json).then(refusRes => {
							this.AUDIT_OPINION = refusRes;
						})
					}
					//数据组装
					let strAreas = ''
					if (res.Nav_Users && res.Nav_Users.length > 0) {
						for (let i = 0; i < res.Nav_Users.length; i++) {
							strAreas += (strAreas.length > 0 ? " " : "") + res.Nav_Users[
								i].Nav_Area.NAME + "  --  " + res.Nav_Users[
								i].Nav_User.NAME + '</br>';
						}
					}
					if (this.model.Nav_Details && this.model.Nav_Details.length > 0) {
						for (let i = 0; i < this.model.Nav_Details.length; i++) {
							if (this.model.Nav_Details[i].Nav_OccupationalRisk == undefined) {
								this.model.Nav_Details[i].Nav_OccupationalRisk = {
									NAME: ""
								};
							}
							this.model.Nav_Details[i].HAZARD_STATUS_SHOW = this.model.Nav_Details[i]
								.HAZARD_STATUS == 0 ? "有效" : "作废";
						}
					}
					this.model.START_TIME = uni.$u.timeFormat(this.model.START_TIME, 'yyyy-mm-dd');
					this.model.END_TIME = uni.$u.timeFormat(this.model.END_TIME, 'yyyy-mm-dd');
					this.model.LEVEL_SHOW = this.renderLevel(this.model.LEVEL + "");
					this.model.Areas = strAreas;
					this.isLoadOK = true;
				})
			},
			renderLevel(level) {
				let str = '';
				if (level == undefined) {
					return str;
				}
				if (level.indexOf('0') >= 0) {
					str += '部门 ';
				}
				if (level.indexOf('1') >= 0) {
					str += '车间 ';
				}
				if (level.indexOf('2') >= 0) {
					str += '班组 ';
				}
				if (level.indexOf('3') >= 0) {
					str += '公司 ';
				}
				return str;
			},
			async handleShowSheet(p) {
				const json = initFilter(this.ORG_ID, "", "", 0)
				let dataSelect = []
				if (p.name === 'Identifying') {
					extendRule(json, 'RISK_TYPE', 1, 1);
					dataSelect = await getIdentifying(json).then(res => {
						return res
					})
				} else if (p.name === 'OccupationlRisk') {
					dataSelect = await getRequest(json, "/HM/OccupationalRisk/OrderPaged").then(res => {
						return res
					})
				} else if (p.name === 'hazardStatus') {
					dataSelect = [{
						NAME: "有效",
						ID: 0
					}, {
						NAME: "无效",
						ID: 1
					}]
				}
				this.comPickerInfo = {
					showSheet: true,
					title: p.title,
					name: p.name,
					dataIndex: p.dataIndex,
					columns: [dataSelect]
				}
			},
			onConfirmPicker(e) {
				const {
					name,
					dataIndex,
					formIndex
				} = this.comPickerInfo
				if (name === 'Identifying') {
					this.model.Nav_Details[dataIndex].Nav_Identifying = e.value[0]
					this.model.Nav_Details[dataIndex].IDENTIFYING_ID = e.value[0].ID
				} else if (name === 'OccupationlRisk') {
					this.model.Nav_Details[dataIndex].Nav_OccupationalRisk = e.value[0]
					this.model.Nav_Details[dataIndex].OCCUPATIONALRISK_ID = e.value[0].ID
				} else if (name === 'hazardStatus') {
					this.model.Nav_Details[dataIndex].HAZARD_STATUS_SHOW = e.value[0].NAME
					this.model.Nav_Details[dataIndex].HAZARD_STATUS = e.value[0].ID
				}

				this.comPickerInfo.showSheet = false
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					dataIndex: undefined,
					formIndex: undefined
				}
			},
			submit() {
				this.model.PUBLISH = "SaveAndNotify";
				this.model.Nav_NoteTaker = null;
				this.model.Nav_Area = null;
				this.model.TaskID = this.TaskID;
				if (this.model.ORG_ID == "") {
					this.model.ORG_ID = this.ORG_ID;
				}
				this.model.Nav_Files.forEach(item => {
					if (item.RECORD_ID == undefined) {
						item.RECORD_ID = this.model.ID;
					}
				});
				this.model.Nav_Details.forEach(item => {
					item.Nav_Area = null;
					item.Nav_Identifying = null;
					item.Nav_OccupationalRisk = null;
					item.Nav_OperationStep = null;
					item.ORG_ID = this.ORG_ID;
				});
				this.model.Nav_Users.forEach(item => {
					item.Nav_User = null;
					item.Nav_Area = null;
				});
				//const ele = this.$refs
				//ele['wForm'].validate().then(res => {
				getRequest(this.model, "/HM/HMHazardRecord/FullUpdate").then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					}
				})
				// }).catch(err => {
				// 	uni.$showErrorInfo('请检查必填项，必填项不能为空')
				// })
			},
		},

	}
</script>

<style>
	.todo-page {
		padding: 16px;
	}

	.todo-page .status-bar {
		height: 120px;
		background-image: linear-gradient(to right, #CFECFF, #6EB6FF);
		position: absolute;
		top: 0;
		left: 0;
		z-index: -10;
	}

	.todo-page .main {
		padding: 62px 8px;
		position: relative;
		z-index: 10;
		background-size: 100% 120px;
		background-repeat: no-repeat;
	}

	.todo-page .swiper-wrap {
		margin-bottom: 16px;
	}

	.tabs-bar {
		display: flex;
		flex-direction: row;
	}

	.tabs-bar .tab {
		display: flex;
		align-items: center;
		position: relative;
		margin-right: 20px;
		padding-bottom: 6px;
	}

	.tabs-bar .tab .t {
		position: relative
	}

	.tabs-bar .tab .t .name {
		font-size: 18px;
		line-height: 22px;
		color: #999;
	}

	.tabs-bar .tab .bottom-widget {
		width: 20px;
		height: 3px;
		background-color: #004F9D;
	}

	.content-list {
		padding: 10px 0;
	}

	.content-list .card {
		border-radius: 8px;
		margin-bottom: 12px;
		font-size: 14px;
		overflow: hidden;
		/*display: flex;*/
		/*flex-direction: row;*/
		padding: 14px;
		/*box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);*/
		/*box-sizing: border-box;*/
		/* #ifndef APP-NVUE */
		box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);
		/* #endif */
		/*#ifdef APP-NVUE */
		border: 1px solid rgba(0, 0, 0, 0.1);
		/*#endif */
	}

	.content-list .card .card-body {
		display: flex;
		flex-direction: row;
	}

	.content-list .card .card-body .left {
		margin-right: 4px;
		width: 20px;
		padding-top: 4px;
	}

	.content-list .card .card-body .left .todo-icon {
		width: 16px;
		height: 16px;
	}

	.content-list .card .card-body .contain {
		flex: 1;
	}

	.content-list .card .card-body .content {
		margin-bottom: 6px;
		display: flex;
		flex-direction: row;
	}

	.content-list .card .card-body .content-text {
		color: #333333;
		line-height: 24px;
		font-weight: bold;
		font-size: 16px;
		flex: 1;
	}

	.content-list .card .card-body .content-bottom {
		display: flex;
		flex-direction: row;
		align-items: flex-end;
	}

	.content-list .card .card-body .date-time {
		flex: 1
	}

	.content-list .card .card-body .content-status {
		width: 56px;
	}

	.content-list .card .card-body .status {
		background: rgba(255, 87, 51, 0.2);
		color: rgba(255, 87, 51, 1);
		text-align: center;
		font-size: 12px;
		width: 56px;
		height: 18px;
		line-height: 18px;
		border-radius: 2px;
	}

	.content-list .card .card-body .status.normal {
		background: rgba(0, 161, 64, 0.2);
		color: rgba(0, 161, 64, 1);
	}

	.content-list .card .card-body .status.warn {
		background: rgba(202, 158, 3, 0.2);
		color: rgba(202, 158, 3, 1);
	}

	.content-list .card .card-body .time {
		font-weight: 400;
		line-height: 18px;
		color: #333333;
		display: flex;
		flex-direction: row;
		margin-top: 4px;
		align-items: center;
	}

	.content-list .card .card-body .time .point {
		width: 8px;
		height: 8px;
		border-radius: 50%;
		margin-right: 4px;
	}

	.content-list .card .card-body .time .point.green {
		background: #00A140;
	}

	.content-list .card .card-body .time .point.red {
		background: #EE0A24;
	}

	.content-list .card .card-body .label {
		color: #666666;
		font-size: 12px;
	}

	.content-list .card .card-body .value {
		font-size: 12px;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		display: block;
		margin-left: auto;
		margin-right: auto;
		padding-left: 14px;
		padding-right: 14px;
		box-sizing: border-box;
		font-size: 18px;
		text-align: center;
		text-decoration: none;
		line-height: 2.55555556;
		border-radius: 5px;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		color: #000;
		background-color: #f8f8f8;
		cursor: pointer;
		color: #fff;
		background-color: #007aff;
		width: 92%;
	}
</style>