<template>
	<view class="todo-page">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules"
					ref="wForm" errorType="border-bottom">
					<u-form-item label="调查名称：" prop="Nav_Survey.NAME" borderBottom>
						<u--input v-if="model.Nav_Survey" v-model="model.Nav_Survey.NAME" border="none" slot="right"
							inputAlign="right" disabled disabledColor="#fff"></u--input>
					</u-form-item>
					<u-form-item label="发起部门：" prop="Nav_LaunchDepartment.NAME" borderBottom>
						<u--input v-if="model.Nav_Survey" v-model="model.Nav_Survey.Nav_LaunchDepartment.NAME"
							border="none" inputAlign="right" disabled disabledColor="#fff"></u--input>
						</u-icon>
					</u-form-item>
					<u-form-item label="发起人员：" prop="Nav_LaunchUser.NAME" borderBottom>
						<u--input v-if="model.Nav_Survey" :value="model.Nav_Survey.Nav_LaunchUser.NAME" border="none"
							inputAlign="right" disabled disabledColor="#fff"></u--input>
					</u-form-item>

					<u-form-item label="截止时间：" prop="END_TIME" borderBottom>
						<u--input v-if="model.Nav_Survey" v-model="model.Nav_Survey.END_TIME" border="none" slot="right"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="调查目的：" prop="Nav_Points_SHOW" borderBottom>
						<rich-text v-html="model.Nav_Points_SHOW" slot="right" disabled></rich-text>
					</u-form-item>
					<u-form-item required class="custom-form-item" label="需求" :borderBottom="false"
						@click="handleQuerySelect({ formIndex: 'Nav_Items', isMultiple: true})">
						<view slot="right" class="custom-form-item-right">
							<view class="multi">
								<u--text v-for="(i, k) in model.Nav_Items" :key="k" class="item"
									:text="k + 1 + '. ' + i.Nav_DEMAND.NAME"></u--text>
							</view>
							<u-icon class="icon" name="arrow-down">
							</u-icon>
						</view>
					</u-form-item>
					<u-form-item label="其他：" prop="TRAIN_MODEL" borderBottom>
						<u--input v-model="model.OTHER" border="none" inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<query-selector :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 {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	export default {
		data() {
			return {
				model: {
					Nav_Users: [],
					ORG_ID: "",
					Nav_Points_SHOW: ""
				},
				rules: {

				},
				qsCom: {
					show: false,
					lists: [],
					formIndex: undefined,
					isMultiple: false,
				},
				curTotal: 0,
				showPopup: false,
				tableKey: '0',
				TaskID: '',
				isLoadOK: false,
				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: {
			loadData() {
				const json = initFilter(this.ORG_ID, "", "")
				extendInclude(json, "Nav_Survey")
				extendInclude(json, "Nav_Survey.Nav_LaunchDepartment")
				extendInclude(json, "Nav_Survey.Nav_LaunchUser")
				extendInclude(json, "Nav_Survey.Nav_Points.Nav_Point")
				extendInclude(json, "Nav_Items")
				extendInclude(json, "Nav_Items.Nav_DEMAND")
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/SE/TrainSurveyListDemand/Get").then(res => {
					this.model = res;
					this.model.Nav_Points_SHOW = "";
					for (let i = 0; i < this.model.Nav_Survey.Nav_Points.length; i++) {
						this.model.Nav_Points_SHOW += (i + 1) + "." + this.model.Nav_Survey
							.Nav_Points[i].Nav_Point.NAME + "<br/>";
					}

				})
			},
			async handleQuerySelect(opt) {
				let column = await this.handleGet(opt, 'init')
				if (column && column.length) {
					this.qsCom = {
						show: true,
						name: opt.NAME,
						formIndex: opt.formIndex,
						isMultiple: opt.isMultiple,
						lists: column
					}
				} else {
					// 暂无数据
				}
			},
			async handleGet(opt, value, pageIndex) {
				const json = initFilter(this.ORG_ID, '', "", 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 = []
				json.IgnoreDataRule = true;
				json.SelectField = [
					"NAME",
					"ID",
				]
				const raw = await getRequest(json, "/SE/TrainDemandEnum/OrderPaged").then(res => {
					this.curTotal = res.TotalCount
					return res
				})
				result = raw.map(item => {
					return {
						...item,
						name: item?.NAME,
						DEMAND_ID: item?.ID
					}
				})
				return result
			},
			onSelectedQS(val) {
				const {
					formIndex,
				} = this.qsCom
				this.model.Nav_Items = [];
				val.forEach(item => {
					let obj = {
						DEMAND_ID: item.ID,
						Nav_DEMAND: {
							NAME: item.NAME
						},
						ID: guid(),
						LIST_ID: this.model.ID,
						ORG_ID: this.ORG_ID,
					}
					this.model.Nav_Items.push(obj);
				})
				this.qsCom.show = false
			},
			submit() {
				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.Nav_Survey = null;
				this.model.Nav_Department = null;
				getRequest(this.model, "/SE/SETrainSurvey/SubmitUserDemand").then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					}
				})
			},

		}
	}
</script>

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

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