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

	});
});

let STOCK_MNG_HIST_GRID_LIST = '/api/stock/mng/hist-grid-list';

function itp_fn_BIZPOP_PO_STOCK_HIST(parentPopFn, args, returnType) {
	
	// console.log(JSON.stringify(args));
	
	let itp_BIZPOP_PO_STOCK_HIST_param = {};
	let itp_BIZPOP_PO_STOCK_HIST_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_PO_STOCK_HIST';
	const THIS_GIRD_ID = '#ITP_BIZPOP_PO_STOCK_HIST_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_PO_STOCK_HIST_jqGridPager';
	
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_BRAND_ID').val(args.brandId);
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_WHS_ID').val(args.whsId);
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_LOCATION').val(args.location);
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_ITEM_ID').val(args.itemId);
	
	var now = new Date();
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST .input-group.date').datepicker(ITP_DATE_LANGUAGE);
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
	$('#ITP_FORM_BIZPOP_PO_STOCK_HIST_TO_DT').datepicker('setDate', new Date());
	
	// 공통코드 표시
	fn_make_select(CODE_LIST, 'INOUT_CD', 'ITP_FORM_BIZPOP_PO_STOCK_HIST_INOUT_CD');
	
	$('#ITP_BIZPOP_PO_STOCK_HIST').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_PO_STOCK_HIST_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_PO_STOCK_HIST').remove();
	}).modal('show');

	$('#ITP_BIZPOP_PO_STOCK_HIST_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_PO_STOCK_HIST_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_PO_STOCK_HIST_jqGridEmpty').on('click', function() {
		itp_fn_BIZPOP_PO_STOCK_HIST_empty.back();
		itp_BIZPOP_PO_STOCK_HIST_param.gridSize = 10;
		$(THIS_GIRD_ID).setGridParam({'postData': JSON.stringify(itp_BIZPOP_PO_STOCK_HIST_param)}).trigger('reloadGrid');
	});

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

	itp_fn_BIZPOP_PO_STOCK_HIST_empty.push();
	let param = $('#ITP_FORM_BIZPOP_PO_STOCK_HIST').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'INOUT_NM', name: 'inoutNm',
				label: '입출고구분', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'SYS_REG_DTTM', name: 'sysRegDttm',
				label: '발생일자', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'INOUT_QTY', name: 'inoutQty',
				label: '입출고수량', //
				width: '10', fixed: false, align: 'right',
				sortable: false, hidden: false,
				formatter: 'integer', formatoptions: {thousandsSeparator: ','}
			},
			{
				index: 'CHG_DVSN_NM', name: 'chgDvsnNm',
				label: '변경구분', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'RELATION_NO', name: 'relationNo',
				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 + STOCK_MNG_HIST_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: false,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_PO_STOCK_HIST', itp_BIZPOP_PO_STOCK_HIST_search, itp_fn_BIZPOP_PO_STOCK_HIST_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);
};
