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

	});
});

let API_POP_GRID_LIST = '/api/item/pop-grid-list';

function itp_fn_BIZPOP_STORE_ITEM(parentPopFn, args, returnType) {
	
	let itp_BIZPOP_STORE_ITEM_param = {};
	let itp_BIZPOP_STORE_ITEM_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_STORE_ITEM';
	const THIS_GIRD_ID = '#ITP_BIZPOP_STORE_ITEM_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_STORE_ITEM_jqGridPager';

	$('#ITP_FORM_BIZPOP_STORE_ITEM_BRAND_ID').val(fn_make_user_info.get('brandId'));

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

	$('#ITP_BIZPOP_STORE_ITEM').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_STORE_ITEM_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_STORE_ITEM').remove();
	}).modal('show');

	$('#ITP_BIZPOP_STORE_ITEM_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_STORE_ITEM_search = true;
		let param = $(THIS_FORM_ID).serializeObject();
		param.gridSize = 10;
		param.gridPage = $.jgrid.defaults.page;
		$(THIS_GIRD_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
	});
	
	$('#ITP_BIZPOP_STORE_ITEM_BTN_SELECT').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }
	    const selRow = $(THIS_GIRD_ID).getRowData(rowKey);
		parentPopFn(selRow);
		$('#ITP_BIZPOP_STORE_ITEM').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_STORE_ITEM_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_STORE_ITEM_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_BRAND_ID').val(itp_BIZPOP_STORE_ITEM_param.sbrandId);
			$(THIS_FORM_ID + '_CBRAND_NM').val(itp_BIZPOP_STORE_ITEM_param.sbrandNm);
		}
	};

	itp_fn_BIZPOP_STORE_ITEM_empty.push();
	let param = $('#ITP_FORM_BIZPOP_STORE_ITEM').serializeObject();
	param.gridSize = 10;
	console.log(JSON.stringify(param));

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'STORE_ITEM_ID', name: 'itemId',
				label: '품목ID', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_UNIT_UNQ_NO', name: 'brandUnitUnqNo',
				label: '품목번호', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STORE_ITEM_NM', name: 'itemNm',
				label: '품목명', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'CMPY_TYPE_NM', name: 'podrPssblDvsnNm',
				label: '발주가능여부', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + API_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		multiboxonly: true,
		rownumbers: false,
		loadComplete: function(data) {
			console.log(data);
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_STORE_ITEM', itp_BIZPOP_STORE_ITEM_search, itp_fn_BIZPOP_STORE_ITEM_empty, true, data.gridRecords, false);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
	    	itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};
