require(['config'], function() {
	require([
		'jquery'
	], function($) {

	});
});

let STOCK_MNG_INFO_STOCK = '/api/stock/mng/info-stock'; // 재고정보
let STOCK_MNG_PROC_STOCK = '/api/stock/mng/proc-stock'; // 재고처리
let WHS_MNG_DETAIL_GRID_LIST = '/api/whs/mng/detail-grid-list';
let WHS_MNG_LOCATION_GRID_LIST = '/api/whs/mng/location-grid-list';

var popPageObj = {
	init: function () {
		this.button();
		this.change();
		//재고정보 조회
		this.fnStockInfo();
	},
	parentPopFn: null,
	args: null,
	returnType: null,
	button: function() {
		$('#ITP_BTN_PO_STOCK_PROC_CLOSE_POP').on('click', function () {
			$('#ITP_BIZPOP_PO_STOCK_PROC').modal('hide'); // 팝업닫기
		});
		$('#ITP_BTN_PO_STOCK_PROC_SAVE_POP').on('click', function () {
			popPageObj.fnStockProc();
		});
	}, 
	change: function() {
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_WHS_ID').on('change', function () {
			popPageObj.fnLocationList();
		});
	}, 	
	fnStockInfo: function() {
		var param = $.param(this.args);
		fn_ajax_call(STOCK_MNG_INFO_STOCK, param, this.fnStockInfoCallback, 'GET');
	}, 
	fnStockInfoCallback: function(result) {
		// alert(JSON.stringify(result))
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_STORE_ID').val(popPageObj.args.storeId);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_SOURCE_WHS_ID').val(result.whsId);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_SOURCE_LOCATION').val(result.location);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP_ITEM_ID').val(result.itemId);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP .fnWhsNm').text(result.whsNm);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP .fnLocationNm').text(result.locationNm);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP .fnItemId').text(result.itemId);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP .fnItemNm').text(result.itemNm);
		$('#ITP_FORM_PO_STOCK_PROC_POPUP .fnStckQty').text(result.stckQty);
		
		// 창고 및 로케이션 리스트
		popPageObj.fnWhsList(function() {
			popPageObj.fnLocationList();
		});
	}, 
	fnStockProc: function() {
		const formId = '#ITP_FORM_PO_STOCK_PROC_POPUP';
		itp_fn_form_clear_validate(null, formId);
		
		var vali_moveQty = itp_fn_form_validate(formId, formId + '_MOVE_QTY', ['empty','number'], undefined);
		
		if (vali_moveQty) {			
			// 창고 & 로케이션 비교
			if($('#ITP_FORM_PO_STOCK_PROC_POPUP_SOURCE_WHS_ID').val() == $('#ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_WHS_ID').val() 
			&& $('#ITP_FORM_PO_STOCK_PROC_POPUP_SOURCE_LOCATION').val() == $('#ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_LOCATION').val()) {
				itp_fn_modal_alert_ajax('동일한 로케이션으로 재고처리를 할 수 없습니다.');
				return;
			}
			var stckQty = parseInt($('#ITP_FORM_PO_STOCK_PROC_POPUP .fnStckQty').text());
			var moveQty = parseInt($('#ITP_FORM_PO_STOCK_PROC_POPUP_MOVE_QTY').val());
			if(moveQty > stckQty) {
				itp_fn_modal_alert_ajax('이동수량은 현재고수량 이하로 입력하세요.');
				return;
			}
			
			let param = $(formId).serializeObject();
			// param.gridInsertData = gridInsertData;
			// console.log(JSON.stringify(param));
			var callbackFn = function() {
				popPageObj.parentPopFn();
				$('#ITP_BIZPOP_PO_STOCK_PROC').modal('hide'); // 팝업닫기
			};
			fn_ajax_call(STOCK_MNG_PROC_STOCK, JSON.stringify(param), callbackFn, 'POST');
		}
	}, 
	fnWhsList: function(fnCall) {
		const brandIdVal = $('#ITP_FORM_PO_STOCK_PROC_POPUP_BRAND_ID').val();	
		const storeIdVal = $('#ITP_FORM_PO_STOCK_PROC_POPUP_STORE_ID').val();
		const param = {sbrandId: brandIdVal,sstoreId: storeIdVal, pagingYn: false};
		
		fn_ajax_call(WHS_MNG_DETAIL_GRID_LIST, JSON.stringify(param), function(result) {
			
			fn_make_select_whs(result.gridRows, 'ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_WHS_ID');
			
			if (fnCall !== undefined || typeof fnCall !== 'undefined') fnCall();
					
		}, 'POST');
	}, 
	fnLocationList: function(fnCall) {
		const brandIdVal = $('#ITP_FORM_PO_STOCK_PROC_POPUP_BRAND_ID').val();	
		const storeIdVal = $('#ITP_FORM_PO_STOCK_PROC_POPUP_STORE_ID').val();
		const whsIdVal = $('#ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_WHS_ID').val();	
		const param = {brandId: brandIdVal, storeId: storeIdVal, whsId: whsIdVal, pagingYn: false};
		
		fn_ajax_call(WHS_MNG_LOCATION_GRID_LIST, JSON.stringify(param), function(result) {
			
			fn_make_select_location(result.gridRows, 'ITP_FORM_PO_STOCK_PROC_POPUP_TARGET_LOCATION');
			
			if (fnCall !== undefined || typeof fnCall !== 'undefined') fnCall();
					
		}, 'POST');
	}
};

function itp_fn_BIZPOP_PO_STOCK_PROC(parentPopFn, args, returnType) {
	$('#ITP_BIZPOP_PO_STOCK_PROC').one('shown.bs.modal', function(e) {
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_PO_STOCK_PROC').remove();
	}).modal('show');

	popPageObj.parentPopFn = parentPopFn;
	popPageObj.args = args;
	popPageObj.returnType = returnType;
	popPageObj.init();
}
