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

	});
});

let FRCOMP_POP_GRID_LIST = '/api/pomng/inoutmng/spply-deli-grid-list';

function itp_fn_BIZPOP_DLV_STTMT_PRN(parentPopFn, args, returnType) {
	// 공통코드
	// fn_make_select(CODE_LIST, 'CMPY_TYPE_CD', 'ITP_FORM_BIZPOP_DLV_STTMT_PRN_CMPY_TYPE_CD', true, '가맹점타입 선택');
	
	let itp_BIZPOP_DLV_STTMT_PRN_param = {};
	let itp_BIZPOP_DLV_STTMT_PRN_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_DLV_STTMT_PRN';
	const THIS_GIRD_ID = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGridPager';

	if(args) {
		$('#ITP_FORM_BIZPOP_DLV_STTMT_PRN_CMPY_NM').val(args.brandNm);
	}
	
	$('#ITP_BIZPOP_DLV_STTMT_PRN').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_DLV_STTMT_PRN').remove();
	}).modal('show');

	$('#ITP_BIZPOP_DLV_STTMT_PRN_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_DLV_STTMT_PRN_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_DLV_STTMT_PRN_BTN_SELECT').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }

		var list = [];
	    var selectedIds = $(THIS_GIRD_ID).getGridParam('selarrrow');

		if(selectedIds.length > 1) { // 1개만 선택
			// return;
		} 
		
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_BIZPOP_DLV_STTMT_PRN').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_DLV_STTMT_PRN_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_DLV_STTMT_PRN_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_CMPY_CD').val(itp_BIZPOP_DLV_STTMT_PRN_param.scmpyCd);
			$(THIS_FORM_ID + '_CMPY_NM').val(itp_BIZPOP_DLV_STTMT_PRN_param.scmpyNm);
			$(THIS_FORM_ID + '_CMPY_TYPE_CD').val(itp_BIZPOP_DLV_STTMT_PRN_param.scmpyTypeCd);
		}
	};

	itp_fn_BIZPOP_DLV_STTMT_PRN_empty.push();
	let param = $('#ITP_FORM_BIZPOP_DLV_STTMT_PRN').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'AFFL_SHOP_ID', name: 'pchPodrUnqNo',
				label: '발주번호', //
				width: '5', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'AFFL_SHOP_NM', name: 'itemId',
				label: '품목번호', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_ID', name: 'itemNm',
				label: '품목명', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_NM', name: 'podrQty',
				label: '발주수량', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_ID', name: 'dlvQty',
				label: '납품서수량', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_ID', name: 'expryDate',
				label: '유통기한', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + FRCOMP_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			console.log(data);
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_DLV_STTMT_PRN', itp_BIZPOP_DLV_STTMT_PRN_search, itp_fn_BIZPOP_DLV_STTMT_PRN_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);
};
