<template>
	<view class="page-wrap">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="dataModel" 
					ref="wForm" errorType="border-bottom">
					<!-- <u-form-item label="审核意见：" prop="CONTEXT" borderBottom v-if="dataModel.IS_PUBLISH==9">
					</u-form-item>
					<u--textarea v-html="dataModel.CONTEXT" border="surround" v-if="dataModel.IS_PUBLISH==9"
						disabled></u--textarea> -->
					<u-form-item label="制定时间" prop="SET_TIME" borderBottom>
						<u--input v-model="dataModel.SET_TIME" disabled disabledColor="#ffffff" placeholder="请输入制定时间" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="制定部门" prop="DepartmentName" borderBottom>
						<u--input v-model="dataModel.Nav_Department.NAME" disabled disabledColor="#ffffff"  placeholder="请输入制定部门" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="制定人员" prop="UserName" borderBottom>
						<u--input v-model="dataModel.Nav_User.NAME" disabled disabledColor="#ffffff" placeholder="请输入制定人员" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="年度" prop="YEAR" borderBottom>
						<u--input v-model="dataModel.YEAR" disabled disabledColor="#ffffff" placeholder="请输入年度" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="考核层级" prop="DEPARTMENT_TYPE_SHOW" borderBottom>
						<u--input v-model="dataModel.DEPARTMENT_TYPE_SHOW" disabled disabledColor="#ffffff" placeholder="请输入征集截至时间" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<uni-collapse :border="false" accordion>
					<uni-card margin="0" spacing="0" padding="0" :is-shadow="true">
						<uni-collapse-item title="考核方案明细">
							<view class="sub-form-wrap" style="height: 500px; overflow: auto">
								<u--form labelPosition="left" labelWidth="auto" labelAlign="center"
									:model="subDataModel" ref="sForm2" errorType="border-bottom">
									<u-swipe-action>
										<u-swipe-action-item style="margin-bottom: 16px;" :options="options"
											v-for="(item, index) in dataModel.Nav_Details">
											<uni-card margin="0" spacing="0" :is-shadow="false"
												style="margin-bottom: 10px;">
												<u-form-item label="考核部门" prop="ChildDepartName" borderBottom>
													<u--input disabled disabledColor="#fff" v-model="item['Nav_Department'].NAME"
																		border="none" inputAlign="right"></u--input>
													
												</u-form-item>
												<u-form-item label="考核人员" prop="ChildUserName" borderBottom>
													<u--input disabled disabledColor="#fff" v-model="item['Nav_User'].NAME"
																		border="none" inputAlign="right"></u--input>
													
												</u-form-item>
												<u-form-item label="考核周期" prop="ASSESSMENT_CYCLE_SHOW" borderBottom
												@click="handleShowSheet({title: '考核周期', name: 'ASSESSMENT_CYCLE', dataIndex: index})">
													<u--input v-model="item.ASSESSMENT_CYCLE_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 label="安全生产目标与指标" prop="UserNames" borderBottom>
												</u-form-item>
												<u--textarea autoHeight type="textarea" v-html="item.UserNames" border="none"
													inputAlign="left">
												</u--textarea>
											</uni-card>
										</u-swipe-action-item>
									</u-swipe-action>
								</u--form>
							</view>
						</uni-collapse-item>
					</uni-card>
				</uni-collapse>
			</view>
		</u-sticky>
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>
		<view class="bottom-button">
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup,
		extendIgnoreDataRule
	} from '../../../../utils/common'
	import {
		getRequest,
		savePlanAudit
	} from '../../../../services/apply/subPages/SCPT/PTServices'
	import {
		getUserLists,
	} from '../../../../services/apply/FOServices/FOServices.js'
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				ID:null,
				TaskID:null,
				tableKey:0,
				curTotal: 0,
				isLoadOK :false,
				showDelModalIndex: undefined,
				userLists: [],
				dataModel: {
					SET_TIME:null,
					Nav_Department:{NAME:''},
					Nav_User:{NAME:''},
				},
				subDataModel: {
					Nav_Details:{
						Nav_Department:{NAME:''},
						Nav_User:{NAME:''},
					}
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				currentOperateUser: {},
				showPopup: false,
				// rules: {
				// 	'YEAR': {
				// 		type: 'string',
				// 		required: true,
				// 		trigger: ['blur', 'change']
				// 	},
				// },
			}
		},
		onLoad(opt) {
			this.handleGetLicenseGet(opt)
		},
		methods: {
			handleGetLicenseGet(opt) {
				this.TaskID = opt.taskID ? opt.taskID : null;
				this.ID = opt.ID ? opt.ID : null;
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "")
				 extendRule(json, 'ID', 1, opt.ID)
				// TODO: id 未获取
				getRequest(json,"/PT/PTAssessmentPlanAudit/GetEdit").then(res => {
					this.dataModel = res
					if(res.DEPARTMENT_TYPE == 0)
					{
						res.DEPARTMENT_TYPE_SHOW = '部门'
					}else if(res.DEPARTMENT_TYPE == 1)
					{
							res.DEPARTMENT_TYPE_SHOW = '车间'
					}else if(res.DEPARTMENT_TYPE == 2)
					{
							res.DEPARTMENT_TYPE_SHOW = '班组'
					}else{
						res.DEPARTMENT_TYPE_SHOW = '公司'
					}
					//数据组装
					
					if (res.Nav_Details && res.Nav_Details.length > 0) {
						for (let i = 0; i < res.Nav_Details.length; i++) {
							let strconfirmFiles = ''
							let details=res.Nav_Details[i];
							if (details.Nav_Contents && details.Nav_Contents.length > 0) {
								for (let j = 0; j < details.Nav_Contents.length; j++) {
								let depart=details.Nav_Contents[j].NAME;
								strconfirmFiles += (strconfirmFiles.length > 0 ? "</br>" : "") + depart;
								details.UserNames = strconfirmFiles
								}
							}
							if(res.Nav_Details[i].ASSESSMENT_CYCLE == 10)
							{
								res.Nav_Details[i].ASSESSMENT_CYCLE_SHOW ='年度'
							}else if(res.Nav_Details[i].ASSESSMENT_CYCLE == 20)
							{
								res.Nav_Details[i].ASSESSMENT_CYCLE_SHOW ='季度'
							}else if(res.Nav_Details[i].ASSESSMENT_CYCLE == 30)
							{
								res.Nav_Details[i].ASSESSMENT_CYCLE_SHOW ='月度'
							}
							else
							{
								res.Nav_Details[i].ASSESSMENT_CYCLE_SHOW ='请选择'
							}
						}
					}
					this.isLoadOK = true
				})
			},
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				let dataSelect = []
				if (p.name === 'ASSESSMENT_CYCLE') {
					dataSelect = [{
							NAME: '年度',
							ID: 10
						},
						{
							NAME: '季度',
							ID: 20
						},
						{
							NAME: '月度',
							ID: 30
						}
					]
				}
				
				if (dataSelect.length) {
					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						dataIndex:p.dataIndex,	
						formIndex: p.formIndex,
						columns: [dataSelect]
					}
				} else {
					// 暂无数据
				}
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					dataIndex: undefined,
					formIndex: undefined
				}
			},
			onConfirmPicker(e) {
				const {
					name,
					dataIndex,
					formIndex
				} = this.comPickerInfo
				if(name === "ASSESSMENT_CYCLE")
				{
					this.dataModel.Nav_Details[dataIndex].ASSESSMENT_CYCLE = e.value[0].ID
					this.dataModel.Nav_Details[dataIndex].ASSESSMENT_CYCLE_SHOW = e.value[0].NAME
				}
				
				this.comPickerInfo.showSheet = false
			},
			submit() {
				// const ele = this.$refs
				// ele['wForm'].validate().then(res => {
					this.dataModel.PUBLISH = "SaveAndNotify";
					if (this.TaskID != "") {
						this.dataModel.TaskID = this.TaskID;
					}
					
					this.dataModel.ID = this.ID;
					this.dataModel.ORG_ID =uni.getStorageSync('orgId')
					savePlanAudit(this.dataModel).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							// if (this.tableKey == null || this.tableKey == 0) {
							// 	uni.navigateTo({
							// 		url: 'jobTaskLicenseRecord'
							// 	})
							// } else {
								uni.navigateBack()
							// }
						}, 'success', 1000)
					})
				// }).catch(err => {
				// 	uni.$showErrorInfo('请检查必填项，必填项不能为空')
				// })
			},
			uploadFilePromise(url) {
				const appInfoData = uni.getStorageSync('appInfo')
				const userId = appInfoData?.User?.ID || ''
				const orgId = uni.getStorageSync('orgId')
				const tenant = uni.getStorageSync('Tenant') || ''
				const remoteUrl = config.serviceHost('/PF/File/UploadFile')
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: remoteUrl,
						filePath: url,
						fileList: url,
						name: 'file',
						formData: {
							OrgId: orgId
						},
						header: {
							Tenant: tenant,
							userid: userId
						},
						success: (res) => {
							if (res.statusCode === 200) {
								const uploadResult = JSON.parse(res.data)
								if (uploadResult.IsSuccessful) {
									resolve(uploadResult.Data)
								}
							}
						}
					});
				})
			},
			}
	}
</script>

<style scoped>
	@import url("../../../../style/css/editTemplate.css");
	.page-wrap {
		padding: 16px 16px 166px;
	}

	.card {
		margin-bottom: 18px;
	}

	.sub-form {
		margin-bottom: 16px;
	}

	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #fff;
		z-index: 999;
	}

	.upload-title {
		color: #303133;
		font-size: 15px;
		line-height: 22px;
		padding: 8px 0;
	}

	.page-wrap>>>.u-upload__button {
		margin-bottom: 0;
	}
</style>
