<template>
	<view class="todo-page">
		<uni-card margin="0" :is-shadow="true">
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model">
				<u-form-item label="任务层级：" prop="LEVEL" borderBottom>
					<u--input v-model="model.LEVEL" border="none" slot="right" inputAlign="right" disabled
						disabledColor="fff"></u--input>
				</u-form-item>
				<u-form-item label="执行时间：" prop="execTime" borderBottom>
					<u--input v-model="model.execTime" 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 v-model="model.Areas" border="none" inputAlign="right" disabled></u--textarea>
				<u-form-item label="辨识人员：" prop="UserNames" borderBottom>
				</u-form-item>
				<u--textarea autoHeight v-html="model.UserNames" border="none" inputAlign="right"
					disabled></u--textarea>
				<view class="upload-title">附件</view>
				<full-upload v-model="model.Nav_Files" :listProp='listPropUpload'
					:listPropVal='listPropValUpload' :isShowBtn="false"></full-upload>
			</u--form>
		</uni-card>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<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">
								<u-collapse-item
									:title="index + 1 + '. ' + item.Nav_Area.NAME+' - '+ item.Nav_Identifying.NAME">
									<u-form-item label="区域" prop="Nav_Area.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Area.NAME"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="职业危害名称" prop="Nav_Identifying.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Identifying.NAME"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="职业危害位置" prop="HAZARD_POSITION" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.HAZARD_POSITION"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="暴露时间" prop="EXPOSURE_TIME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.EXPOSURE_TIME"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="暴露限值" prop="EXPOSURE_LIMIT_VALUE" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.EXPOSURE_LIMIT_VALUE"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="暴露人数" prop="EXPOSURE_NUMBER" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.EXPOSURE_NUMBER"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="可能造成的职业风险" prop="Nav_OccupationalRisk.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_OccupationalRisk.NAME"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<audit-process :show="showAudit" @close="handleCloseAudit" :dataSource="auditData" />
		<check-action v-if="isLoadOK && tableKey === '1'&&isAudit==='true' " :okApi="okApi" :refuseApi="refuseApi"
			:dataSource="$store.state.auditModel"></check-action>
		<view class="bottom-button">
			<button type="primary" class="bottom" v-if='isLoadOK&&tableKey==1&&!isAudit'
				@click="onTableBtnAgree">签到</button>
		</view>
	</view>
</template>

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

	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_Request: "",
					Nav_NoteTaker: ""
				},
				isAudit: false,
				TaskID: "",
				tableKey: '0',
				isLoadOK: false,
				auditData: [],
				showAudit: false,
				orgId: uni.getStorageSync('orgId')
			}
		},
		onLoad(option) {
			this.model.ID = option.ID;
			this.TaskID = option.taskID;
			this.tableKey = option.tableKey ? option.tableKey : '0'
			this.isAudit = option.isAudit
			this.okApi = option.okApi
			this.refuseApi = option.refuseApi
			this.loadData()
		},
		methods: {
			loadData() {
				const json = initFilter(this.orgId, "", "CODE", 1)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, 'Nav_InitiatingDepartment');
				extendInclude(json, 'Nav_Users.Nav_User');
				extendInclude(json, 'Nav_Details.Nav_Area');
				extendInclude(json, 'Nav_Details.Nav_OccupationalRisk');
				extendInclude(json, 'Nav_Details.Nav_Identifying');
				extendInclude(json, 'Nav_Users.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
				extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
				getRequest(json, "/HM/HazardRecord/Get").then(res => {
					this.model = res
					let strUserName = '';
					let strIdentifyings = '';
					let strAreas = '';
					res.Nav_Users = this.removeDuplicateUser(res.Nav_Users);
					if (res.Nav_Users && res.Nav_Users.length > 0) {
						for (let i = 0; i < res.Nav_Users.length; i++) {
							if (res.Nav_Users[i].DEAL_STATUS == 0) {
								strUserName += (strUserName.length > 0 ? " " : "") +
									"<text style='color:red'>" +
									res.Nav_Users[i].Nav_User.NAME + "</text>&nbsp;";
							} else {
								strUserName += (strUserName.length > 0 ? " " : "") + res.Nav_Users[
									i].Nav_User.NAME + "&nbsp;";
							}

						}
					}
					if (res.Nav_Details && res.Nav_Details.length > 0) {
						for (let i = 0; i < res.Nav_Details.length; i++) {
							strAreas += (strAreas.length > 0 ? " " : "") + res.Nav_Details[
								i].Nav_Area.NAME;
						}
					}
					this.model.LEVEL = this.transLevel(this.model.LEVEL);
					this.model.execTime = uni.$u.timeFormat(this.model.START_TIME, 'yyyy-mm-dd') + '~' + uni.$u
						.timeFormat(this.model.END_TIME, 'yyyy-mm-dd');
					this.model.UserNames = strUserName;
					this.model.Areas = strAreas;
					if (this.model.SHIFT_STATUS == 2) {
						this.isSign = true;
						this.isAudit = false;
					} else if (this.model.SHIFT_STATUS == 3) {
						this.isSign = false;
						this.isAudit = true;
					}
					this.isLoadOK = true
				})
			},
			onTableBtnAgree() {
				let json = {};
				json.ID = this.model.ID;
				json.TaskID = this.TaskID;
				getRequest(json, "/HM/HMHazardRecord/PersonalAgree").then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					}
				})
			},
			transLevel(level) {
				if (level == 0) {
					return '部门 ';
				}
				if (level == 1) {
					return '车间 ';
				}
				if (level == 2) {
					return '班组 ';
				}
				if (level == 3) {
					return '公司 ';
				}
			},
			removeDuplicateUser(arr) {
				let obj = {};
				arr = arr.reduce((newArr, next) => {
					obj[next.USER_ID] ? "" : (obj[next.USER_ID] = true && newArr.push(next));
					return newArr;
				}, []);
				return arr;
			},
			loadAuditData() {
				const json = initFilter(this.orgId, this.model.ID, "CODE", 1)
				return new Promise((resolve, reject) => {
					getRequest(json, "/PF/Approve/GetApproveInfo").then(res => {
						if (res) {
							if (res && res[0].Nav_ApproveDetails.length > 0) {
								resolve(res)
							}
						} else {
							uni.showToast({
								title: '暂无审批数据',
								icon: 'none'
							})
						}
					}).catch(err => {
						reject(err)
					})
				})
			},
			handleCloseAudit() {
				this.showAudit = false
			}
		},
		async onNavigationBarButtonTap() {
			const res = await this.loadAuditData()
			if (res) {
				this.auditData = res
				this.showAudit = true
			}
		}
	}
</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>