<template>
	<sgc-modal :title="title" :subTitle="subTitle" @sgcCloseModal="closeModal" :toolbars="toolbars">
		<sgc-loading :loadStatus="loadStatus" loadType="write">
			<form novalidate @submit.prevent="submitData">
				<ion-list v-if="writeModel" class="item-form">
					<sgc-item-input
						label="ROOM.LBL_ROOM"
						type="select"
						:required="true"
						:code-key="codeKey.meetingRoom"
						v-model="writeModel.meetingroomId"
						ref="meetingroomId"
					></sgc-item-input>
					<sgc-item-input
						label="ROOM.LBL_USE_PDEPT_CD"
						type="select"
						:required="true"
						codeKey="RE009"
						v-model="writeModel.usePdeptCd"
						ref="usePdeptCd"
					></sgc-item-input>
					<sgc-item-input label="ROOM.LBL_MEETING_NAME" type="text" :required="true" v-model="writeModel.meetingName" ref="meetingName"></sgc-item-input>
					<sgc-item-input label="ROOM.LBL_SUBSC_NAME" type="text" :required="true" v-model="writeModel.subscName" ref="subscName"></sgc-item-input>
					<ion-item-group v-if="showType == 'ONE'">
						<sgc-item-input label="ROOM.LBL_REYYYYMMDD" type="datetime" :required="true" v-model="writeModel.startYyyymmdd" ref="one_startYyyymmdd"></sgc-item-input>
						<sgc-item-input
							label="ROOM.LBL_HHMM"
							type="hour-range"
							:required="true"
							codeKey="RE001"
							v-model="writeModel.startHhmm"
							v-model:to="writeModel.endHhmm"
							ref="one_startEndHhmm"
						></sgc-item-input>
					</ion-item-group>
					<ion-item-group v-else-if="showType == 'RANGE'">
						<sgc-item-input
							label="ROOM.LBL_REVYYYYMMDD_RANGE"
							type="datetime-range"
							:required="true"
							v-model="writeModel.startYyyymmdd"
							v-model:to="writeModel.endYyyymmdd"
							ref="range_startEndYyyymmdd"
						></sgc-item-input>
						<sgc-item-input label="ROOM.LBL_KEY_YN" type="radio" codeKey="YESNO" v-model="writeModel.keyYn" ref="keyYn"></sgc-item-input>
					</ion-item-group>
					<ion-item-group v-if="showType == 'REP'">
						<sgc-item-input
							label="ROOM.LBL_REVYYYYMMDD_RANGE"
							type="datetime-range"
							:required="true"
							v-model="writeModel.startYyyymmdd"
							v-model:to="writeModel.endYyyymmdd"
							ref="rep_startEndYyyymmdd"
						></sgc-item-input>
						<sgc-item-input
							label="ROOM.LBL_HHMM"
							type="hour-range"
							:required="true"
							codeKey="RE001"
							v-model="writeModel.startHhmm"
							v-model:to="writeModel.endHhmm"
							ref="rep_startEndHhmm"
						></sgc-item-input>
						<sgc-item-input
							label="ROOM.LBL_REPEAT_CON"
							type="radio"
							:required="true"
							codeKey="WEEKMONTH"
							@sgcChange="changeWeek()"
							v-model="writeModel.week"
							ref="week"
						></sgc-item-input>
						<sgc-item-input
							label="ROOM.LBL_REPEAT_WEEK"
							type="checkbox"
							codeKey="WEEK"
							@sgcChange="changeWeek()"
							v-model="writeModel.weekList"
							ref="weekList"
						></sgc-item-input>
					</ion-item-group>
					<sgc-item-input label="ROOM.LBL_ATTEND_NUM" type="number" :required="true" v-model="writeModel.attendNum" ref="attendNum"></sgc-item-input>
					<sgc-item-input label="ROOM.LBL_MEETING_CON" type="textarea" :required="true" v-model="writeModel.meetingCon" ref="meetingCon"></sgc-item-input>
				</ion-list>
			</form>
		</sgc-loading>
	</sgc-modal>
</template>

<script lang="ts">
import { commonService, defineComponent, Icon, IonPageModules, IonFormModules, modalController, utils } from '../../../services/index';

import { RoomRemInfoVo, WriteDataVo } from '../../../services/common.interface';

/**
 * 임직원 - 회의실 예약 등록
 *
 * 회의실 예약을 등록하는 모달
 */
export default defineComponent({
	name: 'PoRoomRevRegModal',
	components: { ...IonPageModules, ...IonFormModules },
	props: ['remInfo', 'showType'],
	setup() {
		return {
			Icon,
			utils,
		};
	},
	mounted() {
		const loginUser = commonService.getSharedService().getLoginInfo();
		this.writeModel.subscName = loginUser.userNm;
		this.writeModel.subscSabun = loginUser.empNo;
		this.loadData();
	},
	data(): WriteDataVo<RoomRemInfoVo, { reservationId: string; showType: string }> {
		return {
			title: 'TIT_ROOMREV_WRITE',
			subTitle: null,
			loadStatus: 'loading',
			writeModel: {
				attendNum: 0,
				bunho: '',
				meetingroomId: 0,
				reservationId: 0,
				endHhmm: '',
				endYyyymmdd: null,
				gubun: '',
				keyYn: 'N',
				meetingCon: '',
				meetingName: '',
				phone: '',
				remark: '',
				startHhmm: '',
				startYyyymmdd: null,
				subscName: '',
				subscSabun: '',
				week: '',
				weekList: [],
				weekFr: 'N',
				weekMu: 'N',
				weekTh: 'N',
				weekTu: 'N',
				weekWe: 'N',
			},
			params: {
				reservationId: '',
				showType: 'CLOSE',
			},
			codeKey: {
				endHhmm: '',
				meetingRoom: 'ROOMCD',
			},
			toolbars: [
				{
					color: 'primary',
					label: 'ROOM.BTN_SUBMIT_REV',
					click: () => {
						this.submitData();
					},
				},
			],
		};
	},
	watch: {
		room() {
			this.loadData();
		},
	},
	methods: {
		/** 모달 닫기 */
		closeModal() {
			return modalController.dismiss();
		},
		/** 요일 변경  */
		changeWeek() {
			const disabled = this.writeModel.week === '10' ? true : false;
			commonService.setFormDisabled(disabled, this.$refs, 'weekList');
		},
		/** 예약 정보 전송 */
		submitData() {
			if (commonService.isNull(this.writeModel.meetingroomId)) {
				return commonService.openFormInvalid('회의실', this.$refs, 'meetingroomId');
			}
			if (commonService.isNull(this.writeModel.meetingName)) {
				return commonService.openFormInvalid('회의제목', this.$refs, 'meetingName');
			}
			if (commonService.isNull(this.writeModel.usePdeptCd)) {
				return commonService.openFormInvalid('사용본부', this.$refs, 'usePdeptNm');
			}
			if (commonService.isNull(this.writeModel.subscName)) {
				return commonService.openFormInvalid('예약자', this.$refs, 'subscName');
			}
			if (commonService.isNull(this.writeModel.attendNum)) {
				return commonService.openFormInvalid('참석인원', this.$refs, 'attendNum');
			}
			if (commonService.isNull(this.writeModel.meetingCon)) {
				return commonService.openFormInvalid('회의내용', this.$refs, 'meetingCon');
			}
			switch (this.showType) {
				case 'ONE':
					if (commonService.isNull(this.writeModel.startYyyymmdd)) {
						return commonService.openFormInvalid('예약일자', this.$refs, 'one_startYyyymmdd');
					}
					if (commonService.isNull(this.writeModel.startHhmm) || commonService.isNull(this.writeModel.endHhmm)) {
						return commonService.openFormInvalid('예약시간', this.$refs, 'one_startEndHhmm');
					}
					commonService.getCreate('api/room/revSave', commonService.getSubmitData(this.writeModel)).then((result) => {
						if (result) {
							commonService.openToast('ROOM.MSG_ROOM_REV_DONE', null, null, this.title, 'success');
							this.closeModal();
						}
					});
					break;
				case 'RANGE':
					if (commonService.isNull(this.writeModel.startYyyymmdd) || commonService.isNull(this.writeModel.endYyyymmdd)) {
						return commonService.openFormInvalid('예약기간', this.$refs, 'range_startEndYyyymmdd');
					}
					commonService.getCreate('api/room/rangeSave', commonService.getSubmitData(this.writeModel)).then((result) => {
						if (result) {
							commonService.openToast('ROOM.MSG_ROOM_REV_DONE', null, null, this.title, 'success');
							this.closeModal();
						}
					});
					break;
				case 'REP':
					if (commonService.isNull(this.writeModel.startYyyymmdd) || commonService.isNull(this.writeModel.endYyyymmdd)) {
						return commonService.openFormInvalid('예약기간', this.$refs, 'rep_startEndYyyymmdd');
					}
					if (commonService.isNull(this.writeModel.startHhmm) || commonService.isNull(this.writeModel.endHhmm)) {
						return commonService.openFormInvalid('예약시간', this.$refs, 'rep_startEndHhmm');
					}
					if (commonService.isNull(this.writeModel.week)) {
						return commonService.openFormInvalid('반복조건', this.$refs, 'week');
					}
					if (this.writeModel.week === '20' && commonService.isNull(this.writeModel.weekList)) {
						return commonService.openFormInvalid('예약요일', this.$refs, 'weekList');
					}
					commonService.getCreate('api/room/repSave', commonService.getSubmitData(this.writeModel)).then((result) => {
						if (result) {
							commonService.openToast('ROOM.MSG_ROOM_REV_DONE', null, null, this.title, 'success');
							this.closeModal();
						}
					});
					break;
			}
		},
		/** 초기 로드시 실행 - 수정 모드를 위해서 사용한 (실제로 사용하지 않음)*/
		loadData() {
			if (commonService.isNotNull(this.remInfo)) {
				if (commonService.isNotNull(this.remInfo.meetingName)) {
					this.writeModel = this.remInfo;
					this.loadStatus = 'loaded';
				} else if (commonService.isNotNull(this.remInfo.reservationId)) {
					this.params.reservationId = this.remInfo.reservationId;
				}
			}
			if (commonService.isNotNull(this.showType)) {
				this.params.showType = this.showType;
			}
			if (commonService.isNotNull(this.params.reservationId)) {
				commonService
					.getView('api/room/viewRem/' + (this.remInfo?.reservationId || this.params.reservationId))
					.then((view) => {
						this.writeModel = view;
					})
					.finally(() => {
						this.loadStatus = commonService.isNotNull(this.writeModel) ? 'loaded' : 'nodata';
					});
			} else {
				this.loadStatus = 'loaded';
			}
			switch (this.showType) {
				case 'ONE':
					this.subTitle = this.$t('ROOM.BTN_REG_ONE');
					break;
				case 'RANGE':
					this.subTitle = this.$t('ROOM.BTN_REG_RANGE');
					break;
				case 'REP':
					this.subTitle = this.$t('ROOM.BTN_REG_REP');
					break;
			}
		},
	},
});
</script>

<style scoped lang="scss">
ion-title > span {
	font-size: 0.7em;
}
</style>
