<template>
	<sgc-modal :title="title" :sub-title="params.subTitle" @sgcCloseModal="closeModal" :toolbars="toolbars">
		<sgc-loading :loadStatus="loadStatus" loadType="view">
			<ion-list v-if="dataView" class="item-view">
				<sgc-item-view label="ROOM.LBL_LOCATION_NM" :text="utils.showTextJoin(' - ', dataView.locationNm, dataView.bunho)"> </sgc-item-view>
				<sgc-item-view label="ROOM.LBL_MEETING_NAME" :text="dataView.meetingName"></sgc-item-view>
				<sgc-item-view label="ROOM.LBL_USE_PDEPT_CD" :text="dataView.usePdeptNm"></sgc-item-view>
				<sgc-item-view label="ROOM.LBL_SUBSC_NAME" :text="dataView.subscName"></sgc-item-view>
				<sgc-item-view label="ROOM.LBL_YYYYMMDD" :text="dataView.pymd"></sgc-item-view>
				<sgc-item-view label="ROOM.LBL_HHMM" :text="dataView.phm"></sgc-item-view>
				<sgc-item-view v-if="dataView.gubun === '30'" label="ROOM.LBL_KEY_YN" :text="dataView.keyYn === 'Y' ? 'Yes' : 'No'"></sgc-item-view>
				<sgc-item-view label="ROOM.LBL_ATTEND_NUM" :text="dataView.attendNum + ''"></sgc-item-view>
				<sgc-item-view
					v-if="params.showType === 'VIEW' || params.showType === 'CANCEL' || params.showType === 'APP'"
					label="ROOM.LBL_MEETING_CON"
					type="textarea"
					:text="dataView.meetingCon"
				></sgc-item-view>
			</ion-list>
			<br />
			<ion-list v-if="dataView && params.showType === 'CLOSE'" class="item-form">
				<sgc-item-input label="ROOM.LBL_END_YYYYMMDD" type="datetime" :readOnly="true" v-model="dataView.endYyyymmdd" ref="endYyyymmdd"></sgc-item-input>
				<sgc-item-input v-if="dataView.gubun !== '30'" label="ROOM.LBL_END_HHMM" type="select" codeKey="RE001" v-model="dataView.endHhmm" ref="endHhmm"></sgc-item-input>
				<sgc-item-input label="ROOM.LBL_REMARK" type="textarea" v-model="dataView.remark" ref="remark"> </sgc-item-input>
			</ion-list>
		</sgc-loading>
	</sgc-modal>
</template>

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

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

/**
 * 임직원 - 회의실 예약 상세
 *
 * 회의실 예약 정보를 보여주는 페이지
 */
export default defineComponent({
	name: 'PoRoomRevViewModal',
	components: { ...IonPageModules, ...IonFormModules },
	props: ['remInfo', 'showType'],
	setup() {
		return {
			Icon,
			utils,
		};
	},
	mounted() {
		this.loadData();
	},
	data(): ViewDataVo<RoomRemInfoVo, { reservationId: string; showType: string; subTitle: string }> {
		return {
			title: 'TIT_ROOMREVVIEW',
			loadStatus: 'loading',
			dataView: null,
			params: {
				reservationId: '',
				showType: 'CLOSE',
				subTitle: null,
			},
			codeKey: {
				endHhmm: 'RE001',
				usePdeptCd: 'RE009',
			},
			toolbars: [
				{
					color: 'danger',
					label: 'ROOM.BTN_REV_END',
					hide: true,
					click: () => {
						this.closeCancelRoomRev('CLOSE');
					},
				},
				{
					color: 'danger',
					label: 'ROOM.BTN_REV_CANCEL',
					hide: true,
					click: () => {
						this.closeCancelRoomRev('CANCEL');
					},
				},
				{
					color: 'danger',
					label: 'ROOM.BTN_REV_APPROV',
					hide: true,
					click: () => {
						this.closeCancelRoomRev('APP');
					},
				},
				{
					color: 'secondary',
					label: 'ROOM.BTN_REV_REJECT',
					hide: true,
					click: () => {
						this.closeCancelRoomRev('REJECT');
					},
				},
			],
		};
	},
	watch: {
		room() {
			this.loadData();
		},
	},
	methods: {
		/** 모달 닫기 */
		closeModal() {
			return modalController.dismiss();
		},
		/**
		 * 예약 정보 취소 혹은 종료하기
		 *
		 * @param type
		 */
		closeCancelRoomRev(type: string) {
			if (this.dataView) {
				switch (type) {
					case 'CANCEL':
						if (this.dataView?.cancelYn !== 'Y') {
							commonService.openToast(this.dataView?.cancelCn || 'MSG_UNKNOWN', null, null, this.title, 'warning');
						} else {
							commonService.openConfirm('ROOM.MSG_CONFIRM_CANCEL', null, { header: 'ROOM.TIT_CONFIRM' }).then((result) => {
								if (result) {
									commonService.getUpdate('api/room/cancelMeetSave', this.dataView).then((result) => {
										if (result) {
											commonService.openToast('ROOM.MSG_CANCEL_DONE', null, null, this.title, 'success');
											this.closeModal();
										}
									});
								}
							});
						}
						break;
					case 'CLOSE':
						if (this.dataView?.closeYn !== 'Y') {
							commonService.openToast(this.dataView?.closeCn || 'MSG_UNKNOWN', null, null, this.title, 'warning');
						} else {
							const today = utils.getDate(null, 'yyyyMMdd');
							const currHour = utils.getDate(null, 'HHmm');
							if (this.dataView.endYyyymmdd < today) {
								commonService.openToast('ROOM.MSG_ERROR_PASTDAY', null, null, this.title, 'warning');
							} else if (this.dataView.endYyyymmdd === today && this.dataView.endHhmm < currHour) {
								commonService.openToast('ROOM.MSG_ERROR_PASTDAY', null, null, this.title, 'warning');
							} else {
								commonService.openConfirm('ROOM.MSG_CONFIRM_CLOSE', null, { header: 'ROOM.TIT_CONFIRM' }).then((result) => {
									if (result) {
										commonService.getUpdate('api/room/closeMeetSave', this.dataView).then((result) => {
											if (result) {
												commonService.openToast('ROOM.MSG_CLOSE_DONE', null, null, this.title, 'success');
												this.closeModal();
											}
										});
									}
								});
							}
						}
						break;
					case 'APP':
						commonService.openConfirm('ROOM.MSG_CONFIRM_APP', null, { header: 'ROOM.TIT_CONFIRM' }).then((result) => {
							if (result) {
								const sendData = {
									reservationId: this.dataView.reservationId,
								};
								commonService.getUpdate('api/room/appSave', sendData).then((result) => {
									if (result) {
										commonService.openToast('ROOM.MSG_APP_DONE', null, null, this.title, 'success');
										this.closeModal();
									}
								});
							}
						});

						break;
					case 'REJECT':
						commonService.openConfirm('ROOM.MSG_CONFIRM_REJECT', null, { header: 'ROOM.TIT_CONFIRM' }).then((result) => {
							if (result) {
								const sendData = {
									reservationId: this.dataView.reservationId,
								};
								commonService.getUpdate('api/room/rejectSave', sendData).then((result) => {
									if (result) {
										commonService.openToast('ROOM.MSG_REJECT_DONE', null, null, this.title, 'success');
										this.closeModal();
									}
								});
							}
						});
						break;
				}
			}
		},
		/** 기존 데이타 불러오기 */
		loadData() {
			if (commonService.isNotNull(this.remInfo)) {
				if (commonService.isNotNull(this.remInfo.meetingName)) {
					this.dataView = 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.dataView = view;
					})
					.finally(() => {
						this.loadStatus = commonService.isNotNull(this.dataView) ? 'loaded' : 'nodata';
						switch (this.showType) {
							case 'CLOSE':
								this.params.subTitle = this.$t('ROOM.TIT_CLOSEMEET');
								this.toolbars[0].hide = false;
								if (this.dataView !== null && this.dataView !== undefined) {
									this.dataView.endYyyymmdd = utils.getDate(null, 'yyyyMMdd');
								}
								break;
							case 'CANCEL':
								this.params.subTitle = this.dataView.gubunNm + '-' + this.$t('ROOM.TIT_CANCEL');
								this.toolbars[1].hide = false;
								break;
							case 'APP':
								this.params.subTitle = this.$t('ROOM.TIT_APPMEET');
								this.toolbars[2].hide = false;
								this.toolbars[3].hide = false;
								break;
						}
					});
			}
		},
	},
});
</script>

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