require(['config'], function() {
	require([], function($) {
		pageObj.init();
	});
});

/********************************************************
 --------------------------------------------------------
 - Object 목록								            -
 --------------------------------------------------------
 * const gridColModel = {}	: 화면 Grid Object			*
 * let pageObj = {}			: 화면공통 Object			*
 * let listObj = {}			: 목록화면 Object			*
 * let modifyObj = {}		: 수정화면 Object			*
 
 *******************************************************/

/*화면 변수*/
const INOUTMNG01010_GRID_ID = '#ITP_INOUTMNG01010_jqGrid';
const INOUTMNG01010_GRID_LIST = '#ITP_INOUTMNG01010_jqGrid_list';
const INOUTMNG01010_GRID_PAGER = '#ITP_INOUTMNG01010_jqGridPager';
const INOUTMNG01010_GRID_EMPTY = '#ITP_INOUTMNG01010_jqGridEmpty';

const INOUTMNG01010_DETAIL_GRID_ID = '#ITP_INOUTMNG01010_DETAIL_jqGrid';
const INOUTMNG01010_DETAIL_GRID_LIST = '#ITP_INOUTMNG01010_DETAIL_jqGrid_list';
const INOUTMNG01010_DETAIL_GRID_PAGER = '#ITP_INOUTMNG01010_DETAIL_jqGridPager';
const INOUTMNG01010_DETAIL_GRID_EMPTY = '#ITP_INOUTMNG01010_DETAIL_jqGridEmpty';


//let ITP_FORM_INOUTMNG01010_DETAIL_IS_WRITING = false;
let INOUTMNG01010_GRID_LAST_ROW_ID;


/*API URL*/
let API_DETAIL_GRID_LIST = '/api/pomng/inoutmng/target-grid-list';	// 목록
let API_DETAIL_SAVE 	 = '/api/pomng/inoutmng/save-inst-shmt';			// 출하지시

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드명
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'PCH_PODR_UNQ_NO', name: 'pchPodrUnqNo',
			label: ITP_MSG_LOCALE.label.pchPodrUnqNo, //구매발주번호
			width: '10', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_DTL_NO', name: 'pchPodrDtlNo',
			label: ITP_MSG_LOCALE.label.pchPodrDtlNo, //구매발주항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목아이디
			width: '10', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '10', fixed: false, align: 'left',
			minwidth: 100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'WHS_ID', name: 'whsId',
			label: ITP_MSG_LOCALE.label.whsId, //납품창고아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.whsNm, //납품창고명
			width: '15', fixed: false, align: 'left',
			minwidth: 220,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'LOCATION', name: 'location',
			label: ITP_MSG_LOCALE.label.location, //납품LOCATION
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'right',
			minwidth: 80,
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: ITP_MSG_LOCALE.label.podrQty, //발주수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'SHMT_QTY', name: 'shmtQty',
			label: ITP_MSG_LOCALE.label.shmtQty, //출고수량
			width: '10', fixed: false, align: 'right',
			sortable: true, editable: false, edittype: 'text',
			hidden: true
		},
		{
			index: 'STCK_QTY', name: 'stckQty',
			label: ITP_MSG_LOCALE.label.stckQty, //현 재고
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'EXPRY_YN', name: 'expryYn',
			label: ITP_MSG_LOCALE.label.expryYn, //유통기한여부
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'EXPRY_PERIOD', name: 'expryPeriod',
			label: ITP_MSG_LOCALE.label.expryPeriod, //유통기한기간
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'EXPRY_DATE', name: 'expryDate',
			label: ITP_MSG_LOCALE.label.expryDate, //유통일자
			width: '20', fixed: false, align: 'center',
			sortable: false, hidden: true,
			editable: false, sorttype: "date",
			editoptions: { dataInit: function(e) { $(e).datepicker(ITP_DATE_LANGUAGE); } }
		},
		{
			index: 'PODR_AMT', name: 'podrAmt',
			label: ITP_MSG_LOCALE.label.podrAmt, //발주금액
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'DLV_REQ_DT', name: 'dlvReqDt',
			label: ITP_MSG_LOCALE.label.dlvReqDt, //납품요청일
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'SHMT_WHS_ID', name: 'shmtWhsId',
			label: ITP_MSG_LOCALE.label.shmtWhsId, //출고창고아이디
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true
		},
		{
			index: 'SHMT_LOCATION', name: 'shmtLocation',
			label: ITP_MSG_LOCALE.label.shmtLocation, //출고LOCATION
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'SHMT_WHS_NM', name: 'shmtWhsNm',
			label: ITP_MSG_LOCALE.label.shmtWhsNm, //출고창고명
			width: '10', fixed: false, align: 'left',
			minwidth: 220,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		}
	],
	detail: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드명
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'PCH_PODR_UNQ_NO', name: 'pchPodrUnqNo',
			label: ITP_MSG_LOCALE.label.pchPodrUnqNo, //구매발주번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_DTL_NO', name: 'pchPodrDtlNo',
			label: ITP_MSG_LOCALE.label.pchPodrDtlNo, //구매발주항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목아이디
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'WHS_ID', name: 'whsId',
			label: ITP_MSG_LOCALE.label.whsId, //납품창고아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.whsNm, //납품창고명
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'LOCATION', name: 'location',
			label: ITP_MSG_LOCALE.label.location, //납품LOCATION
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: ITP_MSG_LOCALE.label.podrQty, //발주수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'SHMT_QTY', name: 'shmtQty',
			label: ITP_MSG_LOCALE.label.shmtQty, //출고수량
			width: '10', fixed: false, align: 'right',
			sortable: true, editable: true, edittype: 'text',
			editrules: { required: true, number: true },
			editoptions: {"onKeyup": "this.value=this.value.replace(/[^0-9]/g,'');"}, 
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'STCK_QTY', name: 'stckQty',
			label: ITP_MSG_LOCALE.label.stckQty, //현 재고
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'EXPRY_YN', name: 'expryYn',
			label: ITP_MSG_LOCALE.label.expryYn, //유통기한여부
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'EXPRY_PERIOD', name: 'expryPeriod',
			label: ITP_MSG_LOCALE.label.expryPeriod, //유통기한기간
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'EXPRY_DATE', name: 'expryDate',
			label: ITP_MSG_LOCALE.label.expryDate, //유통일자
			width: '20', fixed: false, align: 'center',
			sortable: false, hidden: false,
			editable: true, sorttype: "date",
			editoptions: { dataInit: function(e) { $(e).datepicker(ITP_DATE_LANGUAGE); } , 'readonly': 'readonly'}
		},
		{
			index: 'PODR_AMT', name: 'podrAmt',
			label: ITP_MSG_LOCALE.label.podrAmt, //발주금액
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'DLV_REQ_DT', name: 'dlvReqDt',
			label: ITP_MSG_LOCALE.label.dlvReqDt, //납품요청일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SHMT_WHS_ID', name: 'shmtWhsId',
			label: ITP_MSG_LOCALE.label.shmtWhsId, //출고창고아이디
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true
		},
		{
			index: 'SHMT_LOCATION', name: 'shmtLocation',
			label: ITP_MSG_LOCALE.label.shmtLocation, //출고LOCATION
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'SHMT_WHS_NM', name: 'shmtWhsNm',
			label: ITP_MSG_LOCALE.label.shmtWhsNm, //출고창고명
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		}
	]
};

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
		this.action();
	},
	ui: {
		init: function () {
			this.view();
			this.form();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('INOUTMNG01010');

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

			// 조회일자 지정
			var now = new Date();
			$('#ITP_TAB_INOUTMNG01010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setDate(now.getDate())));
			$('#ITP_FORM_INOUTMNG01010_SEARCH_TO_DT').datepicker('setDate', new Date(now.setDate(now.getDate() + 30)));
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_INOUTMNG01010_DETAIL');
		},
		grid: function() {
			itp_fn_jqgrid_resize(INOUTMNG01010_GRID_ID, INOUTMNG01010_GRID_LIST, 'lg');
			//listObj.empty.init();
			
		},
		ready: function() {
			listObj.init();
		}
	},
	action: function () {
		var _this = this;
		// 출하창고 조회 버튼 클릭
		$('#ITP_FORM_INOUTMNG01010_SEARCH_SHMT_WHS_NM').on('click', function() {
			_this.popup('S');
		});
		// 출하창고 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_INOUTMNG01010_DELETE_SHMT_WHS_NM').on('click', function() {
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMT_WHS_NM').val('');
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMTWHS_ID').val('');

		});

		// 납품장소 조회 버튼 클릭
		$('#ITP_FORM_INOUTMNG01010_SEARCH_WHS_NM').on('click', function() {
			_this.popup('W');
		});
		// 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_INOUTMNG01010_DELETE_WHS_NM').on('click', function() {
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_ID').val('');
		});
	},
	popup: function (arg) {
		var popFn;
		// 팝업
		if (arg == "S") {
			popFn = this.callback.shmtwhs;
		} else {
			popFn = this.callback.whs;
		}

		var whsDvsn = "";

		if (arg == "S") {
			whsDvsn = "W01";
		} else {
			whsDvsn = "W02";
		}

		const key = { brandId: fn_make_user_info.get('brandId'), storeId: fn_make_user_info.get('storeId'), whsDvsn: whsDvsn };



		fn_call_popup('biz', 'BIZPOP_WHS_LOCATION', '#ITP_ASIDE', popFn, key, 'S');
		
		
	},
	callback: {
		shmtwhs: function(rowDataPop) {
			if (rowDataPop) {
				$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMT_WHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMTWHS_ID').val(rowDataPop.whsId);
			}

		},
		whs: function(rowDataPop) {
			if (rowDataPop) {
				$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
			}

		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_INOUTMNG01010_SRH'				: listObj.button.search();			break;	// 조회 버튼 클릭
						case 'ITP_BTN_INOUTMNG01010_OUC'				: listObj.button.shmtcreat();		break;	// 출하생성 버튼
						case 'ITP_BTN_INOUTMNG01010_OUT'				: modifyObj.button.save(); 			break;	// 출하지시 버튼
						case 'ITP_BTN_INOUTMNG01010_CANCELLIST'			: modifyObj.button.cancel(); 		break;	// 취소/목록 버튼
						case 'ITP_BTN_INOUTMNG01010_EXCEL'				: listObj.excelDown(); 				break;	// 엑셀다운로드
					}
				});
			});
			
			$('body').on('click', function(e) {
				
				var gridId = (INOUTMNG01010_DETAIL_GRID_ID).replace('#', '');
				var rids = $(INOUTMNG01010_DETAIL_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				
				itp_fn_grid_reset_selection(e, last_row_id, gridId, 'INOUTMNG01010_DETAIL');	
			});

		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_INOUTMNG01010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_INOUTMNG01010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_AJAX_INOUTMNG01010_LIST_CONTAINER').show();
			$('#ITP_FORM_INOUTMNG01010_DELETE_SHMT_WHS_NM').show();
			$('#ITP_FORM_INOUTMNG01010_DELETE_WHS_NM').show();
			fn_show_btn_auth_array(['#ITP_BTN_INOUTMNG01010_SRH', '#ITP_BTN_INOUTMNG01010_OUC']);
			$('#ITP_FORM_INOUTMNG01010_DETAIL_SBRAND_ID').val(fn_make_user_info.get('brandId'));
			
			$('#ITP_BTN_INOUTMNG01010_EXCEL').show();
		} else if(mode == 'DETAIL') { // 출하지시 상세
			$('#ITP_AJAX_INOUTMNG01010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_INOUTMNG01010_OUT', '#ITP_BTN_INOUTMNG01010_CANCELLIST']);
			$('#ITP_FORM_INOUTMNG01010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			
			//pageObj.grid.init(param);
		} 
	},
	grid: {
		init: function(param) {
			this.gridId = INOUTMNG01010_DETAIL_GRID_ID;
			this.gridList = INOUTMNG01010_DETAIL_GRID_LIST;
			this.gridEmpty = INOUTMNG01010_DETAIL_GRID_EMPTY;
			var now = new Date();
			$('#ITP_TAB_INOUTMNG01010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_INOUTMNG01010_DETAIL_DLV_SCH_DT').datepicker('setDate', new Date(param.dlvSchDt));
			$('#ITP_FORM_INOUTMNG01010_DETAIL .fnBrandNm').text(param.brandNm);
			
			this.gridRows = param.recordData;
			this.unload();
			this.load(param.recordData);
			
		},
		gridId: '',
		button: {},
		gridList: '',
		gridEmpty: '',
		gridRows: '',
		load: function(gridRows) {
			
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: gridColModel.detail,
				data: gridRows,
				loadComplete: function(data) {
					$(INOUTMNG01010_DETAIL_GRID_LIST).find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					data.records === 0 ? _this.clearData(mode) : $(INOUTMNG01010_DETAIL_GRID_EMPTY).hide();
					
				},
				onCellSelect: function(rowid, cellIdx, cellValue) {
					INOUTMNG01010_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowid, cellIdx, INOUTMNG01010_GRID_LAST_ROW_ID, _this.gridId, ['pchReqDtlNo']);
				}
			};
			itp_fn_grid_make_local(option);
		},
		clearData : function(mode) {
			$(INOUTMNG01010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
			$(INOUTMNG01010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(INOUTMNG01010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(INOUTMNG01010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_INOUTMNG01010_search: false,
	button: {
		search: function() {
			listObj.itp_INOUTMNG01010_search = true;
			let param = $('#ITP_FORM_INOUTMNG01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(INOUTMNG01010_GRID_ID).data('grid-param',param);
			$(INOUTMNG01010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		shmtcreat: function() {
			//선택되어진 row 값 전송할예정
			let gridUpdateData = [];
			const rowKey = $(INOUTMNG01010_GRID_ID).getGridParam('selrow');
			if (!rowKey) {
				return;
		    }

			var selectedIds = $(INOUTMNG01010_GRID_ID).getGridParam('selarrrow');
			
			var brandNm = "";
			var maxDlvSchDt = "";
			
			for (var i=selectedIds.length-1; i>=0; i--) {
				var rowData = $(INOUTMNG01010_GRID_ID).jqGrid('getRowData', selectedIds[i]);
				
				gridUpdateData.push(rowData);	
				if (rowData.dlvReqDt > maxDlvSchDt) {
					maxDlvSchDt = rowData.dlvReqDt;
				}
				sbrandNm =rowData.brandNm;
			}
			
			let param = {brandNm: sbrandNm, dlvSchDt: maxDlvSchDt };
			param.recordData = gridUpdateData;
			pageObj.switchScreen('DETAIL');
			itp_fn_form_clear_validate(null, '#ITP_FORM_INOUTMNG01010_DETAIL');
			pageObj.grid.init(param);
		}
	},
	empty: {
		init: function() {
			/*
			var _this = this;
			this.push();
			$(INOUTMNG01010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_INOUTMNG01010_param.gridSize = $.jgrid.defaults.rowNum;
				$(INOUTMNG01010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_INOUTMNG01010_param)}).trigger('reloadGrid');
			});
			*/
		},
		itp_INOUTMNG01010_param: {},
		push: function() {
			/*
			let param = $('#ITP_FORM_INOUTMNG01010_SEARCH').serializeObject();
			listObj.itp_INOUTMNG01010_param = param;
			*/
		},
		back: function() {
			/*
			$('#ITP_FORM_INOUTMNG01010_SEARCH_FROM_DT').val(this.itp_INOUTMNG01010_param.fromDt);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_TO_DT').val(this.itp_INOUTMNG01010_param.toDt);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMTWHS_ID').val(this.itp_INOUTMNG01010_param.sshmtWhsId);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SSHMT_WHS_NM').val(this.itp_INOUTMNG01010_param.sshmtWhsNm);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_NM').val(this.itp_INOUTMNG01010_param.swhsNm);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_SWHS_ID').val(this.itp_INOUTMNG01010_param.swhsId);
			$('#ITP_FORM_INOUTMNG01010_SEARCH_ITEM_NM').val(this.itp_INOUTMNG01010_param.sItemNm);
			*/
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			itp_fn_jqgrid_resize(INOUTMNG01010_GRID_ID, INOUTMNG01010_GRID_LIST, 'lg');
			itp_fn_fire_window_resize();
			this.search();
			
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			let param = $('#ITP_FORM_INOUTMNG01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			
			var option = {
				multiselect: true,
				autowidth: true,
				gridId: INOUTMNG01010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: INOUTMNG01010_GRID_PAGER,
				rownumbers: false,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, INOUTMNG01010_GRID_ID, true, 'number', 'INOUTMNG01010', listObj.itp_INOUTMNG01010_search, listObj.empty, true, data.gridRecords, true);
				},
				onPaging: function(action) {
					itp_fn_grid_paging(INOUTMNG01010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		button: {
		},
		unload : function() {
			$.jgrid.gridUnload(INOUTMNG01010_GRID_ID);
		},
		clearData : function() {
			$(INOUTMNG01010_GRID_ID).jqGrid('clearGridData', true);
			$(INOUTMNG01010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(INOUTMNG01010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(INOUTMNG01010_GRID_EMPTY).show();
		}
	},
	excelDown: function() {
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_INOUTMNG01010_SEARCH').serializeObject(),
			'gridColumns': $(INOUTMNG01010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '출하지시대상정보.xlsx',
			'sheetName': '출하지시대상리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function (param) {
		this.load(param);
	},
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_INOUTMNG01010_DETAIL');
			//const selectVal = $('#ITP_FORM_INOUTMNG01010_VIEW_STORE_ID').val();
			//const key = {brandId:fn_make_user_info.get('brandId'), storeId: selectVal, viewCd: 'R'};
			//var param = $.param(key);
			//modifyObj.load(param);
		},
		cancel: function() {
			document.getElementById('ITP_FORM_INOUTMNG01010_DETAIL').reset();
			listObj.grid.search();
		},
		save: function() {
			$('body').trigger('click');
			const formId = ('#ITP_FORM_INOUTMNG01010_DETAIL');
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				
				var modalFn = {
					callBack: () => {
						itp_fn_form_event.removeChar('#ITP_FORM_INOUTMNG01010_DETAIL');
						var chkQtyYn = "N";
						let gridInsertData = [];
						const rowData = $(INOUTMNG01010_DETAIL_GRID_ID).getRowData();
						
						$.each(rowData, function(key, value) {
							
							if (value.shmtQty == '' || parseInt(value.shmtQty) < 1 ) {
								alert("출고수량을 입력해주세요!");
								chkQtyYn = "Y";
								return false;
							}
							
							if (parseInt(value.stckQty) < parseInt(value.shmtQty)) {
								alert("출고지시할 수량이 부족합니다.");
								chkQtyYn = "Y";
								return false;
							}
							
							
							if (parseInt(value.podrQty) < parseInt(value.shmtQty) ) {
								 alert("출고수량이 발주수량보다 클수 없습니다!");
								chkQtyYn = "Y";
								return false;
							}
							
							gridInsertData.push(value);
						});
						
						if (chkQtyYn == "Y") return;
						
						let param = $(formId).serializeObject();
						param.viewCd = "C";
						param.gridInsertData = gridInsertData;
						console.log(JSON.stringify(param));
						var searhFn = function () {
							//ITP_FORM_INOUTMNG01010_DETAIL_IS_WRITING = false;
							pageObj.grid.clearData();
							modifyObj.button.cancel();
						}
						fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('출하지시 하시겠습니까?', modalFn);
			}
		}
	},
	
	load: function(param) {
		//console.log(param);
		//pageObj.grid.load("DETAIL",param);
	}
};


