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

//화면변수
const RTNMNG04010_GRID_ID = '#ITP_RTNMNG04010_jqGrid';
const RTNMNG04010_GRID_LIST = '#ITP_RTNMNG04010_jqGrid_list';
const RTNMNG04010_GRID_PAGER = '#ITP_RTNMNG04010_jqGridPager';
const RTNMNG04010_GRID_EMPTY = '#ITP_RTNMNG04010_jqGridEmpty';

const RTNMNG04010_DETAIL_GRID_ID = '#ITP_RTNMNG04010_DETAIL_jqGrid';
const RTNMNG04010_DETAIL_GRID_LIST = '#ITP_RTNMNG04010_DETAIL_jqGrid_list';
const RTNMNG04010_DETAIL_GRID_PAGER = '#ITP_RTNMNG04010_DETAIL_jqGridPager';
const RTNMNG04010_DETAIL_GRID_EMPTY = '#ITP_RTNMNG04010_DETAIL_jqGridEmpty';

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

//API URL
let API_RTNMNG_RTN_DETAIL_GRID_LIST =  '/api/rtnmng/rtn/detail-grid-list';
let API_RTNMNG_RTN_INFO_RTNODR =  '/api/rtnmng/rtn/info-rtnOdr'; 
let API_RTNMNG_RTN_CANCEL_RTNPO =  '/api/rtnmng/rtn/cancel-rtnPo';

//화면 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: '9', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_PODR_UNQ_NO', name: 'rtnPodrUnqNo',
			label: '반품발주번호', 
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_ODR_ST_NM', name: 'rtnOdrStNm',
			label: '발주상태', 
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_ODR_REG_DT', name: 'rtnOdrRegDt',
			label: '반품발주일자', 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_WHS_NM', name: 'rtnWhsNm',
			label: '수거처', 
			width: '9', 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: 'RTN_DODR_TOTAL_AMT', name: 'rtnDodrTotalAmt',
			label: '총 반품발주금액', 
			width: '8', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'rtn_Dodr_Item_Qty', name: 'rtnDodrItemQty',
			label: '반품발주품목수', 
			width: '8', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'COL_REQ_DT', name: 'colReqDt',
			label: '수거요청일', 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'col_Dvsn_Nm', name: 'colDvsnNm',
			label: '수거구분', 
			width: '7', 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: 'RTN_PODR_DTL_NO', name: 'rtnPodrDtlNo',
			label: ITP_MSG_LOCALE.label.pchPodrDtlNo, 		// 발주항번
			width: '4', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			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: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RODR_DTL_ST_NM', name: 'rodrDtlStNm',
			label: '발주상세상태', 
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, 			// 단가
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, 				// 기본단위
			width: '4', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_QTY', name: 'rtnReqQty',
			label: '반품요청수량', 
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'RTN_ODR_QTY', name: 'rtnOdrQty',
			label: '반품발주수량', 
			width: '5', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'RTN_ODR_AMT', name: 'rtnOdrAmt',
			label: ITP_MSG_LOCALE.label.podrAmt, 			// 발주금액
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		}
	]
};


/*화면공통 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('RTNMNG04010');
			// 공통코드 표시
			$('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 defaultToDate = new Date(now);
			defaultToDate.setDate(now.getDate() + 10);
			$('#ITP_TAB_RTNMNG04010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_RTNMNG04010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_RTNMNG04010_SEARCH_TO_DT').datepicker('setDate', defaultToDate);
		},
		grid: function() {
			itp_fn_jqgrid_resize(RTNMNG04010_GRID_ID, RTNMNG04010_GRID_LIST, 'lg');
			listObj.empty.init();
			
		},
		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_RTNMNG04010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_RTNMNG04010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_RTNMNG04010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_RTNMNG04010_CANCELLIST'		: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_RTNMNG04010_RTNCANCEL'		: modifyObj.button.del();		break;	// 발주 취소 버튼(예정)
						case 'ITP_BTN_RTNMNG04010_EXCEL'			: listObj.excelDown();		break;	// 엑셀 다운로드
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_RTNMNG04010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_RTNMNG04010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
            fn_show_btn_auth_array(['#ITP_BTN_RTNMNG04010_SRH', '#ITP_BTN_RTNMNG04010_NEWREG']);
            $('#ITP_AJAX_RTNMNG04010_LIST_CONTAINER').show();
            $('#ITP_FORM_RTNMNG04010_SEARCH #ITP_FORM_RTNMNG04010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_BTN_RTNMNG04010_SEARCH_SPPLY_ID').show();
            $('#ITP_BTN_RTNMNG04010_DELETE_SPPLY_ID').show();
            $('#ITP_BTN_RTNMNG04010_SEARCH_WHS_ID').show();
            $('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SWHS_NM').show();
            $('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SRTN_WHS_NM').show();
            $('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SSPPLY_NM').show();
			$('#ITP_BTN_RTNMNG04010_EXCEL').show();
            
        } else if(mode == 'MODIFY') { // 수정
            $('#ITP_AJAX_RTNMNG04010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_RTNMNG04010_CANCELLIST', '#ITP_BTN_RTNMNG04010_RTNCANCEL']);

            $('#ITP_BTN_RTNMNG04010_CANCELLIST').show();
            $('#ITP_BTN_RTNMNG04010_DELETE').show();
            
            $('#ITP_FORM_RTNMNG04010_DETAIL_BRAND_ID').attr('readonly', true);
            $('#ITP_FORM_RTNMNG04010_DETAIL_BRAND_NM').attr('readonly', true);
            $('#ITP_FORM_RTNMNG04010_DETAIL_BSNS_REG_NO').attr('readonly', true);
            $('#ITP_FORM_RTNMNG04010_DETAIL_CORP_REG_NO').attr('readonly', true);
            $('#ITP_FORM_RTNMNG04010_DETAIL_SEARCH_AFFL_SHOP').hide();
            $('#ITP_FORM_RTNMNG04010_DETAIL_DELETE_AFFL_SHOP').hide();
			$('#ITP_FORM_RTNMNG04010_DETAIL .itp_form_info').show();
			$('#ITP_FORM_RTNMNG04010_DETAIL .itp_form_change').hide();

            $('#ITP_FORM_RTNMNG04010_DETAIL #ITP_FORM_RTNMNG04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
        } 
    }
};


/*목록화면 Object*/
let listObj = {
	init: function() {
		this.grid.init();
		this.action();
	},
	itp_RTNMNG04010_param: {},
	itp_RTNMNG04010_search: false,
	button: {
		search: function() {
			listObj.itp_RTNMNG04010_search = true;
			let param = $('#ITP_FORM_RTNMNG04010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            param.sidx = '0';
			$(RTNMNG04010_GRID_ID).data('grid-param',param);
            $(RTNMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        }
	},
	empty: {
		init: function() {
			var _this = this;
            this.push();
            $(RTNMNG04010_GRID_EMPTY).on('click', function() {
                _this.back();
                listObj.itp_RTNMNG04010_param.gridSize = $.jgrid.defaults.rowNum;
                $(RTNMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(listObj.itp_RTNMNG04010_param)}).trigger('reloadGrid');
            });
		},
		push: function() {
            let param = $('#ITP_FORM_RTNMNG04010_SEARCH').serializeObject();
            listObj.itp_RTNMNG04010_param = param;
            param.sidx = '0';
        },
        back: function() {
            $('#ITP_FORM_RTNMNG04010_SERVICE_BRAND_TYPE').val(listObj.itp_RTNMNG04010_param.brandType);
            $('#ITP_FORM_RTNMNG04010_SERVICE_ST_CD').val(listObj.itp_RTNMNG04010_param.stCd);
            $('#ITP_FORM_RTNMNG04010_SEARCH_BRAND_NM').val(listObj.itp_RTNMNG04010_param.brandNm);
        }
	},
	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_RTNMNG04010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.sidx = '0';
            var option = {
            	gridId: RTNMNG04010_GRID_ID,
            	colModel: gridColModel.list,
            	param: param,
            	url: DOMAIN + API_RTNMNG_RTN_DETAIL_GRID_LIST,
            	pager: RTNMNG04010_GRID_PAGER,
            	onCellSelect: function(rowId, cellIdx, cellValue) {
                    var cm = $(this).jqGrid('getGridParam', 'colModel');
                    var colNm = cm[cellIdx].name;
                    if (colNm === 'rtnPodrUnqNo') {
                        const rtnPodrUnqNoVal = $(this).jqGrid('getCell', rowId, 'rtnPodrUnqNo');
                        const brandIdVal = $(this).jqGrid('getCell', rowId, 'brandId');
                        const key = {rtnPodrUnqNo: rtnPodrUnqNoVal, brandId: brandIdVal};
                        var param = $.param(key);
                        modifyObj.init(param);
                    }
				},
				loadComplete: function(data) {
                    itp_fn_grid_load_complete(data, RTNMNG04010_GRID_ID, true, 'number', 'RTNMNG04010', listObj.itp_RTNMNG04010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(RTNMNG04010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(RTNMNG04010_GRID_ID).jqGrid('setCell', rowId, 'rtnPodrUnqNo', '', ITP_GRID_COL_STYLE.link);
                    });
				},
				onPaging: function(action) {
                    itp_fn_grid_paging(RTNMNG04010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
		},
		clearData: function() {
			$(RTNMNG04010_GRID_ID).jqGrid('clearGridData', true);
            $(RTNMNG04010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(RTNMNG04010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(RTNMNG04010_GRID_EMPTY).show();
		},
		unload: function() {
			$.jgrid.gridUnload(RTNMNG04010_GRID_ID);
		}
	},
	action: function() {
		var _this = this;
		// 매장/창고 조회 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_POP_SWHS_NM').on('click', function() {
			_this.popup('whs');
		});
		// 매장/창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SWHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_RTNMNG04010_SEARCH_SWHS_ID').val('');
		});
		
		// 매장/창고 조회 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_POP_SRTN_WHS_NM').on('click', function() {
			_this.popup('rwhs');
		});
		// 매장/창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SRTN_WHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SRTN_WHS_NM').val('');
			$('#ITP_FORM_RTNMNG04010_SEARCH_SRTN_WHS_ID').val('');
		});
		
		// 공급업체 조회 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_POP_SSPPLY_NM').on('click', function() {
			_this.popup('spply');
		});
		// 공급업체 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG04010_SEARCH_DEL_SSPPLY_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SSPPLY_NM').val('');
			$('#ITP_FORM_RTNMNG04010_SEARCH_SSPPLY_ID').val('');
		});
		
	},
	popup: function(keyword) {
		if(keyword === 'spply'){
			var popFn = this.callback.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'){
			var popFn = this.callback.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');
		} else if(keyword === 'rwhs'){
			var popFn = this.callback.rwhs;
			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: {
		whs: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
			$('#ITP_FORM_RTNMNG04010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
		}, 
		rwhs: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SRTN_WHS_NM').val(rowDataPop.whsNm);
			$('#ITP_FORM_RTNMNG04010_SEARCH_SRTN_WHS_ID').val(rowDataPop.whsId);
		},
		spply: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG04010_SEARCH_SSPPLY_NM').val(rowDataPop.spplyNm);
			$('#ITP_FORM_RTNMNG04010_SEARCH_SSPPLY_ID').val(rowDataPop.spplyId);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_RTNMNG_RTN_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_RTNMNG04010_SEARCH').serializeObject(),
			'gridColumns': $(RTNMNG04010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '반품발주현황.xlsx',
			'sheetName': '반품발주현황리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};


/*수정화면 Object*/
let modifyObj = {
	init: function(param) {
		this.load(param);
	},
	itp_RTNMNG04010_param: {},
	itp_RTNMNG04010_search: false,
	button: {
		cancel: function() {
			listObj.grid.search();
		},
		del: function() {
			
			var modalFn = {
				callBack: () => {

					var param = {brandId: $('#ITP_FORM_RTNMNG04010_DETAIL_BRAND_ID').val(), rtnPodrUnqNo: $('#ITP_FORM_RTNMNG04010_DETAIL_RTN_PODR_UNQ_NO').val()};
					var searhFn = function () {
						listObj.grid.search();
					}
					fn_ajax_call(API_RTNMNG_RTN_CANCEL_RTNPO, JSON.stringify(param), searhFn, 'POST');
				}
			};
			itp_fn_modal_confirm('반품발주를 취소 하시겠습니까?', modalFn);
		}
	},
	empty: {
		init: function() {
            var _this = this;
            this.push();
            $(RTNMNG04010_VIEW_GRID_EMPTY).on('click', function() {
                _this.back();
                viewObj.itp_RTNMNG04010_param.gridSize = $.jgrid.defaults.rowNum;
                $(RTNMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(viewObj.itp_RTNMNG04010_param)}).trigger('reloadGrid');
            });
        },
        push: function() {
            let param = $('#ITP_FORM_RTNMNG04010_DETAIL').serializeObject();
            viewObj.itp_RTNMNG04010_param = param;
        },
        back: function() {
            $('#ITP_FORM_RTNMNG04010_SERVICE_BRAND_TYPE').val(viewObj.itp_RTNMNG04010_param.brandType);
            $('#ITP_FORM_RTNMNG04010_SERVICE_ST_CD').val(viewObj.itp_RTNMNG04010_param.stCd);
            $('#ITP_FORM_RTNMNG04010_SEARCH_BRAND_NM').val(viewObj.itp_RTNMNG04010_param.brandNm);
        }
	},
	load: function(param) {
		fn_ajax_call(API_RTNMNG_RTN_INFO_RTNODR, param, this.callback, 'GET');
	},
	callback: function(result) {
		
		// 그리드 전화번호 '-' 함수
		let telNo = '';
		if(result.rtnMgrTelNo != '') {
			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;
			}				
			telNo = convertTel(result.rtnMgrTelNo);	
		}
		
		pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_RTNMNG04010_DETAIL #ITP_FORM_RTNMNG04010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_RTNMNG04010_DETAIL #ITP_FORM_RTNMNG04010_DETAIL_RTN_PODR_UNQ_NO').val(result.rtnPodrUnqNo);
		$('#ITP_FORM_RTNMNG04010_DETAIL #ITP_FORM_RTNMNG04010_DETAIL_RTN_ODR_ST_CD').val(result.rtnOdrStCd);
		
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnBrandNm').text(result.brandNm);				
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnRtnPodrUnqNo').text(result.rtnPodrUnqNo + ' / ' + result.rtnOdrStNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnColPlace').text(result.rtnWhsNm + ' - ' + result.rtnLocationNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnWhsNm').text(result.whsNm + ' - ' + result.locationNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnSpplyNm').text(result.spplyNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnRtnOdrRegDt').text(result.rtnOdrRegDt);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnPodrMgrNm').text(result.rtnMgrNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnPodrMgrTelNo').text(telNo);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnColDvsnNm').text(result.colDvsnNm);
		$('#ITP_FORM_RTNMNG04010_DETAIL .fnRtnDodrTotalAmt').text(itp_fn_number_comma(result.rtnDodrTotalAmt));
		
		if('RO20' == result.rtnOdrStCd) { // 반품발주요청
			$('#ITP_BTN_RTNMNG04010_RTNCANCEL').show();
		} else {
			$('#ITP_BTN_RTNMNG04010_RTNCANCEL').hide();
		}
		
		modifyObj.grid.init('MODIFY', result.rtnOdrDtlList)
		
	},
	grid: {
		init: function(mode, gridData) {
			this.mode = mode;
			this.gridId = (mode === 'VIEW') ? RTNMNG04010_VIEW_GRID_ID : RTNMNG04010_DETAIL_GRID_ID;
			this.gridList = (mode === 'VIEW') ? RTNMNG04010_VIEW_GRID_LIST : RTNMNG04010_DETAIL_GRID_LIST;
			this.gridEmpty = (mode === 'VIEW') ? RTNMNG04010_VIEW_GRID_EMPTY : RTNMNG04010_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 = $(RTNMNG04010_DETAIL_GRID_ID).jqGrid('getRowData', index);
						if(row.podrDtlStCd == 'POD2') {
							$("#jqg_ITP_RTNMNG04010_DETAIL_jqGrid_" + index).prop("checked", false);
						}
					}
				},
				onSelectAll: function(rowIds, status) {
					for(let i = 0; i < rowIds.length; i++){
						let podrDtlStCd = $(RTNMNG04010_DETAIL_GRID_ID).getCell(rowIds[i], 'podrDtlStCd');
						if(podrDtlStCd == 'POD2') {
							$("#jqg_ITP_RTNMNG04010_DETAIL_jqGrid_" + rowIds[i]).prop("checked", false);
						} else {
							$("#jqg_ITP_RTNMNG04010_DETAIL_jqGrid_" + rowIds[i]).prop("checked", status);
						}
					}
				},
				loadComplete: function(data) {
					let datas = data.rows;
					for(let i = 0; i < datas.length; i++) {
						let podrDtlStCd = datas[i].podrDtlStCd;
						if(podrDtlStCd == 'POD2') {
							$('#jqg_ITP_RTNMNG04010_DETAIL_jqGrid_' + (i + 1)).attr('disabled', true);
						}
					}
					itp_fn_grid_load_complete(data, RTNMNG04010_DETAIL_GRID_ID, true, 'number', 'RTNMNG04010', modifyObj.itp_RTNMNG04010_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;
	},
};
