<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_User" borderBottom>
						<u--input v-if='model.Nav_User' v-model="model.Nav_User.NAME" disabled disabledColor="#fff"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="反馈人员：" prop="Nav_ReplyUser" borderBottom>
						<u--input v-if='model.Nav_ReplyUser' v-model="model.Nav_ReplyUser.NAME" border="none"
							inputAlign="right" disabled disabledColor="#fff"></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" v-if="!isReply">
						<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_Items"
								v-for="(item, index) in model.Nav_Items.filter(i => !i.IS_DELETED)">
								<u-collapse-item :title="(index + 1) + '. ' + item.TYPE_SHOW+' | '+item.DESC">
									<view slot="right-icon" class="row-action"></view>
									<view slot="value" class="row-action" v-if="!isReply">
										<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="TYPE_SHOW" borderBottom
										@click="handleChange(item)">
										<u--input v-model="item.TYPE_SHOW" 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="事件描述：" prop="DESC" borderBottom>
									</u-form-item>
									<u-textarea v-model="item.DESC" placeholder="请输入事件描述"
										:disabled="isReply"></u-textarea>
									<u-form-item v-if="model.STATUS==1" label="反馈意见：" prop="REPLY" borderBottom>
									</u-form-item>
									<u-textarea v-if="model.STATUS==1" v-model="item.REPLY" placeholder="请输入反馈意见"></u-textarea>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<u-modal :show="showDelModalIndex >= 0" @confirm="confirmDel" @cancel="cancelDel" showCancelButton
			title="确认删除？"></u-modal>
		<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
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				model: {
					Nav_Items: []
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				rules: {
					'TITLE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
				},
				currentOperateUser: {},
				showPopup: false,
				tableKey: '0',
				TaskID: '',
				isLoadOK: false,
				showDelModalIndex: undefined,
				ORG_ID: uni.getStorageSync('orgId'),
				isReply: false
			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			this.loadData();
		},
		methods: {
			handleAdd() {
				this.model.Nav_Items.unshift({
					ID: guid(),
					DETAIL_ID: this.model.ID,
					ORG_ID: this.ORG_ID,
					IS_DELETED: false,
					TYPE_SHOW: "",
					DESC: "",
				})
			},
			handleDelRowBefore(index, item) {
				if (item.USER_ID == "") {
					this.model.Nav_Items.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_User")
				extendInclude(json, "Nav_ReplyUser")
				extendInclude(json, "Nav_Items")
				extendInclude(json, "Nav_Files.Nav_ImgFile.Nav_File")
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/OG/EventGetDetail/Get").then(res => {
					this.model = res;
					if (this.model.STATUS == 1) {
						this.isReply = true
					}
					this.model.Nav_Items.forEach(item => {
						if (item.TYPE == 0) {
							item.TYPE_SHOW = "交通"
						} else if (item.TYPE == 1) {
							item.TYPE_SHOW = "火灾"
						} else if (item.TYPE == 2) {
							item.TYPE_SHOW = "爆炸"
						} else if (item.TYPE == 3) {
							item.TYPE_SHOW = "溺水"
						} else if (item.TYPE == 4) {
							item.TYPE_SHOW = "中毒"
						} else if (item.TYPE == 5) {
							item.TYPE_SHOW = "触电"
						} else if (item.TYPE == 6) {
							item.TYPE_SHOW = "其他"
						}
					})
				})
			},
			handleChange(item) {
				this.currentOperate = item;
				let column = []
				column = [{
						NAME: '交通',
						ID: 0
					},
					{
						NAME: '火灾',
						ID: 1
					},
					{
						NAME: '爆炸',
						ID: 2
					},
					{
						NAME: '溺水',
						ID: 3
					},
					{
						NAME: '中毒',
						ID: 4
					},
					{
						NAME: '触电',
						ID: 5
					},
					{
						NAME: '其他',
						ID: 6
					}
				]
				if (this.isReply == false) {
					this.comPickerInfo = {
						showSheet: true,
						columns: [column]
					}
				}
			},
			onConfirmPicker(e) {
				this.currentOperate.TYPE = e.value[0].ID
				this.currentOperate.TYPE_SHOW = e.value[0].NAME
				this.comPickerInfo.showSheet = false
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
				}
			},
			submit() {
				if (this.TaskID != "") {
					this.model.TaskID = this.TaskID;
				}
				if (this.model.ORG_ID == "") {
					this.model.ORG_ID = this.ORG_ID;
				}
				this.model.PUBLISH = "SaveAndNotify";
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					getRequest(this.model, "/OG/OGEventGet/UpdateUser").then(res => {
						if (res) {
							uni.$showMsgFunc('操作成功!', () => {
								uni.navigateBack()
							}, 'success', 1000)
						}
					})
				}).catch(err => {
					uni.$showErrorInfo('请检查必填项，必填项不能为空')
				})
			},
		}
	}
</script>

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

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