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

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

const RTN_REQ_ST_CD_TEMP_SAVE		="RR00";
const RTN_REQ_ST_CD_REQUEST_SAVE	="RR20";

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_MRTNMNG02010 .itp_input.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH_FROM_DT').datepicker('setDate', itp_fn_date_add('M', 0));
			$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH_TO_DT').datepicker('setDate', itp_fn_date_add('M', 1));
		},
		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_MRTNMNG02010_LIST"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MRTNMNG02010_LIST_SEARCH_WHS_NM'	: mobPopObj.popWhsNm.init(VIEW_MODE_LIST); 		break;
					case 'ITP_BTN_MRTNMNG02010_LIST_DELETE_WHS_NM'	: mobPopObj.popWhsNm.delete(VIEW_MODE_LIST); 	break;
					case 'ITP_BTN_MRTNMNG02010_LIST_SEARCH'			: mobContObj.list.search();					break;
				}
				return false;
			});

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

			$('button[id^="ITP_BTN_MRTNMNG02010_MODIFY"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MRTNMNG02010_MODIFY_SEARCH_WHS_NM'	: mobPopObj.popWhsNm.init(VIEW_MODE_MODIFY); 	break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_DELETE_WHS_NM'	: mobPopObj.popWhsNm.delete(VIEW_MODE_MODIFY); 	break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_ADD_ROW'			: mobPopObj.popItem.init();						break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_DEL_ROW'			: mobContObj.modify.list.detachRow();		break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_CANCEL'			: mobContObj.modify.cancel();				break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_REQ_ORDER'		: mobPopObj.popReqConfirm.init(RTN_REQ_ST_CD_REQUEST_SAVE);	break;
					case 'ITP_BTN_MRTNMNG02010_MODIFY_TEMP_SAVE'		: mobPopObj.popReqConfirm.init(RTN_REQ_ST_CD_TEMP_SAVE);	break;
				}
				return false;
			});

			$(document).on('click', '#ITP_LIST_MRTNMNG02010_LIST_ROWS li', function() {
				var index = $(this).index();
				var item = mobContObj.list.rows[index];
				(item.rtnReqStCd === RTN_REQ_ST_CD_TEMP_SAVE) ? mobContObj.modify.init(item) : mobContObj.detail.init(item);
			});

			// $(document).on('click', '#ITP_LIST_MRTNMNG02010_MODIFY_ROWS li', function(e) {
			// 	if($(e.target).hasClass('DETAIL_ITEM_PLUS')) {
			// 		mobContObj.modify.list.actPlus($(this));
			// 	} else if($(e.target).hasClass('DETAIL_ITEM_MINUS')) {
			// 		mobContObj.modify.list.actMinus($(this));
			// 	} else {
			// 	}
			// });

			$(document).off('click', '#ITP_LIST_MRTNMNG02010_POP_WHS_ROWS li button');
			$(document).on('click', '#ITP_LIST_MRTNMNG02010_POP_WHS_ROWS li button', function() {
				mobPopObj.popWhsNm.choice($(this));
			});
		}
	},
	switchScreen: function(mode) {
		$('#ITP_MOBILE_MRTNMNG02010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === VIEW_MODE_LIST) {
			$('#ITP_AJAX_MRTNMNG02010_LIST_CONTAINER').show();
		} else if(mode === VIEW_MODE_DETAIL) {
			$('#ITP_AJAX_MRTNMNG02010_DETAIL_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MRTNMNG02010_LIST_AREA', false);
		} else if(mode === VIEW_MODE_MODIFY) {
			$('#ITP_AJAX_MRTNMNG02010_MODIFY_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MRTNMNG02010_LIST_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_MRTNMNG02010_LIST_ROWS').empty();
			setTimeout(function() {
				mobContObj.list.load();
			}, 100);
		},
		pager: function(totPage) {
			(this.listPage < totPage) ?
				CommonObj.moreView('#ITP_LIST_MRTNMNG02010_ITEM_AREA', true, mobContObj.list.load) :
				CommonObj.moreView('#ITP_LIST_MRTNMNG02010_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_MRTNMNG02010_LIST_AREA .panel-group').append($('#ITP_LIST_MRTNMNG02010_LIST_ROWCOPY').html());
					var $li = $('#ITP_LIST_MRTNMNG02010_LIST_AREA .panel-group > .history_list5:last');
					// $li.find('.fnRtnReqUnqNo').text(item.rtnReqUnqNo);
					$li.find('.fnRtnReqDt').text(item.colReqDt);
					// $li.find('.fnRtnReqDt').text(item.rtnReqStCd === 'RR00' ? item.colReqDt : item.rtnReqDt);
					$li.find('.fnRtnReqMgrNm').text(item.rtnReqMgrNm);
					$li.find('.fnRtnReqStNm').text(item.rtnReqStNm);
					$li.find('.fnRtnReqAmt').text(CommonObj.currency.add(item.rtnReqAmt));
					$li.find('.fnRtnReqItemQty').text(CommonObj.comma.set(item.rtnReqItemQty));
					$li.find('.fnRtnWhsNm').text(item.rtnWhsNm);
					$li.find('.fnRtnReqRjctNm').text(item.pchReqRjctNm);
					mobContObj.list.rows.push(item);
				});
			};
			const param = $('#ITP_FORM_MRTNMNG02010_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');
		},
	},
	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.view(result);
			};
			const param = {'brandId':fn_make_user_info.get('brandId'), 'rtnReqUnqNo': item.rtnReqUnqNo};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MRTNMNG02010_DETAIL_CONTAINER';
			$(id).find('.fnRtnReqDt').text(result.rtnReqDt);
			$(id).find('.fnRtnReqAmt').text(CommonObj.currency.add(result.rtnReqAmt));
			$(id).find('.fnRtnWhsNm').text(result.rtnWhsNm);
			$(id).find('.fnRtnApprNm').text('(' + result.pchApprNm + ')' + CommonObj.currency.add(result.pchApprAmt));
			$.each(result.rtnReqDtlList, function (i, item) {
				$('#ITP_LIST_MRTNMNG02010_DETAIL_AREA .panel-group').append($('#ITP_LIST_MRTNMNG02010_DETAIL_ROWCOPY').html());
				var $li = $('#ITP_LIST_MRTNMNG02010_DETAIL_AREA .panel-group > .item-row:last');
				$li.find('.fnItemNm').text(item.itemNm);
				$li.find('.fnUnit').text(item.unit);
				$li.find('.fnRtnAmt').text(CommonObj.comma.set(item.rtnAmt));
				$li.find('.fnRtnQty').text(CommonObj.currency.add(item.rtnQty, ''));
				_this.rows.push(item);
			});
		},
		cancel: function () {
			this.rows.length = 0;
			var id = '#ITP_AJAX_MRTNMNG02010_DETAIL_CONTAINER';
			$(id).find('.fnDlvReqDt').text('');
			$(id).find('.fnRtnReqTotalAmt').text('');
			$(id).find('.fnLocationNm').text('');
			$(id).find('.fnRtnApprNm').text('');
			$('#ITP_LIST_MRTNMNG02010_DETAIL_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'), 'rtnReqUnqNo': item.rtnReqUnqNo};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MRTNMNG02010_MODIFY_CONTAINER';
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_COL_REQ_DT').val(result.colReqDt.replace(/\./g, "."));
			$('#ITP_FORM_MRTNMNG02010_MODIFY_COL_REQ_DT').datepicker('setDate', result.colReqDt.replace(/\./g, "."));
			$(id).find('.fnRtnReqTotalAmt').text(CommonObj.currency.add(result.pchReqTotalAmt));
			$(id).find('.fnOrdUseAmt').text(CommonObj.currency.add(result.ordUseAmt));
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_RTN_REQ_UNQ_NO').val(result.rtnReqUnqNo);
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_DVSN').val(result.whsDvsn);
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_RTN_REQ_MGR_ID').val(result.rtnReqMgrId);
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_NM').val(result.rtnWhsNm);
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_ID').val(result.rtnWhsId);
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_LOCATION').val(result.rtnLocation);
			_this.list.init(result.rtnReqDtlList);
		},
		list: {
			rows: [],
			init: function(rtnReqDtlList) {
				var _this = this;
				var existRows = [];
				$.each(rtnReqDtlList, 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.rtnQty = item.rtnQty;
							} 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_MRTNMNG02010_MODIFY_AREA .mb_0').append($('#ITP_LIST_MRTNMNG02010_MODIFY_ROWCOPY').html());
						var $li = $('#ITP_LIST_MRTNMNG02010_MODIFY_AREA .mb_0 > .item-row:last');
						$li.find('.fnBrandUnitUnqNo').val(item.brandUnitUnqNo);
						$li.find('.fnItemNm').text(item.itemNm);
						$li.find('.fnRtnQty').text(item.rtnQty);
						$li.find('.fnUnitAmt').text(CommonObj.currency.add(item.unitAmt));
						$('.item-row .DETAIL_ITEM_PLUS')[$li.index()].addEventListener('click', function() { _this.actPlus(this); });
						$('.item-row .DETAIL_ITEM_MINUS')[$li.index()].addEventListener('click', function() { _this.actMinus(this); });
						_this.rtnReqAmtTot();
					}
				});
			},
			detachRow: function () {
				if($('#ITP_LIST_MRTNMNG02010_MODIFY_AREA input:checkbox[name=brandUnitUnqNo]:checked').length > 0) {
					var _this = this;
					$('#ITP_LIST_MRTNMNG02010_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.rtnReqAmtTot();
				}
			},
			actPlus: function(elem) {
				var $div = $(elem).siblings('div');
				var num = Number($div.text());
				$($div).text((num + 1).toString());
				this.rows[$div.parents('li').index()]['rtnQty'] = $($div).text();
				this.rtnReqAmtTot();
			},
			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()]['rtnQty'] = $($div).text();
					this.rtnReqAmtTot();
				}
			},
			/*ctPlus: function(elem) {
				console.log('actPlus');
				var index = $(elem).index();
				var num = Number($(elem).find('.fnRtnQty').text()) + 1;
				$(elem).find('.fnRtnQty').text(num.toString());
				this.rows[index]['rtnQty'] = num;
				this.rtnReqAmtTot();
			},
			actMinus: function(elem) {
				var index = $(elem).index();
				var num = Number($(elem).find('.fnRtnQty').text()) - 1;
				if(num < 1) {
					itp_fn_modal_alert('수량을 확인하세요.');
				} else {
					$(elem).find('.fnRtnQty').text(num.toString());
					this.rows[index]['rtnQty'] = num;
					this.rtnReqAmtTot();
				}
			},*/
			rtnReqAmtTot: function() {
				var reqAmt = 0;
				$.each(this.rows, function(index, item) {
					if(item.viewCd !== 'D') {
						var qty = item.rtnQty;
						var amt = item.unitAmt;
						reqAmt += Number(qty) * amt;
					}
				});
				$('#ITP_FORM_MRTNMNG02010_MODIFY').find('.fnRtnReqAmtTot').text(CommonObj.currency.add(reqAmt));
			}
		},
		cancel: function () {
			mobPageObj.switchScreen(VIEW_MODE_LIST);
		},
		clear: function() {
			this.list.rows.length = 0;
			var id = '#ITP_AJAX_MRTNMNG02010_MODIFY_CONTAINER';
			$(id).find('.fnDlvReqDt').val('');
			$(id).find('.fnRtnReqTotalAmt').text('');
			$(id).find('.fnOrdUseAmt').text('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_COL_REQ_DT').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_RTN_REQ_UNQ_NO').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_DVSN').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_RTN_REQ_MGR_ID').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_NM').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_ID').val('');
			$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_LOCATION').val('');
			$('#ITP_LIST_MRTNMNG02010_MODIFY_ROWS').children().remove();
		}
	}
};

let mobPopObj = {
	popItem: {
		popId: 'ITP_POP_MRTNMNG02010_ITEM_AREA',
		init: function() {
			mobPopObj.show(this.popId);
			this.search();
		},
		search: function() {
			$('#ITP_LIST_MRTNMNG02010_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_MRTNMNG02010_POP_ITEM_AREA .panel-group').append($('#ITP_LIST_MRTNMNG02010_POP_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MRTNMNG02010_POP_ITEM_AREA .panel-group > .row_2:last');
					$li.find('#ITP_FORM_MRTNMNG02010_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_MRTNMNG02010_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_MRTNMNG02010_POP_ITEM_"]').off('click').on('click', function() {
						var id = $(this).attr('id');
						switch (id) {
							case 'ITP_BTN_MRTNMNG02010_POP_ITEM_PLUS'	: _this.actPlus($(this)); 		break;
							case 'ITP_BTN_MRTNMNG02010_POP_ITEM_MINUS'	: _this.actMinus($(this)); 		break;
							case 'ITP_BTN_MRTNMNG02010_POP_ITEM_ADD'		: _this.addItems(); 			break;
							// case 'ITP_BTN_MRTNMNG02010_POP_ITEM_ADD'		: _this.actItemAdd($(this)); 	break;
							case 'ITP_BTN_MRTNMNG02010_POP_ITEM_SEARCH'	: mobPopObj.popItem.search(); 	break;
							case 'ITP_BTN_MRTNMNG02010_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['rtnQty'] = $li.find('.fnRtnQty').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_MRTNMNG02010_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['rtnQty'] = $(this).closest('li').find('.fnRtnQty').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_MRTNMNG02010_POP_ITEM_ROWS').children().remove();
			mobPopObj.hide('ITP_POP_MRTNMNG02010_ITEM_AREA');
		}
	},
	popWhsNm: {
		popId: 'ITP_POP_MRTNMNG02010_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_MRTNMNG02010_POP_WHS_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result.gridRows);
			};
			const param = $('#ITP_FORM_MRTNMNG02010_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_MRTNMNG02010_POP_WHS_AREA .panel-group').append($('#ITP_LIST_MRTNMNG02010_POP_WHS_ROWCOPY').html());
				var $li = $('#ITP_LIST_MRTNMNG02010_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) {
					if(_this.mode === VIEW_MODE_LIST) {
						$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH #ITP_FORM_MRTNMNG02010_LIST_SEARCH_WHS_ID').val(item.whsId);
						$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH #ITP_FORM_MRTNMNG02010_LIST_SEARCH_WHS_NM').val(item.whsNm);
					} else if(_this.mode === VIEW_MODE_MODIFY) {
						$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_ID').val(item.whsId);
						$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_NM').val(item.whsNm);
						$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_LOCATION').val(item.location);
					}
					mobPopObj.hide(_this.popId);
					return false;
				}
			});
		},
		action: function() {
			var _this = this;
			$('button[id^="ITP_BTN_MRTNMNG02010_POP"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MRTNMNG02010_POP_WHS_SEARCH'	: _this.search();				break;
					case 'ITP_BTN_MRTNMNG02010_POP_WHS_CHOICE'	: _this.choice($(this)); 		break;
					case 'ITP_BTN_MRTNMNG02010_POP_WHS_CLOSE'	: mobPopObj.hide(_this.popId);	break;
				}
				return false;
			});
		},
		delete: function(mode) {
			if(mode === VIEW_MODE_LIST) {
				$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH #ITP_FORM_MRTNMNG02010_LIST_SEARCH_WHS_ID').val('');
				$('#ITP_FORM_MRTNMNG02010_LIST_SEARCH #ITP_FORM_MRTNMNG02010_LIST_SEARCH_WHS_NM').val('');
			} else if(mode === VIEW_MODE_MODIFY) {
				$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_ID').val('');
				$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_WHS_NM').val('');
				$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_LOCATION').val('');
			}
		}
	},
	popReqConfirm: {
		popId: 'ITP_POP_MRTNMNG02010_REQ_AREA',
		rtnReqStCd: '',
		init: function (rtnReqStCd) {
			let mgrId = $('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_RTN_REQ_MGR_ID').val();
			if(mgrId !== fn_make_user_info.get('userId')) {
				itp_fn_modal_alert(ITP_MSG_LOCALE.message.ajax.diffWrite);
				return;
			}
			if($('#ITP_LIST_MRTNMNG02010_MODIFY_ROWS').children().length < 1) {
				itp_fn_modal_alert( "요청할 품목내역이 없습니다.");
				return;
			}
			this.rtnReqStCd = rtnReqStCd;
			mobPopObj.show(this.popId);
			this.view();
			this.action();
		},
		view: function() {
			if(this.rtnReqStCd === RTN_REQ_ST_CD_REQUEST_SAVE) {
				$('#ITP_POP_MRTNMNG02010_REQ_AREA .msg_save').text('반품요청 하시겠습니까?');
			} else {
				$('#ITP_POP_MRTNMNG02010_REQ_AREA .msg_save').text('임시저장 하시겠습니까?');
			}
			$('#ITP_POP_MRTNMNG02010_REQ_AREA .fnPopDlvReqDt').text($('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_COL_REQ_DT').val());
			$('#ITP_POP_MRTNMNG02010_REQ_AREA .fnItemQty').text(CommonObj.currency.add($('#ITP_LIST_MRTNMNG02010_MODIFY_ROWS').children().length, '개'));
			$('#ITP_POP_MRTNMNG02010_REQ_AREA .fnRtnTotalAmt').text($('#ITP_FORM_MRTNMNG02010_MODIFY .fnRtnReqAmtTot').text());
		},
		action: function() {
			var _this = this;
			$('#ITP_POP_MRTNMNG02010_REQ_AREA button[id="ITP_BTN_MRTNMNG02010_POP_REQ_CONFIRM"]').off('click').on('click', function() {
				$('#ITP_FORM_MRTNMNG02010_MODIFY #ITP_FORM_MRTNMNG02010_MODIFY_PCH_REQ_ST_CD').val(_this.rtnReqStCd);
				_this.save('#ITP_FORM_MRTNMNG02010_MODIFY');
			});
		},
		save: function(formId) {
			var _this = this;
			var gridInsertData = [];
			var gridUpdateData = [];
			var gridDeleteData = [];
			$.each(mobContObj.modify.list.rows, function(index, item) {
				// var item = {};
				// item['brandUnitUnqNo']	= row.brandUnitUnqNo;
				// item['itemId']			= row.itemId;
				// item['itemNm']			= row.itemNm;
				// item['rtnAmt']			= parseInt(row.unitAmt) * parseInt(row.rtnQty);
				// item['rtnDvsn']			= row.rtnDvsn;
				// item['rtnQty']			= row.rtnQty;
				// item['rtnReqDtlNo']		= row.rtnReqDtlNo;
				// item['rtnReqUnqNo']		= row.rtnReqUnqNo;
				// item['rtnRsn']			= row.rtnRsn;
				// item['rtnRsnDvsn']		= row.rtnRsnDvsn;
				// item['spplyId']			= row.spplyId;
				// item['storeUnitUnqNo']	= row.storeUnitUnqNo;
				// item['unit']			= row.unit;
				// item['unitAmt']			= row.unitAmt;
				// item['unitGubun']		= row.unitGubun;
				item['rtnAmt']			= parseInt(item.unitAmt) * parseInt(item.rtnQty);
				if(item.viewCd === 'D') {
					gridDeleteData.push(item);
				} else if(item.viewCd === 'C') {
					gridInsertData.push(item);
				} else {
					item.viewCd = 'U';
					gridUpdateData.push(item);
				}
			});
			let param = $(formId).serializeObject();
			param['rtnReqStCd'] = this.rtnReqStCd;
			param['colReqDt'] = param.colReqDt.replace(/[^0-9]/g, "");
			param.gridInsertData = gridInsertData;
			param.gridUpdateData = gridUpdateData;
			param.gridDeleteData = gridDeleteData;
			console.log(JSON.stringify(param));
			var saveFn = function (result) {
				console.log(result);
				mobPopObj.hide(_this.popId);
				mobContObj.list.init();
			};
			fn_ajax_call(API_MOBILE_SAVE, JSON.stringify(param), saveFn, 'POST');
		}
	},
	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();
	}
};
