<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="CREATE_TIME" borderBottom>
						<u--input v-model="dataModel.CREATE_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="DESCRIPTION" borderBottom>
						<u--input v-model="dataModel.DESCRIPTION" disabledColor="#ffffff" placeholder="请输入变化描述" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item class="custom-form-item" label="变化类型" prop="ChangeType" :borderBottom="false"
					@click="handleQuerySelect({title: '变化类型', name: 'ChangeType', isMultiple: true})">
						<view slot="right" class="custom-form-item-right">
							<view class="multi">
								<u--text v-for="(i, k) in dataModel.Nav_Types" :key="k" class="item" :text="i.NAME"></u--text>
							</view>
							<u-icon class="icon" name="arrow-down">
							</u-icon>
						</view>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<query-selector :defaultChecked ="qsCom.defaultValue" :multiple="qsCom.isMultiple" :total="curTotal" :show="qsCom.show" :lists="qsCom.lists"
			@close="onCloseQS" @search="onSearchQS" @select="onSelectedQS" />
		<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,
		saveChangeIdentify,
		getChangeTypeLists
	} from '../../../../services/apply/subPages/SCPT/PTServices'
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				ID:null,
				TaskID:null,
				tableKey:0,
				curTotal: 0,
				isLoadOK :false,
				showDelModalIndex: undefined,
				userLists: [],
				dataModel: {
					TIME:uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					Nav_Department:{NAME:uni.getStorageSync('appInfo')?.User?.Nav_Department?.NAME || ''},
					Nav_User:{NAME:uni.getStorageSync('appInfo')?.User?.NAME || ''},
					Nav_Types:[{
						Nav_Type: {
							NAME: '',
						},
					}]
				},
				subDataModel: {
					
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				qsCom: {
					show: false,
					lists: [],
					title: '',
					// dataIndex: undefined,
					// formIndex: undefined,
					isMultiple: false,
					defaultValue: [],
					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 : '00000000-0000-0000-0000-000000000000';
				this.ID = opt.ID ? opt.ID : guid();
				if (opt.ID !== undefined) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "")
				 extendRule(json, 'ID', 1, opt.ID)
				// TODO: id 未获取
				getRequest(json,"/PR/ChangeIdentifyEvaluation/Get").then(res => {
					this.dataModel = res
					this.isLoadOK = true
				})
				}
			},
			async handleQuerySelect(opt) {
				let column = await this.handleGet(opt, 'init')
			
				if (column && column.length) {
					this.qsCom = {
						...this.qsCom,
						show: true,
						title: opt.title,
						name: opt.name,
						// formIndex: opt.formIndex,
						// dataIndex: opt.dataIndex,
						isMultiple: opt.isMultiple,
						lists: column
					}
				} else {
					// 暂无数据
				}
			},
			async handleGet(opt, value, pageIndex) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, '', "", 0, pageIndex || 1)
				if (value !== 'init') {
					const tempGroup = initFilterGroup(false);
					extendGroupRule(tempGroup, 'NAME', 9, value)
					extendFilterGroup(json, tempGroup);
				}
				json.Limit = 20
				if (pageIndex) {
					json.Start = (pageIndex - 1) * 20;
				}
				let result = []
				if (opt.name === 'ChangeType') {
					const raw = await getChangeTypeLists(json).then(res => {
						this.curTotal = res.TotalCount
						return res.Data
					})
					result = raw.map(item => {
						return {
							...item,
							name: item?.NAME,
							id: item?.ID
						}
					})
				}
				return result
			},
			onSelectedQS(val) {debugger
				const { name } = this.qsCom
					
					if (name === 'ChangeType') {
						const dp = val.map(item => {
							return {
								ID: guid(),
								CHANGE_IDENTIFY_EVALUATION_ID:this.ID || guid(),
								ORG_ID: uni.getStorageSync('orgId'),
								TYPE_ID: item.ID,
								NAME: item.NAME
							}
						})
						this.dataModel.Nav_Types = dp
					}
				this.qsCom.show = false
			},
			async onSearchQS(val, pi) {
				const result = await this.handleGet(this.qsCom, val, pi)
				this.qsCom.lists = result.map(i => {
					return {
						...i,
						name: i.NAME,
						NAME: i.NAME,
						id: i.ID
					}
				})
			},
			async handleQuerySelect(opt) {
				let column = await this.handleGet(opt, 'init')
			
				if (column && column.length) {
					this.qsCom = {
						...this.qsCom,
						show: true,
						title: opt.title,
						name: opt.name,
						// formIndex: opt.formIndex,
						// dataIndex: opt.dataIndex,
						isMultiple: opt.isMultiple,
						lists: column
					}
				} else {
					// 暂无数据
				}
			},
			onCloseQS() {
				this.qsCom = {
					show: false,
					lists: [],
					title: '',
					name: '',
					formIndex: undefined,
					isMultiple: false,
					dataIndex: undefined
				}
			},
			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')
					// const resultDeal = this.dataModel.Nav_Types.map((obj, index) => {
					
					// 		return Object.assign({}, obj, {
					// 			Nav_Types:obj.Nav_Types
					// 		})
					// 	return obj
					// })
					// this.dataModel.Nav_Types = resultDeal
					saveChangeIdentify(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>
