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

	});
});

let API_POP_GRID_LIST = '/api/spply/pop-grid-list';
//let API_POP_GRID_LIST = '/api/spply/detail-grid-list';


function itp_fn_BIZPOP_STORE_SPPLY(parentPopFn, args, returnType) {
	
	let itp_BIZPOP_STORE_SPPLY_param = {};
	let itp_BIZPOP_STORE_SPPLY_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_STORE_SPPLY';
	const THIS_GIRD_ID = '#ITP_BIZPOP_STORE_SPPLY_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_STORE_SPPLY_jqGridPager';

	$('#ITP_FORM_BIZPOP_STORE_SPPLY_BRAND_ID').val(fn_make_user_info.get('brandId'));
	
	$('#ITP_BIZPOP_STORE_SPPLY').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_STORE_SPPLY_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_STORE_SPPLY').remove();
	}).modal('show');

	$('#ITP_BIZPOP_STORE_SPPLY_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_STORE_SPPLY_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_SPPLY_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_SPPLY').modal('hide'); // 팝업닫기
	});

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

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

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

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'BRAND_ID', name: 'brandId',
				label: '브랜드ID', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'BRAND_NM', name: 'brandNm',
				label: '브랜드명', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'SPPLY_ID', name: 'spplyId',
				label: '공급업체ID', //
				width: '8', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'SPPLY_NM', name: 'spplyNm',
				label: '공급업체명', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ST_CD', name: 'stCd',
				label: '상태', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: true
			}
		],
		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_SPPLY', itp_BIZPOP_STORE_SPPLY_search, itp_fn_BIZPOP_STORE_SPPLY_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);
};
