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

/*API URL*/
const API_MOBILE_GRID_LIST 			= '/api/unpaid/mng/detail-grid-list';	// 목록
const API_MOBILE_INFO 				= '/api/unpaid/mng/info-unpaid';		// 상세
const API_MOBILE_HISTORY			= '/api/unpaid/mng/stl-grid-list';		// 이력
const API_POP_SEARCH_LIST			= '/api/store/pop-grid-list';

// 이지페이 결제 생성
let EASYPAY_PAYMENT_CREATE 			= '/api/easypay/payment/create';
let EASYPAY_PAYMENT_REQUEST 		= '/api/easypay/payment/request';

const PAGE_MODE_LIST				= "LIST";
const PAGE_MODE_VIEW				= "VIEW";
const PAGE_MODE_PAY					= "PAY";

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

			// 조회일자 지정
			$('#ITP_MOBILE_MLOANMNG03010 .itp_input.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH_FROM_DT').datepicker('setDate', itp_fn_date_add('M', -1));
			$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH_TO_DT').datepicker('setDate', itp_fn_date_add('M', 0));

			if(fn_make_user_info.get('authTpCd') === '50') {
				$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_FORM_MLOANMNG03010_LIST_SEARCH_STORE_NM').val(fn_make_user_info.get('storeNm'));
				$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_BTN_MLOANMNG03010_LIST_SEARCH_STORE_NM').hide();
				$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_BTN_MLOANMNG03010_LIST_DELETE_STORE_NM').hide();
			}

			CommonObj.btnPay('#ITP_BTN_MLOANMNG03010_VIEW_PAYMENT');
		},
		info: function() {
			$('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_MLOANMNG03010_LIST"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MLOANMNG03010_LIST_SEARCH_STORE_NM'	: mobPopObj.popStoreNm.init(); 		break;
					case 'ITP_BTN_MLOANMNG03010_LIST_DELETE_STORE_NM'	: mobPopObj.popStoreNm.delete(); 	break;
					case 'ITP_BTN_MLOANMNG03010_LIST_SEARCH'			: mobContObj.list.search();		break;
				}
				return false;
			});

			$('button[id^="ITP_BTN_MLOANMNG03010_VIEW"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MLOANMNG03010_VIEW_SEARCH'		: mobContObj.view.history.search();	break;
					case 'ITP_BTN_MLOANMNG03010_VIEW_PAYMENT'		: mobContObj.view.payment.view();	break;
					case 'ITP_BTN_MLOANMNG03010_VIEW_CANCEL_LIST'	: mobContObj.view.cancel();			break;
				}
				return false;
			});

			$('button[id^="ITP_BTN_MLOANMNG03010_PAY"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MLOANMNG03010_PAY_REQ'			: mobContObj.view.payment.request();	break;
					case 'ITP_BTN_MLOANMNG03010_PAY_CANCEL'			: mobContObj.view.payment.cancel();	break;
				}
				return false;
			});

			$('select[id="ITP_FORM_MLOANMNG03010_PAY_EMAIL_CD"]').on('change', function() {
				var val = $(this).text();
				var index = $("#ITP_FORM_MLOANMNG03010_PAY_EMAIL_CD option").index($("#ITP_FORM_MLOANMNG03010_PAY_EMAIL_CD option:selected"));
				var emailId = '#ITP_FORM_MLOANMNG03010_PAY_EMAIL_NM';
				(index === 0) ? $(emailId).val('').attr('readonly', false) : $(emailId).val($("#ITP_FORM_MLOANMNG03010_PAY_EMAIL_CD option:selected").text()).attr('readonly', true);
				$('#ITP_FORM_MLOANMNG03010_PAY_ORD_EMAIL').val(
					$('#ITP_FORM_MLOANMNG03010_PAY_EMAIL_ID').val() + '@' + $('#ITP_FORM_MLOANMNG03010_PAY_EMAIL_NM').val()
				);
			});

			$(document).on('click', '#ITP_LIST_MLOANMNG03010_LIST_ITEM_ROWS li', function() {
				var item = mobContObj.list.rows[$(this).index()];
				mobContObj.view.init(item);
			});

			$(document).on('click', '#ITP_LIST_MLOANMNG03010_POP_STORE_ROWS li button', function() {
				mobPopObj.popWhsNm.choice($(this));
			});
		}
	},
	switchScreen: function(mode) {
		this.viewMode = mode;
		$('#ITP_MOBILE_MLOANMNG03010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === PAGE_MODE_LIST) {
			$('#ITP_AJAX_MLOANMNG03010_LIST_CONTAINER').show();
		} else if(mode === PAGE_MODE_VIEW) {
			$('#ITP_AJAX_MLOANMNG03010_VIEW_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MLOANMNG03010_LIST_ITEM_AREA', false);
		} else if(mode === PAGE_MODE_PAY) {
			$('#ITP_AJAX_MLOANMNG03010_PAY_CONTAINER').show();
		}
	},
	ready: function() {
		mobContObj.list.init();
	}
};

let mobContObj = {
	list: {
		listPage: 1,
		rows: [],
		init: function() {
			mobPageObj.switchScreen(PAGE_MODE_LIST);
			this.search();
		},
		search: function() {
			this.listPage = 1;
			this.rows.length = 0;
			$('#ITP_LIST_MLOANMNG03010_LIST_ITEM_ROWS').empty();
			setTimeout(function() {
				mobContObj.list.load();
			}, 100);
		},
		pager: function(totPage) {
			(this.listPage < totPage) ?
				CommonObj.moreView('#ITP_LIST_MLOANMNG03010_LIST_ITEM_AREA', true, mobContObj.list.load) :
				CommonObj.moreView('#ITP_LIST_MLOANMNG03010_LIST_ITEM_AREA', false);
			this.listPage += 1;
		},
		load: function() {
			var _this = this;
			var callbackFn = function(result) {
				console.log(result);
				mobContObj.list.pager(result.gridTotal);
				$.each(result.gridRows, function (i, item) {
					$('#ITP_LIST_MLOANMNG03010_LIST_ITEM_AREA .panel-group').append($('#ITP_LIST_MLOANMNG03010_LIST_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MLOANMNG03010_LIST_ITEM_AREA .panel-group > .item-row:last');
					$li.find('.fnStoreNm').text(item.storeNm);
					$li.find('.fnLoanDvsnNm').text(item.loanDvsnNm);
					$li.find('.fnSttlMgntUnqNo').text(item.sttlMgntUnqNo);
					$li.find('.fnSttlStNm').text(item.sttlStNm);
					$li.find('.fnSttlReqDt').text(item.sttlReqDt);
					$li.find('.fnSttlReqAmt').text(CommonObj.currency.add(item.sttlReqAmt));
					$li.find('.fnSttlDt').text(CommonObj.nullToString(item.sttlDt));
					$li.find('.fnUnpaidAmt').text(CommonObj.currency.add(item.unpaidAmt));
					$li.find('.fnUseAmtTotal').text(CommonObj.currency.add(item.useAmtTotal));
					mobContObj.list.rows.push(item);
				});
			};
			const param = $('#ITP_FORM_MLOANMNG03010_LIST_SEARCH').serializeObject();
			param.gridPage = mobContObj.list.listPage;
			param.gridSize = mobile_list_size;
			// console.log(JSON.stringify(param));
			fn_ajax_call(API_MOBILE_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
		}
	},
	view: {
		item: null,
		init: function(item) {
			this.item = item;
			mobPageObj.switchScreen(PAGE_MODE_VIEW);
			this.search(item);
		},
		search: function(item) {
			var _this = this;
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result);
			};
			const param = {'sttlMgntUnqNo': item.sttlMgntUnqNo};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MLOANMNG03010_VIEW_CONTAINER';
			$(id).find('.fnStoreNm').text(result.storeNm);
			$(id).find('.fnSttlStNm').text(result.sttlStNm);
			$(id).find('.fnSttlReqDt').text(result.sttlReqDt);
			$(id).find('.fnSttlDt').text(result.sttlDt);
			$(id).find('.fnSttlReqAmt').text(CommonObj.currency.add(result.sttlReqAmt));
			$(id).find('.fnUnpaidAmt').text(CommonObj.currency.add(result.unpaidAmt));
			$(id).find('.fnUseAmtTotal').text(CommonObj.currency.add(result.useAmtTotal));
			$('#ITP_FORM_MLOANMNG03010_VIEW #ITP_FORM_MLOANMNG03010_VIEW_STORE_ID').val(result.storeId);
			$('#ITP_FORM_MLOANMNG03010_VIEW #ITP_FORM_MLOANMNG03010_VIEW_STTL_MGNT_UNQ_NO').val(result.sttlMgntUnqNo);
			$('#ITP_FORM_MLOANMNG03010_VIEW #ITP_FORM_MLOANMNG03010_VIEW_UNPAID_AMT').val(result.unpaidAmt);
			$('#ITP_FORM_MLOANMNG03010_VIEW #ITP_FORM_MLOANMNG03010_VIEW_RCPT_YN').val(result.rcptYn);

			$('#ITP_FORM_MLOANMNG03010_PAY #ITP_FORM_MLOANMNG03010_PAY_STORE_ID').val(result.storeId);
			$('#ITP_FORM_MLOANMNG03010_PAY #ITP_FORM_MLOANMNG03010_PAY_GOODS_AMT').val(result.unpaidAmt);
			_this.history.init(result.sttlMgntUnqNo);
		},
		history: {
			rows: [],
			init: function(sttlMgntUnqNo) {
				// 조회일자 지정
				$('#ITP_FORM_MLOANMNG03010_VIEW_SEARCH_FROM_DT').val(itp_fn_date_add('M', -1));
				$('#ITP_FORM_MLOANMNG03010_VIEW_SEARCH_TO_DT').val(itp_fn_date_add('M', 0));
				this.sttlMgntUnqNo = sttlMgntUnqNo;
				this.search();
			},
			search: function() {
				this.listPage = 0;
				this.totPage = 0;
				this.rows.length = 0;
				$('#ITP_LIST_MLOANMNG03010_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_MLOANMNG03010_VIEW_ITEM_AREA .panel-group').append($('#ITP_LIST_MLOANMNG03010_VIEW_ITEM_ROWCOPY').html());
						var $li = $('#ITP_LIST_MLOANMNG03010_VIEW_ITEM_AREA .panel-group > .item-row:last');
						$li.find('.fnItemId').text(item.itemId);
						$li.find('.fnItemNm').text(item.itemNm);
						$li.find('.fnUnitAmt').text(CommonObj.currency.add(item.unitAmt));
						$li.find('.fnUnit').text(item.unit);
						$li.find('.fnPodrQyt').text(item.podrQty + '(' + item.dlvQty + ')');
						$li.find('.fnWhsColDt').text(item.whsColDt);
						$li.find('.fnWhsColQty').text(CommonObj.comma.set(item.whsColQty) + ' ' + item.unit);
						$li.find('.fnWhsColAmt').text(CommonObj.currency.add(item.whsColAmt));
						$li.find('.fnSttlDvsnNm').text(item.sttlDvsnNm);
						_this.rows.push(item);
					});
				};
				var errFn = function() { _this.isSearch = false;};
				const param = {};
				param.gridPage = ++this.listPage;
				param.gridSize = this.listSize;
				param.sttlMgntUnqNo = _this.sttlMgntUnqNo;
				// console.log(JSON.stringify(param));
				fn_ajax_call(API_MOBILE_HISTORY, JSON.stringify(param), callbackFn, 'POST', errFn);
			}
		},
		payment: {
			view: function() {
				// if(CommonObj.isApp) {
				// 	mobPageObj.switchScreen(PAGE_MODE_PAY);
				// 	this.load();
				// } else {
				// 	this.popup();
				// }
				this.popup();
			},
			load: function() {
				var callbackFn = function(result) {
					console.log(result);
					var id = '#ITP_AJAX_MLOANMNG03010_PAY_CONTAINER';
					$(id).find('.fnPoOrdNo').text(result.poOrdNo);
					$(id).find('.fnOrdNm').text(result.ordNm);
					$(id).find('.fnGoodsNm').text(result.goodsNm);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_PO_ORD_NO').val(result.poOrdNo);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_ORD_NM').val(result.ordNm);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_GOODS_NM').val(result.goodsNm);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_MALL_ID').val(result.mallId);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_MOBILE_URL').val(result.kicc_action_mobile_url);
					$(id).find('#ITP_FORM_MLOANMNG03010_PAY_JS_URL').val(result.kicc_javascript_url);
					if(result.ordEmail) {
						var email = result.ordEmail.split('@');
						if(email.length === 2) {
							$(id).find('#ITP_FORM_MLOANMNG03010_PAY_ORD_EMAIL').val(email);
							$(id).find('#ITP_FORM_MLOANMNG03010_PAY_EMAIL_ID').val(email[0]);
							$(id).find('#ITP_FORM_MLOANMNG03010_PAY_EMAIL_NM').val(email[1]);
						}
					}
				};
				// 결제타입코드 : 10(선불(충전)), 20(후불(결제)), 30(후불(선결제))
				var param = $('#ITP_FORM_MLOANMNG03010_VIEW').serializeObject();
				fn_ajax_call(EASYPAY_PAYMENT_CREATE, JSON.stringify(param), callbackFn, 'POST');
			},
			popup: function() {
				// 결제 팝업 결과
				var popFn = function(rowDataPop) {
					console.log('rowDataPop : ' + JSON.stringify(rowDataPop));
					mobContObj.list.search();
				};
				// 결제 팝업
				// 결제타입코드 : 10(선불(충전)), 20(후불(결제)), 30(후불(선결제))
				var args = {
					'payTpCd': '20',
					'brandId': $('#ITP_FORM_MLOANMNG03010_VIEW_BRAND_ID').val(),
					'storeId': $('#ITP_FORM_MLOANMNG03010_VIEW_STORE_ID').val(),
					'sttlMgntUnqNoList': $('#ITP_FORM_MLOANMNG03010_VIEW_STTL_MGNT_UNQ_NO').val(),
					'goodsAmt': $('#ITP_FORM_MLOANMNG03010_VIEW_UNPAID_AMT').val()
				};
				fn_call_popup('biz', 'BIZPOP_PO_PAYMENT', '#ITP_ASIDE', popFn, args, 'M');
			},
			request: function() {
				var param = $('#ITP_FORM_MLOANMNG03010_PAY').serializeObject();
				console.log(param);
				location.replace('app:payment:' + JSON.stringify(param));
			},
			payResult: function(result) {
				console.log(result);
			},
			cancel: function() {
				$(id).find('.fnPoOrdNo').text('');
				$(id).find('.fnOrdNm').text('');
				$(id).find('.fnGoodsNm').text('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_PO_ORD_NO').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_ORD_NM').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_GOODS_NM').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_GOODS_AMT').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_MALL_ID').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_MOBILE_URL').val('');
				$(id).find('#ITP_FORM_MLOANMNG03010_PAY_JS_URL').val('');
				mobPageObj.switchScreen(PAGE_MODE_VIEW);
			}
		},
		payRequest: function() {
			location.replace('app:payment');
		},
		payCancel: function() {
			mobPageObj.switchScreen(PAGE_MODE_VIEW);
		},
		cancel: function () {
			var id = '#ITP_AJAX_MLOANMNG03010_VIEW_CONTAINER';
			$(id).find('.fnStoreNm').text('');
			$(id).find('.fnLoanDvsnNm').text('');
			$(id).find('.fnSttlStNm').text('');
			$(id).find('.fnSttlReqDt').text('');
			$(id).find('.fnSttlAmt').text('');
			$(id).find('.fnSttlDt').text('');
			$('#ITP_LIST_MLOANMNG03010_VIEW_ITEM_AREA .panel-group').empty();
			mobPageObj.switchScreen(PAGE_MODE_LIST);
		}
	}
};

let mobPopObj = {
	popStoreNm: {
		popId: 'ITP_POP_MLOANMNG03010_STORE_AREA',
		rows: [],
		init: function () {
			this.rows.length = 0;
			mobPopObj.show(this.popId);
			this.search();
			this.action();
		},
		search: function () {
			var _this = this;
			$('#ITP_LIST_MLOANMNG03010_POP_STORE_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result.gridRows);
			};
			const param = $('#ITP_FORM_MLOANMNG03010_POP_STORE').serializeObject();
			fn_ajax_call(API_POP_SEARCH_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		view: function(gridRows) {
			this.rows = gridRows;
			$.each(gridRows, function (i, item) {
				$('#ITP_LIST_MLOANMNG03010_POP_STORE_AREA .panel-group').append($('#ITP_LIST_MLOANMNG03010_POP_STORE_ROWCOPY').html());
				var $li = $('#ITP_LIST_MLOANMNG03010_POP_STORE_AREA .panel-group > .row_2:last');
				$li.find('.fnStoreNm').text(item.storeNm);
				$li.find('.fnStoreStNm').text(item.storeStNm);
				$li.find('.fnStoreId').data('store-id', item.storeId);
			});
		},
		choice: function(elem) {
			var _this = this;
			var storeId = $(elem).data('store-id');
			$.each(this.rows, function (i, item) {
				if(storeId === item.storeId) {
					$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_FORM_MLOANMNG03010_LIST_SEARCH_STORE_ID').val(item.storeId);
					$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_FORM_MLOANMNG03010_LIST_SEARCH_STORE_NM').val(item.storeNm);
					_this.close();
					return false;
				}
			});
		},
		action: function() {
			var _this = this;
			$('button[id^="ITP_BTN_MLOANMNG03010_POP_STORE"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MLOANMNG03010_POP_STORE_SEARCH'	: _this.search();			break;
					case 'ITP_BTN_MLOANMNG03010_POP_STORE_CHOICE'	: _this.choice($(this)); 	break;
					case 'ITP_BTN_MLOANMNG03010_POP_STORE_CLOSE'	: _this.close(); 			break;
				}
				return false;
			});
		},
		delete: function() {
			$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_FORM_MLOANMNG03010_LIST_SEARCH_STORE_ID').val('');
			$('#ITP_FORM_MLOANMNG03010_LIST_SEARCH #ITP_FORM_MLOANMNG03010_LIST_SEARCH_STORE_NM').val('');
		},
		close: function() {
			this.rows.length = 0;
			$('#ITP_POP_MLOANMNG03010_LIST_WHS_AREA .panel-group').empty();
			$('#ITP_FORM_MLOANMNG03010_POP_STORE #ITP_FORM_MLOANMNG03010_POP_STORE_KEYWORD').val('');
			mobPopObj.hide(this.popId);
		}
	},
	show: function(popId) {
		$('button[id$="_CLOSE"]').off('click').on('click', function() {
			console.log($(this).attr('id'));
			if($(this).hasClass('btn-pop-close')) {
				mobPopObj.hide(popId);
			}
		});
		$('#' + popId).show();
	},
	hide: function(popId) {
		$('#' + popId).closest('.mobile-pop-close').hide();
	}
};
