require(['config'], function() {
	require([
		'kakao.map.util'
	], function($) {
		pageObj.init();
	});
});

/********************************************************
 --------------------------------------------------------
 - Object 목록								            -
 --------------------------------------------------------
 * let pageObj = {}			: 화면공통 Object			*
 * let viewObj = {}			: 상세화면 Object			*
 * let modifyObj = {}		: 수정화면 Object			*
 *******************************************************/

let ITP_FORM_STINFO02011_DETAIL_IS_WRITING = false;

/*API URL*/
let API_DETAIL_GRID_LIST 	= '/api/store/detail-grid-list';	// 목록
let API_DETAIL_INFO 		= '/api/store/info-store';			// 상세
let API_DETAIL_SAVE 		= '/api/store/save-store';			// 저장
let API_INTI_PW				= '/api/store/inti-storePw';		// 패스워드 초기화

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
	},
	ui: {
		init: function () {
			this.view();
			this.form();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('STINFO02011');

			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});

			// 조회일자 지정
			var now = new Date();
			$('#ITP_TAB_STINFO02011 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_STINFO02011_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_STINFO02011_SEARCH_TO_DT').datepicker('setDate', new Date());

			// form alert message 주입
			$('input[data-alert-required="ONLYCHK"]').each(function() {
				if($(this).data('alert-msg') === 'addrses') {
					$(this).data('alert-msg', ITP_MSG_LOCALE.message.form.emptyAddr);
				}
			});
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_STINFO02011_DETAIL');
		},
		ready: function() {
			viewObj.init();
		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_STINFO02011_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_STINFO02011_CANCELLIST'			: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_STINFO02011_SAVE'					: modifyObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_FORM_STINFO02011_DETAIL_ADRES_SCH'	: modifyObj.button.address();	break;	// 신규등록 주소 찾기
					}
				});
			});

		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_STINFO02011_"]').hide();
		$('#ITP_TAB_STINFO02011').find('div[id$="_CONTAINER"]').hide();

		if(mode == 'LIST') { // 목록
			$('#ITP_AJAX_STINFO02011_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO02011_SRH', '#ITP_BTN_STINFO02011_NEWREG']);
			$('#ITP_FORM_STINFO02011_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
		} else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_STINFO02011_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO02011_SAVE']);

			$('#ITP_FORM_STINFO02011_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_STINFO02011_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});

			// 브랜드그룹 정보 설정
			$('#ITP_FORM_STINFO02011_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
			$('#ITP_FORM_STINFO02011_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_STINFO02011_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));

			$('#ITP_FORM_STINFO02011_DETAIL .fnStoreNm').text('(자동부여)');
			$('#ITP_FORM_STINFO02011_DETAIL_STORE_ID').removeAttr('readonly');
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_info').hide();
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_info').find('input, select').each(function(i, elem) {
				if($(this).data('check-required')) $(this).data('check-required', '');
				$(this).attr('disabled', true);
			});
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_change').show();
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_change').find('input, select').each(function(i, elem) {
				if($(this).data('check-required')) $(this).data('check-required', 'empty');
				$(this).attr('disabled', false);
			});

		} else if(mode == 'MODIFY') { // 수정
			$('#ITP_AJAX_STINFO02011_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO02011_CANCELLIST', '#ITP_BTN_STINFO02011_SAVE']);

			$('#ITP_FORM_STINFO02011_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
			$('#ITP_FORM_STINFO02011_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_STINFO02011_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));

			$('#ITP_FORM_STINFO02011_DETAIL_STORE_ID').attr('readonly', true);
			// $('#ITP_BTN_STINFO02011_RESET_PW').show();
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_info').show();
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_info').find('input, select').each(function(i, elem) {
				if($(this).data('check-required')) $(this).data('check-required', 'empty');
				$(this).attr('disabled', false);
			});
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_change').hide();
			$('#ITP_FORM_STINFO02011_DETAIL .itp_form_change').find('input, select').each(function(i, elem) {
				if($(this).data('check-required')) $(this).data('check-required', '');
				$(this).attr('disabled', true);
			});

		} else if(mode == 'VIEW') { // 보기
			$('#ITP_AJAX_STINFO02011_VIEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO02011_MODIFY']);
		}
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function () {
		const key = {brandId:fn_make_user_info.get('brandId'), storeId: fn_make_user_info.get('storeId'), viewCd: 'R'};
		var param = $.param(key);
		this.load(param);
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		console.log(result);
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_STINFO02011_VIEW #ITP_FORM_STINFO02011_VIEW_STORE_ID').val(result.storeId);
		$('#ITP_FORM_STINFO02011_VIEW .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnStCd').text(result.storeStNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnStoreId').text(result.storeId);
		$('#ITP_FORM_STINFO02011_VIEW .fnStoreDvsn').text(result.storeDvsnNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnStoreNm').text(result.storeNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnBsnsCndt').text(result.bsnsCndt);
		$('#ITP_FORM_STINFO02011_VIEW .fnBsnsRegNo').text(result.bsnsRegNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnCtgrBsns').text(result.ctgrBsns);
		$('#ITP_FORM_STINFO02011_VIEW .fnCorpRegNo').text(result.corpRegNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnTelNo').text(result.telNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnOwnerNm').text(result.ownerNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnFaxNo').text(result.faxNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnMblNo').text(result.telNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnEmalAddr').text(result.emalAddr);
		$('#ITP_FORM_STINFO02011_VIEW .fnAreaCd').text(result.areaNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnFromDt').text(result.openDt);
		$('#ITP_FORM_STINFO02011_VIEW .fnZipNo').text(result.zipNo);
		$('#ITP_FORM_STINFO02011_VIEW .fnLoadAddr').text(result.addr1);
		$('#ITP_FORM_STINFO02011_VIEW .fnDtlAddr').text(result.addr2);

		var cmpyAdres = result.addr1/* + ' ' + result.addr*/;
		kakaoMap.init('ITP_FORM_STINFO02011_VIEW', cmpyAdres);

		$('#ITP_FORM_STINFO02011_VIEW #ITP_FORM_STINFO02011_VIEW_BNK_CD').val(result.storeBnkCd);
		let storeBnkNm;
		if (result.storeBnkCd == '') {
			storeBnkNm = '';
		} else {
			storeBnkNm = $('#ITP_FORM_STINFO02011_VIEW_BNK_CD option:selected').text();
		}
		$('#ITP_FORM_STINFO02011_VIEW .fnStoreAcctInfo').text('(' + storeBnkNm + ') ' + result.storeAcctNo + ' (예금주) ' + result.storeOwacNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnLoanDvsn').text(result.loanDvsnNm + " - (" + itp_fn_number_comma(result.limitAmt) + "원)");
		$('#ITP_FORM_STINFO02011_VIEW .fnBrandAcctInfo').text('(' + result.brandBnkNm + ') ' + result.brandAcctNo + ' (예금주) ' + result.brandOwacNm);
		$('#ITP_FORM_STINFO02011_VIEW .fnAcctBal').text(itp_fn_number_comma(result.acctBal));
		$('#ITP_FORM_STINFO02011_VIEW .fnWhsUseYn').text(result.whsUseYn);
		$('#ITP_FORM_STINFO02011_VIEW .fnLimitAmt').text(result.limitAmt);
		$('#ITP_FORM_STINFO02011_VIEW .fnSysRegDttm').text(result.sysRegDttm);
		$('#ITP_FORM_STINFO02011_VIEW .fnCloseDt').text(result.closeDt);
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function () {
	},
	button: {
		modify: function () {
			itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO02011_DETAIL');
			const key = {brandId:fn_make_user_info.get('brandId'), storeId: fn_make_user_info.get('storeId'), viewCd: 'R'};
			var param = $.param(key);
			modifyObj.load(param);
		},
		address: function() {
			kakaoMap.address('ITP_FORM_STINFO02011_DETAIL');
		},
		cancel: function() {
			viewObj.init();
		},
		save: function() {
			const formId = '#ITP_FORM_STINFO02011_DETAIL';
			itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_DETAIL');
			itp_fn_form_clear_validate(null, formId);
			
			$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LOAN_DVSN').attr('disabled', false);
			$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LIMIT_AMT').attr('readonly', false);

			if (itp_fn_form_event.isValid(formId)) {
				let param = $(formId).serializeObject();
				console.log(JSON.stringify(param));
				var searhFn = function() {
					ITP_FORM_STINFO02011_DETAIL_IS_WRITING = false;
					viewObj.init();
				};
				fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
			}
		}
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		console.log(result);
		pageObj.switchScreen('MODIFY');

		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
		$('#ITP_FORM_STINFO02011_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_STORE_ST_CD').val(result.storeStCd);
		$('#ITP_FORM_STINFO02011_DETAIL .fnStoreNm').text(result.storeId);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_STORE_DVSN').val(result.storeDvsn);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_STORE_NM').val(result.storeNm);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_BSNS_CNDT').val(result.bsnsCndt);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_BSNS_REG_NO').val(result.bsnsRegNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_CTGR_BSNS').val(result.ctgrBsns);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_CORP_REG_NO').val(result.corpRegNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_TEL_NO').val(result.telNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_OWNER_NM').val(result.ownerNm);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_FAX_NO').val(result.faxNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_MBL_NO').val(result.telNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_EMAL_ADDR').val(result.emalAddr);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_AREA_CD').val(result.areaCd);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_FROM_DT').val(result.openDt);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_ZIPNO').val(result.zipNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_ADRES').val(result.addr1);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_ADRES_DTL').val(result.addr2);

		var cmpyAdres = result.addr1/* + ' ' + result.addr*/;
		kakaoMap.init('ITP_FORM_STINFO02011_DETAIL', cmpyAdres);

		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_BNK_CD').val(result.storeBnkCd);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_ACCT_NO').val(result.storeAcctNo);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_OWAC_NM').val(result.storeOwacNm);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LOAN_DVSN').val(result.loanDvsn);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LIMIT_AMT').val(result.limitAmt);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_WHS_USE_YN').val(result.whsUseYn);
		$('#ITP_FORM_STINFO02011_DETAIL .fnBrandAcctInfo').text(result.brandBnkNm + ' ' + result.brandAcctNo + ' ' + result.brandOwacNm);
		$('#ITP_FORM_STINFO02011_DETAIL .fnAcctBal').text(result.acctBal);
		$('#ITP_FORM_STINFO02011_DETAIL .fnTmpRcvAcctTotal').text(result.tmpRcvAcctTotal);
		$('#ITP_FORM_STINFO02011_DETAIL .fnSysChgDttm').text(result.sysChgDttm);
		
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LOAN_DVSN').attr('disabled', true);
		$('#ITP_FORM_STINFO02011_DETAIL #ITP_FORM_STINFO02011_DETAIL_LIMIT_AMT').attr('readonly', true);
		
	}
};
