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

/*API URL*/
const API_MOBILE_GRID_LIST 			= '/api/pomng/detail-grid-list';	// 목록
const API_MOBILE_INFO 				= '/api/pomng/info-pchReq';			// 상세
const API_MOBILE_SAVE 				= '/api/pomng/save-pchReq';			// 저장
const API_POP_SEARCH_LIST			= '/api/whs/mng/pop-whs-search';
const API_POP_ITEM_LIST 			= '/api/item/pop-grid-list';

const PCH_REQ_ST_CD_TEMP_SAVE		="PR00";
const PCH_REQ_ST_CD_REQUEST_SAVE	="PR20";

const VIEW_MODE_LIST				= "LIST";
const VIEW_MODE_DETAIL				= "DETAIL";
const VIEW_MODE_MODIFY				= "MODIFY";

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

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

			if(fn_make_user_info.get('authTpCd') === '50') {
				$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_NM').attr('readonly', true);
				// $('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_BTN_MPOMNG02010_LIST_SEARCH_WHS_NM').attr('disabled', true);
				// $('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_BTN_MPOMNG02010_LIST_DELETE_WHS_NM').attr('disabled', true);

				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_NM').attr('readonly', true);
				// $('#ITP_FORM_MPOMNG02010_MODIFY #ITP_BTN_MPOMNG02010_MODIFY_SEARCH_WHS_NM').attr('disabled', true);
				// $('#ITP_FORM_MPOMNG02010_MODIFY #ITP_BTN_MPOMNG02010_MODIFY_DELETE_WHS_NM').attr('disabled', true);
			}
		},
		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'));
		},
		action: function() {
		}
	},
	event: {
		init: function() {
			this.button();
		},
		button: function() {
			$('button[id^="ITP_BTN_MPOMNG02010_LIST"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MPOMNG02010_LIST_SEARCH_WHS_NM'	: mobPopObj.popWhsNm.init(VIEW_MODE_LIST); 		break;
					case 'ITP_BTN_MPOMNG02010_LIST_DELETE_WHS_NM'	: mobPopObj.popWhsNm.delete(VIEW_MODE_LIST); 	break;
					case 'ITP_BTN_MPOMNG02010_LIST_SEARCH'			: mobContObj.list.search();						break;
				}
				return false;
			});

			$('button[id^="ITP_BTN_MPOMNG02010_VIEW"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MPOMNG02010_VIEW_CANCEL_LIST'		: mobContObj.detail.cancel();	break;
				}
				return false;
			});

			$('button[id^="ITP_BTN_MPOMNG02010_MODIFY"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MPOMNG02010_MODIFY_SEARCH_WHS_NM'		: mobPopObj.popWhsNm.init(VIEW_MODE_MODIFY); 	break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_DELETE_WHS_NM'		: mobPopObj.popWhsNm.delete(VIEW_MODE_MODIFY); 	break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_ADD_ROW'			: mobPopObj.popItem.init();						break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_DEL_ROW'			: mobContObj.modify.list.detachRow();			break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_CANCEL'			: mobContObj.modify.cancel();					break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_REQ_ORDER'			: mobPopObj.popReqConfirm.init(PCH_REQ_ST_CD_REQUEST_SAVE);	break;
					case 'ITP_BTN_MPOMNG02010_MODIFY_TEMP_SAVE'			: mobPopObj.popReqConfirm.init(PCH_REQ_ST_CD_TEMP_SAVE);	break;
				}
				return false;
			});
		}
	},
	switchScreen: function(mode) {
		$('#ITP_MOBILE_MPOMNG02010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});

		if(mode === VIEW_MODE_LIST) {
			$('#ITP_AJAX_MPOMNG02010_LIST_CONTAINER').show();
			if(fn_make_user_info.get('authTpCd') === '50') {
				$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_NM').attr('readonly', true);
				$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_ID').attr('disabled', true);
			}
		} else if(mode === VIEW_MODE_DETAIL) {
			$('#ITP_AJAX_MPOMNG02010_VIEW_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MPOMNG02010_ITEM_AREA', false);
		} else if(mode === VIEW_MODE_MODIFY) {
			$('#ITP_AJAX_MPOMNG02010_MODIFY_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MPOMNG02010_ITEM_AREA', false);
		}
		this.viewMode = mode;
	},
	ready: function() {
		mobContObj.list.init();
	}
};

let mobContObj = {
	list: {
		listPage: 1,
		rows: [],
		init: function() {
			mobPageObj.switchScreen(VIEW_MODE_LIST);
			this.search();
		},
		search: function() {
			this.listPage = 1;
			this.rows.length = 0;
			$('#ITP_LIST_MPOMNG02010_ITEM_ROWS').children().remove();
			setTimeout(function() {
				mobContObj.list.load();
			}, 100);
		},
		pager: function(totPage) {
			(this.listPage < totPage) ?
				CommonObj.moreView('#ITP_LIST_MPOMNG02010_ITEM_AREA', true, mobContObj.list.load) :
				CommonObj.moreView('#ITP_LIST_MPOMNG02010_ITEM_AREA', false);
			this.listPage += 1;
		},
		load: function() {
			var callbackFn = function(result) {
				console.log(result);
				mobContObj.list.pager(result.gridTotal);
				$.each(result.gridRows, function (i, item) {
					$('#ITP_LIST_MPOMNG02010_ITEM_AREA .panel-group').append($('#ITP_LIST_MPOMNG02010_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MPOMNG02010_ITEM_AREA .panel-group > .item-row:last');
					$li.find('.fnReqTypeNm').text(item.reqTypeNm);
					$li.find('.fnPchReqDt').text(item.pchReqDt);
					$li.find('.fnDlvReqDt').text(item.dlvReqDt);
					$li.find('.fnPchReqMgrNm').text(item.pchReqMgrNm);
					$li.find('.fnPchReqStNm').text(item.pchReqStNm);
					$li.find('.fnWhsLocationNm').text(item.whsLocationNm);
					$li.find('.fnPchReqItemQty').text(CommonObj.comma.set(item.pchReqItemQty));
					$li.find('.fnPchReqTotalAmt').text(CommonObj.currency.add(item.pchReqTotalAmt));
					$li.find('.fnPchReqRjctDt').text(item.pchReqRjctDt);
					$li.find('.fnPchReqRjctNm').text(item.pchReqRjctNm);
					mobContObj.list.rows.push(item);
				});
				mobContObj.list.action();
			};
			const param = $('#ITP_FORM_MPOMNG02010_LIST_SEARCH').serializeObject();
			param.gridPage = mobContObj.list.listPage;
			param.gridSize = mobile_list_size;
			fn_ajax_call(API_MOBILE_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		action: function () {
			var _this = this;
			$(document).off('click', '#ITP_LIST_MPOMNG02010_ITEM_ROWS li');
			$(document).on('click', '#ITP_LIST_MPOMNG02010_ITEM_ROWS li', function() {
				var index = $(this).index();
				var item = _this.rows[index];
				(item.pchReqStCd === PCH_REQ_ST_CD_TEMP_SAVE) ? mobContObj.modify.init(item) : mobContObj.detail.init(item);
			});
		}
	},
	detail: {
		rows: [],
		init: function(item) {
			mobPageObj.switchScreen(VIEW_MODE_DETAIL);
			this.search(item);
		},
		search: function(item) {
			var _this = this;
			var callbackFn = function(result) {
				console.log(result);
				_this.rows = result.pchReqDtlList;
				_this.view(result);
			};
			const param = {'brandId':fn_make_user_info.get('brandId'), 'pchReqUnqNo': item.pchReqUnqNo};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MPOMNG02010_VIEW_CONTAINER';
			$(id).find('.fnDlvReqDt').text(result.dlvReqDt);
			$(id).find('.fnPchReqTotalAmt').text(CommonObj.currency.add(result.pchReqTotalAmt));
			$(id).find('.fnLocationNm').text(result.locationNm);
			$(id).find('.fnPchReqStnm').text(result.pchReqStNm);
			$(id).find('.fnPcReqRjctDt').text(result.pchReqRjctDt);
			$(id).find('.fnPchReqRjctNm').text(result.pchReqRjctNm);
			$.each(result.pchReqDtlList, function (i, item) {
				$('#ITP_LIST_MPOMNG02010_VIEW_AREA .panel-group').append($('#ITP_LIST_MPOMNG02010_VIEW_ROWCOPY').html());
				var $li = $('#ITP_LIST_MPOMNG02010_VIEW_AREA .panel-group > .item-row:last');
				$li.find('.fnItemNm').text(item.itemNm);
				$li.find('.fnPchReqQty').text(item.pchReqQty + ' ' + item.unit);
				$li.find('.fnPchReqAmt').text(CommonObj.currency.add(item.pchReqAmt));
				mobContObj.detail.rows.push(item);
			});
		},
		cancel: function () {
			this.rows.length = 0;
			var id = '#ITP_AJAX_MPOMNG02010_VIEW_CONTAINER';
			$(id).find('.fnDlvReqDt').text('');
			$(id).find('.fnPchReqTotalAmt').text('');
			$(id).find('.fnLocationNm').text('');
			$(id).find('.fnPchApprNm').text('');
			$('#ITP_LIST_MPOMNG02010_VIEW_AREA .panel-group').empty();
			mobPageObj.switchScreen(VIEW_MODE_LIST);
		}
	},
	modify: {
		init: function(item) {
			mobPageObj.switchScreen(VIEW_MODE_MODIFY);
			this.clear();
			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'), 'pchReqUnqNo': item.pchReqUnqNo};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MPOMNG02010_MODIFY_CONTAINER';
			$('#ITP_FORM_MPOMNG02010_MODIFY_DLV_REQ_DT').datepicker('setDate', result.dlvReqDt);
			$(id).find('.fnPchReqTotalAmt').text(CommonObj.currency.add(result.pchReqTotalAmt));
			$(id).find('.fnOrdUseAmt').text(CommonObj.currency.add(result.ordUseAmt));
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_BRAND_ID').val(result.brandId);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_STORE_ID').val(result.storeId);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_DVSN').val(result.loanDvsn);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_MGNT_UNQ_NO').val(result.loanMgntUnqNo);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_UNQ_NO').val(result.pchReqUnqNo);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_DVSN').val(result.pchReqDvsn);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_MGR_ID').val(result.pchReqMgrId);
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_MALL_ID').val(result.mallId);

			$('#ITP_FORM_MPOMNG02010_MODIFY .fnOrdUseAmt').text(CommonObj.currency.add(result.ordUseAmt));
			if(result.whsId !== '' && result.location !== '') {
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_ID').val(result.whsId);
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_NM').val(result.whsNm);
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOCATION').val(result.location);
			}

			_this.list.init(result.pchReqDtlList);
		},
		list: {
			rows: [],
			init: function(pchReqDtlList) {
				var _this = this;
				var existRows = [];
				$.each(pchReqDtlList, function (i, item) {
					item['viewCd'] = 'U';
					existRows.push(item);
				});
				this.attachRow(existRows);
			},
			attachRow: function (items) {
				var _this = this;
				var fnIsNotExist = function(rows, item) {
					var isPushRow = true;
					var isAddItem = true;
					$.each(rows, function(i, row) {
						if(row.brandUnitUnqNo === item.brandUnitUnqNo) {
							if(row.viewCd === 'D') {
								row.viewCd = 'U';
								row.pchReqQty = item.pchReqQty;
							} else {
								isAddItem = false;
							}
							isPushRow = false;
							return false;
						}
					});
					if(isPushRow) _this.rows.push(item);
					return isAddItem;
				};
				$.each(items, function(i, item) {
					if(fnIsNotExist(_this.rows, item)) {
						$('#ITP_LIST_MPOMNG02010_MODIFY_AREA .panel-group').append($('#ITP_LIST_MPOMNG02010_MODIFY_ROWCOPY').html());
						var $li = $('#ITP_LIST_MPOMNG02010_MODIFY_AREA .panel-group > .item-row:last');
						$li.find('.fnBrandUnitUnqNo').val(item.brandUnitUnqNo);
						$li.find('.fnItemNm').text(item.itemNm);
						$li.find('.fnPchReqQty').text(item.pchReqQty);
						$li.find('.fnUnitAmt').text(CommonObj.currency.add(item.unitAmt));
						$('.item-row #ITP_BTN_MPOMNG02010_MODIFY_PLUS')[$li.index()].addEventListener('click', function() { _this.actPlus(this); });
						$('.item-row #ITP_BTN_MPOMNG02010_MODIFY_MINUS')[$li.index()].addEventListener('click', function() { _this.actMinus(this); });
						_this.pchReqAmtTot();
					}
				});
			},
			detachRow: function () {
				if($('#ITP_LIST_MPOMNG02010_MODIFY_AREA input:checkbox[name=brandUnitUnqNo]:checked').length > 0) {
					var _this = this;
					$('#ITP_LIST_MPOMNG02010_MODIFY_ROWS li').each(function(index, elem) {
						var __this = this;
						var isChecked = $(this).find('input[type="checkbox"]').is(':checked');
						if(isChecked) {
							if(_this.rows[index].viewCd === 'U') {
								_this.rows[index].viewCd = 'D';
							} else {
								$.each(_this.rows, function(i, row){
									if(row.brandUnitUnqNo === $(__this).find('input[type="checkbox"]').val()) {
										_this.rows.splice(i, 1);
										return false;
									}
								});
							}
							$(this).remove();
						}
					});
					this.pchReqAmtTot();
				}
			},
			actPlus: function(elem) {
				var $div = $(elem).siblings('div');
				var num = Number($div.text());
				$($div).text((num + 1).toString());
				this.rows[$div.parents('li').index()]['pchReqQty'] = $($div).text();
				this.pchReqAmtTot();
			},
			actMinus: function(elem) {
				var $div = $(elem).siblings('div');
				var num = Number($div.text());
				if(num <= 1) {
					itp_fn_modal_alert('수량을 확인하세요.');
				} else {
					$($div).text((num - 1).toString());
					this.rows[$div.parents('li').index()]['pchReqQty'] = $($div).text();
					this.pchReqAmtTot();
				}
			},
			pchReqAmtTot: function() {
				var reqAmt = 0;
				$.each(this.rows, function(index, item) {
					if(item.viewCd !== 'D') {
						var qty = item.pchReqQty;
						var amt = item.unitAmt;
						reqAmt += Number(qty) * amt;
					}
				});
				$('#ITP_AJAX_MPOMNG02010_MODIFY_CONTAINER').find('.fnPchReqAmtTot').text(CommonObj.currency.add(reqAmt));
			}
		},
		cancel: function () {
			mobPageObj.switchScreen(VIEW_MODE_LIST);
		},
		clear: function() {
			this.list.rows.length = 0;
			var id = '#ITP_AJAX_MPOMNG02010_MODIFY_CONTAINER';
			$(id).find('.fnDlvReqDt').val('');
			$(id).find('.fnPchReqTotalAmt').text('');
			$(id).find('.fnOrdUseAmt').text('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_DVSN').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_MGNT_UNQ_NO').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_UNQ_NO').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_DVSN').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_NM').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_ID').val('');
			$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOCATION').val('');
			$('#ITP_LIST_MPOMNG02010_MODIFY_AREA .panel-group').empty();
		}
	}
};

let mobPopObj = {
	popItem: {
		popId: 'ITP_POP_MPOMNG02010_ITEM_AREA',
		init: function() {
			mobPopObj.show(this.popId);
			this.search();
		},
		search: function() {
			$('#ITP_LIST_MPOMNG02010_POP_ITEM_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				mobPopObj.popItem.rows = result.gridRows;
				$.each(mobPopObj.popItem.rows, function (i, item) {
					$('#ITP_LIST_MPOMNG02010_POP_ITEM_AREA .panel-group').append($('#ITP_LIST_MPOMNG02010_POP_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MPOMNG02010_POP_ITEM_AREA .panel-group > .row_2:last');
					$li.find('#ITP_FORM_MPOMNG02010_POP_ITEM_CHECKBOX').val(item.itemId);
					$li.find('.fnItemNm').text(item.itemNm);
					$li.find('.fnUnitAmt').text(CommonObj.currency.add(item.unitAmt));
					$li.find('.fnOrdAmt').text(CommonObj.currency.add(item.unitAmt));
				});
				mobPopObj.popItem.addRow.init();
			};
			let param = $('#ITP_FORM_MPOMNG02010_POPUP_ITEM').serializeObject();
			fn_ajax_call(API_POP_ITEM_LIST, JSON.stringify(param), callbackFn, 'POST');
		},
		addRow: {
			init: function() {
				this.button.init();
			},
			button: {
				init: function() {
					var _this = this;
					$('button[id^="ITP_BTN_MPOMNG02010_POP_ITEM_"]').off('click').on('click', function() {
						var id = $(this).attr('id');
						switch (id) {
							case 'ITP_BTN_MPOMNG02010_POP_ITEM_PLUS'	: _this.actPlus($(this)); 		break;
							case 'ITP_BTN_MPOMNG02010_POP_ITEM_MINUS'	: _this.actMinus($(this)); 		break;
							case 'ITP_BTN_MPOMNG02010_POP_ITEM_ADD'		: _this.addItems(); 			break;
							// case 'ITP_BTN_MPOMNG02010_POP_ITEM_ADD'		: _this.actItemAdd($(this)); 	break;
							case 'ITP_BTN_MPOMNG02010_POP_ITEM_SEARCH'	: mobPopObj.popItem.search(); 	break;
							case 'ITP_BTN_MPOMNG02010_POP_ITEM_CLOSE'	: mobPopObj.hide(id); 			break;
						}
						return false;
					});
				},
				actPlus: function(elem) {
					var $div = $(elem).siblings('div');
					var num = Number($div.text());
					$($div).text((num + 1).toString());
					this.reqAmtUnit(elem, $($div).text());
				},
				actMinus: function(elem) {
					var $div = $(elem).siblings('div');
					var num = Number($div.text());
					if(num <= 1) {
						itp_fn_modal_alert('수량을 확인하세요.');
					} else {
						$($div).text((num - 1).toString());
						this.reqAmtUnit(elem, $($div).text());
					}
				},
				reqAmtUnit: function(elem, qty) {
					var $li = $(elem).closest('li');
					var unitAmt = CommonObj.onlyNumber($li.find('.fnUnitAmt').text());
					$li.find('.fnOrdAmt').text(CommonObj.currency.add(Number(unitAmt ? unitAmt : 0) * Number(qty)));
				},
				actItemAdd: function(elem) {
					var $li = $(elem).closest('li');
					var item = mobPopObj.popItem.rows[$li.index()];
					item['viewCd'] = 'C';
					item['pchReqQty'] = $li.find('.fnPchReqQty').html();
					// var index = $(elem).closest('li').index();
					// var item = mobPopObj.popItem.rows[index];
					// item['pchReqQty'] = $(elem).closest('li').find('.fnPchReqQty').html();
					mobContObj.list.attachRow(item);
				},
				addItems: function() {
					var items = [];
					$('#ITP_LIST_MPOMNG02010_POP_ITEM_ROWS').find('input:checkbox').each(function() {
						if($(this).is(':checked')) {
							var index = $(this).closest('li').index();
							var item = mobPopObj.popItem.rows[index];
							item['viewCd'] = 'C';
							item['pchReqQty'] = $(this).closest('li').find('.fnPchReqQty').html();
							items.push(item);
						}
					});
					if(items.length > 0) {
						mobContObj.modify.list.attachRow(items);
						mobPopObj.popItem.close();
					} else {
						itp_fn_modal_alert('품목을 선택하세요.');
					}
				}
			}
		},
		delRow: function() {

		},
		close: function() {
			this.rows.length = 0;
			$('#ITP_LIST_MPOMNG02010_POP_ITEM_ROWS').children().remove();
			mobPopObj.hide('ITP_POP_MPOMNG02010_ITEM_AREA');
		}
	},
	popWhsNm: {
		popId: 'ITP_POP_MPOMNG02010_WHS_AREA',
		mode: '',
		rows: [],
		init: function (mode) {
			this.mode = mode;
			this.rows.length = 0;
			mobPopObj.show(this.popId);
			this.search();
			this.action();
		},
		search: function () {
			var _this = this;
			$('#ITP_LIST_MPOMNG02010_POP_WHS_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result.gridRows);
			};
			const param = $('#ITP_FORM_MPOMNG02010_POP_WHS').serializeObject();
			(fn_make_user_info.get('authTpCd') === '50') ? param.swhsDvsn = 'W02' : param.swhsDvsn = (mobPageObj.viewMode === VIEW_MODE_LIST) ? '' : 'W01';
			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_MPOMNG02010_POP_WHS_AREA .panel-group').append($('#ITP_LIST_MPOMNG02010_POP_WHS_ROWCOPY').html());
				var $li = $('#ITP_LIST_MPOMNG02010_POP_WHS_AREA .panel-group > .row_2:last');
				$li.find('.fnWhsId').data('whs-id', item.whsId);
				$li.find('.fnWhsNm').text(item.whsNm);
				$li.find('.fnLocationNm').text(item.locationNm);
			});
		},
		action: function() {
			var _this = this;
			$('#ITP_LIST_MPOMNG02010_POP_WHS_AREA button[id^="ITP_BTN_MPOMNG02010_POP"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MPOMNG02010_POP_WHS_SEARCH'	: _this.search();				break;
					case 'ITP_BTN_MPOMNG02010_POP_WHS_CHOICE'	: _this.choice($(this)); 		break;
					case 'ITP_BTN_MPOMNG02010_POP_WHS_CLOSE'	: mobPopObj.hide(_this.popId);	break;
				}
				return false;
			});
		},
		choice: function(elem) {
			var _this = this;
			var whsId = $(elem).data('whs-id');
			$.each(this.rows, function (i, item) {
				if(whsId === item.whsId) {
					if(_this.mode === VIEW_MODE_LIST) {
						$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_ID').val(item.whsId);
						$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_NM').val(item.whsNm);
					} else if(_this.mode === VIEW_MODE_MODIFY) {
						$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_ID').val(item.whsId);
						$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_NM').val(item.whsNm);
						$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOCATION').val(item.location);
					}
					mobPopObj.hide(_this.popId);
					return false;
				}
			});
		},
		delete: function(mode) {
			if(mode === VIEW_MODE_LIST) {
				$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_ID').val('');
				$('#ITP_FORM_MPOMNG02010_LIST_SEARCH #ITP_FORM_MPOMNG02010_LIST_SEARCH_WHS_NM').val('');
			} else if(mode === VIEW_MODE_MODIFY) {
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_ID').val('');
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_WHS_NM').val('');
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOCATION').val('');
			}
		}
	},
	popReqConfirm: {
		popId: 'ITP_POP_MPOMNG02010_REQ_AREA',
		pchReqStCd: '',
		saveMode: 1, // 1 : 임시저장, 2 : 구매요청, 요청금액이 가능금액보다 클 경우 (3 : PG 정보 없음, 4 : 선불매장, 5 : 후불매장)
		init: function (pchReqStCd) {
			let mgrId = $('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_MGR_ID').val();
			if(mgrId !== fn_make_user_info.get('userId')) {
				itp_fn_modal_alert_ajax(ITP_MSG_LOCALE.message.ajax.diffWrite);
				return;
			}
			this.formId = '#ITP_FORM_MPOMNG02010_MODIFY';
			this.pchReqStCd = pchReqStCd;
			mobPopObj.show(this.popId);
			this.view();
			this.action();
		},
		view: function() {
			var popMsg = '';
			var loanDvsn = $('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_DVSN').val();
			var ordUseAmt = CommonObj.onlyNumber($('#ITP_FORM_MPOMNG02010_MODIFY .fnOrdUseAmt').text());
			var pchReqAmtTot = CommonObj.onlyNumber($('#ITP_FORM_MPOMNG02010_MODIFY .fnPchReqAmtTot').text());
			if(this.pchReqStCd === PCH_REQ_ST_CD_REQUEST_SAVE) { // 구매요청
				if(loanDvsn === 'LD03') {		// 무정산
					this.saveMode = 2;
					popMsg = '구매요청 하시겠습니까?';
					$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('확인');
				} else {
					if (Number(ordUseAmt) < Number(pchReqAmtTot)) { // 요청금액이 가능금액보다 클 경우
						if(CommonObj.isPayPossible()) {	// PG 정보 여부
							if(loanDvsn === 'LD01') {	// 선불매장
								this.saveMode = 4;
								popMsg = 	'선불매장이므로 결재를 하셔야 구매요청이 진행이 됩니다.<br>' +
									'결재를 하시겠습니까?';
								$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('결제');
							} else if(loanDvsn === 'LD02') { // 후불매장
								this.saveMode = 5;
								popMsg = 	'구매한도가 부족합니다.<br>' +
									'선결재를 하신 후에 구매요청을 하시기 바랍니다.<br>' +
									'결재를 하시겠습니까?';
								$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('결제');
							}
						} else {
							this.saveMode = 3;
							if(loanDvsn === 'LD01') {	// 선불매장
								popMsg = 	'선불매장이므로 입금을 하시고, 입금등록 후 브랜드에서 승인이 되어야  구매요청을 하실 수 있습니다.';
							} else if(loanDvsn === 'LD02') { // 후불매장
								popMsg = 	'구매한도가 부족합니다.<br>' +
									'선입금을 하시고, 입금등록 후 브랜드에서 승인이 되어야 구매요청을  하실 수 있습니다.\n';
							}
							$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('확인');
						}
					} else {
						this.saveMode = 2;
						popMsg = '구매요청 하시겠습니까?';
						$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('확인');
					}
				}
			} else { // 임시저장
				this.saveMode = 1;
				popMsg = '임시저장 하시겠습니까?';
				$('#ITP_POP_MPOMNG02010_REQ_AREA #ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM').text('확인');
			}
			$('#ITP_POP_MPOMNG02010_REQ_AREA .fnPopDlvReqDt').text($('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_DLV_REQ_DT').val());
			$('#ITP_POP_MPOMNG02010_REQ_AREA .fnItemQty').text(CommonObj.currency.add($('#ITP_LIST_MPOMNG02010_MODIFY_ROWS').children().length, '개'));
			var ordAmt = Number(pchReqAmtTot) - Number(ordUseAmt);
			$('#ITP_POP_MPOMNG02010_REQ_AREA .fnOrdAmt').text(CommonObj.currency.add(ordAmt > 0 ? ordAmt : 0));
			// $('#ITP_POP_MPOMNG02010_REQ_AREA .fnOrdAmt').text(CommonObj.currency.add(pchReqAmtTot));
			$('#ITP_POP_MPOMNG02010_REQ_AREA .pop_msg').html(popMsg);
		},
		action: function() {
			var _this = this;
			$('#ITP_POP_MPOMNG02010_REQ_AREA button[id="ITP_BTN_MPOMNG02010_POP_REQ_CONFIRM"]').off('click').on('click', function() {
				$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_ST_CD').val(_this.pchReqStCd);
				_this.save();
			});
		},
		save: function() {
			var loanDvsn = $('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_LOAN_DVSN').val();
			var procSave = function (isReload) {
				let param = $('#ITP_FORM_MPOMNG02010_MODIFY').serializeObject();
				var gridInsertData = [];
				var gridUpdateData = [];
				var gridDeleteData = [];
				$.each(mobContObj.modify.list.rows, function(i, item) {
					if(item.viewCd === 'D') {
						gridDeleteData.push(item);
					} else if(item.viewCd === 'C') {
						gridInsertData.push(item);
					} else {
						item.viewCd = 'U';
						gridUpdateData.push(item);
					}
				});
				param.gridInsertData = gridInsertData;
				param.gridUpdateData = gridUpdateData;
				param.gridDeleteData = gridDeleteData;
				param['dlvReqDt'] = param.dlvReqDt.replace(/[^0-9]/g, "");
				var saveFn = function (result) {
					mobPopObj.hide('ITP_POP_MPOMNG02010_REQ_AREA');
					if(isReload) mobContObj.list.init();
				};
				console.log(param);
				fn_ajax_call(API_MOBILE_SAVE, JSON.stringify(param), saveFn, 'POST');
			};
			if (this.saveMode === 1 || this.saveMode === 2 || this.saveMode === 3) {
				if(this.saveMode === 3)
					$('#ITP_FORM_MPOMNG02010_MODIFY #ITP_FORM_MPOMNG02010_MODIFY_PCH_REQ_ST_CD').val(PCH_REQ_ST_CD_TEMP_SAVE);
				procSave(true);
			} else if (this.saveMode === 4 || this.saveMode === 5) {
				// 결제 팝업 결과
				var popFn = function(rowDataPop) {
					procSave((loanDvsn === 'LD01'));
				};
				// 결제 팝업
				var ordUseAmt = CommonObj.onlyNumber($('#ITP_FORM_MPOMNG02010_MODIFY .fnOrdUseAmt').text());
				var pchReqAmtTot = CommonObj.onlyNumber($('#ITP_FORM_MPOMNG02010_MODIFY .fnPchReqAmtTot').text());
				var args = {
					'payTpCd'	: '40',
					'brandId'	: $('#ITP_FORM_MPOMNG02010_MODIFY_BRAND_ID').val(),
					'storeId'	: $('#ITP_FORM_MPOMNG02010_MODIFY_STORE_ID').val(),
					'goodsAmt'	: Number(pchReqAmtTot - ordUseAmt)
				};
				fn_call_popup('biz', 'BIZPOP_PO_PAYMENT', '#ITP_ASIDE', popFn, args, 'M');
			}
		}
	},
	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();
	}
};
