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

	});
});

let STOCK_MNG_DETAIL_GRID_LIST = '/api/stock/mng/detail-grid-list';

function itp_fn_BIZPOP_PO_STOCK_ITEM_ADD(parentPopFn, schParam, returnType) {
	
	$('#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD_BRAND_ID').val(schParam.brandId);
	$('#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD_WHS_ID').val(schParam.whsId);
	$('#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD_LOCATION').val(schParam.location);
	
	// 공통코드 표시
    $('select').each(function() {
        if($(this).data('select-code')) {
            fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
        }
    });
	
	let itp_BIZPOP_PO_STOCK_ITEM_ADD_param = {};
	let itp_BIZPOP_PO_STOCK_ITEM_ADD_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD';
	const THIS_GIRD_ID = '#ITP_BIZPOP_PO_STOCK_ITEM_ADD_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_PO_STOCK_ITEM_ADD_jqGridPager';
	
	$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_PO_STOCK_ITEM_ADD_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD').remove();
	}).modal('show');

	$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD_BTN_SEARCH').on('click', function() {
		itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_search(THIS_FORM_ID, THIS_GIRD_ID);
	});
	
	$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD_BTN_SELECT').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }

		var list = [];
	    var selectedIds = $(THIS_GIRD_ID).getGridParam('selarrrow');
		
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD').modal('hide'); // 팝업닫기		
	});

	$('#ITP_BIZPOP_PO_STOCK_ITEM_ADD_jqGridEmpty').on('click', function() {
		itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_empty.back();
		itp_BIZPOP_PO_STOCK_ITEM_ADD_param.gridSize = 10;
		$(THIS_GIRD_ID).setGridParam({'postData': JSON.stringify(itp_BIZPOP_PO_STOCK_ITEM_ADD_param)}).trigger('reloadGrid');
	});

	var itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_PO_STOCK_ITEM_ADD_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_ITEM_ST_CD').val(itp_BIZPOP_PO_STOCK_ITEM_ADD_param.sitemStCd);
			$(THIS_FORM_ID + '_ITEM_GUBUN').val(itp_BIZPOP_PO_STOCK_ITEM_ADD_param.sitemGubun);
			$(THIS_FORM_ID + '_ITEM_TYPE').val(itp_BIZPOP_PO_STOCK_ITEM_ADD_param.sitemType);
			$(THIS_FORM_ID + '_ITEM_NM').val(itp_BIZPOP_PO_STOCK_ITEM_ADD_param.sitemNm);
		}
	};

	itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_empty.push();
	let param = $('#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
	            index: 'BRAND_ID', name: 'brandId',
	            label: ITP_MSG_LOCALE.label.brandId, //브랜드ID
	            width: '10', fixed: false, align: 'center',
	            sortable: false, hidden: true
	        },
	        {
	            index: 'ITEM_ID', name: 'itemId',
	            label: ITP_MSG_LOCALE.label.itemId, //품목번호
	            width: '10', fixed: false, align: 'center',
	            sortable: false, hidden: false
	        },
	        {
	            index: 'ITEM_NM', name: 'itemNm',
	            label: ITP_MSG_LOCALE.label.itemNm, //품목명
	            width: '13', fixed: false, align: 'center',
	            sortable: false, hidden: false
	        },
	        {
				index: 'UNIT', name: 'unit',
				label: "단위", 
				width: '7', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STCK_QTY', name: 'stckQty',
				label: "현재고수량", 
				width: '8', fixed: false, align: 'right',
				sortable: false, hidden: false,
				formatter: 'integer', formatoptions: {thousandsSeparator: ','}
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + STOCK_MNG_DETAIL_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_PO_STOCK_ITEM_ADD', itp_BIZPOP_PO_STOCK_ITEM_ADD_search, itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_empty, true, data.gridRecords, false);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
			param = $('#ITP_FORM_BIZPOP_PO_STOCK_ITEM_ADD').serializeObject();
			param.gridSize = 10;
	    	itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};

var itp_fn_BIZPOP_PO_STOCK_ITEM_ADD_search = function(FORM_ID, GRID_ID) {	
	itp_BIZPOP_PO_STOCK_ITEM_ADD_search = true;
	let param = $(FORM_ID).serializeObject();
	param.gridSize = 10;
	param.gridPage = $.jgrid.defaults.page;
	$(GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
};
