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			*
 * let sfflyObj = {}		: 공급업체담당자 Object		*
 *******************************************************/

/*화면 변수*/
const RTNMNG02010_GRID_ID = '#ITP_RTNMNG02010_jqGrid';
const RTNMNG02010_GRID_LIST = '#ITP_RTNMNG02010_jqGrid_list';
const RTNMNG02010_GRID_PAGER = '#ITP_RTNMNG02010_jqGridPager';
const RTNMNG02010_GRID_EMPTY = '#ITP_RTNMNG02010_jqGridEmpty';

const RTNMNG02010_VIEW_GRID_ID = '#ITP_RTNMNG02010_VIEW_jqGrid';
const RTNMNG02010_VIEW_GRID_LIST = '#ITP_RTNMNG02010_VIEW_jqGrid_list';
const RTNMNG02010_VIEW_GRID_PAGER = '#ITP_RTNMNG02010_VIEW_jqGridPager';
const RTNMNG02010_VIEW_GRID_EMPTY = '#ITP_RTNMNG02010_VIEW_jqGridEmpty';

const RTNMNG02010_DETAIL_GRID_ID = '#ITP_RTNMNG02010_DETAIL_jqGrid';
const RTNMNG02010_DETAIL_GRID_LIST = '#ITP_RTNMNG02010_DETAIL_jqGrid_list';
const RTNMNG02010_DETAIL_GRID_PAGER = '#ITP_RTNMNG02010_DETAIL_jqGridPager';
const RTNMNG02010_DETAIL_GRID_EMPTY = '#ITP_RTNMNG02010_DETAIL_jqGridEmpty';

//let ITP_FORM_RTNMNG02010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_RTNMNG02010_DETAIL_IS_WRITING = false;

let RTNMNG02010_GRID_LAST_ROW_ID;
let SCREEN_MODE;

/* 공통코드 리스트 */
let ITP_COMMON_CD_RTN_DVSN = fn_make_common_cd_list(CODE_LIST, 'RTN_DVSN', false);
let ITP_COMMON_CD_RTN_RSN_DVSN = fn_make_common_cd_list(CODE_LIST, 'RTN_RSN_DVSN', false);

/*API URL*/
let API_DETAIL_GRID_LIST 	= '/api/rtnmng/detail-grid-list';	// 목록
let API_DETAIL_INFO 		= '/api/rtnmng/info-rtnReq';		// 상세
let API_DETAIL_SAVE 		= '/api/rtnmng/save-rtnReq';		// 저장

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			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: 'STORE_ID', name: 'storeId',
			label: ITP_MSG_LOCALE.label.storeId, //매장아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_REQ_UNQ_NO', name: 'rtnReqUnqNo',
			label: '반품요청번호', 
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_ST_CD', name: 'rtnReqStCd',
			label: '상태', 
			width: '0', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_REQ_ST_NM', name: 'rtnReqStNm',
			label: '상태', 
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'rtn_Whs_Nm', name: 'rtnWhsNm',
			label: '반품장소', 
			width: '10', fixed: false, align: 'left',
			minwidth: 220,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		}, 
		{
			index: 'RTN_REQ_DT', name: 'rtnReqDt',
			label: '반품요청일', 
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_AMT', name: 'rtnReqAmt',
			label: '반품요청금액', 
			width: '9', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_REQ_ITEM_QTY', name: 'rtnReqItemQty',
			label: '요청품목수', 
			width: '8', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'COL_REQ_DT', name: 'colReqDt',
			label: '수거요청일자', 
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_MGR_NM', name: 'rtnReqMgrNm',
			label: '반품요청자',
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_RJCT_DT', name: 'rtnRjctDt',
			label: '반려일자',
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_RJCT_NM', name: 'rtnRjctNm',
			label: '반려담당자',
			width: '9', fixed: false, align: 'center',
			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: 'RTN_REQ_UNQ_NO', name: 'rtnReqUnqNo',
			label: '반품요청고유번호', 
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_REQ_DTL_NO', name: 'rtnReqDtlNo',
			label: '반품요청상세번호', 
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId,
			width: '15', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			editrules: { required: true }
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm,
			width: '15', fixed: false, align: 'left',
			sortable: true, editable: false, edittype: 'text',
			editrules: { required: true }, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit,
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt,
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},		
		{
			index: 'RTN_QTY', name: 'rtnQty',
			label: '반품수량', 
			width: '10', fixed: false, align: 'right',
			sortable: true, editable: true, edittype: 'text',
			classes: 'input_color',
			editrules: {required: true, number:true},
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' },
			editoptions: {"onKeyup": "this.value=this.value.replace(/[^0-9]/g,'');", maxlength: 10}
		},
		{
			index: 'RTN_AMT', name: 'rtnAmt',
			label: '반품예상금액', 
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', hidden: false,
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_REQ_DTL_STNM', name: 'rtnReqDtlStNm',
			label: '반품상세상태',
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text', hidden: false
		},
		{
			index: 'rtnReqDtlStCd', name: 'rtnReqDtlStCd',
			label: '반품상세상태코드',
			width: '10', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', hidden: true
		},
		{
			index: 'RTN_DVSN', name: 'rtnDvsn',
			label: '반품구분',
			width: '10', fixed: false, align: 'left',
			classes: 'input_color',
			sortable: false, editable: true, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_COMMON_CD_RTN_DVSN},
			editrules: {required: true}
		},
		{
			index: 'RTN_RSN_DVSN', name: 'rtnRsnDvsn',
			label: '사유구분',
			classes: 'input_color',
			width: '10', fixed: false, align: 'left',
			sortable: false, editable: true, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_COMMON_CD_RTN_RSN_DVSN},
			editrules: {required: true}
		},
		{
			index: 'RTN_RSN', name: 'rtnRsn',
			label: '반품사유', 
			classes: 'input_color',
			width: '15', fixed: false, align: 'left',
			sortable: false, editable: true, edittype: 'text', editoptions: {maxlength: 200}, classes: 'ui-ellipsis'
		},
		{
			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
		},
		{
			index: 'UNIT_GUBUN', name: 'unitGubun',
			label: ITP_MSG_LOCALE.label.unitGubun,
			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();
	},
	itp_RTNMNG02010_param: {},
	itp_RTNMNG02010_search: false,
	ui: {
		init: function () {
			this.view();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('RTNMNG02010');

			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});
			
			// 브랜드/매장
			$('#ITP_FORM_RTNMNG02010_SEARCH_SBRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_RTNMNG02010_SEARCH_SSTORE_ID').val(fn_make_user_info.get('storeId'));
			
			var now = new Date();
			$('#ITP_TAB_RTNMNG02010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_RTNMNG02010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_RTNMNG02010_SEARCH_TO_DT').datepicker('setDate', new Date(now.setDate(now.getDate()+30)));			
			
		},
		grid: function() {
			itp_fn_jqgrid_resize(RTNMNG02010_GRID_ID, RTNMNG02010_GRID_LIST, 'lg');
			
		},
		ready: function() {
			listObj.init();
		}
	},
	action: function () {
		var _this = this;
		// 납품장소 조회 버튼 클릭
		$('#ITP_FORM_RTNMNG02010_SSEARCH_SWHS_NM').on('click', function() {
			_this.popup('S');
		});
		// 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_RTNMNG02010_SDELETE_SWHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG02010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_RTNMNG02010_SEARCH_SWHS_ID').val('');			
		});
		
		// 수정 화면에서의 납품장소 조회 버튼 클릭
		$('#ITP_FORM_RTNMNG02010_SEARCH_WHS_NM').on('click', function() {
			_this.popup('U');
		});
		
		// 납품장소 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_RTNMNG02010_DELETE_WHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG02010_DETAIL_WHS_NM').val('');
			$('#ITP_FORM_RTNMNG02010_DETAIL_WHS_ID').val('');
			$('#ITP_FORM_RTNMNG02010_DETAIL_LOCATION_NM').val('');
			$('#ITP_FORM_RTNMNG02010_DETAIL_LOCATION').val('');			
		});
		
	},
	popup: function (arg) {
		var popFn ;
		// 팝업
		if (arg == "S") {
			popFn = this.callback.create;
		} else {
			popFn = this.callback.modify;
		}
		
		var whsDvsn = "";
		if (fn_make_user_info.get('storeId') == '' ) {
			whsDvsn = "W01";
		} else {
			whsDvsn = "W02";
		}
		
		const key = {brandId:fn_make_user_info.get('brandId'),storeId:fn_make_user_info.get('storeId'), whsDvsn: whsDvsn};
		fn_call_popup('biz', 'BIZPOP_WHS_LOCATION', '#ITP_ASIDE', popFn, key, 'S');
		
	},
	callback: {
		create: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_RTNMNG02010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_RTNMNG02010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
			}			
		},
		modify: function(rowDataPop) {
			if(rowDataPop) {
				$('#ITP_FORM_RTNMNG02010_DETAIL_WHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_RTNMNG02010_DETAIL_WHS_ID').val(rowDataPop.whsId);
				$('#ITP_FORM_RTNMNG02010_DETAIL_LOCATION_NM').val(rowDataPop.locationNm);
				$('#ITP_FORM_RTNMNG02010_DETAIL_LOCATION').val(rowDataPop.location);
			}
			
		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_RTNMNG02010_SRH'					: listObj.button.search();		break;	// 조회 버튼 클릭
						//case 'ITP_BTN_RTNMNG02010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_RTNMNG02010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_RTNMNG02010_CANCELLIST'			: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_RTNMNG02010_REQ'					: modifyObj.button.save("RR20"); break;	// 구매요청
						case 'ITP_BTN_RTNMNG02010_SAVE'					: modifyObj.button.save("RR00"); break;	// 임시저장
						case 'ITP_BTN_RTNMNG02010_DETAIL_ADDROW'			: pageObj.grid.button.addRow(); break;	// 품목추가
						case 'ITP_BTN_RTNMNG02010_DETAIL_DELROW'			: pageObj.grid.button.delRow(); break;	// 품목삭제
						case 'ITP_BTN_RTNMNG02010_EXCEL'			: listObj.excelDown(); break;	// 엑셀다운로드
					}
				});
			});
			
			$('body').on('click', function(e) {
				var gridId = RTNMNG02010_DETAIL_GRID_ID.replace('#', '');
				var rids = $(RTNMNG02010_DETAIL_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				
				itp_fn_grid_reset_selection(e, last_row_id, gridId, 'RTNMNG02010_DETAIL');	
				
				pageObj.grid.recal();
			});
		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_RTNMNG02010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_RTNMNG02010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			fn_show_btn_auth_array(['#ITP_BTN_RTNMNG02010_SRH']);
			$('#ITP_AJAX_RTNMNG02010_LIST_CONTAINER').show();
			$('#ITP_FORM_RTNMNG02010_DETAIL_SBRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_RTNMNG02010_DETAIL_SSTORE_ID').val(fn_make_user_info.get('storeId'));
			$('#ITP_BTN_RTNMNG02010_EXCEL').show();
			
			SCREEN_MODE = 'LIST' ;
		} else if(mode == 'ADD') { // 등록
			fn_show_btn_auth_array(['#ITP_BTN_RTNMNG02010_CANCELLIST', '#ITP_BTN_RTNMNG02010_SAVE']);
			$('#ITP_AJAX_RTNMNG02010_DETAIL_CONTAINER').show();

			$('#ITP_FORM_RTNMNG02010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_RTNMNG02010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});
			SCREEN_MODE = 'DETAIL' ;
			
		} else if(mode == 'MODIFY') { // 수정
			fn_show_btn_auth_array(['#ITP_BTN_RTNMNG02010_CANCELLIST', '#ITP_BTN_RTNMNG02010_SAVE', '#ITP_BTN_RTNMNG02010_REQ']);
			
			$('#ITP_AJAX_RTNMNG02010_DETAIL_CONTAINER').show();
			$('#ITP_FORM_RTNMNG02010_DETAIL .itp_form_info').show();
			$('#ITP_FORM_RTNMNG02010_DETAIL .itp_form_change').hide();
			SCREEN_MODE = 'DETAIL' ;
		} else if(mode == 'VIEW') { // 보기
			fn_show_btn_auth_array(['#ITP_BTN_RTNMNG02010_MODIFY', '#ITP_BTN_RTNMNG02010_CANCELLIST']);
			$('#ITP_AJAX_RTNMNG02010_VIEW_CONTAINER').show();
			SCREEN_MODE = 'VIEW' ;
		}
	},
	grid: {
		init: function(mode, gridRows) {
			this.gridId = (mode === 'VIEW') ? RTNMNG02010_VIEW_GRID_ID : RTNMNG02010_DETAIL_GRID_ID;
			this.gridList = (mode === 'VIEW') ? RTNMNG02010_VIEW_GRID_LIST : RTNMNG02010_DETAIL_GRID_LIST;
			this.gridEmpty = (mode === 'VIEW') ? RTNMNG02010_VIEW_GRID_EMPTY : RTNMNG02010_DETAIL_GRID_EMPTY;
			this.gridRows = gridRows;
			this.unload();
			this.load(mode, gridRows);
		},
		
		gridId: '',
		gridList: '',
		gridEmpty: '',
		gridRows: '',
		button: {
			addRow: function() {
				// 팝업
				var popFn = function(rowDataPop) {
					// 기존등록 데이터
					var exists = '';
					const rowData = $(RTNMNG02010_DETAIL_GRID_ID).getRowData();
					$.each(rowData, function(key, value) {
						if (value.viewCd != "D") {
							exists = exists + value.brandUnitUnqNo + ';';	
						}
					});

					$.each(rowDataPop, function(key, value) {
						if (exists.indexOf(value.brandUnitUnqNo) < 0 && value.podrPssblDvsn == 'PO01') {
							value['viewCd'] = "C";
							$(RTNMNG02010_DETAIL_GRID_ID).jqGrid('addRowData', value.brandUnitUnqNo, value, 'last');
						}
					});
					$(RTNMNG02010_DETAIL_GRID_EMPTY).hide();
				};
				const key = {brandId:fn_make_user_info.get('brandId'),storeId:fn_make_user_info.get('storeId')};
				fn_call_popup('biz', 'BIZPOP_ITEM', '#ITP_ASIDE', popFn, key, 'S');  // ==>dwkim 추후 작업 진행  파라메타 설명필요
			},
			delRow: function() {
				itp_fn_grid_del_row(RTNMNG02010_DETAIL_GRID_ID);
				pageObj.grid.recal();
			}
		},
		load: function(mode, gridRows) {
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: gridColModel.detail,
				data: gridRows,
				multiselect: true,
				loadComplete: function(data) {
					$(_this.mode === 'VIEW' ? RTNMNG02010_VIEW_GRID_LIST : RTNMNG02010_DETAIL_GRID_LIST).find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					data.records === 0 ? _this.clearData() : $(_this.mode === 'VIEW' ? RTNMNG02010_VIEW_GRID_EMPTY : RTNMNG02010_DETAIL_GRID_EMPTY).hide();
				},
				onCellSelect: function(rowid, cellIdx, cellValue) {
					if(mode !== 'VIEW') {
						RTNMNG02010_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowid, cellIdx, RTNMNG02010_GRID_LAST_ROW_ID, RTNMNG02010_DETAIL_GRID_ID, ['rtnReqUnqNo']);
					}
				}
			};
			itp_fn_grid_make_local(option);
		},
		recal: function() {
			if (SCREEN_MODE === 'DETAIL') {
				var ids = $(RTNMNG02010_DETAIL_GRID_ID).getDataIDs();
				$.each(ids, function(idx, rowId) {
					var viewCd = jQuery(RTNMNG02010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'viewCd');
					var unitAmt = jQuery(RTNMNG02010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'unitAmt');
					var rtnQty = jQuery(RTNMNG02010_DETAIL_GRID_ID).jqGrid('getCell', rowId, 'rtnQty');
					
					if(viewCd != 'D' && unitAmt != null && unitAmt != '' && rtnQty != null && rtnQty != '') {		
						jQuery(RTNMNG02010_DETAIL_GRID_ID).jqGrid('setRowData',rowId,{rtnAmt: parseInt(unitAmt)*parseInt(rtnQty)});
					}
				});
			}
			
		},
		clearData : function() {
			$(this.gridId).jqGrid('clearGridData', true);
			$(this.mode === 'VIEW' ? RTNMNG02010_VIEW_GRID_LIST : RTNMNG02010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(this.mode === 'VIEW' ? RTNMNG02010_VIEW_GRID_EMPTY : RTNMNG02010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(this.mode === 'VIEW' ? RTNMNG02010_VIEW_GRID_EMPTY : RTNMNG02010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
		
	}
	
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_RTNMNG02010_search: false,
	button: {
		search: function() {
			listObj.itp_RTNMNG02010_search = true;
			let param = $('#ITP_FORM_RTNMNG02010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(RTNMNG02010_GRID_ID).data('grid-param',param);
			$(RTNMNG02010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create: function() {
			if(!fn_make_user_info.isEmpty()) {
				//createObj.init();
			}
		}
	},
	empty: {
		init: function() {
			
		},
		itp_RTNMNG02010_param: {},
		push: function() {
			
		},
		back: function() {
		
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			itp_fn_jqgrid_resize(RTNMNG02010_GRID_ID, RTNMNG02010_GRID_LIST, 'lg');
			itp_fn_fire_window_resize();
			//this.reload();
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			let param = $('#ITP_FORM_RTNMNG02010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.sbrandId = fn_make_user_info.get('brandId');
			param.sstoreId = fn_make_user_info.get('storeId');

			var option = {
				gridId: RTNMNG02010_GRID_ID,
				colModel: gridColModel.list,
				//autowidth: true,
				//shrinkToFit:false, 
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: RTNMNG02010_GRID_PAGER,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'rtnReqUnqNo') {
						const selectVal = $(this).jqGrid('getCell', rowId, 'rtnReqUnqNo');
						const key = {brandId:fn_make_user_info.get('brandId'), rtnReqUnqNo: selectVal, viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, RTNMNG02010_GRID_ID, true, 'number', 'RTNMNG02010', listObj.itp_RTNMNG02010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(RTNMNG02010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(RTNMNG02010_GRID_ID).jqGrid('setCell', rowId, 'rtnReqUnqNo', '', ITP_GRID_COL_STYLE.link);
					});
				},
				onPaging: function(action) {
					itp_fn_grid_paging(RTNMNG02010_GRID_ID, action, param);
				}
			};
			
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(RTNMNG02010_GRID_ID).jqGrid('clearGridData', true);
			$(RTNMNG02010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(RTNMNG02010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(RTNMNG02010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(RTNMNG02010_GRID_ID);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_RTNMNG02010_SEARCH').serializeObject(),
			'gridColumns': $(RTNMNG02010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '반품요청현황.xlsx',
			'sheetName': '반품요청리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function (param) {
		this.load(param);
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_RTNMNG02010_VIEW #ITP_FORM_RTNMNG02010_VIEW_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_RTNMNG02010_VIEW #ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_UNQ_NO').val(result.rtnReqUnqNo);
		$('#ITP_FORM_RTNMNG02010_VIEW #ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_ST_CD').val(result.rtnReqStCd);
		$('#ITP_FORM_RTNMNG02010_VIEW #ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_MGR_ID').val(result.rtnReqMgrId);
		
		$('#ITP_FORM_RTNMNG02010_VIEW .fnBrandNm').text(result.brandNm);		
		$('#ITP_FORM_RTNMNG02010_VIEW .fnRtnReqUnqNo').text(result.rtnReqUnqNo + ' / ' + result.rtnReqStNm);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnRtnWhsNm').text(result.rtnWhsNm);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnColReqDt').text(result.colReqDt);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnNote').text(result.note);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnRtnRjctDt').text(result.rtnRjctDt);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnRtnRjctNm').text(result.rtnRjctNm);
		$('#ITP_FORM_RTNMNG02010_VIEW .fnRtnRjctRsn').text(result.rtnRjctRsn);
		
		// $('#ITP_FORM_RTNMNG02010_VIEW .fnPchReqTotalAmt').text(itp_fn_number_comma(result.pchReqTotalAmt));
		
		// 품목  리스트 조회
		pageObj.grid.init('VIEW', result.rtnReqDtlList);
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function (param) {
		pageObj.grid.init('DETAIL', []);
		//this.load(param);
	},
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_RTNMNG02010_DETAIL');
			
			const selectVal = $('#ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_UNQ_NO').val();
			const writeVal = $('#ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_MGR_ID').val();
			const stCd = $('#ITP_FORM_RTNMNG02010_VIEW_RTN_REQ_ST_CD').val();
			
			var userId = fn_make_user_info.get('userId');
			if (writeVal != userId) {
				itp_fn_modal_alert(ITP_MSG_LOCALE.message.ajax.diffWrite);
				return;
			}			
			if (stCd != "RR00") {
				itp_fn_modal_alert(ITP_MSG_LOCALE.message.ajax.notWrite);
				return;
			}
			
			const key = {brandId:fn_make_user_info.get('brandId'), rtnReqUnqNo: selectVal, viewCd: 'R'};
			var param = $.param(key);
			modifyObj.load(param);
		},
		save: function(arg) {
			$('body').trigger('click');
			
			const formId = '#ITP_FORM_RTNMNG02010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			
			itp_fn_grid_save_rows(RTNMNG02010_DETAIL_GRID_ID);
			
			$('#ITP_FORM_RTNMNG02010_DETAIL_RTN_REQ_ST_CD').val(arg);

			if (itp_fn_form_event.isValid(formId)) {
				
				const rowData1 = $(RTNMNG02010_DETAIL_GRID_ID).getRowData();				
				var dataCnt = 0;
				$.each(rowData1, function(key, value) {
					if (value.viewCd != 'D') {
						dataCnt = dataCnt + 1;
					} 
				});
				
				if (dataCnt == 0) {
					itp_fn_modal_alert( "요청할 품목내역이 없습니다.");
					return ;
				}
				
				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());
				
				var colReqDt = $('#ITP_FORM_RTNMNG02010_DETAIL_COL_REQ_DT').val();
				if (today > colReqDt) {
					alert("수거요청일은 오늘 이후이어야 합니다.");
					return;
				}	
				
				
				
				var rtnQtyCheck = true;
				var rtnDvsnCheck = true;
				var rtnRsnDvsnCheck = true;
				var rtnRsnCheck = true;
				
				$.each(rowData1, function(key, value) {					
					if(value.rtnQty == '') {
						rtnQtyCheck = false;
					}
					if(value.rtnDvsn == '') {
						rtnDvsnCheck = false;
					}
					if(value.rtnRsnDvsn == '') {
						rtnRsnDvsnCheck = false;
					}
					if(value.rtnRsn == '') {
						rtnRsnCheck = false;
					}
				});
				
				if(rtnQtyCheck == false) {
					itp_fn_modal_alert( "반품수량을 입력하세요.");
					return;
				}		
				if(rtnDvsnCheck == false) {
					itp_fn_modal_alert( "반품구분을 입력하세요.");
					return;
				}
				if(rtnRsnDvsnCheck == false) {
					itp_fn_modal_alert( "사유구분을 입력하세요.");
					return;
				}
				if(rtnRsnCheck == false) {
					itp_fn_modal_alert( "반품사유를 입력하세요.");
					return;
				}
				
				var saveReq = {
					callBack: function(args) {
							$(formId).val(arg);
							let gridInsertData = [];
							let gridUpdateData = [];
							let gridDeleteData = [];
							const rowData = $(RTNMNG02010_DETAIL_GRID_ID).getRowData();
							
							$.each(rowData, function(key, value) {
								if (value.viewCd != 'R') {
									if (value.viewCd == 'C') {
										gridInsertData.push(value);
									} else if (value.viewCd == 'U') {
										gridUpdateData.push(value);
									} else if (value.viewCd == 'D') {
										gridDeleteData.push(value);
									}
								}
							});
			
							let param = $(formId).serializeObject();
							param.gridInsertData = gridInsertData;
							param.gridUpdateData = gridUpdateData;
							param.gridDeleteData = gridDeleteData;
							
							console.log(JSON.stringify(param));
							var searhFn = function() {
								ITP_FORM_RTNMNG02010_DETAIL_IS_WRITING = false;
								modifyObj.button.clear();
								//listObj.grid.search();
							};
							fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');				
						}
				};
				if (arg == 'RR20') {
					itp_fn_modal_confirm('반품 요청하시겠습니까?', saveReq, null);
				} else {
					itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.tmpSave, saveReq, null);
				}
			}
		},
		clear: function () {
			document.getElementById('ITP_FORM_RTNMNG02010_DETAIL').reset();
			listObj.grid.search();
        },
		cancel: function () {
            listObj.grid.search();
       }
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		pageObj.switchScreen('MODIFY');
		
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_REQ_UNQ_NO').val(result.rtnReqUnqNo);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_REQ_ST_CD').val(result.rtnReqStCd);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_REQ_MGR_ID').val(result.rtnReqMgrId);
		
		$('#ITP_FORM_RTNMNG02010_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_RTNMNG02010_DETAIL .fnRtnReqUnqNo').text(result.rtnReqUnqNo + ' / ' + result.rtnReqStNm);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_WHS_ID').val(result.rtnWhsId);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_WHS_NM').val(result.rtnWhsNm);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_LOCATION').val(result.rtnLocation);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_RTN_LOCATION_NM').val(result.rtnLocationNm);
		
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_COL_REQ_DT').val(result.colReqDt);
		$('#ITP_FORM_RTNMNG02010_DETAIL #ITP_FORM_RTNMNG02010_DETAIL_NOTE').val(result.note);

		// 품목  리스트 조회
		pageObj.grid.init('DETAIL', result.rtnReqDtlList);
	},
	 
};


