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

/*API URL*/
const API_MOBILE_GRID_LIST 			= '/api/stock/mng/detail-grid-list';	// 목록
const API_MOBILE_INFO 				= '/api/stock/mng/info-stock';			// 상세
const API_MOBILE_SAVE 				= '/api/stock/mng/proc-stock';			// 저장
const API_MOBILE_HISTORY			= '/api/stock/mng/hist-grid-list';		// 이력
const API_MOBILE_WHS_GRID_LIST 		= '/api/whs/mng/detail-grid-list';
const API_MOBILE_LOCATION_LIST 		= '/api/whs/mng/location-grid-list';
const API_POP_SEARCH_LIST			= '/api/store/pop-grid-list';

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

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() {
			if(fn_make_user_info.get('authTpCd') === '50') {
				$('#ITP_BTN_MSTOCKMNG01010_LIST_ADD_STCK').hide();
			}
		},
		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.action();
		},
		action: function() {
			$('button[id^="ITP_BTN_MSTOCKMNG01010_LIST"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MSTOCKMNG01010_LIST_SEARCH_WHS_NM'	: mobPopObj.popWhsNm.init(); 		break;
					case 'ITP_BTN_MSTOCKMNG01010_LIST_DELETE_WHS_NM'	: mobPopObj.popWhsNm.delete(); 		break;
					case 'ITP_BTN_MSTOCKMNG01010_LIST_SEARCH'			: mobContObj.list.search();		break;
					case 'ITP_BTN_MSTOCKMNG01010_LIST_ADD_STCK'			: mobContObj.list.procStck(); 	break;
				}
				return false;
			});

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

			$('select[id="ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_WHS_NM"]').on('change', function() {
				mobContObj.list.locationList();
			});

			$(document).on('click', '#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_ROWS li', function() {
				var item = mobContObj.list.rows[$(this).index()];
				mobContObj.list.choice(item);
				$('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_ROWS li').removeClass('check');
				$(this).addClass('check');
			});

			$(document).on('click', '#ITP_LIST_MSTOCKMNG01010_POP_WHS_ROWS li button', function() {
				mobPopObj.popWhsNm.choice($(this));
			});
		}
	},
	switchScreen: function(mode) {
		$('#ITP_MOBILE_MSTOCKMNG01010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === PAGE_MODE_LIST) {
			$('#ITP_AJAX_MSTOCKMNG01010_LIST_CONTAINER').show();
		} else if(mode === PAGE_MODE_VIEW) {
			$('#ITP_AJAX_MSTOCKMNG01010_VIEW_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_AREA', false);
		}
		this.viewMode = mode;
	},
	ready: function() {
		mobContObj.list.init();
	}
};

let mobContObj = {
	list: {
		listPage: 1,
		rows: [],
		item: null,
		init: function() {
			mobPageObj.switchScreen(PAGE_MODE_LIST);
			// this.search();
		},
		search: function() {
			this.listPage = 1;
			this.rows.length = 0;
			$('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_ROWS').empty();
			setTimeout(function() {
				mobContObj.list.load();
			}, 100);
		},
		pager: function(totPage) {
			(this.listPage < totPage) ?
				CommonObj.moreView('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_AREA', true, mobContObj.list.load) :
				CommonObj.moreView('#ITP_LIST_MSTOCKMNG01010_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_MSTOCKMNG01010_LIST_ITEM_AREA .panel-group').append($('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MSTOCKMNG01010_LIST_ITEM_AREA .panel-group > .item-row:last');
					$li.find('.fnWhsId').text(item.whsId);
					$li.find('.fnWhsNm').text(item.whsNm);
					$li.find('.fnLocationNm').text(CommonObj.nullToString(item.locationNm));
					$li.find('.fnItemId').text(item.itemId);
					$li.find('.fnItemNm').text(item.itemNm);
					$li.find('.fnUnit').text(item.unit);
					$li.find('.fnUnitQty').text(item.unitQty);
					$li.find('.fnStckQty').text(item.stckQty);
					$li.find('.fnPrpStckQty').text(item.prpStckQty);
					$li.find('.fnSftStckRt').text(CommonObj.nullToString(item.sftStckRt));
					mobContObj.list.rows.push(item);
				});
			};
			const param = $('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH').serializeObject();
			param.gridPage = mobContObj.list.listPage;
			param.gridSize = mobile_list_size;
			param.pagingYn = true;
			// console.log(JSON.stringify(param));
			fn_ajax_call(API_MOBILE_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		choice: function(item) {
			this.item = item;
		},
		procStck: function() {
			if(!this.item) {
				itp_fn_modal_alert(ITP_MSG_LOCALE.message.ajax.noData);
				return;
			}
			mobPopObj.popProcStck.init(this.item);
		}
	},
	view: {
		init: function(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 = {
				'brandId': fn_make_user_info.get('brandId'),
				'spplyId': fn_make_user_info.get('spplyId'),
				'loanMgntUnqNo': item.loanMgntUnqNo
			};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MSTOCKMNG01010_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.useAmtTotal));
			$(id).find('.fnTmpRcvAcctTotal').text(CommonObj.currency.add(result.tmpRcvAcctTotal));
			$(id).find('.fnUseAmtTotal').text(CommonObj.currency.add(result.useAmtTotal));
			_this.history.init(result.loanMgntUnqNo);
		},
		cancel: function () {
			var id = '#ITP_AJAX_MSTOCKMNG01010_VIEW_CONTAINER';
			$(id).find('.fnItemid').text('');
			$(id).find('.fnItemNm').text('');
			$(id).find('.fnUPodrQty').text('');
			$(id).find('.fnUnitAmt').text('');
			$(id).find('.fnUnit').text('');
			$(id).find('.fnPodrQty').text('');
			$(id).find('.fnPodrAmt').text('');
			$(id).find('.fnDlvReqDt').text('');
			$('#ITP_LIST_MSTOCKMNG01010_VIEW_ITEM_AREA .panel-group').empty();
			mobPageObj.switchScreen(PAGE_MODE_LIST);
		}
	}
};

let mobPopObj = {
	popWhsNm: {
		popId: 'ITP_POP_MSTOCKMNG01010_WHS_AREA',
		rows: [],
		init: function () {
			this.rows.length = 0;
			mobPopObj.show(this.popId);
			this.search();
			this.action();
		},
		search: function () {
			var _this = this;
			$('#ITP_LIST_MSTOCKMNG01010_POP_WHS_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result.gridRows);
			};
			const param = $('#ITP_FORM_MSTOCKMNG01010_POP_WHS').serializeObject();
			(fn_make_user_info.get('authTpCd') === '50') ? param.swhsDvsn = 'W02' : param.swhsDvsn = 'W01';
			fn_ajax_call(API_MOBILE_WHS_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		view: function(gridRows) {
			this.rows = gridRows;
			$.each(gridRows, function (i, item) {
				$('#ITP_LIST_MSTOCKMNG01010_POP_WHS_AREA .panel-group').append($('#ITP_LIST_MSTOCKMNG01010_POP_WHS_ROWCOPY').html());
				var $li = $('#ITP_LIST_MSTOCKMNG01010_POP_WHS_AREA .panel-group > .row_2:last');
				$li.find('.fnWhsNm').text(item.whsNm);
				$li.find('.fnLocationNm').text(item.locationNm);
				$li.find('.fnWhsId').data('whs-id', item.whsId);
			});
		},
		choice: function(elem) {
			var _this = this;
			var whsId = $(elem).data('whs-id');
			$.each(this.rows, function (i, item) {
				if(whsId === item.whsId) {
					$('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH #ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_WHS_ID').val(item.whsId);
					$('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH #ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_WHS_NM').val(item.whsNm);
					_this.close();
					_this.location(item.whsId);
					return false;
				}
			});
		},
		location: function(whsId) {
			var callbackFn = function(result) {
				fn_make_select_location(result.gridRows, 'ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_LOCATION');
			};
			var param = {
				'brandId': fn_make_user_info.get('brandId'),
				'storeId': fn_make_user_info.get('storeId'),
				'whsId': whsId
			};
			fn_ajax_call(API_MOBILE_LOCATION_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		action: function() {
			var _this = this;
			$('button[id^="ITP_BTN_MSTOCKMNG01010_POP_WHS"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MSTOCKMNG01010_POP_WHS_SEARCH'	: _this.search();			break;
					case 'ITP_BTN_MSTOCKMNG01010_POP_WHS_CHOICE'	: _this.choice($(this)); 	break;
					case 'ITP_BTN_MSTOCKMNG01010_POP_WHS_CLOSE'		: _this.close(); 			break;
				}
				return false;
			});
		},
		delete: function() {
			$('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH #ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_WHS_ID').val('');
			$('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH #ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_WHS_NM').val('');
			$('#ITP_FORM_MSTOCKMNG01010_LIST_SEARCH #ITP_FORM_MSTOCKMNG01010_LIST_SEARCH_LOCATION option').remove();
		},
		close: function() {
			this.rows.length = 0;
			$('#ITP_POP_MSTOCKMNG01010_LIST_WHS_AREA .panel-group').empty();
			$('#ITP_FORM_MSTOCKMNG01010_POP_WHS #ITP_FORM_MSTOCKMNG01010_POP_WHS_KEYWORD').val('');
			mobPopObj.hide(this.popId);
		}
	},
	popProcStck: {
		item: null,
		popId: 'ITP_POP_MSTOCKMNG01010_ITEM_AREA',
		init: function(item) {
			this.item = item;
			this.reset();
			this.action();
			this.view();
			mobPopObj.show(this.popId);
		},
		view: function() {
			console.log(this.item);
			var id = '#' + this.popId;
			$(id).find('.fnWhsNm').text(this.item.whsNm);
			$(id).find('.fnLocation').text(this.item.locationNm);
			$(id).find('.fnItemId').text(this.item.itemId);
			$(id).find('.fnItemNm').text(this.item.itemNm);
			$(id).find('.fnStckQty').text(CommonObj.currency.add(this.item.stckQty, '개'));
			$(id).find('.fnWhsNm').text(this.item.whsNm);
			$(id).find('.fnMgntLevel').text(this.item.mgntLevel);
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_BRAND_ID').val(this.item.brandId);
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_ITEM_ID').val(this.item.itemId);
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_SOURCE_LOCATION').val(this.item.location);
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_SOURCE_WHS_ID').val(this.item.whsId);
			this.whsList();
		},
		action: function() {
			var _this = this;
			$('button[id^="ITP_BTN_MSTOCKMNG01010_POP_PROC_STCK"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MSTOCKMNG01010_POP_PROC_STCK_REQ'			: _this.procStck();	break;
					case 'ITP_BTN_MSTOCKMNG01010_POP_PROC_STCK_CANCEL'		: _this.cancel(); 	break;
				}
				return false;
			});
			$('select[id^="ITP_FORM_MSTOCKMNG01010_POP_WHS_NM"]').off('change').on('change', function() {
				_this.locationList();
			});
			itp_fn_form_event.onKeyup('#ITP_FORM_MSTOCKMNG01010_POP');
		},
		whsList: function() {
			var _this = this;
			const brandIdVal = $('#ITP_FORM_MSTOCKMNG01010_POP_BRAND_ID').val();
			const storeIdVal = $('#ITP_FORM_MSTOCKMNG01010_POP_STORE_ID').val();
			const param = {sbrandId: brandIdVal, sstoreId: storeIdVal, pagingYn: false};

			fn_ajax_call(API_MOBILE_WHS_GRID_LIST, JSON.stringify(param), function(result) {
				fn_make_select_whs(result.gridRows, 'ITP_FORM_MSTOCKMNG01010_POP_WHS_NM');
				_this.locationList();
			}, 'POST');
		},
		locationList: function() {
			const brandIdVal = $('#ITP_FORM_MSTOCKMNG01010_POP_BRAND_ID').val();
			const storeIdVal = $('#ITP_FORM_MSTOCKMNG01010_POP_STORE_ID').val();
			const whsIdVal = $('#ITP_FORM_MSTOCKMNG01010_POP_WHS_NM').val();
			const param = {brandId: brandIdVal, storeId: storeIdVal, whsId: whsIdVal, pagingYn: false};
			fn_ajax_call(API_MOBILE_LOCATION_LIST, JSON.stringify(param), function(result) {
				fn_make_select_location(result.gridRows, 'ITP_FORM_MSTOCKMNG01010_POP_LOCATION');
			}, 'POST');
		},
		procStck: function() {
			var _this = this;
			var qty = $('#ITP_POP_MSTOCKMNG01010_ITEM_AREA #ITP_FORM_MSTOCKMNG01010_POP_QTY').val();
			if(qty.length < 1 || qty === '0') {
				itp_fn_modal_alert('이동 수량을 입력하세요.');
				return;
			}
			if(qty > this.item.stckQty) {
				itp_fn_modal_alert('재고 수량을 초과하여 입력할 수 없습니다.');
				return;
			}
			var param = $('#ITP_FORM_MSTOCKMNG01010_POP').serializeObject();
			console.log(JSON.stringify(param));
			fn_ajax_call(API_MOBILE_SAVE, JSON.stringify(param), function() {
				_this.cancel();
				mobContObj.list.search();
			}, 'POST');
		},
		reset: function() {
			var id = '#' + this.popId;
			$(id).find('.fnWhsNm').text('');
			$(id).find('.fnLocation').text('');
			$(id).find('.fnItemNm').text('');
			$(id).find('.fnStckQty').text('');
			$(id).find('.fnWhsNm').text('');
			$(id).find('.fnMgntLevel').text('');
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_BRAND_ID').val('');
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_ITEM_ID').val('');
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_SOURCE_LOCATION').val('');
			$(id).find('#ITP_FORM_MSTOCKMNG01010_POP_SOURCE_WHS_ID').val('');
		},
		cancel: function() {
			mobPopObj.hide(this.popId);
		}
	},
	show: function(popId) {
		$('button[id$="_CLOSE"]').off('click').on('click', function() {
			if($(this).hasClass('btn-pop-close')) {
				mobPopObj.hide(popId);
			}
		});
		$('#' + popId).show();
	},
	hide: function(popId) {
		$('#' + popId).closest('.mobile-pop-close').hide();
	}
};
