require(['config'], function() {
	require([
		'jquery'
	], function($) {

	});
});

let DSPT_MNG_REQUEST_DSPT = '/api/dspt/mng/request-dspt';

var popPageObj = {
	init: function () {
		this.button();
		this.form();
		this.change();
		
		// 공통코드 표시
		$('select').each(function() {
	        if($(this).data('select-code')) {
	            fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
	        }
	    });

		$('#ITP_BIZPOP_DSPT_INFO_REG .input-group.date').datepicker(ITP_DATE_LANGUAGE);
		$('#ITP_FORM_DSPT_INFO_REG_POPUP_DPST_DT').datepicker('setDate', new Date());
		
		// 데이터 화면 표시
		$('#ITP_FORM_DSPT_INFO_REG_POPUP_BRAND_ID').val(popPageObj.args.brandId);
		$('#ITP_FORM_DSPT_INFO_REG_POPUP_STORE_ID').val(popPageObj.args.storeId);
		$('#ITP_FORM_DSPT_INFO_REG_POPUP_STTL_MGNT_UNQ_NO').val(popPageObj.args.sttlMgntUnqNo);
		$('#ITP_FORM_DSPT_INFO_REG_POPUP_DPST_AMT').val(popPageObj.args.goodsAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		
		$('#ITP_FORM_DSPT_INFO_REG_POPUP .fnBrandNm').text(popPageObj.args.brandNm);
		$('#ITP_FORM_DSPT_INFO_REG_POPUP .fnStoreNm').text(popPageObj.args.storeNm);
		
	},
	parentPopFn: null,
	args: null,
	returnType: null,
	button: function() {
		$('#ITP_BTN_PO_REJECT_REASON_CLOSE_POP').on('click', function () {
			$('#ITP_BIZPOP_DSPT_INFO_REG').modal('hide'); // 팝업닫기
		});
		$('#ITP_BIZPOP_DSPT_INFO_REG_BTN_DSPT_REQ').on('click', function () {
			// popPageObj.fnRjctRsnConfirm();
			
			const formId = '#ITP_FORM_DSPT_INFO_REG_POPUP';
			itp_fn_form_clear_validate(null, formId);
			
			var vali_dpstAmt = itp_fn_form_validate(formId, formId + '_DPST_AMT', ['empty','number'], undefined); // 입금금액
			var vali_dpstDt = itp_fn_form_validate(formId, formId + '_DPST_DT', ['empty'], undefined); // 입금일자
			var vali_dpstNm = itp_fn_form_validate(formId, formId + '_DPST_NM', ['empty'], undefined); // 입금자명
			var vali_dpstBnkCd = itp_fn_form_validate(formId, formId + '_DPST_BNK_CD', ['empty'], undefined); // 입금은행
			var vali_rcvAcctNo = itp_fn_form_validate(formId, formId + '_RCV_ACCT_NO', ['empty'], undefined); // 입금계좌번호
			
			if (vali_dpstAmt && vali_dpstDt && vali_dpstNm && vali_dpstBnkCd && vali_rcvAcctNo) {	
				
				var modalFn = {
					callBack: () => {
				
						let param = $(formId).serializeObject();
						var searhFn = function(result) {
							// console.log('##### ' + JSON.stringify(result));
							
							popPageObj.parentPopFn(result);
							$('#ITP_BIZPOP_DSPT_INFO_REG').modal('hide'); // 팝업닫기							
						};
						fn_ajax_call(DSPT_MNG_REQUEST_DSPT, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('입금 요청을 진행하시겠습니까?', modalFn);
			}
			
		});
	},
	form: function() {
		itp_fn_form_event.onKeyup('#ITP_FORM_DSPT_INFO_REG_POPUP');
	},
	change: function() {
	}, 	
	fnRjctRsnConfirm: function() {
		const formId = '#ITP_FORM_PO_REJECT_REASON_POPUP';
		itp_fn_form_clear_validate(null, formId);
		
		var vali_dsptRjctRsn = itp_fn_form_validate(formId, formId + '_DSPT_RJCT_RSN', ['empty'], undefined);
		
		if (vali_dsptRjctRsn) {		
			var dsptRjctRsn = $('#ITP_FORM_PO_REJECT_REASON_POPUP_DSPT_RJCT_RSN').val();	
			popPageObj.parentPopFn(dsptRjctRsn);
			$('#ITP_BIZPOP_DSPT_INFO_REG').modal('hide'); // 팝업닫기
		}
	} 
};

function itp_fn_BIZPOP_DSPT_INFO_REG(parentPopFn, args, returnType) {
	$('#ITP_BIZPOP_DSPT_INFO_REG').one('shown.bs.modal', function(e) {
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_DSPT_INFO_REG').remove();
	}).modal('show');

	popPageObj.parentPopFn = parentPopFn;
	popPageObj.args = args;
	popPageObj.returnType = returnType;
	popPageObj.init();
}
