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

	});
});

let API_POP_GRID_LIST = '/api/whs/mng/pop-whs-search';

function itp_fn_BIZPOP_WHS_LOCATION(parentPopFn, args, returnType) {
	console.log(args);
	let itp_BIZPOP_WHS_LOCATION_param = {};
	let itp_BIZPOP_WHS_LOCATION_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_WHS_LOCATION';
	const THIS_GIRD_ID = '#ITP_BIZPOP_WHS_LOCATION_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_WHS_LOCATION_jqGridPager';

	$('#ITP_FORM_BIZPOP_WHS_LOCATION_BRAND_ID').val(fn_make_user_info.get('brandId'));
	$('#ITP_FORM_BIZPOP_WHS_LOCATION_STORE_ID').val(fn_make_user_info.get('storeId'));
	$('#ITP_FORM_BIZPOP_WHS_LOCATION_WHS_DVSN').val(args.whsDvsn);

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

	$('#ITP_BIZPOP_WHS_LOCATION_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_WHS_LOCATION_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_WHS_LOCATION_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_WHS_LOCATION').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_WHS_LOCATION_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_WHS_LOCATION_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_BRAND_ID').val(itp_BIZPOP_WHS_LOCATION_param.sbrandId);
			$(THIS_FORM_ID + '_STROE_ID').val(itp_BIZPOP_WHS_LOCATION_param.sstoreId);
		}
	};

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

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'BRAND_ID', name: 'brandId',
				label: '브랜드아이디', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'BRAND_NM', name: 'brandNm',
				label: '브랜드명', //
				width: '12', fixed: false, align: 'left',
				sortable: false, hidden: false
			},
			{
				index: 'WHS_ID', name: 'whsId',
				label: '창고아이디', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'WHS_NM', name: 'whsNm',
				label: '창고명', //
				width: '6', fixed: false, align: 'right',
				sortable: false, hidden: false
			},
			{
				index: 'LOCATION', name: 'location',
				label: 'LOCATION', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'LOCATION_NM', name: 'locationNm',
				label: 'LOCATION명', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STCK_DVSN', name: 'stckDvsn',
				label: '창고구분', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'STCK_DVSN_NM', name: 'stckDvsnNm',
				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 + API_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: false,
		rownumbers: false,
		loadComplete: function(data) {
			console.log(data);
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_WHS_LOCATION', itp_BIZPOP_WHS_LOCATION_search, itp_fn_BIZPOP_WHS_LOCATION_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);
};
