<template>
	<sgc-modal :title="title" @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="ELPS.LBL_DEFT_PRCG_GB_NM"
						type="select"
						:required="true"
						:code-key="codeKey.deftPrcgGbCd"
						v-model="writeModel.deftPrcgGbCd"
						ref="deftPrcgGbCd"
					></sgc-item-input>
					<sgc-item-input label="ELPS.LBL_PRCG_PR_STRT_DT" type="datetime" :required="true" v-model="writeModel.prcgPrStrtDt" ref="prcgPrStrtDt"></sgc-item-input>
					<sgc-item-input
						label="ELPS.LBL_PRCG_PR_TM"
						type="hour-range"
						:required="true"
						v-model="writeModel.prcgPrStrtTm"
						v-model:to="writeModel.prcgPrEndTm"
						ref="prcgPrTm"
					></sgc-item-input>
					<sgc-item-input label="ELPS.LBL_PGPR_NM" type="text" :required="true" v-model="writeModel.pgprNm" ref="pgprNm"></sgc-item-input>
					<sgc-item-input label="ELPS.LBL_RMK_CN" type="textarea" :required="true" v-model="writeModel.rmkCn" ref="rmkCn"></sgc-item-input>
				</ion-list>
			</form>
		</sgc-loading>
	</sgc-modal>
</template>

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

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

/**
 * 협력업체 - 하자 경과 등록
 *
 * 하자 경과 정도를 등록함
 */
export default defineComponent({
	name: 'DeftElpsRegModal',
	components: { ...IonPageModules, ...IonFormModules },
	props: ['elps'],
	setup() {
		return {
			Icon,
			utils,
		};
	},
	mounted() {
		this.loadData();
	},
	data(): WriteDataVo<TcsDeftElpsVo> {
		return {
			title: 'ELPS.TIT_REG_ING',
			loadStatus: 'loading',
			writeModel: {
				/** 회사 코드 */
				cmpnCd: '',

				/** 하자번호 */
				deftNo: '',

				/** 하자처리구분코드[cs013] */
				deftPrcgGbCd: '',

				/** 하자처리구분명 */
				deftPrcgGbNm: '',

				/** 메일 내용 */
				mailCn: '',

				/** 수정 일자 */
				mdftDt: '',

				/** 수정 자 */
				mdftPr: '',

				/** 처리자 구분코드[cs016] */
				pgprGbCd: '',

				/** 처리자 구분명 */
				pgprGbNm: '',

				/** 처리자 명 */
				pgprNm: '',

				/** 처리 비용 */
				prcgAmt: '',

				/** 처리경과순번 */
				prcgElpsSeq: '',

				/** 처리예정종료일자 */
				prcgPrEndDt: '',

				/** 처리예정종료시간to */
				prcgPrEndTm: '',

				/** 처리예정시작일자 */
				prcgPrStrtDt: '',

				/** 처리예정시작시간from */
				prcgPrStrtTm: '',

				/** 처리예정시작시간from to*/
				prcgPrStrtEndTm: '',

				/** 처리유형코드[cs012] */
				prcgTypeCd: '',

				/** 처리유형명 */
				prcgTypeNm: '',

				/** 비고내용 */
				rmkCn: '',

				/** 작성 일자 */
				wrteDt: '',

				/** 작성 자 */
				wrtePr: '',
			},
			codeKey: {
				deftPrcgGbCd: '',
				prcgPrStrtEndTm: 'CS029',
			},
			toolbars: [
				{
					color: 'danger',
					label: 'BTN_SUBMIT',
					click: () => {
						this.submitData();
					},
				},
			],
		};
	},
	watch: {
		elps() {
			this.loadData();
		},
	},
	methods: {
		/** 모달 닫기 */
		closeModal() {
			return modalController.dismiss();
		},
		/** 모달 제목 변경 */
		changeTitle() {
			switch (this.writeModel.deftPrcgGbCd) {
				case 'WRK98':
					this.title = 'ELPS.TIT_REG_DONE';
					this.codeKey.deftPrcgGbCd = 'CS013_JOBEND';
					break;
				case 'WRK99':
					this.title = 'ELPS.TIT_REG_LAST';
					this.codeKey.deftPrcgGbCd = 'CS013_LAST';
					break;
				default:
					this.title = 'ELPS.TIT_REG_ING';
					this.codeKey.deftPrcgGbCd = 'CS013_G';
					break;
			}
		},
		/** 하자 경과 정보 서버 전송 */
		submitData() {
			if (commonService.isNull(this.writeModel.deftPrcgGbCd)) {
				return commonService.openFormInvalid('처리구분', this.$refs, 'deftPrcgGbCd');
			}
			if (commonService.isNull(this.writeModel.prcgPrStrtDt)) {
				return commonService.openFormInvalid('날자', this.$refs, 'prcgPrStrtDt');
			}
			if (commonService.isNull(this.writeModel.prcgPrStrtTm)) {
				return commonService.openFormInvalid('시작시간', this.$refs, 'prcgPrTm');
			}
			if (commonService.isNull(this.writeModel.prcgPrEndTm)) {
				return commonService.openFormInvalid('종료시간', this.$refs, 'prcgPrTm');
			}
			if (commonService.isNull(this.writeModel.pgprNm)) {
				return commonService.openFormInvalid('처리자', this.$refs, 'pgprNm');
			}
			if (commonService.isNull(this.writeModel.rmkCn)) {
				return commonService.openFormInvalid('내용', this.$refs, 'rmkCn');
			}
			let confirmMessage = '';
			let submitEndMessage = '';
			switch (this.writeModel.deftPrcgGbCd) {
				case 'WRK98':
					confirmMessage = 'ELPS.MSG_CONFIRM_DONE';
					submitEndMessage = 'ELPS.MSG_GUIDE_PHOTO';
					break;
				case 'WRK99':
					confirmMessage = 'ELPS.MSG_CONFIRM_LAST';
					break;
				default:
					confirmMessage = 'ELPS.MSG_CONFIRM_ING';
					break;
			}
			commonService.openConfirm(confirmMessage).then((bl) => {
				if (bl) {
					commonService.getUpdate('api/updateTcsDeftElps', commonService.getSubmitData(this.writeModel, { prcgPrStrtDt: 'date' })).then((view) => {
						if (view) {
							if (commonService.isNotNull(submitEndMessage)) {
								commonService.openToast(submitEndMessage, null, null, this.title, 'success');
							}
							modalController.dismiss(view, 'changed');
						}
					});
				}
			});
		},
		/** 기존 하자 경과 정보 불러 오기 (수정 모드 시 - 사용 안함)*/
		loadData() {
			if (commonService.isNotNull(this.elps)) {
				if (commonService.isNotNull(this.elps.rmkCn)) {
					this.writeModel = commonService.getIonicData(this.elps, { prcgPrStrtDt: 'date' });
					this.loadStatus = 'loaded';
					this.changeTitle();
				} else if (commonService.isNotNull(this.elps.deftNo) && commonService.isNotNull(this.elps.prcgElpsSeq)) {
					commonService
						.getView('api/selectTcsDeftElpsView/' + this.elps.deftNo + '/' + this.elps.prcgElpsSeq)
						.then((view) => {
							this.writeModel = view;
						})
						.finally(() => {
							this.loadStatus = commonService.isNotNull(this.writeModel) ? 'loaded' : 'nodata';
							this.changeTitle();
						});
				} else {
					this.writeModel.deftNo = this.elps.deftNo;
					switch (this.elps.deftPrcgGbCd) {
						case 'ING':
							this.writeModel.deftPrcgGbCd = 'WRK02';
							break;
						case 'DONE':
							this.writeModel.deftPrcgGbCd = 'WRK98';
							break;
						case 'END':
							this.writeModel.deftPrcgGbCd = 'WRK99';
							break;
					}
					this.loadStatus = 'loaded';
					this.changeTitle();
				}
			}
		},
	},
});
</script>

<style scoped lang="scss"></style>
