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

/********************************************************
--------------------------------------------------------
- Object 목록								            -
--------------------------------------------------------
* const gridColModel = {}	: 화면 Grid Object			*
* let pageObj = {}			: 화면공통 Object			*
* let listObj = {}			: 목록화면 Object			*
* let viewObj = {}			: 상세화면 Object			*
* let modifyObj = {}		: 수정화면 Object			*
* let createObj = {}		: 신규화면 Object			*
*******************************************************/

/*화면 변수*/
const POMNG05010_GRID_ID = '#ITP_POMNG05010_jqGrid';
const POMNG05010_GRID_LIST = '#ITP_POMNG05010_jqGrid_list';
const POMNG05010_GRID_PAGER = '#ITP_POMNG05010_jqGridPager';
const POMNG05010_GRID_EMPTY = '#ITP_POMNG05010_jqGridEmpty';

/*API URL*/
let API_DETAIL_GRID_LIST 	= '/api/pomng/inoutmng/deli-grid-list';// 목록
let API_DETAIL_INFO 		= '/api/pomng/inoutmng/info-inv';	// 상세

/*화면 Grid ColModel*/
const gridColModel = {
	list: [ //DLV1 뭔지 물어보기
		{
			index: 'Brand_ID', name: 'brandId',			
			label: ITP_MSG_LOCALE.label.brandId, 		
			width: '18', fixed: false, align: 'center',
			sortable: false, hidden: true				
		},
		{
			index: 'Brand_Nm', name: 'brandNm',			
			label: ITP_MSG_LOCALE.label.brandNm, 		//브랜드이름
			width: '20', fixed: false, align: 'left',	
			minwidth:150,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'DLV_STTMT_NO', name: 'dlvSttmtUnqNo',	
			label: ITP_MSG_LOCALE.label.dlvSttmtUnqNo, 		//납품서번호
			width: '20', fixed: false, align: 'center',	
			minwidth:140,	
			sortable: false, hidden: false					
		},
		{
			index: 'PODR_REG_DT', name: 'pchPodrUnqNo',	
			label: '발주번호', 								//발주번호
			width: '14', fixed: false, align: 'center',	
			minwidth:140,
			sortable: false, hidden: false				
		},
		{
			index: 'PODR_REG_DT', name: 'spplyNm',	
			label: '공급업체', 								//공급업체
			width: '14', fixed: false, align: 'left',	
			minwidth:140,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'DLV_ST_CD', name: 'dlvStNm',		
			label: '납품상태', 								//납품서상태
			width: '12', fixed: false, align: 'center',	
			minwidth:50,
			sortable: false, hidden: false				
		},
		{
			index: 'WHS_NM', name: 'whsNm',				
			label: '입고처', 			//납품장소
			width: '20', fixed: false, align: 'left',	
			minwidth:220,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'PODR_REG_DT', name: 'podrRegDt',	
			label: '발주생성일', 							//발주생성일
			width: '14', fixed: false, align: 'center',	
			minwidth:90,
			sortable: false, hidden: false				
		},
		{
			index: 'DLV_REQ_DT', name: 'dlvReqDt',		
			label: ITP_MSG_LOCALE.label.dlvReqDt, 		//납품요청일
			width: '14', fixed: false, align: 'center',	
			minwidth:90,
			sortable: false, hidden: false				
		},
		{
			index: 'DLV_REG_DT', name: 'dlvRegDt',		
			label:	'납품생성일', 							//발주생성일
			width: '14', fixed: false, align: 'center',	
			minwidth:90,
			sortable: false, hidden: false				
		},
		{
			index: 'DLV_SCH_DT', name: 'dlvSchDt',		
			label: ITP_MSG_LOCALE.label.dlvSchDt, 		//납품예정일
			width: '14', fixed: false, align: 'center',	
			minwidth:90,
			sortable: false, hidden: false				
		},
		{
			index: 'DLV_CMPLT_DT', name: 'dlvCmpltDt',	
			label: '납품완료일', 	//납품완료일
			width: '10', fixed: false, align: 'center',	
			minwidth:90,
			sortable: false, hidden: false				
		},
		{
			index: 'ITEM_ID', name: 'itemId',			
			label: ITP_MSG_LOCALE.label.itemId, 		//품목번호
			width: '11', fixed: false, align: 'center',	
			minwidth:150,
			sortable: false, hidden: false				
		},
		{
			index: 'ITEM_NM', name: 'itemNm',			
			label: ITP_MSG_LOCALE.label.itemNm, 		//품목명
			width: '10', fixed: false, align: 'left',	
			minwidth:180,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'PODR_QTY', name: 'podrQty',			
			label: ITP_MSG_LOCALE.label.podrQty, 		//수주수량
			width: '6', fixed: false, align: 'right',	
			minwidth:80,
			sortable: false, hidden: false				
		},
		{
			index: 'DLV_QTY', name: 'dlvQty',			
			label: ITP_MSG_LOCALE.label.dlvQty, 		//납품수량
			width: '6', fixed: false, align: 'right',	
			minwidth:80,
			sortable: false, hidden: false				
		},
		{
			index: 'UNIT', name: 'unit',				
			label: ITP_MSG_LOCALE.label.unit, 			//단가
			width: '6', fixed: false, align: 'right',	
			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: 'DLV_MGR_NM', name: 'dlvMgrNm',		
			label: ITP_MSG_LOCALE.label.dlvMgrNm, 		//납품담당자
			width: '8', fixed: false, align: 'left',	
			minwidth:100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'DLV_MGR_TEL_NO', name: 'dlvMgrTelNo',	
			label: ITP_MSG_LOCALE.label.dlvMgrTelNo, 	//납품담당연락처
			width: '10', fixed: false, align: 'center',	
			minwidth:120,
			sortable: false, hidden: false				
		}
	]
};

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

			// 공통코드 표시
			$('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_POMNG05010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_POMNG05010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_POMNG05010_SEARCH_TO_DT').datepicker('setDate', new Date());
			
			$('#ITP_BTN_POMNG05010_EXCEL').show();
		},
	},
	action: function () {
		var _this = this;
		// 검색 공급사 
		$('#ITP_BTN_POMNG05010_SEARCH_SPPLY_NM').on('click', function() {
			_this.popup('SS');
		});
		// 검색 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG05010_DELETE_SPPLY_NM').on('click', function() {
			$('#ITP_FORM_POMNG05010_SEARCH_SPPLY_NM').val('');
			$('#ITP_FORM_POMNG05010_SEARCH_SPPLY_ID').val('');
		});
		// 검색 납품장소
		$('#ITP_BTN_POMNG05010_SEARCH_SWHS_NM').on('click', function() {
			_this.popup('SW');
		});
		// 검색 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG05010_DELETE_SWHS_NM').on('click', function() {
			$('#ITP_FORM_POMNG05010_SEARCH_WHS_NM').val('');
			$('#ITP_FORM_POMNG05010_SEARCH_WHS_ID').val('');
   		});
	},
	popup: function (arg) {
		var popFn ;
		// 팝업
		if (arg == "SW") {
			popFn = this.callback.searWhs;
		} else if (arg == "SS") {
			popFn = this.callback.searSpply;
		} 
		
		const key = {brandId:fn_make_user_info.get('brandId'),storeId:fn_make_user_info.get('storeId')};
		
		if (arg == "SW" ) {
			const keya = {brandId:fn_make_user_info.get('brandId'),storeId:fn_make_user_info.get('storeId'), whsDvsn:""};
			fn_call_popup('biz', 'BIZPOP_WHS_LOCATION', '#ITP_ASIDE', popFn, keya, 'S');
		} else if (arg == "SS") {
			fn_call_popup('biz', 'BIZPOP_STORE_SPPLY', '#ITP_ASIDE', popFn, key, 'S');
		}
	},
	callback: {
		searSpply: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG05010_SEARCH_SPPLY_NM').val(rowDataPop.spplyNm);
				$('#ITP_FORM_POMNG05010_SEARCH_SPPLY_ID').val(rowDataPop.spplyId);
			}
		},
		searWhs: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG05010_SEARCH_WHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_POMNG05010_SEARCH_WHS_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_POMNG05010_SRH'	: listObj.button.search();	break;	// 조회 버튼 클릭
						case 'ITP_BTN_POMNG05010_PRT'	: listObj.button.print();	break; 		// 납품서출력 버튼
						case 'ITP_BTN_POMNG05010_EXCEL'	: listObj.excelDown();		break;	// 엑셀
					}
				});
			});
		}
	},
	screen: function() {
		$('#ITP_FORM_POMNG05010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));//input hidden 값 넣기				
		$('#ITP_FORM_POMNG05010_SEARCH_DTL_GUBUN').val('dtlGubun');
		$('#ITP_BTN_POMNG05010_DELETE_SPPLY_NM').show();
		$('#ITP_BTN_POMNG05010_DELETE_SWHS_NM').show();
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_POMNG05010_search: false,
	button: {
		search: function() {
			listObj.itp_POMNG05010_search = true;
			let param = $('#ITP_FORM_POMNG05010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			console.log(param);
			$(POMNG05010_GRID_ID).data('grid-param',param);
			$(POMNG05010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		print: function() {
			if(!fn_make_user_info.isEmpty()) {
				const rowKey = $(POMNG05010_GRID_ID).getGridParam('selrow'); // 그리드 셀 선택
				if (!rowKey) {//선택 안했을시
					alert("납품서를 선택해주세요 (대표 납품서번호)") ; //출력
					return; //리턴
				}
				var selectedIds = $(POMNG05010_GRID_ID).getGridParam('selarrrow');
				if(selectedIds.length > 1) { // 1이상 선택시
					alert("납품서 하나만 선택해주세요 (대표 납품서번호)") ;//출력
					return; //리턴
				} 
				var selRowData ; //변수선언
				
				for (var i=selectedIds.length-1; i>=0; i--) {//1개만 선택시
					selRowData = $(POMNG05010_GRID_ID).jqGrid('getRowData', selectedIds[i]);//cell 선택
				}
				const key = {brandId :fn_make_user_info.get('brandId'), dlvSttmtUnqNo : selRowData.dlvSttmtUnqNo}; //popup key 값 설정
				fn_call_popup('biz', 'BIZPOP_DLV_STTMT_PRN', '#ITP_ASIDE',null, key, 'S'); //config 참고 421줄  팝업 이름,키
			}
		}
	},
	empty: {
		init: function() {
			
		},
		itp_POMNG05010_param: {},
		push: function() {
			
		},
	},
	grid: {
		init: function () {
			itp_fn_jqgrid_resize(POMNG05010_GRID_ID, POMNG05010_GRID_LIST, 'lg');
			itp_fn_fire_window_resize();
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.screen();
			this.load();
		},
		load: function() {
			let param = $('#ITP_FORM_POMNG05010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.sbrandId = fn_make_user_info.get('brandId');
			var option = {
				gridId: POMNG05010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: POMNG05010_GRID_PAGER,
				multiselect: true, 
				rownumbers: false,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, POMNG05010_GRID_ID, true, 'number', 'POMNG05010', listObj.itp_POMNG05010_search, listObj.empty, true, data.gridRecords, true);
				},
				onPaging: function(action) {
					itp_fn_grid_paging(POMNG05010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_POMNG05010_SEARCH').serializeObject(),
			'gridColumns': $(POMNG05010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '납품서현황.xlsx',
			'sheetName': '납품서현황리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};

