<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="NAME" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_Collection.NAME" disabled
							disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="征集开始时间：" prop="START_TIME" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_Collection.START_TIME" disabled
							disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="征集结束时间：" prop="END_TIME" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_Collection.END_TIME" disabled
							disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="征集部门：" prop="Nav_Department" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_Collection.Nav_Department.NAME"
							disabled disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="征集人员：" prop="Nav_User" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_Collection.Nav_User.NAME" disabled
							disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="填写人员：" prop="Nav_User" borderBottom>
						<u--input v-if="model.Nav_Collection" v-model="model.Nav_User.NAME" disabled
							disabledColor="#fff" border="none" inputAlign="right"></u--input>
					</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">
						<u-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-if="model.Nav_Opinions"
								v-for="(item, index) in model.Nav_Opinions.filter(i => !i.IS_DELETED)">
								<u-collapse-item :title="(index + 1) + '. ' + item.OPINION_CONTENT">
									<view slot="right-icon" class="row-action"></view>
									<view slot="value" class="row-action">
										<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="DESC" borderBottom>
									</u-form-item>
									<u-textarea v-model="item.OPINION_CONTENT" placeholder="请输入事件描述"></u-textarea>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<view class="bottom-button">
			<button type="primary" @click="submit">提交</button>
		</view>
		<u-datetime-picker :show="showCheckDate2" mode="datetime" v-model='currentTime' @confirm="handleCheckDate2"
			@close="showCheckDate2 = false;" @cancel="showCheckDate2 = false;"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import config from '../../../../config/common'
	export default {
		data() {
			return {
				model: {
					ORG_ID: "",
					Nav_Opinions:[]
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				rules: {
					'JOB_CONTENT': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				currentTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
				tableKey: '0',
				TaskID: '',
				showCheckDate2: false,
				ORG_ID: uni.getStorageSync('orgId')
			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			this.loadData();
		},
		methods: {
			handleAdd() {
				this.model.Nav_Opinions.unshift({
					ID: guid(),
					COLLECTION_USER_ID: this.model.ID,
					ORG_ID: this.ORG_ID,
					IS_DELETED: false,
					OPINION_CONTENT: "",
				})
			},
			handleDelRowBefore(index, item) {
				if (item.USER_ID == "") {
					this.model.Nav_Opinions.splice(index, 1)
				} else {
					this.showDelModalIndex = index
				}
			},
			cancelDel() {
				this.showDelModalIndex = undefined
			},
			confirmDel() {
				this.model.Nav_Items.filter(i => !i.IS_DELETED)[this.showDelModalIndex].IS_DELETED = true;
				this.showDelModalIndex = undefined
			},
			loadData() {
				const json = initFilter(this.ORG_ID, "", "")
				extendInclude(json, "Nav_Collection.Nav_Department")
				extendInclude(json, "Nav_Collection.Nav_User")
				extendInclude(json, "Nav_User")
				extendInclude(json, "Nav_Opinions")
				// TODO: id 未获取
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/OG/EmployeeOpinionCollectionUser/Get").then(res => {
					this.model = res
				})
			},
			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;
					}
					getRequest(this.model, "/OG/OGEmployeeOpinionCollection/FullUpdateUser").then(res => {
						if (res) {
							uni.$showMsgFunc('操作成功!', () => {
								uni.navigateBack()
							}, 'success', 1000)
						}
					})
				}).catch(err => {
					uni.$showErrorInfo('请检查必填项，必填项不能为空')
				})
			},
			handleCheckDate2(e) {
				this.showCheckDate2 = false;
				this.model.END_TIME = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM:ss');
			},
		}
	}
</script>

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

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