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

	});
});

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

function itp_fn_BIZPOP_NEW_ITEM(parentPopFn, args, returnType) {
	console.log(args);
	let itp_BIZPOP_NEW_ITEM_param = {};
	let itp_BIZPOP_NEW_ITEM_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_NEW_ITEM';
	const THIS_GIRD_ID = '#ITP_BIZPOP_NEW_ITEM_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_NEW_ITEM_jqGridPager';

	$('#ITP_FORM_BIZPOP_NEW_ITEM_BRAND_ID').val(fn_make_user_info.get('brandId'));
	$('#ITP_FORM_BIZPOP_NEW_ITEM_STORE_ID').val(fn_make_user_info.get('storeId'));

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

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

	$('#ITP_BIZPOP_NEW_ITEM_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_NEW_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_NEW_ITEM_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개만 선택
			 itp_fn_modal_alert('하나의 품목만 선택해주세요.');
			 return;
		}
		
		const selRow = $(THIS_GIRD_ID).getRowData(rowKey);
		parentPopFn(selRow);
		$('#ITP_BIZPOP_NEW_ITEM').modal('hide'); // 팝업닫기
		/*
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_BIZPOP_NEW_ITEM').modal('hide'); // 팝업닫기
		*/
	});

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

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

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

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'ITEM_ID', name: 'itemId',
				label: '품목ID', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ITEM_NM', name: 'itemNm',
				label: '품목명', //
				width: '12', fixed: false, align: 'left',
				sortable: false, hidden: false
			},
			{
				index: 'UNIT', name: 'unit',
				label: '단위', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'UNIT_AMT', name: 'unitAmt',
				label: '단가', //
				width: '6', fixed: false, align: 'right',
				sortable: false, hidden: false,
				formatter: 'integer', formatoptions: {thousandsSeparator: ','}
			},
			{
				index: 'PODR_PSSBL_DVSN_NM', name: 'podrPssblDvsnNm',
				label: '발주가능여부명', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'PODR_PSSBL_DVSN', name: 'podrPssblDvsn',
				label: '발주가능여부', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'BRAND_UNIT_UNQ_NO', name: 'brandUnitUnqNo',
				label: '브랜드단가고유번호', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'UNIT_GUBUN', name: 'unitGubun',
				label: '단가구분', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'STORE_UNIT_UNQ_NO', name: 'storeUnitUnqNo',
				label: '매장단가고유번호', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'WHS_PASS_QTY', name: 'whsPassQty',
				label: '출고가능수량', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true,
				formatter: 'integer', formatoptions: {thousandsSeparator: ','}
			}
		],
		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_NEW_ITEM', itp_BIZPOP_NEW_ITEM_search, itp_fn_BIZPOP_NEW_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);
};
