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


//화면변수
const POMNG04010_GRID_ID = '#ITP_POMNG04010_jqGrid';
const POMNG04010_GRID_LIST = '#ITP_POMNG04010_jqGrid_list';
const POMNG04010_GRID_PAGER = '#ITP_POMNG04010_jqGridPager';
const POMNG04010_GRID_EMPTY = '#ITP_POMNG04010_jqGridEmpty';

const POMNG04010_DETAIL_GRID_ID = '#ITP_POMNG04010_DETAIL_jqGrid';
const POMNG04010_DETAIL_GRID_LIST = '#ITP_POMNG04010_DETAIL_jqGrid_list';
const POMNG04010_DETAIL_GRID_PAGER = '#ITP_POMNG04010_DETAIL_jqGridPager';
const POMNG04010_DETAIL_GRID_EMPTY = '#ITP_POMNG04010_DETAIL_jqGridEmpty';

let ITP_PODR_DTL_ST_CD = fn_make_common_cd_list(CODE_LIST, 'PODR_DTL_ST_CD', true, '구매발주상세상태코드');


//API URL
let API_POMNG_PO_DETAIL_GRID_LIST =  '/api/pomng/po/detail-grid-list';
let API_POMNG_PO_INFO_PCHODR =  '/api/pomng/po/info-pchOdr';
let API_POMNG_PO_CANCEL_PCHPO =  '/api/pomng/po/cancel-pchPo';


//화면 grid ColModel
const gridColModel = {
	list:[
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			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: 'PCH_PODR_UNQ_NO', name: 'pchPodrUnqNo',
			label: ITP_MSG_LOCALE.label.pchPodrUnqNo, 		// 발주번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PCH_ODR_ST_NM', name: 'pchOdrStNm',
			label: ITP_MSG_LOCALE.label.pchOdrStNm, 		// 발주상태명
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PODR_DT', name: 'podrDt',
			label: ITP_MSG_LOCALE.label.podrDt, 			// 발주일자
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.locationNm, 		// 입고처
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			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: 'PODR_TOTAL_AMT', name: 'podrTotalAmt',
			label: ITP_MSG_LOCALE.label.podrTotalAmt, 		// 총 발주금액
			width: '8', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'PODR_ITEM_QTY', name: 'podrItemQty',
			label: ITP_MSG_LOCALE.label.podrItemQty, 		// 발주품목수
			width: '7', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'PODR_MGR_NM', name: 'podrMgrNm',
			label: ITP_MSG_LOCALE.label.podrMgrNm, 			// 발주담당자
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	],
	view: [],
	detail: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, 			// 품목번호
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, 			// 품목명
			width: '6', fixed: false, align: 'left',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, 			// 단가
			width: '3', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, 				// 기본단위
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: ITP_MSG_LOCALE.label.podrQty, 			// 발주수량
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'PODR_AMT', name: 'podrAmt',
			label: ITP_MSG_LOCALE.label.podrAmt, 			// 발주금액
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'WHS_PASS_QTY', name: 'whsPassQty',
			label: ITP_MSG_LOCALE.label.whsPassQty, 		// 출고가능수량
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_DTL_NO', name: 'pchPodrDtlNo',
			label: ITP_MSG_LOCALE.label.pchPodrDtlNo, 		// 발주항번
			width: '3', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'PODR_DTL_ST_CD', name: 'podrDtlStCd',
			label: ITP_MSG_LOCALE.label.podrDtlStCd, 		// 구매발주상세상태코드
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_PODR_DTL_ST_CD}
		}
	]
};


/*화면공통 Object*/
let pageObj = {
	init: function() {
		this.ui.init();
		this.event.init();
	},
	ui: {
		init: function() {
			this.view();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('POMNG04010');
			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});
			// 조회일자 지정	
			var now = new Date();
			var defaultToDate1 = new Date(now);
			var defaultToDate2 = new Date(now);
			defaultToDate1.setDate(now.getDate() - 30);
			defaultToDate2.setDate(now.getDate() + 30);
			$('#ITP_TAB_POMNG04010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_POMNG04010_SEARCH_FROM_DT').datepicker('setDate', defaultToDate1);
			$('#ITP_FORM_POMNG04010_SEARCH_TO_DT').datepicker('setDate', defaultToDate2);
		},
		grid: function() {
			itp_fn_jqgrid_resize(POMNG04010_GRID_ID, POMNG04010_GRID_LIST, 'lg');
			//listObj.empty.init();
			//itp_fn_fire_window_resize();
		},
		ready: function() {
			listObj.init();
		}
	},
	event: {
		init: function() {
			this.button();
		},
		button: function() {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_POMNG04010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_POMNG04010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_POMNG04010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_POMNG04010_CANCELLIST'		: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_POMNG04010_DELETE'			: modifyObj.button.del();		break;	// 발주 취소 버튼(예정)
						case 'ITP_BTN_POMNG04010_EXCEL'			    : listObj.excelDown();			break;	// 엑셀
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_POMNG04010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_POMNG04010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
            fn_show_btn_auth_array(['#ITP_BTN_POMNG04010_SRH', '#ITP_BTN_POMNG04010_NEWREG']);
            $('#ITP_AJAX_POMNG04010_LIST_CONTAINER').show();
            $('#ITP_FORM_POMNG04010_SEARCH #ITP_FORM_POMNG04010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_BTN_POMNG04010_SEARCH_SPPLY_ID').show();
            $('#ITP_BTN_POMNG04010_DELETE_SPPLY_ID').show();
            $('#ITP_BTN_POMNG04010_SEARCH_WHS_ID').show();
            $('#ITP_BTN_POMNG04010_DELETE_WHS_ID').show();
			$('#ITP_BTN_POMNG04010_EXCEL').show();
            
        } /*else if(mode == 'ADD') { // 등록
            $('#ITP_AJAX_POMNG04010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_POMNG04010_CANCELLIST', '#ITP_BTN_POMNG04010_SAVE']);

            $('#ITP_FORM_POMNG04010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
                $(this).attr('id') === 'ITP_FORM_POMNG04010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
                if(elem.type === 'select') {
                    $(this).val('').prop('selected', true);
                }
            });
            $('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_FILE_NO').val('');
            $('#ITP_FORM_POMNG04010_DETAIL_BRAND_ID').removeAttr('readonly');
            $('#ITP_FORM_POMNG04010_DETAIL_BRAND_NM').removeAttr('readonly');
            $('#ITP_FORM_POMNG04010_DETAIL_BSNS_REG_NO').removeAttr('readonly');
            $('#ITP_FORM_POMNG04010_DETAIL_CORP_REG_NO').removeAttr('readonly');
            // $('#ITP_FORM_POMNG04010_DETAIL_SEARCH_AFFL_SHOP').removeAttr('disabled');
            // $('#ITP_FORM_POMNG04010_DETAIL_DELETE_AFFL_SHOP').removeAttr('disabled');
            $('#ITP_FORM_POMNG04010_DETAIL_SEARCH_AFFL_SHOP').show();
            $('#ITP_FORM_POMNG04010_DETAIL_DELETE_AFFL_SHOP').show();
			$('#ITP_FORM_POMNG04010_DETAIL .itp_form_info').hide();
			$('#ITP_FORM_POMNG04010_DETAIL .itp_form_change').show();

            $('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
            $('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));

            $('.fnBrandId').text('(자동부여)');
        } */else if(mode == 'MODIFY') { // 수정
            $('#ITP_AJAX_POMNG04010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_POMNG04010_CANCELLIST', '#ITP_BTN_POMNG04010_SAVE']);

            $('#ITP_BTN_POMNG04010_CANCELLIST').show();
            $('#ITP_BTN_POMNG04010_DELETE').show();
            
            $('#ITP_FORM_POMNG04010_DETAIL_BRAND_ID').attr('readonly', true);
            $('#ITP_FORM_POMNG04010_DETAIL_BRAND_NM').attr('readonly', true);
            $('#ITP_FORM_POMNG04010_DETAIL_BSNS_REG_NO').attr('readonly', true);
            $('#ITP_FORM_POMNG04010_DETAIL_CORP_REG_NO').attr('readonly', true);
            // $('#ITP_FORM_POMNG04010_DETAIL_SEARCH_AFFL_SHOP').attr('disabled', true);
            // $('#ITP_FORM_POMNG04010_DETAIL_DELETE_AFFL_SHOP').attr('disabled', true);
            $('#ITP_FORM_POMNG04010_DETAIL_SEARCH_AFFL_SHOP').hide();
            $('#ITP_FORM_POMNG04010_DETAIL_DELETE_AFFL_SHOP').hide();
			$('#ITP_FORM_POMNG04010_DETAIL .itp_form_info').show();
			$('#ITP_FORM_POMNG04010_DETAIL .itp_form_change').hide();

            $('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
        } /*else if(mode == 'VIEW') { // 보기
            $('#ITP_AJAX_POMNG04010_VIEW_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_POMNG04010_MODIFY', '#ITP_BTN_POMNG04010_CANCELLIST']);
        }*/
    }
};


/*목록화면 Object*/
let listObj = {
	init: function() {
		this.grid.init();
		this.action();
	},
	itp_POMNG04010_param: {},
	itp_POMNG04010_search: false,
	button: {
		search: function() {
			listObj.itp_POMNG04010_search = true;
			let param = $('#ITP_FORM_POMNG04010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            param.sidx = '0';
            console.log(JSON.stringify(param));
			$(POMNG04010_GRID_ID).data('grid-param',param);
            $(POMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        }
	},
	empty: {
		init: function() {
			
		},
		push: function() {
          
        },
        back: function() {
          
        }
	},
	grid: {
		init: function() {
			// 데이터 없을때
            //listObj.empty.push();
            this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
            let param = $('#ITP_FORM_POMNG04010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.sidx = '0';
            console.log(JSON.stringify(param));
            var option = {
            	gridId: POMNG04010_GRID_ID,
            	colModel: gridColModel.list,
            	param: param,
            	url: DOMAIN + API_POMNG_PO_DETAIL_GRID_LIST,
            	pager: POMNG04010_GRID_PAGER,
            	onCellSelect: function(rowId, cellIdx, cellValue) {
                    var cm = $(this).jqGrid('getGridParam', 'colModel');
                    var colNm = cm[cellIdx].name;
                    if (colNm === 'pchPodrUnqNo') {
                        const pchPodrUnqNoVal = $(this).jqGrid('getCell', rowId, 'pchPodrUnqNo');
                        const brandIdVal = $(this).jqGrid('getCell', rowId, 'brandId');
                        const key = {pchPodrUnqNo: pchPodrUnqNoVal, brandId: brandIdVal};
                        var param = $.param(key);
                        modifyObj.init(param);
                    }
				},
				loadComplete: function(data) {
					console.log(data);
                    itp_fn_grid_load_complete(data, POMNG04010_GRID_ID, true, 'number', 'POMNG04010', listObj.itp_POMNG04010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(POMNG04010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(POMNG04010_GRID_ID).jqGrid('setCell', rowId, 'pchPodrUnqNo', '', ITP_GRID_COL_STYLE.link);
                    });
				},
				onPaging: function(action) {
                    itp_fn_grid_paging(POMNG04010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
		},
		clearData: function() {
			$(POMNG04010_GRID_ID).jqGrid('clearGridData', true);
            $(POMNG04010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(POMNG04010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(POMNG04010_GRID_EMPTY).show();
		},
		unload: function() {
			$.jgrid.gridUnload(POMNG04010_GRID_ID);
		}
	},
	action: function() {
		var _this = this;
		// 공급업체 조회 버튼 클릭
		$('#ITP_BTN_POMNG04010_SEARCH_SPPLY_ID').on('click', function() {
			_this.popup('spply');
		});
		// 공급업체 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG04010_DELETE_SPPLY_ID').on('click', function() {
			$('#ITP_FORM_POMNG04010_DETAIL_SPPLY_NM').val('');
			$('#ITP_FORM_POMNG04010_DETAIL_SPPLY_ID').val('');
		});
		// 매장/창고 조회 버튼 클릭
		$('#ITP_BTN_POMNG04010_SEARCH_WHS_ID').on('click', function() {
			_this.popup('whs');
		});
		// 매장/창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_POMNG04010_DELETE_WHS_ID').on('click', function() {
			$('#ITP_FORM_POMNG04010_DETAIL_WHS_NM').val('');
			$('#ITP_FORM_POMNG04010_DETAIL_WHS_ID').val('');
		});
	},
	popup: function(keyword) {
		var popFn = this.callback.create;
		
		if(keyword === 'spply'){
			const key = {brandId:fn_make_user_info.get('brandId'),storeId:fn_make_user_info.get('storeId')};
			fn_call_popup('biz', 'BIZPOP_STORE_SPPLY', '#ITP_ASIDE', popFn, key, 'S');
		} else if(keyword === 'whs'){
			const key1 = {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, key1, 'S');
		}
	},
	callback: {
		create: function(rowDataPop) {
			console.log(rowDataPop);
			if(rowDataPop.hasOwnProperty('whsId') == false) {
				$('#ITP_FORM_POMNG04010_DETAIL_SPPLY_NM').val(rowDataPop.spplyNm);
				$('#ITP_FORM_POMNG04010_DETAIL_SPPLY_ID').val(rowDataPop.spplyId);
			} else {
				$('#ITP_FORM_POMNG04010_DETAIL_WHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_POMNG04010_DETAIL_WHS_ID').val(rowDataPop.whsId);
			}
		}
	},
	excelDown: function() {
		
		var param = {
			'url': API_POMNG_PO_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_POMNG04010_SEARCH').serializeObject(),
			'gridColumns': $(POMNG04010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '구매발주현황.xlsx',
			'sheetName': '구매발주현황리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};


/*수정화면 Object*/
let modifyObj = {
	init: function(param) {
		this.load(param);
	},
	itp_POMNG04010_param: {},
	itp_POMNG04010_search: false,
	button: {
		cancel: function() {
			listObj.grid.search();
		},
		del: function() {
			const formId = '#ITP_FORM_POMNG04010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			itp_fn_grid_save_rows(POMNG04010_DETAIL_GRID_ID);
			let gridUpdateData = [];
			for(var i=0; i<$(POMNG04010_DETAIL_GRID_ID).getGridParam('reccount'); i++) {
				$(POMNG04010_DETAIL_GRID_ID).jqGrid('saveCell', i+1, 2)
			}
			const selection = $(POMNG04010_DETAIL_GRID_ID).getGridParam('selarrrow');
			
			if (selection.length == 0 ) {
				alert("취소 대상 품목이 없습니다.");
				return ;
			}
			
			let param = $(formId).serializeObject();
			let newData = [];
			for(let i = 0; i < selection.length; i++){
				let before = $(POMNG04010_DETAIL_GRID_ID).getRowData(selection[i]);
				before.pchPodrUnqNo = param.pchPodrUnqNo;
				newData.push(before);
			}
			param.gridUpdateData = newData;
			
			var modalFn = {
				callBack: () => {
					var searhFn = function() {
						ITP_FORM_POMNG02010_DETAIL_IS_WRITING = false;
						listObj.grid.search();
					};
					fn_ajax_call(API_POMNG_PO_CANCEL_PCHPO, JSON.stringify(param), searhFn, 'POST');
				}
			};
			itp_fn_modal_confirm('발주 취소 하시겠습니까?', modalFn);
						
		}
	},
	empty: {
		init: function() {
            var _this = this;
            this.push();
            $(POMNG04010_VIEW_GRID_EMPTY).on('click', function() {
                _this.back();
                viewObj.itp_POMNG04010_param.gridSize = $.jgrid.defaults.rowNum;
                $(POMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(viewObj.itp_POMNG04010_param)}).trigger('reloadGrid');
            });
        },
        push: function() {
            let param = $('#ITP_FORM_POMNG04010_DETAIL').serializeObject();
            viewObj.itp_POMNG04010_param = param;
        },
        back: function() {
            $('#ITP_FORM_POMNG04010_SERVICE_BRAND_TYPE').val(viewObj.itp_POMNG04010_param.brandType);
            $('#ITP_FORM_POMNG04010_SERVICE_ST_CD').val(viewObj.itp_POMNG04010_param.stCd);
            $('#ITP_FORM_POMNG04010_SEARCH_BRAND_NM').val(viewObj.itp_POMNG04010_param.brandNm);
        }
	},
	load: function(param) {
		fn_ajax_call(API_POMNG_PO_INFO_PCHODR, param, this.callback, 'GET');
	},
	callback: function(result) {
		console.log(result);
		
		// 그리드 전화번호 '-' 함수
		function convertTel(value){
			answer = (value.startsWith('0')) ? value.replace(/[^0-9]/g, "")
					.replace(/(^02|^0505|^1[0-9]{3}|^0[0-9]{2})([0-9]+)?([0-9]{4})$/,"$1-$2-$3")
					.replace("--", "-") : '';
			return answer;
		}		
		let telNo = convertTel(result.podrMgrTelNo);
		console.log(result);
		pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_AFFL_SHOP_ID').val(result.afflShopId);
		$('#ITP_FORM_POMNG04010_DETAIL .fnBrandNm').text(result.brandNm);				
		$('#ITP_FORM_POMNG04010_DETAIL .fnPchPodrUnqNo').text(result.pchPodrUnqNo);
		$('#ITP_FORM_POMNG04010_DETAIL .fnSpplyNm').text(result.spplyId);
		$('#ITP_FORM_POMNG04010_DETAIL .fnPodrDt').text(result.podrDt);
		$('#ITP_FORM_POMNG04010_DETAIL .fnPchOdrStCd').text(result.pchOdrStNm);
		$('#ITP_FORM_POMNG04010_DETAIL .fnPodrTotalAmt').text(result.podrTotalAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		$('#ITP_FORM_POMNG04010_DETAIL .fnPodrMgrNm').text(result.podrMgrNm);
		$('#ITP_FORM_POMNG04010_DETAIL .fnPodrMgrNo').text(telNo);
		$('#ITP_FORM_POMNG04010_DETAIL .fnDlvDvsn').text(result.dlvDvsnNm);
		$('#ITP_FORM_POMNG04010_DETAIL .fnDlvReqDt').text(result.dlvReqDt);
		$('#ITP_FORM_POMNG04010_DETAIL .fnShmtWhsNm').text(result.shmtWhsNm);
		$('#ITP_FORM_POMNG04010_DETAIL .fnWhsNm').text(result.whsNm);
		
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_BRANDID').val(result.brandId);
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_DLV_DVSN').val(result.dlvDvsn);
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_PCH_PODR_UNQ_NO').val(result.pchPodrUnqNo);
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_SHM_LOCATION').val(result.shmtLocation);
		$('#ITP_FORM_POMNG04010_DETAIL #ITP_FORM_POMNG04010_DETAIL_SHM_WHS_ID').val(result.shmtWhsId);
		
		modifyObj.grid.init('MODIFY', result.pchOdrDtlList)
	},
	grid: {
		init: function(mode, gridData) {
			this.mode = mode;
			this.gridId = (mode === 'VIEW') ? POMNG04010_VIEW_GRID_ID : POMNG04010_DETAIL_GRID_ID;
			this.gridList = (mode === 'VIEW') ? POMNG04010_VIEW_GRID_LIST : POMNG04010_DETAIL_GRID_LIST;
			this.gridEmpty = (mode === 'VIEW') ? POMNG04010_VIEW_GRID_EMPTY : POMNG04010_DETAIL_GRID_EMPTY;
			this.unload();
			this.load(mode, gridData);
		},
		mode: '',
		colModel: gridColModel.view,
		load: function(mode, gridData) {
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: gridColModel.detail,
				data: gridData,
				multiselect: true,
				onSelectRow: function(index, status) {
					if(index) {
						var row = $(POMNG04010_DETAIL_GRID_ID).jqGrid('getRowData', index);
						if(row.podrDtlStCd == 'POD2') {
							$("#jqg_ITP_POMNG04010_DETAIL_jqGrid_" + index).prop("checked", false);
						}
					}
				},
				onSelectAll: function(rowIds, status) {
					for(let i = 0; i < rowIds.length; i++){
						let podrDtlStCd = $(POMNG04010_DETAIL_GRID_ID).getCell(rowIds[i], 'podrDtlStCd');
						if(podrDtlStCd == 'POD2') {
							$("#jqg_ITP_POMNG04010_DETAIL_jqGrid_" + rowIds[i]).prop("checked", false);
						} else {
							$("#jqg_ITP_POMNG04010_DETAIL_jqGrid_" + rowIds[i]).prop("checked", status);
						}
					}
				},
				loadComplete: function(data) {
					console.log(data);
					let datas = data.rows;
					for(let i = 0; i < datas.length; i++) {
						let podrDtlStCd = datas[i].podrDtlStCd;
						if(podrDtlStCd == 'POD2') {
							$('#jqg_ITP_POMNG04010_DETAIL_jqGrid_' + (i + 1)).attr('disabled', true);
						}
					}
					itp_fn_grid_load_complete(data, POMNG04010_DETAIL_GRID_ID, true, 'number', 'POMNG04010', modifyObj.itp_POMNG04010_search, modifyObj.empty, true, data.gridRecords, true);
				}
			};
			itp_fn_grid_make_local(option);
		},
		unload: function() {
			$.jgrid.gridUnload(this.gridId);
		}
	},
	isValid: function(formId) {
		var isValid = false;
		$(formId).find('input, select, checkbox, textarea').each(function(k) {
			if($(this).data('check-required')) {
				var arry = $(this).data('check-required').split(',');
				isValid = itp_fn_form_validate(formId, '#' + $(this).attr('id'), arry, undefined);
				if(!isValid) return false;
			}
		});
		return isValid;
	},
};
