<template>
	<view class="todo-page">
		<view class="card">
			<view class="background"></view>
			<view>
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules"
					ref="wForm" errorType="border-bottom" class="demo-ruleForm">
					<u-form-item label="姓名：" borderBottom>
						<u--input v-if="model.Nav_EDUCard" v-model="model.Nav_EDUCard.Nav_User.NAME" border="none"
							inputAlign="right" disabled disabledColor="#ffffff" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="身份证号：" borderBottom>
						<u--input v-if="model.Nav_EDUCard" v-model="model.Nav_EDUCard.Nav_User.ID_CARD" border="none"
							inputAlign="right" disabled disabledColor="#ffffff" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="入职时间：" borderBottom>
						<u--input v-if="model.Nav_EDUCard" v-model="model.Nav_EDUCard.Nav_User.ENTRYTIME" border="none"
							inputAlign="right" disabled disabledColor="#ffffff" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="部门：" borderBottom>
						<u--input v-if="model.Nav_EDUCard" v-model="model.Nav_EDUCard.Nav_Department.NAME" border="none"
							inputAlign="right" disabled disabledColor="#ffffff" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="岗位：" borderBottom>
						<u--input v-if="model.Nav_EDUCard" v-model="model.Nav_EDUCard.Nav_Post.NAME" border="none"
							inputAlign="right" disabled disabledColor="#ffffff" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="培训内容：" borderBottom>
					</u-form-item>
					<u--textarea autoHeight v-if="model.Nav_Content" v-model="model.Nav_Content.NAME" border="none"
						disabled disabledColor="#ffffff"></u--textarea>
					<u-form-item label="开始时间：" required prop="START_TIME" @click="showCheckDate({ name: 'START_TIME'})" borderBottom>
						<u--input disabledColor="#fff" v-model="model.START_TIME" disabled placeholder="请选择开始时间"
							suffixIcon="arrow-down" suffixIconStyle="font-size:14px" fontSize="14px" border="none"
							customStyle="margin:0px;display:flex;padding:3px 0px"  inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="结束时间：" required prop="END_TIME" @click="showCheckDate({name: 'END_TIME'})" borderBottom>
						<u--input disabledColor="#fff" v-model="model.END_TIME" disabled placeholder="请选择结束时间"
							suffixIcon="arrow-down" suffixIconStyle="font-size:14px" fontSize="14px" border="none"
							customStyle="margin:0px;display:flex;padding:3px 0px"  inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="培训地点：" required prop="TAINNING_ADDR" borderBottom>
						<u--input v-model="model.TAINNING_ADDR" border="none" slot="right"
							inputAlign="right" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="培训学时：" required prop="TRAINNING_TIME" borderBottom>
						<u--input v-model="model.TRAINNING_TIME" type="number" border="none" slot="right"
							inputAlign="right" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="培训老师：" required prop="TEACHER" borderBottom>
						<u--input v-model="model.TEACHER" border="none" slot="right" inputAlign="right" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item label="培训形式：" required prop="TRAIN_MODEL" borderBottom labelWidth="80" class="over-form">
						<u-checkbox-group v-model="model.TRAIN_MODEL_SHOW" shape="square"
							@change="checkboxChange($event)" slot="right">
							<u-checkbox :customStyle="{margin: '5px',}" v-for="(item, index) in checkboxList1"
								:key="index" :label="item.name" :name="item.name">
							</u-checkbox>
						</u-checkbox-group>
					</u-form-item>
					<u-form-item label="考核结果：" required prop="EXAMINATION_RESULTS" borderBottom
						@click="handleShowSheet({title: '考核结果', name: 'EXAMINATION_RESULTS'})">
						<u--input :value="model.EXAMINATION_RESULTS_SHOW" placeholder="请选择考核结果" border="none"
							inputAlign="right" disabled disabledColor="#fff" fontSize="14px"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" size="14" color="#606266"></u-icon>
					</u-form-item>
					<u-form-item label="分数：" required prop="SCORE" borderBottom>
						<u--input v-model="model.SCORE" type="number" border="none" slot="right"
							inputAlign="right" fontSize="14px"></u--input>
					</u-form-item>
					<view class="upload-title">附件：</view>
					<full-upload v-model="model.Nav_TrainRecordFile" :isShowBtn='true' :listProp='listPropUpload'
						:listPropVal='listPropValUpload'></full-upload>
				</u--form>
			</view>
		</view>
		<query-selector :show="showPopup" :lists="Lists" @close="handleClosePopup" @search="handleSearch"
			@select="handleSelected" />
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			:defaultIndex="[0]" @close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>
		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" mode="datetime"
			v-model='dateTimePickerInfo.defaultDateTime' :formatter="formatter" @confirm="handleCheckDate"
			@close="dateTimePickerInfo.showCheckDate = false;"
			@cancel="dateTimePickerInfo.showCheckDate = false;"></u-datetime-picker>
		<view class="bottom-button">
			<u-button type="primary" @click="submit" color="#3d4b70">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	export default {
		data() {
			return {
				Lists: [],
				listPropUpload: ['NOTIFY_ID'],
				listPropValUpload: [],
				model: {
					Nav_TrainUserList: [],
					ORG_ID: "",
					Nav_Files: [],
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				rules: {
					'START_TIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'END_TIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'TAINNING_ADDR': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'TRAINNING_TIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'TEACHER': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'TRAIN_MODEL': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'SCORE': {
						type: 'number',
						required: true,
						trigger: ['blur', 'change']
					}
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
					value: '',
					name: ''
				},
				formatter: null,
				showPopup: false,
				tableKey: '0',
				TaskID: '',
				isLoadOK: false,
				isAdd: 0,
				ORG_ID: uni.getStorageSync('orgId'),
				checkboxList1: [{
						name: '讲授法',
					},
					{
						name: '视听法',
					},
					{
						name: '研讨法',
					},
					{
						name: '演示法',
					}
				]
			}
		},
		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: {
			checkboxChange(arr) {
				let tempModel = "_" + arr.join("_") + "_";
				tempModel = tempModel.replaceAll("讲授法", 1);
				tempModel = tempModel.replaceAll("视听法", 2);
				tempModel = tempModel.replaceAll("研讨法", 3);
				tempModel = tempModel.replaceAll("演示法", 4);
				console.log(tempModel,'tempModel')
				this.model.TRAIN_MODEL = tempModel;
			},
			loadData() {
				const json = initFilter(this.ORG_ID, "", "")
				if (this.listPropValUpload.length == 0) {
					this.listPropValUpload.push(this.model.ID)
				}
				extendInclude(json, "Nav_User")
				extendInclude(json, "Nav_Department")
				extendInclude(json, "Nav_Content")
				extendInclude(json, "Nav_EDUCard.Nav_Post")
				extendInclude(json, "Nav_EDUCard.Nav_Department")
				extendInclude(json, "Nav_EDUCard.Nav_User")
				extendInclude(json, "Nav_TrainSignatureFile.Nav_ImgFile")
				extendInclude(json, "Nav_TrainRecordFile.Nav_ImgFile")
				// TODO: id 未获取
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/SE/ThreeLevelSafeTrainRecord/Get").then(res => {
					this.model = res
					this.model.Nav_EDUCard.Nav_User.ENTRYTIME = uni.$u.timeFormat(this.model.Nav_EDUCard.Nav_User
						.ENTRYTIME, 'yyyy-mm-dd');
					if (this.model.TRAIN_MODEL != null || this.model.TRAIN_MODEL != undefined) {
						if (this.model.TRAIN_MODEL.indexOf(1) > -1) {
							this.model.TRAIN_MODEL_SHOW = "讲授法";
						} else if (this.model.TRAIN_MODEL.indexOf(2) > -1) {
							this.model.TRAIN_MODEL_SHOW = "视听法";
						} else if (this.model.TRAIN_MODEL.indexOf(3) > -1) {
							this.model.TRAIN_MODEL_SHOW = "研讨法";
						} else if (this.model.TRAIN_MODEL.indexOf(4) > -1) {
							this.model.TRAIN_MODEL_SHOW = "演示法";
						}
					}
					if (this.model.EXAMINATION_RESULTS != null || this.model.EXAMINATION_RESULTS != undefined) {
						if (this.model.EXAMINATION_RESULTS == 0) {
							this.model.EXAMINATION_RESULTS_SHOW = "不合格";
						} else if (this.model.EXAMINATION_RESULTS == 1) {
							this.model.EXAMINATION_RESULTS_SHOW = "合格";
						}
					}else{
							this.model.EXAMINATION_RESULTS_SHOW = "合格";
							this.model.EXAMINATION_RESULTS = "1";
					}
				})
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					dataIndex: undefined,
					formIndex: undefined
				}
			},
			async handleShowSheet(p, index) {
				let column = []
				this.levelIndex = index;
				if (p.name === 'EXAMINATION_RESULTS') {
					column = [{
							NAME: '合格',
							ID: 1
						},
						{
							NAME: '不合格',
							ID: 0
						}
					]
				}
				if (column.length) {
					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						formIndex: p.formIndex,
						columns: [column]
					}
				} else {
					// 暂无数据
				}
			},
			//显示时间控件
			showCheckDate(p) {
				if (p.name === 'START_TIME') {
					if (this.model.START_TIME == null) {
						this.model.START_TIME = uni.$u.timeFormat(new Date(),
							'yyyy-mm-dd hh:MM')
					}
					this.dateTimePickerInfo = {
						showCheckDate: true,
						dataIndex: p.dataIndex,
						defaultDateTime: this.model.START_TIME,
						name: p.name
					}
				} else if (p.name === 'END_TIME') {
					if (this.model.END_TIME == null) {
						this.model.END_TIME = uni.$u.timeFormat(new Date(),
							'yyyy-mm-dd hh:MM')
					}
					this.dateTimePickerInfo = {
						showCheckDate: true,
						dataIndex: p.dataIndex,
						defaultDateTime: this.model.END_TIME,
						name: p.name
					}
				}
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				if (name === 'START_TIME') {
					this.model.START_TIME = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd hh:MM')
				}
				if (name === 'END_TIME') {
					this.model.END_TIME = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd hh:MM')
				}
				this.dateTimePickerInfo.showCheckDate = false
			},
			onConfirmPicker(e) {
				const {
					name,
					formIndex
				} = this.comPickerInfo
				this.model[name] = e.value[0].ID
				this.model[name + '_SHOW'] = e.value[0].NAME
				this.comPickerInfo.showSheet = false
			},
			submit() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.model.PUBLISH = "SaveAndNotify";
					if (this.TaskID != "") {
						this.model.TaskID = this.TaskID;
					}
					if (this.model.ORG_ID == "") {
						this.model.ORG_ID = this.ORG_ID;
					}
					this.model.START_TIME = uni.$u.timeFormat(this.model.START_TIME,
						'yyyy-mm-dd hh:MM:ss');
					this.model.END_TIME = uni.$u.timeFormat(this.model.END_TIME,
						'yyyy-mm-dd hh:MM:ss');
					this.model.Nav_TrainRecordFile.forEach(item => {
						if (item.TRAIN_RECORD_ID == undefined) {
							item.TRAIN_RECORD_ID = this.model.ID;
						}
						if (item.ORG_ID == undefined) {
							item.ORG_ID = this.model.ORG_ID;
						}
					});
					getRequest(this.model, "/SE/SEThreeLevelSafeTrainRecord/FullUpdate").then(res => {
						if (res) {
							uni.$showMsgFunc('操作成功!', () => {
								uni.navigateBack()
							}, 'success', 1000)
						}
					})
				}).catch(err => {
					console.log(err)
					uni.$showErrorInfo('请检查必填项，必填项不能为空')
				})
			},

		}
	}
</script>

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

	.todo-page {
		/* padding: 16px 16px 70px; */
	}
	.background {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #edf1fd;
		z-index: -1;
	}
	
	.card {
		margin: 20px 16px 140px 16px;
		/* border: 1px solid #EBEEF5; */
		padding: 20px 30px 80px 30px;
		/* box-shadow: rgba(0, 0, 0, 0.08) 0px 0px 3px 1px; */
		background-color: #ffffff;
		border-radius: 10px;
	}
	.upload-title {
		font-size: 14px;
		color: #8e8b9c;
		line-height: 22px;
		padding: 8px 0px;
	}
	.demo-ruleForm>>>.u-form-item__body__left__content__label {
		font-size: 14px;
		color: #8e8b9c;
		/* justify-content: flex-start; */
	}
	
	.demo-ruleForm>>>.u-form-item__body__left__content__ {
		left: -11px;
		top: 0px
	}
	
	.demo-ruleForm>>>.u-textarea__field {
		font-size: 14px;
	}
	
	.demo-ruleForm>>>.u-cell__title-text {
		font-size: 14px;
	}
	/* .over-form>>>.u-form-item__body__right__content {
		display: flex;
		flex-direction: column;
	} */
	.over-form>>>.u-checkbox-group--row {
		display: flex;
		flex-wrap: wrap;
		width: 50%;
		justify-content: flex-end;
		/* flex-direction: column; */
	}
	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #edf1fd;
		z-index: 999;
	}
</style>