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

	});
});

let API_POP_INV_INFO = '/api/pomng/inoutmng/info-inv';

const THIS_FORM_ID = '#ITP_FORM_BIZPOP_DLV_STTMT_PRN';
const THIS_GIRD_ID = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid';
const THIS_GIRD_LIST = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid_list';
const THIS_GIRD_PAGER = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGridPager';
const THIS_GIRD_EMPTY = '#ITP_BIZPOP_DLV_STTMT_PRN_jqGridEmpty';

function itp_fn_BIZPOP_DLV_STTMT_PRN(parentPopFn, args, returnType) {
	
	
	let itp_BIZPOP_DLV_STTMT_PRN_param = args;
	let itp_BIZPOP_DLV_STTMT_PRN_search = false;

	
	$('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_BRAND_ID').val(args.brandId);
	$('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_DLV_STTMT_UNQ_NO').val(args.dlvSttmtUnqNo);
	

	$('#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_PO_REJECT_BTN_CANCEL').on('click', function() {
		let transParm = {pchReqUnqNo: $('#ITP_FORM_ITP_BIZPOP_PO_REJECT_PCH_REQ_UNQ_NO').val(),pchReqRjctRsn: '', retVal:'N', storeId:$('#ITP_FORM_ITP_BIZPOP_PO_REJECT_STORE_ID').val()}
		parentPopFn(transParm);
		$('#ITP_BIZPOP_PO_REJECT').modal('hide'); // 팝업닫기
	});
*/

	$('#ITP_BIZPOP_DLV_STTMT_PRN_jqGridEmpty').on('click', function() {
		itp_fn_BIZPOP_DLV_STTMT_PRN_empty.back();
		//itp_BIZPOP_PO_REJECT_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 = {brandId: $('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_BRAND_ID').val(), dlvSttmtUnqNo: $('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_DLV_STTMT_UNQ_NO').val() };
			itp_BIZPOP_DLV_STTMT_PRN_param = param;
		},
		back: function() {
		}
	};

	itp_fn_BIZPOP_DLV_STTMT_PRN_empty.push();
	let param = {brandId: $('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_BRAND_ID').val(), dlvSttmtUnqNo: $('#ITP_FORM_ITP_BIZPOP_DLV_STTMT_PRN_DLV_STTMT_UNQ_NO').val() };
	console.log(JSON.stringify(param));
	
	const gridColModel = {
		detail: [
			{
					index: 'PCH_PODR_UNQ_NO', name: 'pchPodrUnqNo',
					label: '발주번호', //
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'ITEM_ID', name: 'itemId',
					label: '품목번호', //
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'ITEM_NM', name: 'itemNm',
					label: '품목명', //
					width: '10', fixed: false, align: 'left',
					sortable: false, hidden: false
				},
				{
					index: 'PODR_QTY', name: 'podrQty',
					label: '발주수량', //
					width: '5', fixed: false, align: 'center',
					sortable: false, hidden: false,
					formatter: 'integer', formatoptions: {thousandsSeparator: ','}
				},
				{
					index: 'DLV_QTY', name: 'dlvQty',
					label: '납품서수량', //
					width: '5', fixed: false, align: 'center',
					sortable: false, hidden: false,
					formatter: 'integer', formatoptions: {thousandsSeparator: ','}
				},
				{
					index: 'EXPRY_DATE', name: 'expryDate',
					label: '유통기한', //
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				}
		]
	};
	
	pageObj.call(param);
	pageObj.button();
};


let pageObj = {
	init: function() {
		this.button();
	},
	call: function( param) {
		fn_ajax_call(API_POP_INV_INFO, JSON.stringify(param), this.callback, 'POST');
	},
	button: function() {
		$('#ITP_BIZPOP_DLV_STTMT_PRN_BTN_PRINT').on('click', function (printDiv){
			  var divToPrint=document.getElementById('ITP_BIZPOP_DLV_STTMT_PRN_PRINT_AREA');
			  $('#ITP_BIZPOP_DLV_STTMT_PRN_BTN_PRINT').hide();
			  $('#ITP_ASIDE .itp_modal .itp_form').css("margin-bottom", "20px");
			  $('#ITP_POPUP_CONTAINER .itp_company_type').css("width", "auto");
			  /*
			  var newWin=window.open('','Print-Window');
			  newWin.document.open();
			  newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
			  newWin.document.close();
			  setTimeout(function(){newWin.close();},10);
			  */
			  itp_fn_printDiv(divToPrint);
			  $('#ITP_BIZPOP_DLV_STTMT_PRN_BTN_PRINT').show();
			  $('#ITP_ASIDE .itp_modal .itp_form').css("margin-bottom", "5px");
		});

		
	}, 
	
	callback: function (result) {
		console.log(result);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvSttmtUnqNo').text(result.dlvSttmtUnqNo);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvRegDt').text(result.dlvRegDt);
		
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvMgrNm').text(result.dlvMgrNm);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvAddr').text(result.dlvAddr);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnWhsMgrTelNo').text(result.whsMgrTelNo);
		
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvBsnsRegno').text(result.dlvBsnsRegno);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvNm').text(result.dlvNm);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvOwnerNm').text(result.dlvOwnerNm);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvAddr').text(result.dlvAddr);
		
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnPoBsnsRegno').text(result.poBsnsRegno);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnPoNm').text(result.poNm);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnPoOwnerNm').text(result.poOwnerNm);
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnPoAddr').text(result.poAddr);
		
		$('#ITP_BIZPOP_DLV_STTMT_PRN .fnDlvQty').text(result.dlvQty);
		
		// 품목  리스트 조회
		pageObj.grid.init(result.invDtlInfoList);
	},
	grid: {
		init: function( gridRows) {
			this.gridRows = gridRows;
			this.unload();
			this.load(gridRows);
		},
		gridId: '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid',
		gridList: '#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid',
		gridEmpty: '#ITP_BIZPOP_DLV_STTMT_PRN_jqGridEmpty',
		gridRows: '',
		load : function(gridRows) {
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: [
						{
								index: 'PCH_PODR_UNQ_NO', name: 'pchPodrUnqNo',
								label: '발주번호', //
								width: '10', fixed: false, align: 'center',
								sortable: false, hidden: false
							},
							{
								index: 'ITEM_ID', name: 'itemId',
								label: '품목번호', //
								width: '10', fixed: false, align: 'center',
								sortable: false, hidden: false
							},
							{
								index: 'ITEM_NM', name: 'itemNm',
								label: '품목명', //
								width: '10', fixed: false, align: 'left',
								sortable: false, hidden: false
							},
							{
								index: 'PODR_QTY', name: 'podrQty',
								label: '발주수량', //
								width: '5', fixed: false, align: 'center',
								sortable: false, hidden: false,
								formatter: 'integer', formatoptions: {thousandsSeparator: ','}
							},
							{
								index: 'DLV_QTY', name: 'dlvQty',
								label: '납품서수량', //
								width: '5', fixed: false, align: 'center',
								sortable: false, hidden: false,
								formatter: 'integer', formatoptions: {thousandsSeparator: ','}
							},
							{
								index: 'EXPRY_DATE', name: 'expryDate',
								label: '유통기한', //
								width: '10', fixed: false, align: 'center',
								sortable: false, hidden: false
							}
					],
				data: gridRows,
				multiselect: false,
				loadComplete: function(data) {
					$('#ITP_BIZPOP_DLV_STTMT_PRN_jqGrid').find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					data.records === 0 ? _this.clearData() : $('#ITP_BIZPOP_DLV_STTMT_PRN_jqGridEmpty').hide();
				},
				onCellSelect: function(rowid, cellIdx, cellValue) {
				}
			};
			itp_fn_grid_make_local(option);
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};



