<template>
	<ion-page>
		<sgc-page :title="title" :toolbars="toolbars">
			<sgc-loading :loadStatus="loadStatus" loadType="write">
				<form novalidate @submit.prevent="submit">
					<ion-list class="item-form">
						<sgc-item-input
							ref="pushSeq"
							:required="true"
							label="PUSH.LBL_PUSH_SEQ"
							type="number"
							v-model="writeModel.pushSeq"
							helper="SELECT PUSH_SEQ FROM MO_PUSH WHERE USEYN = 'Y'"
						>
						</sgc-item-input>
						<sgc-item-input ref="pushTitle" label="PUSH.LBL_PUSH_TITLE" type="text" :required="true" v-model="writeModel.pushTitle"></sgc-item-input>
						<sgc-item-input ref="pushContents" label="PUSH.LBL_PUSH_CONTENTS" type="textarea" :required="true" v-model="writeModel.pushContents"></sgc-item-input>
						<sgc-item-input
							ref="pushLink"
							label="PUSH.LBL_PUSH_LINK"
							type="text"
							v-model="writeModel.pushLink"
							helper="/home | /address | /po/roomrev ..."
						></sgc-item-input>
						<sgc-item-input
							ref="pushImage"
							label="PUSH.LBL_PUSH_IMAGE"
							type="text"
							v-model="writeModel.pushImage"
							helper="/resources/images/login/logo.png | https://~~~"
						>
						</sgc-item-input>
						<sgc-item-input
							ref="pushType"
							label="PUSH.LBL_PUSH_TYPE"
							:required="true"
							v-model="writeModel.pushType"
							helper="TEST | NOTI - use for table MO_PUSHHIST.PUSH_TYPE"
						>
						</sgc-item-input>
					</ion-list>
				</form>
			</sgc-loading>
		</sgc-page>
	</ion-page>
</template>

<script lang="ts">
import { WriteDataVo } from '../../services/common.interface';
import { commonService, defineComponent, IonPageModules } from '../../services/index';

/**
 * 개발 - 임시 푸시 전송 테스트
 *
 * 개발 환경에서만 작동함
 * 임시로 특정 사용자에게 푸시 메세지를 전송함
 */
export default defineComponent({
	name: 'PushDevPage',
	components: {
		...IonPageModules,
	},
	data(): WriteDataVo<{
		pushType: string;
		pushTitle: string;
		pushContents: string;
		pushLink: string;
		pushImage: string;
		pushSeq: number;
	}> {
		return {
			title: 'TIT_PUSHDEV',
			loadStatus: 'loaded',
			writeModel: {
				pushType: 'TEST',
				pushTitle: '',
				pushContents: '',
				pushLink: '',
				pushImage: '',
				pushSeq: 0,
			},
			toolbars: [
				{
					color: 'primary',
					label: 'PUSH.BTN_SEND',
					click: () => {
						this.submit();
					},
				},
			],
		};
	},
	methods: {
		/** 푸시 메세지 전송 */
		submit() {
			if (commonService.isNull(this.writeModel.pushSeq)) {
				return commonService.openFormInvalid('대상번호', this.$refs, 'pushSeq');
			}
			if (commonService.isNull(this.writeModel.pushTitle)) {
				return commonService.openFormInvalid('제목', this.$refs, 'pushTitle');
			}
			if (commonService.isNull(this.writeModel.pushContents)) {
				return commonService.openFormInvalid('내용', this.$refs, 'pushContents');
			}
			if (commonService.isNull(this.writeModel.pushType)) {
				return commonService.openFormInvalid('알림종류', this.$refs, 'pushType');
			}
			commonService.openConfirm('PUSH.MSG_CONFIRM_SEND').then((bl) => {
				if (bl) {
					commonService.getCreate('api/sendpushtest', commonService.getSubmitData(this.writeModel, {})).then((result: any) => {
						if (result && result.pushToken) {
							commonService.openToast('PUSH.MSG_SEND_RESULT', result, null, this.title, 'success');
						} else {
							commonService.openToast('PUSH.MSG_SEND_ERROR', result, null, this.title, 'warning');
						}
					});
				}
			});
		},
	},
});
</script>
<style scoped lang="scss"></style>
