<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" ref="wForm"
					errorType="border-bottom">
					<u-form-item label="识别部门：" prop="Nav_Department" borderBottom>
						<u--input v-if="model.Nav_Department" v-model="model.Nav_Department.NAME" border="none"
							slot="right" inputAlign="right" disabled disabledColor="#fff"></u--input>
					</u-form-item>
					</u-form-item>
					<u-form-item label="识别人员：" prop="Nav_User" borderBottom>
						<u--input v-if="model.Nav_User" v-model="model.Nav_User.NAME" border="none" slot="right"
							inputAlign="right" disabled disabledColor="#fff"></u--input>
					</u-form-item>
					<u-form-item label="截止时间：" prop="END_TIME" borderBottom>
						<u--input v-if="model.END_TIME" v-model="model.END_TIME.substring(0,10)" border="none"
							slot="right" inputAlign="right" disabled disabledColor="#fff"></u--input>
					</u-form-item>
					<u-form-item class="custom-form-item" label="需求类别：" :borderBottom="false"
						@click="handleQuerySelect({ formIndex: 'Nav_DemandTypes', isMultiple: true})">
						<view slot="right" class="custom-form-item-right">
							<view class="multi">
								<u--text v-for="(i, k) in model.Nav_DemandTypes" :key="k" class="item"
									:text="k + 1 + '. ' + i.Nav_DemandType.NAME"></u--text>
							</view>
							<u-icon class="icon" name="arrow-down">
							</u-icon>
						</view>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<view class="sub-form-btns">
						<view class="sub-form-btn" @click="handleAdd">
							<u-icon class="icon" name="plus-circle" color="#3c9cff" size="21"></u-icon>
							<u--text type="primary" text="新增法律法规"></u--text>
						</view>
					</view>
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<uni-collapse>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for="(item, index) in model.Nav_DemandLaw.filter(i => !i.IS_DELETED)">
								<uni-collapse-item title-border="none" :show-arrow="false" :border="false" :open="true">
									<view slot="title" class="custom-collapse-title">
										<view class="down">
											<uni-icons type="bottom"></uni-icons>
										</view>
										<view class="text">{{index + 1 + '. ' +item.lawName}}</view>
										<view class="action" @click.stop>
											<u-icon @click="handleDelRowBefore(index, item)" class="icon" name="trash"
												color="#ff4d4f" size="21"></u-icon>
										</view>
									</view>
									<u-form-item label="法律法规：" prop="Nav_Law" borderBottom
										@click="handleChange('Nav_Law',item)">
										<u--input v-model="item.lawName" placeholder="请选择法律法规" border="none"
											inputAlign="right" disabled disabledColor="#fff"></u--input>
										<u-icon style="margin-left: 4px;" slot="right" name="arrow-down">
										</u-icon>
									</u-form-item>

									<u-form-item label="备注：">
										<u--input v-model="item.REMARK" placeholder="请输入备注" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
								</uni-collapse-item>
							</uni-card>
						</uni-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<view class="sub-form-btns">
						<view class="sub-form-btn" @click="handleAddOther">
							<u-icon class="icon" name="plus-circle" color="#3c9cff" size="21"></u-icon>
							<u--text type="primary" text="新增其他"></u--text>
						</view>
					</view>
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<uni-collapse>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for="(item, index) in model.Nav_DemandOther.filter(i => !i.IS_DELETED)">
								<uni-collapse-item title-border="none" :show-arrow="false" :border="false" :open="true">
									<view slot="title" class="custom-collapse-title">
										<view class="down">
											<uni-icons type="bottom"></uni-icons>
										</view>
										<view class="text">{{index + 1 + '. ' +item.NAME}}</view>
										<view class="action" @click.stop>
											<u-icon @click="handleDelRowBefore(index, item)" class="icon" name="trash"
												color="#ff4d4f" size="21"></u-icon>
										</view>
									</view>
									<u-form-item label="其他：" borderBottom>
										<u--input v-model="item.NAME" placeholder="请输入其他" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="备注：">
										<u--input v-model="item.REMARK" placeholder="请输入备注" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
								</uni-collapse-item>
							</uni-card>
						</uni-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<query-selector :show="showPopup" :lists="Lists" @close="handleClosePopup" @search="handleSearch"
			@select="handleSelected" />
		<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 {
				Lists: [],
				listPropUpload: ['NOTIFY_ID'],
				listPropValUpload: [],
				model: {
					Nav_DemandLaw: [],
					Nav_DemandOther: []
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				showPopup: false,
				tableKey: '0',
				TaskID: '',
				isLoadOK: false,
				isAdd: 0,
				ORG_ID: uni.getStorageSync('orgId'),
				curTotal: 0,
				qsCom: {
					show: false,
					lists: [],
					formIndex: undefined,
					isMultiple: false,
				},
				currentOperate: "",
			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			if (option.isAdd != 1) {
				this.loadData();
			} else {
				this.isAdd = 1;
				this.model.ID = guid();
			}
		},
		methods: {
			handleDelRowBefore(index, item) {
				if (item.LAW_ID == "" || item.LAW_ID == undefined) {
					this.model.Nav_DemandLaw.splice(this.showDelModalIndex, 1)
				} else {
					this.showDelModalIndex = index
				}
			},
			handleAdd() {
				let count1 = this.model.Nav_DemandLaw.length + 1;
				this.model.Nav_DemandLaw.unshift({
					DISTINGUISH_ID: this.model.ID,
					ID: guid(),
					ORG_ID: this.ORG_ID,
					LAW_ID: "",
					IS_DELETED: false,
					REMARK: "",
					ROW_NO: count1,
					lawName:""
				})
			},
			handleAddOther() {
				let count2 = this.model.Nav_DemandOther.length + 1;
				this.model.Nav_DemandOther.unshift({
					DISTINGUISH_ID: this.model.ID,
					ID: guid(),
					ORG_ID: this.ORG_ID,
					IS_DELETED: false,
					REMARK: "",
					NAME: "",
					ROW_NO: count2,
				})
			},
			//单选start
			handleChange(title, item) {
				this.currentOperate = item;
				this.currSearchType = title;
				this.showPopup = true;
				this.handleSearch('init');
			},
			handleSearch(val) {
				const json = initFilter(this.ORG_ID, "", "NAME")
				json.Limit = 20
				if (val !== 'init') {
					const tempGroup = initFilterGroup(false);
					extendGroupRule(tempGroup, 'NAME', 9, val)
					extendFilterGroup(json, tempGroup);
				}
				if (this.currSearchType == "Nav_Law") {
					getRequest(json, "/LR/Law/OrderPaged").then(res => {
						this.Lists = res.map(i => {
							return {
								...i,
								name: i.NAME,
							}
						})
					})
				}
			},
			handleSelected(val) {
				this.showPopup = false;
				this.Lists = [];
				if (this.currSearchType == 'Nav_Law') {
					this.currentOperate.lawName = val.NAME;
					this.currentOperate.LAW_ID = val.ID;
				}
			},
			handleClosePopup() {
				this.showPopup = false
			},
			//单选end
			//多选start
			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
					}
				}
			},
			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 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 = []
				const raw = await getRequest(json, "/LR/LawType/OrderPaged").then(res => {
					this.curTotal = res.TotalCount
					return res
				})
				result = raw.map(item => {
					return {
						...item,
						name: item?.NAME,
						DEMANDTYPE_ID: item?.ID
					}
				})
				return result
			},
			onSelectedQS(val) {
				const {
					formIndex,
				} = this.qsCom
				this.model.Nav_DemandTypes = [];
				val.forEach(item => {
					let obj = {
						DEMANDTYPE_ID: item.ID,
						Nav_DemandType: {
							NAME: item.NAME
						},
						ID: guid(),
						DISTINGUISH_ID: this.model.ID,
						ORG_ID: this.ORG_ID,
					}
					this.model.Nav_DemandTypes.push(obj);
				})
				this.qsCom.show = false
			},
			onCloseQS() {
				this.qsCom = {
					show: false,
					lists: [],
					formIndex: undefined,
					isMultiple: false,
					dataIndex: undefined
				}
			},
			//多选end

			loadData() {
				const json = initFilter(this.ORG_ID, "", "")
				if (this.listPropValUpload.length == 0) {
					this.listPropValUpload.push(this.model.ID)
				}
				extendInclude(json, "Nav_DemandTypes.Nav_DemandType")
				extendInclude(json, "Nav_Department")
				extendInclude(json, "Nav_User")
				extendInclude(json, "Nav_DemandTypes")
				extendInclude(json, "Nav_DemandLaw")
				extendInclude(json, "Nav_DemandLaw.Nav_Law")
				extendInclude(json, "Nav_DemandOther")
				// TODO: id 未获取
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/LR/DemandDistinguish/Get").then(res => {
					this.model = res
				})
			},



			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_Department = null;
				this.model.Nav_User = null;
				this.model.Nav_DemandTypes.forEach(item => {
					item.Nav_DemandType = null;
				});
				this.model.DISTINGUISH_TIME=uni.$u.timeFormat(new Date(),'yyyy-mm-dd hh:MM:ss');
				getRequest(this.model, "/LR/LRDemandDistinguish/FullUpdate").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>