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

/*API URL*/
const API_MOBILE_GRID_LIST 			= '/api/rtnmng/inoutmng/spply-target-rtn-grid-list';	// 목록
const API_MOBILE_INFO 				= '/api/rtnmng/inoutmng/init-spply-col';				// 상세
const API_MOBILE_SAVE 				= '/api/rtnmng/inoutmng/save-spply-col';				// 저장
const API_POP_SEARCH_LIST			= '/api/whs/mng/pop-whs-search';

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() {
			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});

			// 조회일자 지정
			$('#ITP_MOBILE_MCOLLMNG02010 .itp_input.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_MCOLLMNG02010_LIST_SEARCH_FROM_DT').datepicker('setDate', itp_fn_date_add('D', -7));
			$('#ITP_FORM_MCOLLMNG02010_LIST_SEARCH_TO_DT').datepicker('setDate', itp_fn_date_add('M', 0));
		},
		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'));
			$('input:hidden[id$="_SPPLY_ID"]').val(fn_make_user_info.get('userId'));
		}
	},
	event: {
		init: function() {
			this.button();
		},
		button: function() {
			$('button[id^="ITP_BTN_MCOLLMNG02010_LIST"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MCOLLMNG02010_LIST_SEARCH_WHS_NM'	: mobPopObj.popWhsNm.init(); 	break;
					case 'ITP_BTN_MCOLLMNG02010_LIST_DELETE_WHS_NM'	: mobPopObj.popWhsNm.delete(); 	break;
					case 'ITP_BTN_MCOLLMNG02010_LIST_SEARCH'		: mobContObj.list.search();		break;
					case 'ITP_BTN_MCOLLMNG02010_LIST_CREATE'		: mobContObj.list.colPrint();	break;
				}
				return false;
			});

			$('button[id^="ITP_BTN_MCOLLMNG02010_VIEW"]').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MCOLLMNG02010_VIEW_COL_ADD'		: mobContObj.view.add();			break;
					case 'ITP_BTN_MCOLLMNG02010_VIEW_CANCEL_LIST'	: mobContObj.view.cancel();		break;
				}
				return false;
			});

			// $(document).on('click', '#ITP_LIST_MCOLLMNG02010_LIST_ITEM_ROWS li button', function() {
			// 	var item = mobContObj.list.rows[$(this).index()];
			// 	mobContObj.view.init(item);
			// });
			$(document).on('click', 'input[type="checkbox"][id="ITP_LIST_MCOLLMNG02010_LIST_ITEM_CHECKBOX"]', function() {
				if($(this).prop('checked')) {
					$('input[type="checkbox"][id="ITP_LIST_MCOLLMNG02010_LIST_ITEM_CHECKBOX"]').prop('checked', false);
					$(this).prop('checked', true);
				}
			});

			$(document).on('click', '#ITP_LIST_MCOLLMNG02010_POP_WHS_ROWS li button', function() {
				mobPopObj.popWhsNm.choice($(this));
			});
		}
	},
	switchScreen: function(mode) {
		$('#ITP_MOBILE_MCOLLMNG02010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === PAGE_MODE_LIST) {
			$('#ITP_AJAX_MCOLLMNG02010_LIST_CONTAINER').show();
		} else if(mode === PAGE_MODE_VIEW) {
			$('#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER').show();
			CommonObj.moreView('#ITP_LIST_MCOLLMNG02010_LIST_ITEM_AREA', false);
		}
		this.viewMode = mode;
	},
	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_MCOLLMNG02010_LIST_ITEM_ROWS').empty();
			this.load();
		},
		pager: function(totPage) {
			(this.listPage < totPage) ?
				CommonObj.moreView('#ITP_LIST_MCOLLMNG02010_LIST_ITEM_AREA', true, mobContObj.list.load) :
				CommonObj.moreView('#ITP_LIST_MCOLLMNG02010_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_MCOLLMNG02010_LIST_ITEM_AREA .panel-group').append($('#ITP_LIST_MCOLLMNG02010_LIST_ITEM_ROWCOPY').html());
					var $li = $('#ITP_LIST_MCOLLMNG02010_LIST_ITEM_AREA .panel-group > .item-row:last');
					$li.find('.fnBrandNm').text(item.brandNm);
					$li.find('.fnRtnPodrUnqNo').text(item.rtnPodrUnqNo);
					$li.find('.fnRtnPodrNo').text(item.rtnPodrNo);
					$li.find('.fnColReqDt').text(item.colReqDt);
					$li.find('.fnRtnWhsNm').text(item.rtnWhsNm);
					$li.find('.fnItemId').text(item.itemId);
					$li.find('.fnItemNm').text(item.itemNm);
					$li.find('.fnRtnDodrItemQty').text(CommonObj.currency.add(item.rtnOdrQty, '개'));
					$li.find('.fnUnitAmt').text(CommonObj.comma.set(item.unitAmt));
					$li.find('.fnRtnDodrTotalAmt').text(CommonObj.comma.set(item.rtnOdrAmt));
					mobContObj.list.rows.push(item);
				});
			};
			var errFn = function() { _this.isSearch = false;};
			const param = $('#ITP_FORM_MCOLLMNG02010_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', errFn);
		},
		colPrint: function() {
			var iChoice = -1;
			$('input[type="checkbox"][id="ITP_LIST_MCOLLMNG02010_LIST_ITEM_CHECKBOX"]').each(function(index) {
				if($(this).is(":checked")) {
					iChoice = index;
					return false;
				}
			});
			if(iChoice === -1) {
				alert("수거서를 선택해주세요 (대표 수거서번호)");
				return;
			}
			mobContObj.view.init(this.rows[iChoice]);
		}
	},
	view: {
		rows: [],
		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('userId'),
				'rtnPodrUnqNo': item.rtnPodrUnqNo
			};
			fn_ajax_call(API_MOBILE_INFO, param, callbackFn, 'GET');
		},
		view: function(result) {
			var _this = this;
			var id = '#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER';
			$(id).find('.fnBrandNm').text(result.brandNm);
			$(id).find('.fnRtnPodrUnqNo').text(result.rtnPodrUnqNo);
			$(id).find('.fnColReqDt').text(result.colReqDt);
			$(id).find('.fnRtnDodrTotalAmt').text(CommonObj.currency.add(result.rtnDodrTotalAmt));
			$(id).find('.fnRtnWhsNm').text(result.rtnWhsNm);
			$(id).find('.fnPickTelNo').text(result.pickTelNo);
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_NM').val(result.rtnMgrNm);
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_TEL_NO').val(result.rtnMgrTelNo);
			$.each(result.pickRtnDtlInfo, function (i, item) {
				$('#ITP_LIST_MCOLLMNG02010_VIEW_ITEM_AREA .panel-group').append($('#ITP_LIST_MCOLLMNG02010_VIEW_ITEM_ROWCOPY').html());
				var $li = $('#ITP_LIST_MCOLLMNG02010_VIEW_ITEM_AREA .panel-group > .item-row:last');
				$li.find('.fnRtnPodrDtlNo').text(item.rtnPodrDtlNo);
				$li.find('.fnItemId').text(item.itemId);
				$li.find('.fnItemNm').text(item.itemNm);
				$li.find('.fnRtnOdrQty').text(CommonObj.comma.set(item.rtnOdrQty));
				$li.find('.fnUnitAmt').text(CommonObj.currency.add(item.unitAmt));
				$li.find('.fnUnit').text(item.unit);
				$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_QTY').val(result.coltQty);
				$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_RTN_RSN_DVSN').val(result.rtnRsnDvsn);
				$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_RTN_RSN').val(result.rtnRsn);
				_this.rows.push(item);
			});
		},
		add: function() {
			var _this = this;
			var param = {};
			var colSchDt = $('#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER #ITP_FORM_MORDMNG02010_VIEW_COL_SCH_DT').val();
			var pickMgrNm = $('#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER #ITP_FORM_MCOLLMNG02010_VIEW_PICK_MGR_NM').val();
			var pickMgrTelNo = $('#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER #ITP_FORM_MCOLLMNG02010_VIEW_PICK_MGR_TEL_NO').val();
			if(colSchDt.length < 1) {
				alert('수거일자를 입력해 주세요.');
				return false;
			}
			if(pickMgrNm.length < 1) {
				alert('수거담당자를 입력해 주세요.');
				return false;
			}
			if(pickMgrTelNo.length < 1) {
				alert('수거담당자 연락처를 입력해 주세요.');
				return false;
			}
			var gridInsertData = [];
			var isValid = false;
			$('#ITP_LIST_MCOLLMNG02010_VIEW_ITEM_AREA .panel-group > .item-row').each(function(index) {
				var row = _this.rows[index];
				var item = {
					'rtnPodrDtlNo': row.rtnPodrDtlNo,
					'rtnPodrUnqNo': row.rtnPodrUnqNo,
					'coltQty': $(this).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_QTY').val(),
					'rtnRsnDvsn': $(this).find("#ITP_FORM_MCOLLMNG02010_VIEW_RTN_RSN_DVSN option:selected").val(),
					'rtnRsn': $(this).find('#ITP_FORM_MCOLLMNG02010_VIEW_RTN_RSN').val()
				};
				if(item.coltQty.length < 1) {
					alert('수거수량을 입력해 주세요.');
					return false;
				}
				if(item.coltQty > row.rtnOdrQty) {
					alert('수거수량은 수거요청수량을 초과할 수 없습니다.');
					return false;
				}
				if(item.rtnRsnDvsn.length < 1) {
					alert('수거사유를 선택해 주세요.');
					return false;
				}
				item.expryDate = item.expryDate.replace(/-/g, ".");
				gridInsertData.push(item);
				isValid = true;
			});
			if(isValid) {
				param['viewCd'] = 'C';
				param['brandId'] = fn_make_user_info.get('brandId');
				param['spplyId'] = fn_make_user_info.get('userId');
				param['colSchDt'] = colSchDt.replace(/-/g, ".");
				param['pickMgrNm'] = pickMgrNm;
				param['pickMgrTelNo'] = pickMgrTelNo;
				param['gridInsertData'] = gridInsertData;
				console.log(JSON.stringify(param));
				var callbackFn = function(result) {
					console.log(result);
					_this.cancel();
				};
				fn_ajax_call(API_MOBILE_SAVE, JSON.stringify(param), callbackFn, 'POST');
			}
		},
		cancel: function () {
			var id = '#ITP_AJAX_MCOLLMNG02010_VIEW_CONTAINER';
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_NM').val('');
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_TEL_NO').val('');
			$(id).find('.fnBrandNm').text('');
			$(id).find('.fnRtnPodrDtlNo').text('');
			$(id).find('.fnRtnOdrRegDt').text('');
			$(id).find('.fnRtnDodrTotalAmt').text('');
			$(id).find('.fnRtnWhsNm').text('');
			$(id).find('.fnPickTelNo').text('');
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_NM').val('');
			$(id).find('#ITP_FORM_MCOLLMNG02010_VIEW_COL_MGR_TEL_NO').val('');
			$('#ITP_LIST_MCOLLMNG02010_VIEW_ITEM_AREA .panel-group').empty();
			mobPageObj.switchScreen(PAGE_MODE_LIST);
		}
	}
};

let mobPopObj = {
	popWhsNm: {
		popId: 'ITP_POP_MCOLLMNG02010_WHS_AREA',
		rows: [],
		init: function () {
			this.rows.length = 0;
			mobPopObj.show(this.popId);
			this.search();
			this.action();
		},
		search: function () {
			var _this = this;
			$('#ITP_LIST_MCOLLMNG02010_POP_WHS_AREA .panel-group').empty();
			var callbackFn = function(result) {
				console.log(result);
				_this.view(result.gridRows);
			};
			const param = $('#ITP_FORM_MCOLLMNG02010_POP_WHS').serializeObject();
			param.swhsDvsn = '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_MCOLLMNG02010_POP_WHS_AREA .panel-group').append($('#ITP_LIST_MCOLLMNG02010_POP_WHS_ROWCOPY').html());
				var $li = $('#ITP_LIST_MCOLLMNG02010_POP_WHS_AREA .panel-group > .item-row: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_MCOLLMNG02010_LIST_SEARCH #ITP_FORM_MCOLLMNG02010_LIST_SEARCH_WHS_ID').val(item.whsId);
					$('#ITP_FORM_MCOLLMNG02010_LIST_SEARCH #ITP_FORM_MCOLLMNG02010_LIST_SEARCH_WHS_NM').val(item.whsNm);
					_this.close();
					return false;
				}
			});
		},
		action: function() {
			var _this = this;
			$('button[id^="ITP_BTN_MCOLLMNG02010_POP_WHS"]').off('click').on('click', function() {
				var id = $(this).attr('id');
				switch (id) {
					case 'ITP_BTN_MCOLLMNG02010_POP_WHS_SEARCH'	: _this.search();			break;
					case 'ITP_BTN_MCOLLMNG02010_POP_WHS_CHOICE'	: _this.choice($(this)); 	break;
					case 'ITP_BTN_MCOLLMNG02010_POP_WHS_CLOSE'	: _this.close(); 			break;
				}
				return false;
			});
		},
		delete: function() {
			$('#ITP_FORM_MCOLLMNG02010_LIST_SEARCH #ITP_FORM_MCOLLMNG02010_LIST_SEARCH_WHS_ID').val('');
			$('#ITP_FORM_MCOLLMNG02010_LIST_SEARCH #ITP_FORM_MCOLLMNG02010_LIST_SEARCH_WHS_NM').val('');
		},
		close: function() {
			this.rows.length = 0;
			$('#ITP_POP_MCOLLMNG02010_LIST_WHS_AREA .panel-group').empty();
			$('#ITP_FORM_MCOLLMNG02010_POP_WHS #ITP_FORM_MCOLLMNG02010_POP_WHS_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();
	}
};
