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

//화면변수
const RTNMNG06010_GRID_ID = '#ITP_RTNMNG06010_jqGrid';
const RTNMNG06010_GRID_LIST = '#ITP_RTNMNG06010_jqGrid_list';
const RTNMNG06010_GRID_PAGER = '#ITP_RTNMNG06010_jqGridPager';
const RTNMNG06010_GRID_EMPTY = '#ITP_RTNMNG06010_jqGridEmpty';

//API URL
let API_POMNG_INOUTMNG_WAREHOUSE_GRID_LIST = '/api/rtnmng/inoutmng/retstock-result-grid-list';  

//화면 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: '브랜드', 
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_PODR_UNQ_NO', name: 'rtnPodrUnqNo',
			label: '반품발주번호', minwidth:140,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_PODR_DTL_NO', name: 'rtnPodrDtlNo',
			label: '항번', 
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, 			// 품목번호
			width: '16', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, 			// 품목명
			width: '12', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RODR_QTY', name: 'rodrQty',
			label: '반품발주수량', 
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'COL_REQ_QTY', name: 'colReqQty',
			label: '수거요청수량', 
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'COL_QTY', name: 'colQty',
			label: '수거수량', 
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false, 
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'COL_AMT', name: 'colAmt',
			label: '수거금액', 
			width: '12', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		}, 
		{
			index: 'RTN_WHS_NM', name: 'rtnWhsNm',
			label: '수거창고', 
			width: '14', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'COL_SCH_DT', name: 'colSchDt',
			label: '수거예정일', 
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PICK_MGR_NM', name: 'pickMgrNm',
			label: '수거담당자명', 
			width: '11', fixed: false, align: 'left',
			sortable: false, hidden: false
		},
		{
			index: 'PICK_MGR_TEL_NO', name: 'pickMgrTelNo',
			label: '수거담당자전화번호', 
			minwidth:140,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		}		
	],
	view: [],
	detail: []
};


/*화면공통 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('RTNMNG06010');
			// 공통코드 표시
			$('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() + 5);
			$('#ITP_TAB_RTNMNG06010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_RTNMNG06010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_RTNMNG06010_SEARCH_TO_DT').datepicker('setDate', defaultToDate);
			
			$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SRTN_WHS_NM').show();
			$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SSPPLY_NM').show();
			//$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SWHS_NM').show();
			
			$('#ITP_BTN_RTNMNG06010_EXCEL').show();
			
		},
		grid: function() {
			itp_fn_jqgrid_resize(RTNMNG06010_GRID_ID, RTNMNG06010_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_RTNMNG06010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_RTNMNG06010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_RTNMNG06010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_RTNMNG06010_CANCELLIST'		: viewObj.button.cancel(); 		break;	// 취소/목록 버튼
						case 'ITP_BTN_RTNMNG06010_DELETE'			: 								break;	// 삭제 버튼
						case 'ITP_BTN_RTNMNG06010_EXCEL'			: listObj.excelDown(); 		break;	// 엑셀다운로드
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_RTNMNG06010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_RTNMNG06010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
            fn_show_btn_auth_array(['#ITP_BTN_RTNMNG06010_SRH', '#ITP_BTN_RTNMNG06010_NEWREG']);
            $('#ITP_AJAX_RTNMNG06010_LIST_CONTAINER').show();

            $('#ITP_FORM_RTNMNG06010_SEARCH #ITP_FORM_RTNMNG06010_SEARCH_SBRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_FORM_RTNMNG06010_SEARCH #ITP_FORM_RTNMNG06010_SEARCH_SSTORE_ID').val(fn_make_user_info.get('storeId'));

            $('#ITP_BTN_RTNMNG06010_SEARCH_SPPLY_ID').show();
            $('#ITP_BTN_RTNMNG06010_DELETE_SPPLY_ID').show();
            $('#ITP_BTN_RTNMNG06010_SEARCH_WHS_ID').show();
            $('#ITP_BTN_RTNMNG06010_DELETE_WHS_ID').show();
        }
    }
}

/*목록화면 Object*/
let listObj = {
	init: function() {
		this.grid.init();
		this.action();
	},
	itp_RTNMNG06010_param: {},
	itp_RTNMNG06010_search: false,
	button: {
		search: function() {
			listObj.itp_RTNMNG06010_search = true;
			let param = $('#ITP_FORM_RTNMNG06010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
			$(RTNMNG06010_GRID_ID).data('grid-param',param);
            $(RTNMNG06010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        }
	},
	empty: {
		init: function() {

		},
		push: function() {
            
        },
        back: function() {
           
        }
	},
	grid: {
		init: function() {
			// 데이터 없을때
            this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
            let param = $('#ITP_FORM_RTNMNG06010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            var option = {
            	gridId: RTNMNG06010_GRID_ID,
            	colModel: gridColModel.list,
            	param: param,
            	url: DOMAIN + API_POMNG_INOUTMNG_WAREHOUSE_GRID_LIST,
            	pager: RTNMNG06010_GRID_PAGER,
            	rownumbers: false,
				loadComplete: function(data) {
                    itp_fn_grid_load_complete(data, RTNMNG06010_GRID_ID, true, 'number', 'RTNMNG06010', listObj.itp_RTNMNG06010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(RTNMNG06010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(RTNMNG06010_GRID_ID).jqGrid('setCell', rowId, 'pchPodrUnqNo', '', ITP_GRID_COL_STYLE.link);
                    });
				},
				onPaging: function(action) {
                    itp_fn_grid_paging(RTNMNG06010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
		},
		clearData: function() {
			$(RTNMNG06010_GRID_ID).jqGrid('clearGridData', true);
            $(RTNMNG06010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(RTNMNG06010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(RTNMNG06010_GRID_EMPTY).show();
		},
		unload: function() {
			$.jgrid.gridUnload(RTNMNG06010_GRID_ID);
		}
	},
	action: function() {
		var _this = this;
		// 공급업체 조회 버튼 클릭
		$('#ITP_BTN_RTNMNG06010_SEARCH_POP_SSPPLY_NM').on('click', function() {
			_this.popup('spply');
		});
		// 공급업체 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SSPPLY_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SSPPLY_NM').val('');
			$('#ITP_FORM_RTNMNG06010_SEARCH_SSPPLY_ID').val('');
		});
		
		// 매장/창고 조회 버튼 클릭
		/*
		$('#ITP_BTN_RTNMNG06010_SEARCH_POP_SWHS_NM').on('click', function() {
			_this.popup('whs');
		});
		// 매장/창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SWHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_RTNMNG06010_SEARCH_SWHS_ID').val('');
		});
		*/
		
		// 매장/창고 조회 버튼 클릭
		$('#ITP_BTN_RTNMNG06010_SEARCH_POP_SRTN_WHS_NM').on('click', function() {
			_this.popup('rwhs');
		});
		// 매장/창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_RTNMNG06010_SEARCH_DEL_SRTN_WHS_NM').on('click', function() {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SRTN_WHS_NM').val('');
			$('#ITP_FORM_RTNMNG06010_SEARCH_SRTN_WHS_ID').val('');
		});
	},
	popup: function(keyword) {		 
		if(keyword === 'spply'){
			var popFn = this.callback.supply;			
			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: {
		supply: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SSPPLY_NM').val(rowDataPop.spplyNm);
			$('#ITP_FORM_RTNMNG06010_SEARCH_SSPPLY_ID').val(rowDataPop.spplyId);
		},
		whs: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
			$('#ITP_FORM_RTNMNG06010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
		}, 
		rwhs: function(rowDataPop) {
			$('#ITP_FORM_RTNMNG06010_SEARCH_SRTN_WHS_NM').val(rowDataPop.whsNm);
			$('#ITP_FORM_RTNMNG06010_SEARCH_SRTN_WHS_ID').val(rowDataPop.whsId);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_POMNG_INOUTMNG_WAREHOUSE_GRID_LIST,
			'param': $('#ITP_FORM_RTNMNG06010_SEARCH').serializeObject(),
			'gridColumns': $(RTNMNG06010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '반품처리현황.xlsx',
			'sheetName': '반품처리현황리스트'
		};
		itp_fn_remote_to_excel(param);
	}
}
