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

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

/*화면 변수*/
const POMNG03010_GRID_ID = '#ITP_POMNG03010_jqGrid';
const POMNG03010_GRID_LIST = '#ITP_POMNG03010_jqGrid_list';
const POMNG03010_GRID_PAGER = '#ITP_POMNG03010_jqGridPager';
const POMNG03010_GRID_EMPTY = '#ITP_POMNG03010_jqGridEmpty';

const POMNG03010_DETAIL_GRID_ID = '#ITP_POMNG03010_DETAIL_jqGrid';
const POMNG03010_DETAIL_GRID_LIST = '#ITP_POMNG03010_DETAIL_jqGrid_list';
const POMNG03010_DETAIL_GRID_PAGER = '#ITP_POMNG03010_DETAIL_jqGridPager';
const POMNG03010_DETAIL_GRID_EMPTY = '#ITP_POMNG03010_DETAIL_jqGridEmpty';

const POMNG03010_NDETAIL_GRID_ID = '#ITP_POMNG03010_NDETAIL_jqGrid';
const POMNG03010_NDETAIL_GRID_LIST = '#ITP_POMNG03010_NDETAIL_jqGrid_list';
const POMNG03010_NDETAIL_GRID_PAGER = '#ITP_POMNG03010_NDETAIL_jqGridPager';
const POMNG03010_NDETAIL_GRID_EMPTY = '#ITP_POMNG03010_NDETAIL_jqGridEmpty';

let ITP_FORM_POMNG03010_DETAIL_IS_WRITING = false;
let POMNG03010_GRID_LAST_ROW_ID;
let SCREEN_MODE ;

/*API URL*/
let API_DETAIL_GRID_LIST 		= '/api/pomng/po-target-grid-list';	// 목록
let API_DETAIL_REJECT 		 	= '/api/pomng/reject-pchReq'; // 구매요청 선택한건 반려 처리 
let API_DETAIL_POPASS_CHK   	= '/api/pomng/po/chk-pchOrd'; // 목록에서 선택한 구매요청건에 대해 발주가능여부 체크 함
let API_DETAIL_PO_INIT_INFO    	= '/api/pomng/po/init-pchOrd'; // 구매요청발주생성 오픈시 호출하여 데이타 세팅함
let API_DETAIL_NEW_INIT_INFO    = '/api/pomng/po/init-poOrd'; // 신규발주생성 오픈시 호출하여 데이타 세팅함
let API_DETAIL_SAVE 		    = '/api/pomng/po/save-pchPo';			// 저장
let API_DETAIL_PASSQTY 		    = '/api/stock/mng/whs-passqty-list';			// 창고선택후 해당 창고의 출고가능수량을 가져옴


/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'PCH_REQ_DTL_NO', name: 'pchReqDtlNo',
			label: ITP_MSG_LOCALE.label.pchReqDtlNo, //구매요청상세번호
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드ID
			width: '10', 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: 'DLV_DVSN', name: 'dlvDvsn',
			label: ITP_MSG_LOCALE.label.dlvDvsnNm, //배송구분
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'PCH_REQ_UNQ_NO', name: 'pchReqUnqNo',
			label: ITP_MSG_LOCALE.label.pchReqUnqNo, //구매요청고유번호
			width: '11', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'DLV_DVSN_NM', name: 'dlvDvsnNm',
			label: ITP_MSG_LOCALE.label.dlvDvsnNm, //배송구분
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'STORE_ID', name: 'storeId',
			label: ITP_MSG_LOCALE.label.storeId, //매장아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STORE_NM', name: 'storeNm',
			label: ITP_MSG_LOCALE.label.storeNm, //매장명
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'SPPLY_ID', name: 'spplyId',
			label: ITP_MSG_LOCALE.label.spplyId, //공급사아이디
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'SPPLY_NM', name: 'spplyNm',
			label: ITP_MSG_LOCALE.label.spplyNm, //공급사명
			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: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.whsNm, //창고명
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'LOCATION', name: 'location',
			label: ITP_MSG_LOCALE.label.location, //창고상세코드
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'LOCATION_NM', name: 'locationNm',
			label: ITP_MSG_LOCALE.label.locationNm, //창고상세명
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true, classes: 'ui-ellipsis'
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목아이디
			width: '11', 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: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //구매요청단가
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PO_UNIT_AMT', name: 'poUnitAmt',
			label: ITP_MSG_LOCALE.label.poUnitAmt, //발주단가
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PCH_REQ_QTY', name: 'pchReqQty',
			label: ITP_MSG_LOCALE.label.pchReqQty, //구매요청수량
			width: '8', 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: 'PCH_REQ_AMT', name: 'pchReqAmt',
			label: ITP_MSG_LOCALE.label.pchReqAmt, //납품요청금액
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PCH_ORD_AMT', name: 'pchOrdAmt',
			label: ITP_MSG_LOCALE.label.pchOrdSpplyAmt, //본사납품금액
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'LEAD_TIME', name: 'leadTime',
			label: ITP_MSG_LOCALE.label.leadTime, //리드타임
			width: '10', fixed: false, align: 'right',
			minwidth: 100,
			sortable: false, hidden: false
		}
	],
	detail: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'PCH_REQ_UNQ_NO', name: 'pchReqUnqNo',
			label: ITP_MSG_LOCALE.label.pchReqUnqNo,  //구매요청번호
			width: '12', fixed: false, align: 'center',
			sortable: false, hidden: false, edittype: 'text',
			editable: false
		},
		{
			index: 'PCH_REQ_DTL_NO', name: 'pchReqDtlNo',
			label: ITP_MSG_LOCALE.label.pchReqDtlNo, //구매요청항번
			width: '30', fixed: false, align: 'center',
			sortable: true, editable: false, edittype: 'text',
			hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, // 품목아이디
			width: '12', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '10', fixed: false, align: 'left',
			sortable: false, editable: false, edittype: 'text',
			hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '7', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text',
			hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, //단위
			width: '8', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			hidden: false
		},
		{
			index: 'PCH_REQ_QTY', name: 'pchReqQty',
			label: ITP_MSG_LOCALE.label.pchReqQty, //구매요청수량
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text',
			hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: ITP_MSG_LOCALE.label.podrQty, //발주수량
			width: '10', fixed: false, align: 'right',
			classes: 'input_color',
			sortable: false, editable: true, edittype: 'text',
			editrules: {required: true}, hidden: false,		
			editoptions: {"onKeyup": "this.value=this.value.replace(/[^0-9]/g,'');", maxlength: 10}, 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_AMT', name: 'podrAmt',
			label: ITP_MSG_LOCALE.label.podrAmt, //발주금액
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text',
			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, editable: false, edittype: 'text',
			hidden: false
		},
		{
			index: 'WHS_PASS_QTY', name: 'whsPassQty',
			label: ITP_MSG_LOCALE.label.whsPassQty, //출고가능수량
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text',
			hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'BRAND_UNIT_UNQ_NO', name: 'brandUnitUnqNo',
			label: ITP_MSG_LOCALE.label.brandUnitUnqNo, //브랜드단가고유번호
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			hidden: true
		},
		{
			index: 'STORE_UNIT_UNQ_NO', name: 'storeUnitUnqNo',
			label: ITP_MSG_LOCALE.label.storeUnitUnqNo, //매장단가고유번호
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			hidden: true
		}
	]
};

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

			// 공통코드 표시
			$('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_POMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_POMNG03010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_POMNG03010_SEARCH_TO_DT').datepicker('setDate', new Date());
			
		},
		grid: function() {
			//listObj.empty.init();
			itp_fn_jqgrid_resize(POMNG03010_GRID_ID, POMNG03010_GRID_LIST, 'lg');
		},
		ready: function() {
			listObj.init();
		}
	},
	action: function () {
		var _this = this;
		// 검색 납품장소
		$('#ITP_FORM_POMNG03010_SSEARCH_SWHS_NM').on('click', function() {
			_this.popup('SW');
		});
		// 검색 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_POMNG03010_SDELETE_SWHS_NM').on('click', function() {
			$('#ITP_FORM_POMNG03010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_POMNG03010_SEARCH_SWHS_ID').val('');
			
		});
		
		// 검색 공급사 
		$('#ITP_FORM_POMNG03010_SSEARCH_SSPPLY_NM').on('click', function() {
			_this.popup('SS');
		});
		// 검색 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_POMNG03010_SDELETE_SSPPLY_NM').on('click', function() {
			$('#ITP_FORM_POMNG03010_SEARCH_SSPPLY_NM').val('');
			$('#ITP_FORM_POMNG03010_SEARCH_SSPPLY_ID').val('');
			
		});
		
		
		// 상세 출하창고
		$('#ITP_BTN_POMNG03010_SEARCH_SHMT_WHS_NM').on('click', function() {
			_this.popup('SHW');
		});
		// 검색 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG03010_DELETE_SHMT_WHS_NM').on('click', function() {
			$('#ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_ID').val('');
			$('#ITP_FORM_POMNG03010_DETAIL_SHMT_LOCATION').val('');
			$('#ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_NM').val('');
			
		});
		
		
		// 검색 공급사 
		$('#ITP_BTN_POMNG03010_NDETAIL_SEARCH_SPPLY_NM').on('click', function() {
			_this.popup('NS');
		});
		// 검색 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG03010_NDETAIL_DELETE_SPPLY_NM').on('click', function() {
			
			var spplyId = $('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val();
			
			
			var records = $(POMNG03010_NDETAIL_GRID_ID).getGridParam('reccount')
			
			if (spplyId != '' && records != 0 ) {
				// 공급업체 삭제시 품목까지 삭제 처리되어집니다. 삭제 하시겠습니까?
				var delSpply = {
						callBack: function(args) {
								$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_NM').val('');
								$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val('');	
								
								// 품목그리드삭제 처리	
								pageObj.grid.clearData('NPC');
								$('#ITP_FORM_POMNG03010_NDETAIL .fnPodrAmt').text('');	
							}
					};
					itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.delAllItem, delSpply, null);
			} else {
				$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_NM').val('');
				$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val('')
			}
		});
		
		// 상세 출하창고
		$('#ITP_BTN_POMNG03010_SEARCH_WHS_NM').on('click', function() {
			_this.popup('NHW');
		});
		// 검색 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG03010_DELETE_WHS_NM').on('click', function() {
			$('#ITP_FORM_POMNG03010_NDETAIL_WHS_ID').val('');
			$('#ITP_FORM_POMNG03010_NDETAIL_LOCATION').val('');
			$('#ITP_FORM_POMNG03010_NDETAIL_WHS_NM').val('');
			
		});
		
		
	},
	popup: function (arg) {
		var popFn ;
		// 팝업
		if (arg == "SW") {
			popFn = this.callback.searWhs;
		} else if (arg == "SS") {
			popFn = this.callback.searSpply;
		}  else if (arg == "SHW") {
			popFn = this.callback.searShmtWhs;
		} else if (arg == "NS") {
			popFn = this.callback.searNSpply;
		} else if (arg == "NHW") {
			popFn = this.callback.searNWhs;
		} else if (arg == "REJ") {
			popFn = this.callback.poRej;
		}
		
		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" || arg == "NS" ) {
			fn_call_popup('biz', 'BIZPOP_STORE_SPPLY', '#ITP_ASIDE', popFn, key, 'S');
		} else if (arg == "SHW" || arg == "NHW") {
			const keyb = {brandId:fn_make_user_info.get('brandId'),storeId:"", whsDvsn:"W01"};
			fn_call_popup('biz', 'BIZPOP_WHS_LOCATION', '#ITP_ASIDE', popFn, keyb, 'S');
		} else if (arg == "REJ" ) {
			/* 선택ROW가져오기 */
			const rowKey = $(POMNG03010_GRID_ID).getGridParam('selrow');
			if (!rowKey) {
				return;
			}
			
			var selectedIds = $(POMNG03010_GRID_ID).getGridParam('selarrrow');
			if(selectedIds.length > 1) { // 1개만 선택
			   alert("구매요청 반려대상에 대해 하나만 선택해주세요 (대표 구매요청번호)") ;
				return;
			} 
			
			var selRowData ;
			
			for (var i=selectedIds.length-1; i>=0; i--) {
				selRowData = $(POMNG03010_GRID_ID).jqGrid('getRowData', selectedIds[i]);
			}
			
			const key1 = {pchReqUnqNo:selRowData.pchReqUnqNo, storeId:selRowData.storeId};
			
			fn_call_popup('biz', 'BIZPOP_PO_REJECT', '#ITP_ASIDE', popFn, key1, 'S');
		} 
		
		
	},
	callback: {
		searWhs: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG03010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_POMNG03010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
			}
			
		},
		searSpply: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG03010_SEARCH_SSPPLY_NM').val(rowDataPop.spplyNm);
				$('#ITP_FORM_POMNG03010_SEARCH_SSPPLY_ID').val(rowDataPop.spplyId);
			}
			
		},
		searNSpply: function(rowDataPop) {
			if(rowDataPop) {
			   //console.log("1111111111111");
			   //console.log(rowDataPop);
				var pspplyId = $('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val();
			   //console.log("222222222222");
			   //console.log(pspplyId);
				if (pspplyId == "") {
				 //  console.log("333333");
				//	console.log(rowDataPop.spplyId);
				//	console.log(rowDataPop.spplyNm);
	
					$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_NM').val(rowDataPop.spplyNm);
					$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val(rowDataPop.spplyId);
				} else {
					var records = $(POMNG03010_NDETAIL_GRID_ID).getGridParam('reccount')
					if (records != 0) {
						if (pspplyId != rowDataPop.spplyId ) {
							alert( "추가한 품목의 공급사와 서로 틀립니다 !");
							return ;
						}
					}
					
				}
			}
			
		},
		searShmtWhs: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_NM').val(rowDataPop.whsNm + " - " + rowDataPop.locationNm);
				$('#ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_ID').val(rowDataPop.whsId);
				$('#ITP_FORM_POMNG03010_DETAIL_SHMT_LOCATION').val(rowDataPop.location);
				
				// 본사배송일경우 해당 창고의 재고를 가져와 출고가능수량에 기입한다.
				// 만약 본사 배송일 경우 진행 처리함		
					
				var param = {};
				let gridUpdateData = [];
				const rowData = $(POMNG03010_DETAIL_GRID_ID).getRowData();
				$.each(rowData, function(key, value) {
					gridUpdateData.push(value);
				});

				param.gridSize = 1000;
				param.gridPage = 1;
				param.brandId 	= fn_make_user_info.get('brandId');
				param.whsId 	= rowDataPop.whsId;
				param.location 	= rowDataPop.location;
				param.gridUpdateData = gridUpdateData;
					
				modifyObj.whsPassQty(param);
			
			}
		},
		searNWhs: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_POMNG03010_NDETAIL_WHS_NM').val(rowDataPop.whsNm + " - " + rowDataPop.locationNm);
				$('#ITP_FORM_POMNG03010_NDETAIL_WHS_ID').val(rowDataPop.whsId);
				$('#ITP_FORM_POMNG03010_NDETAIL_LOCATION').val(rowDataPop.location);
			}
		},
		poRej: function(rowDataPop) {
			if(rowDataPop) {
				
				if (rowDataPop.retVal == "Y") {
				  modifyObj.button.rejectSave(rowDataPop);
				}
				
				
			}
		}
		
		
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_POMNG03010_SRH'					: listObj.button.search();			break;	// 조회 버튼 클릭
						case 'ITP_BTN_POMNG03010_POCREATE'				: listObj.button.chkpo(); 		break;	// prpo 버튼
						case 'ITP_BTN_POMNG03010_NEWPOCREATE'			: listObj.button.newpocreate();	break;	// 신규po 버튼
						case 'ITP_BTN_POMNG03010_REJECT'				: listObj.button.reject();  		break;	// 반려
						case 'ITP_BTN_POMNG03010_PO'					: modifyObj.button.save(); 			break;	// 발주
						case 'ITP_BTN_POMNG03010_CANCELLIST'			: modifyObj.button.cancel(); 		break;	// 취소/목록 버튼
						case 'ITP_BTN_POMNG03010_NDETAIL_ADDROW'			: pageObj.grid.button.addRow(); 	break;	// 품목추가
						case 'ITP_BTN_POMNG03010_NDETAIL_DELROW'			: pageObj.grid.button.delRow(); 	break;	// 품목삭제
						case 'ITP_BTN_POMNG03010_EXCEL'					: listObj.excelDown(); 	break;	// 품목삭제
					}
				});
			});
			
			$('body').on('click', function(e) {
				
				var gridId = ((SCREEN_MODE === 'NPC') ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).replace('#', '');
				var rids = $( (SCREEN_MODE === 'NPC') ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				
				itp_fn_grid_reset_selection(e, last_row_id, gridId, (SCREEN_MODE === 'NPC') ?  'POMNG03010_NDETAIL' : 'POMNG03010_DETAIL');	
				pageObj.grid.recal();
			});

		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_POMNG03010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_POMNG03010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_AJAX_POMNG03010_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_POMNG03010_SRH', '#ITP_BTN_POMNG03010_POCREATE','#ITP_BTN_POMNG03010_NEWPOCREATE', '#ITP_BTN_POMNG03010_REJECT']);
			$('#ITP_FORM_POMNG03010_DETAIL_SBRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_POMNG03010_DETAIL_SSTORE_ID').val(fn_make_user_info.get('storeId'));
			
			$('#ITP_FORM_POMNG03010_SDELETE_SSPPLY_NM').show();
			$('#ITP_FORM_POMNG03010_SDELETE_SWHS_NM').show();
			$('#ITP_BTN_POMNG03010_EXCEL').show();
			SCREEN_MODE = 'LIST' ;
		} else if(mode == 'NPC') { // 신규발주등록
			$('#ITP_AJAX_POMNG03010_NDETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_POMNG03010_PO', '#ITP_BTN_POMNG03010_CANCELLIST', '#ITP_BTN_POMNG03010_NDETAIL_ADDROW','#ITP_BTN_POMNG03010_NDETAIL_DELROW']);
			$('#ITP_FORM_POMNG03010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			SCREEN_MODE = 'NPC' ;
			pageObj.grid.init('NPC');
		/*
			$('#ITP_AJAX_POMNG03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_POMNG03010_PO', '#ITP_BTN_POMNG03010_CANCELLIST', '#ITP_BTN_POMNG03010_NDETAIL_ADDROW', '#ITP_BTN_POMNG03010_NDETAIL_DELROW']);
			$('#ITP_FORM_POMNG03010_NDETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_POMNG03010_NDETAIL_STORE_ID').val(fn_make_user_info.get('storeId'));
			
			$('#ITP_FORM_POMNG03010_NDETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_POMNG03010_NDETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});
			
			var now = new Date();
			$('#ITP_TAB_POMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_POMNG03010_NDETAIL_DLV_REQ_DT').datepicker('setDate', new Date(now.setDate(now.getDate()+5)));
			*/
		} else if(mode == 'POC') { // 구매요청 발주
			$('#ITP_AJAX_POMNG03010_DETAIL_CONTAINER').show();
			$('#ITP_BTN_POMNG03010_DELETE_SHMT_WHS_NM').show();
			fn_show_btn_auth_array(['#ITP_BTN_POMNG03010_PO', '#ITP_BTN_POMNG03010_CANCELLIST']);
			$('#ITP_FORM_POMNG03010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_POMNG03010_DETAIL_STORE_ID').val(fn_make_user_info.get('storeId'));
			
			
			
			
			SCREEN_MODE = 'POC' ;
			
			
		}
	},
	grid: {
		init: function(mode, gridRows) {
			this.gridId = (mode === 'NPC') ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID;
			this.gridList = (mode === 'NPC') ? POMNG03010_NDETAIL_GRID_LIST : POMNG03010_DETAIL_GRID_LIST;
			this.gridEmpty = (mode === 'NPC') ? POMNG03010_NDETAIL_GRID_EMPTY : POMNG03010_DETAIL_GRID_EMPTY;
			this.gridRows = gridRows;
			this.unload();
			this.load(mode, gridRows);
			
		},
		gridId: '',
		button: {
			addRow: function() {
				// 팝업
				var popFn = function(rowDataPop) {
					// 기존등록 데이터
					var exists = '';
					const rowData = $(POMNG03010_NDETAIL_GRID_ID).getRowData();
					$.each(rowData, function(key, value) {
						if (value.viewCd != "D") {
							exists = exists + value.brandUnitUnqNo + ';';	
						}
					});
					
					var diffchk = "N";
					var spplyId = $('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val();
					var spplyNm = $('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_NM').val();
					
					// 넘어오는 데이타가 서로 상이한 공급업체인지 파악
					var chkSpplyId = "";
					var chkSpplyNm = "";
					$.each(rowDataPop, function(key, value) {
						
						if (chkSpplyId != "" ) {
							if (value.spplyId != chkSpplyId ) {
								alert("서로다른 공급사 품목을 발주 진행 할 수 없습니다.");
								diffchk = "Y" ;
								return false ;
							} 
						} else {
							chkSpplyId = value.spplyId;
							chkSpplyNm = value.spplyNm;
						}
					});
					
					if (diffchk == "Y") return;
				
					if (spplyId == "" ) {
						$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val(chkSpplyId);
						$('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_NM').val(chkSpplyNm);
					} else {
						if (spplyId != chkSpplyId ) {
							alert("납품 공급사와 품목 공급사 정보가 서로 다릅니다.");
							diffchk = "Y" ;
						}
					}
					
					if (diffchk == "Y") return;
					
					/*
					$.each(rowDataPop, function(key, value) {
						// 공급업체가 상이사면 오류 알럿 출력
						$.each(rowData, function(pkey, pvalue) {
							console.log(rowData);
							if (value.spplyId != pvalue.spplyId ) {
								alert("2 서로다른 공급사 품목을 발주 진행 할 수 없습니다.")
								diffchk = "Y";
								return false;
							}
						});
						if (diffchk == "Y") {
							return false;
						}
					});
					
					if (diffchk == "Y") return;
					*/
					
					$.each(rowDataPop, function(key, value) {
						// 공급업체가 상이사면 오류 알럿 출력
						if (exists.indexOf(value.brandUnitUnqNo) < 0 && value.podrPssblDvsn == 'PO01') {
							value['viewCd'] = "C";
							
							$(POMNG03010_NDETAIL_GRID_ID).jqGrid('addRowData', value.brandUnitUnqNo, value, 'last');
						}
					});
					$(POMNG03010_NDETAIL_GRID_EMPTY).hide();
					
				};
				const key = {brandId:fn_make_user_info.get('brandId')};
				fn_call_popup('biz', 'BIZPOP_ITEM', '#ITP_ASIDE', popFn, key, 'S');  // ==>dwkim 추후 작업 진행  파라메타 설명필요
			},
			delRow: function() {
				itp_fn_grid_del_row(POMNG03010_NDETAIL_GRID_ID);
				pageObj.grid.recal();
			}
			
		},
		gridList: '',
		gridEmpty: '',
		gridRows: '',
		load: function(mode, gridRows) {
			
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: gridColModel.detail,
				data: gridRows,
				//cellEdit: true,
				multiselect: (mode === 'NPC'),
				loadComplete: function(data) {
					$(mode === 'NPC' ? POMNG03010_NDETAIL_GRID_LIST : POMNG03010_DETAIL_GRID_LIST).find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					
					data.records === 0 ? _this.clearData(mode) : $(_this.mode === 'NPC' ? _this.clearData(mode) : POMNG03010_DETAIL_GRID_EMPTY).hide();
					
				},
				onCellSelect: function(rowid, cellIdx, cellValue) {
					POMNG03010_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowid, cellIdx, POMNG03010_GRID_LAST_ROW_ID, _this.gridId, ['pchReqDtlNo']);
				}
			};
			itp_fn_grid_make_local(option);
		},
		recal: function() {
			//PCH_REQ_AMT
			if (SCREEN_MODE == 'NPC' || SCREEN_MODE === 'POC') {
				var ids = $((SCREEN_MODE === 'NPC') ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).getDataIDs();
				$.each(ids, function(idx, rowId) {
					var viewCd = jQuery(SCREEN_MODE === 'NPC' ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'viewCd');
					var unitAmt = jQuery(SCREEN_MODE === 'NPC' ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'unitAmt');
					var podrQty = jQuery(SCREEN_MODE === 'NPC' ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'podrQty');
					
					if(viewCd != 'D' && unitAmt != null && unitAmt != '' && podrQty != null && podrQty != '') {
					
						jQuery(SCREEN_MODE === 'NPC' ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('setRowData',rowId,{podrAmt: parseInt(unitAmt)*parseInt(podrQty)});
					}
				});
				
				const rowData = $((SCREEN_MODE == 'NPC') ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).getRowData();
				var podrAmt = 0 ;
				$.each(rowData, function(key, value) {
					if (value.podrAmt != null && value.podrAmt != "" && value.viewCd != "D") {
						podrAmt = podrAmt + parseInt(value.podrAmt) ;
					}
					
				});
			
				if (SCREEN_MODE == 'NPC')  {
					$('#ITP_FORM_POMNG03010_NDETAIL .fnPodrAmt').text(itp_fn_number_comma(podrAmt));	
				} else {
					$('#ITP_FORM_POMNG03010_DETAIL .fnPodrAmt').text(itp_fn_number_comma(podrAmt));
				}
			}
		},
		clearData : function(mode) {
			$(mode === 'NPC' ? POMNG03010_NDETAIL_GRID_ID : POMNG03010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
			$(mode === 'NPC' ? POMNG03010_NDETAIL_GRID_LIST : POMNG03010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(mode === 'NPC' ? POMNG03010_NDETAIL_GRID_EMPTY : POMNG03010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(mode === 'NPC' ? POMNG03010_NDETAIL_GRID_EMPTY : POMNG03010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_POMNG03010_search: false,
	button: {
		search: function() {
			listObj.itp_POMNG03010_search = true;
			let param = $('#ITP_FORM_POMNG03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			console.log(111);
			console.log(JSON.stringify(param));
			$(POMNG03010_GRID_ID).data('grid-param',param);
			$(POMNG03010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		pocreate: function(param) {
			pageObj.switchScreen('POC');
			itp_fn_form_clear_validate(null, '#ITP_FORM_POMNG03010_DETAIL');
			modifyObj.init(param);
		},
		newpocreate: function() {
			pageObj.switchScreen('NPC');
			itp_fn_form_clear_validate(null, '#ITP_FORM_POMNG03010_NDETAIL');
			createObj.init();
		},
		reject: function() {
			//팝업 오픈
			pageObj.popup('REJ');
		},
		chkpo: function() {	
			let gridUpdateData = [];
			const rowKey = $(POMNG03010_GRID_ID).getGridParam('selrow');
			if (!rowKey) {
				return;
		    }
	
		    var selectedIds = $(POMNG03010_GRID_ID).getGridParam('selarrrow');
			
		    for (var i=selectedIds.length-1; i>=0; i--) {
				gridUpdateData.push($(POMNG03010_GRID_ID).jqGrid('getRowData', selectedIds[i]));	
			}
			
			let param = {};
			param.brandId=fn_make_user_info.get('brandId');
			param.gridUpdateData = gridUpdateData;
			var chkFn = function(result) {
				if (result.code == "Y") {
					//발수 생성화면으로 전화 
					listObj.button.pocreate(param);
				}
			};
			fn_ajax_call(API_DETAIL_POPASS_CHK, JSON.stringify(param), chkFn, 'POST');
		}, 
	},
	empty: {
		init: function() {
			/*
			var _this = this;
			this.push();
			$(POMNG03010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_POMNG03010_param.gridSize = $.jgrid.defaults.rowNum;
				$(POMNG03010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_POMNG03010_param)}).trigger('reloadGrid');
			});
			*/
		},
		itp_POMNG03010_param: {},
		push: function() {
			/*
			let param = $('#ITP_FORM_POMNG03010_SEARCH').serializeObject();
			listObj.itp_POMNG03010_param = param;
			*/
		},
		back: function() {
			/*
			$('#ITP_FORM_POMNG03010_SEARCH_FROM_DT').val(this.itp_POMNG03010_param.fromDt);
			$('#ITP_FORM_POMNG03010_SEARCH_TO_DT').val(this.itp_POMNG03010_param.toDt);
			$('#ITP_FORM_POMNG03010_SEARCH_SPPLY_ID').val(this.itp_POMNG03010_param.sspplyId);
			$('#ITP_FORM_POMNG03010_SEARCH_SPPLY_NM').val(this.itp_POMNG03010_param.sspplyNm);
			$('#ITP_FORM_POMNG03010_SEARCH_WHS_NM').val(this.itp_POMNG03010_param.swhsNm);
			$('#ITP_FORM_POMNG03010_SEARCH_WHS_ID').val(this.itp_POMNG03010_param.swhsId);
			$('#ITP_FORM_POMNG03010_SEARCH_ITEM_NM').val(this.itp_POMNG03010_param.sitemNm);
			*/
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			//itp_fn_jqgrid_resize(POMNG03010_GRID_ID, POMNG03010_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_POMNG03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			
			var option = {
				multiselect: true,
				autowidth: true,
				gridId: POMNG03010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: POMNG03010_GRID_PAGER,
				rownumbers: false,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, POMNG03010_GRID_ID, true, 'number', 'POMNG03010', listObj.itp_POMNG03010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(POMNG03010_GRID_ID).getDataIDs();
					var css = {'background-color':'#FFCCCC'};
					$.each(ids, function(idx, rowId) {
						$(POMNG03010_GRID_ID).jqGrid('setCell', rowId, 'storeId', '', ITP_GRID_COL_STYLE.link);
						$(POMNG03010_GRID_ID).jqGrid('setCell', rowId, 'dlvReqDt', '', css);
						$(POMNG03010_GRID_ID).jqGrid('setCell', rowId, 'whsNm', '', css);
					});
					$(POMNG03010_GRID_ID).setSelection(1, true);
				},
				onPaging: function(action) {
					itp_fn_grid_paging(POMNG03010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		button: {
		},
		unload : function() {
			$.jgrid.gridUnload(POMNG03010_GRID_ID);
		},
		clearData : function() {
			$(POMNG03010_GRID_ID).jqGrid('clearGridData', true);
			$(POMNG03010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(POMNG03010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(POMNG03010_GRID_EMPTY).show();
		}
	},
	excelDown: function() {
		
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_POMNG03010_SEARCH').serializeObject(),
			'gridColumns': $(POMNG03010_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_POMNG03010_DETAIL');
			const selectVal = $('#ITP_FORM_POMNG03010_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_POMNG03010_DETAIL').reset();
			document.getElementById('ITP_FORM_POMNG03010_NDETAIL').reset();
			listObj.grid.search();
		},
		rejectSave: function(param) {
		
			var modalFn = {
					callBack: () => {
						var searhFn = function () {
							ITP_FORM_POMNG03010_DETAIL_IS_WRITING = false;
							pageObj.grid.clearData();
							modifyObj.button.cancel();
						}
						fn_ajax_call(API_DETAIL_REJECT, param, searhFn, 'GET');
					}
				};
				itp_fn_modal_confirm('구매요청 반려 하시겠습니까?', modalFn);
		},
		save: function() {
			$('body').trigger('click');
			const formId = (SCREEN_MODE == 'NPC' ? '#ITP_FORM_POMNG03010_NDETAIL' : '#ITP_FORM_POMNG03010_DETAIL');
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				const rowData1 = "";
				
				var now = new Date();
					var today = now.getFullYear() +
									"." + (now.getMonth()+1 > 9 ? (now.getMonth()+1).toString() : "0" + (now.getMonth()+1).toString()) +
									"." + (now.getDate() > 9 ? now.getDate().toString() : "0" + now.getDate().toString());
									
				if (SCREEN_MODE == 'NPC') {
					const rowData1 = $(POMNG03010_NDETAIL_GRID_ID).getRowData();
				
					var dataCnt = 0;
					$.each(rowData1, function(key, value) {
						console.log(value.viewCd);
						if (value.viewCd != 'D') {
							dataCnt = dataCnt + 1;
						} 
					});
					
					if (dataCnt == 0) {
						alert( "발주 생성할 품목내역이 없습니다.");
						return ;
					}
					
					//공급업체 체크
					if ( $('#ITP_FORM_POMNG03010_NDETAIL_SPPLY_ID').val() == "") {
						alert("공급업체를 선택해주세요!")
							return;
					}
					
					if ( $('#ITP_FORM_POMNG03010_NDETAIL_WHS_ID').val() == "") {
						alert("납품장소를 선택해주세요!")
							return;
					}
					
					
					var ndlvReqDt = $('#ITP_FORM_POMNG03010_NDETAIL_DLV_REQ_DT').val();
					if (today > ndlvReqDt) {
						alert("납품요청일은 오늘 이후이어야 합니다.");
						return;
					}	
					
					
				} else if (SCREEN_MODE == 'POC') {
					// 본사배송일 경우 출하창고 필수
					var dlvDvsn = $('#ITP_FORM_POMNG03010_DETAIL_DLV_DVSN').val();
					
					if (dlvDvsn == 'DL01') {
						var chkWhsId = $('#ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_ID').val();
						if (chkWhsId == "") {
							alert("출하창고를 선택해주세요!")
							return;
						}
					}	
					
					var dlvReqDt = $('#ITP_FORM_POMNG03010_DETAIL_DLV_REQ_DT').val();
					if (today > dlvReqDt) {
						alert("납품요청일은 오늘 이후이어야 합니다.");
						return;
					}	
					
				}
				
				var modalFn = {
					callBack: () => {
						
						if (SCREEN_MODE == 'NPC') {
							itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_NDETAIL');
						} else {
							itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_DETAIL');
						}
						
						
						let gridInsertData = [];
						const rowData = (SCREEN_MODE == 'NPC' ? $(POMNG03010_NDETAIL_GRID_ID).getRowData() : $(POMNG03010_DETAIL_GRID_ID).getRowData());
						 
					    var jobCd = "";
						if (SCREEN_MODE == 'NPC') {
							jobCd = $('#ITP_FORM_POMNG03010_NDETAIL_JOB_CD').val()
						} else {
							jobCd = $('#ITP_FORM_POMNG03010_DETAIL_JOB_CD').val()
						}
						
						
						var chkdlvDvsn = $('#ITP_FORM_POMNG03010_DETAIL_DLV_DVSN').val();
						
						
						var chkQtyYn = "N";
						$.each(rowData, function(key, value) {
							
							if (value.podrQty == '' || value.podrQty < 1 ) {
								alert("발주수량을 입력해주세요!");
								chkQtyYn = "Y";
								return false;
							}
							
							if (jobCd == 'R' && parseInt(value.podrQty) > parseInt(value.pchReqQty) ) {
								 alert("발주수량이 구매요청 수량보다 클수 없습니다!");
								chkQtyYn = "Y";
								return false;
							}
							
							if (chkdlvDvsn == 'DL01') {
								if (jobCd == 'R' && parseInt(value.podrQty) > parseInt(value.whsPassQty) ) {
									 alert("출고가능 수량이 부족합니다.!");
									chkQtyYn = "Y";
									return false;
								}
							}
							
							gridInsertData.push(value);
						});
						
						if (chkQtyYn == "Y") return;
						
		
						let param = $(formId).serializeObject();
						param.gridInsertData = gridInsertData;
						
						var searhFn = function () {
							ITP_FORM_POMNG03010_DETAIL_IS_WRITING = false;
							pageObj.grid.clearData();
							modifyObj.button.cancel();
						}
						console.log(JSON.stringify(param));
						fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('발주 진행 하시겠습니까?', modalFn);
			}
		}
	},
	
	load: function(param) {
		fn_ajax_call(API_DETAIL_PO_INIT_INFO, JSON.stringify(param), this.callback, 'POST');
	},
	callback: function (result) {
		
		pageObj.switchScreen('POC');
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_VIEW_CD').val('C');
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_POMNG03010_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_SPPLY_ID').val(result.spplyId);
		$('#ITP_FORM_POMNG03010_DETAIL .fnPchReqNm').text(result.spplyNm);
		$('#ITP_FORM_POMNG03010_DETAIL .fnPodrDt').text(result.podrDt);
		$('#ITP_FORM_POMNG03010_DETAIL .fnWhsNm').text(result.dlvPlace);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_WHS_ID').val(result.whsId);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_LOCATION').val(result.location);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_PODR_MGR_ID').val(result.podrMgrId);
		$('#ITP_FORM_POMNG03010_DETAIL .fnPodrMgrNm').text(result.podrMgrNm);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_PODR_MGR_TEL_NO').val(result.podrMgrTelNo);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_DLV_DVSN').val(result.dlvDvsn);
		$('#ITP_FORM_POMNG03010_DETAIL .fnDlvDvsnNm').text(result.dlvDvsnNm);
		$('#ITP_FORM_POMNG03010_DETAIL .fnPodrAmt').text(itp_fn_number_comma(result.podrTotalAmt));
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_DLV_REQ_DT').val(result.dlvReqDt);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_SHMT_WHS_ID').val(result.shmtWhsId);
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DELETE_SHMT_WHS_NM').val(result.shmtWhsNm + " - " + result.shmtLocationNm );
		$('#ITP_FORM_POMNG03010_DETAIL #ITP_FORM_POMNG03010_DETAIL_SHMT_LOCATION').val(result.shmtLocation);
		
		if (result.dlvDvsn == "DL01") {  //본사배송일경우
			$('#ITP_BTN_POMNG03010_SEARCH_SHMT_WHS_NM').attr("disabled", false);
			$('#ITP_BTN_POMNG03010_DELETE_SHMT_WHS_NM').attr("disabled", false);
			
		} else {  // 공급사 배송일 경우 
			$('#ITP_BTN_POMNG03010_SEARCH_SHMT_WHS_NM').attr("disabled", true);
			$('#ITP_BTN_POMNG03010_DELETE_SHMT_WHS_NM').attr("disabled", true);
		}
		
		
		
		// 품목  리스트 조회
		pageObj.grid.init('POC', result.itemList);
		// modifyObj.grid.load(result.spplyMgrList);
	},
	whsPassQty: function(param) {
		console.log(JSON.stringify(param));
		// 추후작업 진행
		fn_ajax_call(API_DETAIL_PASSQTY, JSON.stringify(param), this.passQtyCallback, 'POST');
	},
	passQtyCallback: function (result) {
		console.log(result);
		console.log(result.passList);
		console.log(result.passList.length);
		const rowData = $(POMNG03010_DETAIL_GRID_ID).getRowData();
		$.each(rowData, function(key, value) {
			
			for (var i = 0 ; i < result.passList.length ; i++) {
				var retrow = result.passList[i];
				if (retrow.itemId == value.itemId) {
					jQuery(POMNG03010_DETAIL_GRID_ID).jqGrid('setRowData',key+1,{whsPassQty: retrow.whsPassQty});
					break;	
				}
			}
		});

		
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		$('#ITP_FORM_POMNG03010_NDETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
		itp_fn_form_clear_validate(null, '#ITP_FORM_POMNG03010_NDETAIL');
		const key = {brandId:fn_make_user_info.get('brandId'), viewCd: 'R'};
		var param = $.param(key);
		createObj.load(param);
		
	},
	button: {
		cancel: function() {
			document.getElementById('ITP_FORM_POMNG03010_DETAIL').reset();
			listObj.grid.search();
		},
		save: function() {
			const formId = '#ITP_FORM_POMNG03010_DETAIL';
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				var modalFn = {
					callBack: (args) => {
						itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_DETAIL');
						let param = $(formId).serializeObject();
						var searhFn = function () {
							ITP_FORM_POMNG03010_DETAIL_IS_WRITING = false;
							listObj.grid.search();
						}
						fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('저장 하시겠습니까?', modalFn);
			}
		}
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_NEW_INIT_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		
		//pageObj.switchScreen('NPC');
		
		$('#ITP_FORM_POMNG03010_NDETAIL #ITP_FORM_POMNG03010_DETAIL_VIEW_CD').val('C');
		$('#ITP_FORM_POMNG03010_NDETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_POMNG03010_NDETAIL #ITP_FORM_POMNG03010_NDETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_POMNG03010_NDETAIL .fnDlvDvsnNm').text(result.dlvDvsnNm);
		$('#ITP_FORM_POMNG03010_NDETAIL #ITP_FORM_POMNG03010_NDETAIL_DLV_DVSN').val(result.dlvDvsn);
		$('#ITP_FORM_POMNG03010_NDETAIL .fnPodrDt').text(result.podrDt);
		$('#ITP_FORM_POMNG03010_NDETAIL .podrMgrId').text(result.podrMgrId);
		$('#ITP_FORM_POMNG03010_NDETAIL .fnPodrMgrNm').text(result.podrMgrNm);
		$('#ITP_FORM_POMNG03010_NDETAIL .fnPodrMgrNo').text(result.podrMgrTelNo);
		
		var now = new Date();
		$('#ITP_TAB_POMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
		$('#ITP_FORM_POMNG03010_NDETAIL_DLV_REQ_DT').datepicker('setDate', new Date(now.setDate(now.getDate()+5)));
		
		// 품목  리스트 조회
		//pageObj.grid.init('DETAIL', result.itemList);
		
		
	}
};
