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


//화면변수
const INOUTMNG04010_GRID_ID = '#ITP_INOUTMNG04010_jqGrid';
const INOUTMNG04010_GRID_LIST = '#ITP_INOUTMNG04010_jqGrid_list';
const INOUTMNG04010_GRID_PAGER = '#ITP_INOUTMNG04010_jqGridPager';
const INOUTMNG04010_GRID_EMPTY = '#ITP_INOUTMNG04010_jqGridEmpty';

let ITP_COL_CMD_DTL_ST_CD = fn_make_common_cd_list(CODE_LIST, 'COL_CMD_DTL_ST_CD', true, '수거상태코드');

//API URL
let API_RTNMNG_INOUTMNG_OUTSEARCH_GRID_LIST = '/api/rtnmng/inoutmng/colsearch-grid-list';
//let API_RTNMNG_INOUTMNG_INFO_INV = '/api/rtnmng/inoutmng/info-pick';


//화면 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: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId,				// 브랜드아이디
			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: 'center',
			minwidth: 100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'SHMT_REQ_UNQ_NO', name: 'colCmdUnqNo',
			label: ITP_MSG_LOCALE.label.colCmdUnqNo,			// 수거지시번호
			width: '10', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_UNQ_NO', name: 'colCmdDtlNo',
			label: ITP_MSG_LOCALE.label.colCmdDtlNo,			// 수거지시항번
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_DTL_NO', name: 'rtnPodrUnqNo',
			label: ITP_MSG_LOCALE.label.rtnPodrUnqNo,			// 반품발주번호
			width: '9', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'PCH_PODR_DTL_NO', name: 'rtnPodrDtlNo',
			label: ITP_MSG_LOCALE.label.rtnPodrDtlNo,			// 반품발주항번
			width: '9', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'SHMT_REQ_ST_CD', name: 'colCmdDtlStCd',
			label: ITP_MSG_LOCALE.label.colCmdDtlStCd,			// 수거지시상태
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_COL_CMD_DTL_ST_CD}
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId,					// 품목번호
			width: '10', 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: 120,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'WHS_NM', name: 'rtnWhsNm',
			label: ITP_MSG_LOCALE.label.rtnWhsNm,			// 반품장소
			width: '10', fixed: false, align: 'left',
			minwidth: 200,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'SHMT_QTY', name: 'rtnQty',
			label: ITP_MSG_LOCALE.label.rtnQty,				// 반품발주수량
			width: '14', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'WHS_QTY', name: 'colQty',
			label: ITP_MSG_LOCALE.label.colQty,				// 수거수량
			width: '14', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'SHMT_AMT', name: 'colAmt',
			label: ITP_MSG_LOCALE.label.colAmt,				// 수거금액
			width: '14', fixed: false, align: 'right',
			minwidth: 80,
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'WHS_DT', name: 'colDt',
			label: ITP_MSG_LOCALE.label.colDt,					// 수거일
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'WHS_CNFM_NM', name: 'colCnfmNm',
			label: ITP_MSG_LOCALE.label.colCnfmNm,				// 수거확인자
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'DLV_STTMT_UNQ_NO', name: 'colPickUnqNo',
			label: ITP_MSG_LOCALE.label.colPickUnqNo,			// 수거서번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		}
	],
	view: [],
	detail: []
}


/*화면공통 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('INOUTMNG04010');
			// 공통코드 표시
			$('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() + 7);
			$('#ITP_TAB_INOUTMNG04010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_TO_DT').datepicker('setDate', defaultToDate);
		},
		grid: function() {
			itp_fn_jqgrid_resize(INOUTMNG04010_GRID_ID, INOUTMNG04010_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_INOUTMNG04010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_INOUTMNG04010_EXCEL'				: listObj.excelDown();		break;	// 엑셀다운로드
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_INOUTMNG04010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_INOUTMNG04010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
            fn_show_btn_auth_array(['#ITP_BTN_INOUTMNG04010_PRINT', '#ITP_BTN_INOUTMNG04010_SRH']);
            $('#ITP_AJAX_INOUTMNG04010_LIST_CONTAINER').show();
            $('#ITP_BTN_INOUTMNG04010_DELETE_RTN_WHS_ID').show();
            $('#ITP_BTN_INOUTMNG04010_DELETE_WHS_ID').show();
            $('#ITP_FORM_INOUTMNG04010_SEARCH #ITP_FORM_INOUTMNG04010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));

			$('#ITP_BTN_INOUTMNG04010_EXCEL').show();
        } 
    },
    action: function() {
    	var _this = this;
    	// 출하창고 조회 버튼 클릭
		$('#ITP_BTN_INOUTMNG04010_PRINT').on('click', function() {
			_this.popup();
		});
	},
	popup: function() {		
		var selectedIds = $(INOUTMNG04010_GRID_ID).getGridParam('selarrrow');
		if(selectedIds.length > 1) { // 1이상 선택시
			alert("수거서 하나만 선택해주세요 (대표 납품서번호)") ;//출력
			return; //리턴
		}
		var selRowData ; //변수선언		
		for (var i=selectedIds.length-1; i>=0; i--) {		//1개만 선택시
			selRowData = $(INOUTMNG04010_GRID_ID).jqGrid('getRowData', selectedIds[i]);		//cell 선택
		}
		const key = {brandId: fn_make_user_info.get('brandId'), colPickUnqNo: selRowData.colPickUnqNo};		// popup key 값 설정
		fn_call_popup('biz', 'BIZPOP_COL_STTMT_PRN', '#ITP_ASIDE', null, key, 'S');
	}
};


/*목록화면 Object*/
let listObj = {
	init: function() {
		this.grid.init();
		this.action();
	},
	itp_INOUTMNG04010_param: {},
	itp_INOUTMNG04010_search: false,
	button: {
		search: function() {
			listObj.itp_INOUTMNG04010_search = true;
			let param = $('#ITP_FORM_INOUTMNG04010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            param.sidx = '0';
            console.log(JSON.stringify(param));
			$(INOUTMNG04010_GRID_ID).data('grid-param',param);
            $(INOUTMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        }
	},
	empty: {
		init: function() {
			var _this = this;
            this.push();
            $(INOUTMNG04010_GRID_EMPTY).on('click', function() {
                _this.back();
                listObj.itp_INOUTMNG04010_param.gridSize = $.jgrid.defaults.rowNum;
                $(INOUTMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(listObj.itp_INOUTMNG04010_param)}).trigger('reloadGrid');
            });
		},
		push: function() {
            let param = $('#ITP_FORM_INOUTMNG04010_SEARCH').serializeObject();
            listObj.itp_INOUTMNG04010_param = param;
            param.sidx = '0';
        },
        back: function() {
            $('#ITP_FORM_INOUTMNG04010_SERVICE_BRAND_TYPE').val(listObj.itp_INOUTMNG04010_param.brandType);
            $('#ITP_FORM_INOUTMNG04010_SERVICE_ST_CD').val(listObj.itp_INOUTMNG04010_param.stCd);
            $('#ITP_FORM_INOUTMNG04010_SEARCH_BRAND_NM').val(listObj.itp_INOUTMNG04010_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_INOUTMNG04010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.sidx = '0';
            console.log(JSON.stringify(param));
            var option = {
            	gridId: INOUTMNG04010_GRID_ID,
            	colModel: gridColModel.list,
            	param: param,
            	url: DOMAIN + API_RTNMNG_INOUTMNG_OUTSEARCH_GRID_LIST,
            	pager: INOUTMNG04010_GRID_PAGER,
				multiselect: true,
				multiboxonly : false,
				rownumbers: false,
            	loadComplete: function(data) {
					console.log(data);
                    itp_fn_grid_load_complete(data, INOUTMNG04010_GRID_ID, true, 'number', 'INOUTMNG04010', listObj.itp_INOUTMNG04010_search, listObj.empty, true, data.gridRecords, true);
				},
				onPaging: function(action) {
                    itp_fn_grid_paging(INOUTMNG04010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
		},
		clearData: function() {
			$(INOUTMNG04010_GRID_ID).jqGrid('clearGridData', true);
            $(INOUTMNG04010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(INOUTMNG04010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(INOUTMNG04010_GRID_EMPTY).show();
		},
		unload: function() {
			$.jgrid.gridUnload(INOUTMNG04010_GRID_ID);
		}
	},
	action: function() {
		var _this = this;
		// 반품창고 조회 버튼 클릭
		$('#ITP_BTN_INOUTMNG04010_SEARCH_RTN_WHS_ID').on('click', function() {
			_this.popup('rtnWhs');
		});
		// 반품창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_INOUTMNG04010_DELETE_RTN_WHS_ID').on('click', function() {
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SRTN_WHS_ID').val('');
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SRTN_WHS_NM').val('');
		});
		// 수거창고 조회 버튼 클릭
		$('#ITP_BTN_INOUTMNG04010_SEARCH_WHS_ID').on('click', function() {
			_this.popup('whs');
		});
		// 수거창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_INOUTMNG04010_DELETE_WHS_ID').on('click', function() {
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SWHS_ID').val('');
		});
	},
	popup: function(keyword) {
		var popFn ;
		
		if(keyword == 'rtnWhs') {
			popFn = this.callback.rtnWhs;
		} else if(keyword == 'whs'){
			popFn = this.callback.whs;
		}
		
		var whsDvsn = "";
		if(keyword == 'rtnWhs') {
			whsDvsn = "";
		} else if(keyword == 'whs'){
			whsDvsn = "W01";
		}
		
		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: {
		rtnWhs: function(rowDataPop) {
			console.log(rowDataPop);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SRTN_WHS_ID').val(rowDataPop.whsId);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SRTN_WHS_NM').val(rowDataPop.whsNm);
			
		},
		whs: function(rowDataPop) {
			console.log(rowDataPop);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
			$('#ITP_FORM_INOUTMNG04010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_RTNMNG_INOUTMNG_OUTSEARCH_GRID_LIST,
			'param': $('#ITP_FORM_INOUTMNG04010_SEARCH').serializeObject(),
			'gridColumns': $(INOUTMNG04010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '수거지시현황.xlsx',
			'sheetName': '수거지시리스트'
		};
		itp_fn_remote_to_excel(param);
	}
}

