require(['config'], function() {
	require([
	], function($) {
		mobPageObj.init();
	});
});

/*API URL*/
const API_MOBILE_INFO 				= '/api/loan/state/info-loan-store';	// 상세
const API_MOBILE_HISTORY			= '/api/loan/state/hist-grid-list';		// 이력

let mobPageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
		this.ready();
	},
	ui: {
		init: function () {
			this.view();
		},
		view: function() {
			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});

			// 조회일자 지정
			$('#ITP_MOBILE_MLOANMNG01011 .itp_input.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH_FROM_DT').datepicker('setDate', itp_fn_date_add('M', -1));
			$('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH_TO_DT').datepicker('setDate', itp_fn_date_add('M', 0));
			
			$('input:hidden[id$="_BRAND_ID"]').val(fn_make_user_info.get('brandId'));
			$('input:hidden[id$="_STORE_ID"]').val(fn_make_user_info.get('storeId'));
		}
	},
	event: {
		init: function() {
			this.button();
		},
		button: function() {
			$('button[id^="ITP_BTN_MLOANMNG01011_VIEW"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MLOANMNG01011_VIEW_SEARCH'		: mobContObj.view.history.search();	break;
				}
				return false;
			});
		}
	},
	ready: function() {
		mobContObj.view.init();
	}
};

let mobContObj = {
	view: {
		init: function() {
			this.load();
		},
		load: function(item) {
			var _this = this;
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result);
			};
			fn_ajax_call(API_MOBILE_INFO, null, callbackFn, 'GET');
		},
		view: function(result) {
			var id = '#ITP_AJAX_MLOANMNG01011_VIEW_CONTAINER';
			$(id).find('.fnStoreNm').text(result.storeNm);
			$(id).find('.fnLoanDvsnNm').text(result.loanDvsnNm);
			var limitAmt;
			if(result.loanDvsn == 'LD01') { // 선불(충전)
				limitAmt = result.chrgeLimitAmt;
			} else if(result.loanDvsn == 'LD02') { // 후불(신용)
				limitAmt = result.credtLimitAmt;
			}
			$(id).find('.fnLimitAmt').text(CommonObj.currency.add(limitAmt));
			$(id).find('.fnUseYnNm').text(result.useYnNm);
			$(id).find('.fnPosbLimitAmt').text(CommonObj.currency.add(limitAmt + result.acctBal - result.useAmtTotal));
			$(id).find('.fnTmpRcvAcctTotal').text(CommonObj.currency.add(result.tmpRcvAcctTotal));
			$(id).find('.fnUseAmtTotal').text(CommonObj.currency.add(result.useAmtTotal));

			$(id).find('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH_LOAN_MGNT_UNQ_NO').val(result.loanMgntUnqNo);
			this.history.init();
		},
		history: {
			rows: [],
			init: function() {
				// 조회일자 지정
				$('#ITP_MOBILE_MLOANMNG01011 .itp_input.date').datepicker(ITP_DATE_LANGUAGE);
				$('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH_FROM_DT').datepicker('setDate', itp_fn_date_add('M', -1));
				$('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH_TO_DT').datepicker('setDate', itp_fn_date_add('M', 0));

				this.search();
				this.action();
			},
			action: function() {
				var _this = this;
				$('#ITP_LIST_MLOANMNG01011_VIEW_ITEM_AREA').on('scroll', function () {
					if($(this).scrollTop() + $(this).innerHeight() + 1 >= $(this)[0].scrollHeight) {
						if(_this.totPage > _this.listPage) {
							_this.load();
						}
					}
				});
			},
			search: function() {
				this.listPage = 0;
				this.totPage = 0;
				this.rows.length = 0;
				$('#ITP_LIST_MLOANMNG01011_VIEW_ITEM_ROWS').empty();
				this.load();
			},
			load: function() {
				var _this = this;
				this.isSearch = true;
				var callbackFn = function(result) {
					console.log(result);
					_this.isSearch = false;
					_this.totPage = result.gridTotal;
					$.each(result.gridRows, function (i, item) {
						$('#ITP_LIST_MLOANMNG01011_VIEW_ITEM_AREA .panel-group').append($('#ITP_LIST_MLOANMNG01011_VIEW_ITEM_ROWCOPY').html());
						var $li = $('#ITP_LIST_MLOANMNG01011_VIEW_ITEM_AREA .panel-group > .item-row:last');
						$li.find('.fnLoanRegDt').text(item.loanRegDt + ' ' + item.loanRegTm);
						$li.find('.fnDpstPayDvsnNm').text(item.dpstPayDvsnNm);
						$li.find('.fnTrscAmt').text(CommonObj.currency.add(item.trscAmt));
						$li.find('.fnTrscAfBal').text(CommonObj.currency.add(item.trscAfBal));
						_this.rows.push(item);
					});
				};
				var errFn = function() { _this.isSearch = false;};
				const param = $('#ITP_FORM_MLOANMNG01011_VIEW_SEARCH').serializeObject();
				param.gridPage = ++this.listPage;
				param.gridSize = this.listSize;
				console.log(JSON.stringify(param));
				fn_ajax_call(API_MOBILE_HISTORY, JSON.stringify(param), callbackFn, 'POST', errFn);
			}
		}
	}
};
