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


//화면변수
const COLLMNG03010_GRID_ID = '#ITP_COLLMNG03010_jqGrid';
const COLLMNG03010_GRID_LIST = '#ITP_COLLMNG03010_jqGrid_list';
const COLLMNG03010_GRID_PAGER = '#ITP_COLLMNG03010_jqGridPager';
const COLLMNG03010_GRID_EMPTY = '#ITP_COLLMNG03010_jqGridEmpty';


//API URL
let API_RTNMNG_INOUTMNG_SPPLY_PICK_GRID_LIST = '/api/rtnmng/inoutmng/spply-pick-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_ID', 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: 'left',
			minwidth: 120,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'COL_PICK_UNQ_NO', name: 'colPickUnqNo',
			label: ITP_MSG_LOCALE.label.colPickUnqNo,		// 수거서번호
			width: '10', fixed: false, align: 'center',
			minwidth: 160,
			sortable: false, hidden: false
		},
		{
			index: 'PICK_ST_NM', name: 'pickStNm',
			label: ITP_MSG_LOCALE.label.pickStNm,			// 수거서상태
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'COL_REQ_DT', name: 'colRegDt',
			label: ITP_MSG_LOCALE.label.colRegDt,			// 수거일자
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'COL_REQ_DT', name: 'colReqDt',
			label: ITP_MSG_LOCALE.label.colReqDt,			// 수거요청일
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'COL_SCH_DT', name: 'colSchDt',
			label: ITP_MSG_LOCALE.label.colSchDt,			// 수거예정일
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			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',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'COL_REQ_QTY', name: 'colReqQty',
			label: ITP_MSG_LOCALE.label.colReqQty,			// 수거수량
			width: '5', fixed: false, align: 'right',
			minwidth: 80,
			sortable: false, hidden: false
		},		
		{
			index: 'RTN_RSN_DVSN_NM', name: 'rtnRsnDvsnNm',
			label: ITP_MSG_LOCALE.label.rtnRsnDvsnNm,		// 반품사유구분
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt,			// 단가
			width: '14', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'COL_REQ_AMT', name: 'colReqAmt',
			label: ITP_MSG_LOCALE.label.colReqAmt,			// 수거금액
			width: '14', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: '납품장소',									// 납품장소
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'PICK_MGR_NM', name: 'pickMgrNm',
			label: ITP_MSG_LOCALE.label.pickMgrNm,			// 수거담당자
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'PICK_MGR_TEL_NO', name: 'pickMgrTelNo',
			label: ITP_MSG_LOCALE.label.pickMgrTelNo,		// 수거담당자연락처
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	],
	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('COLLMNG03010');
			// 공통코드 표시
			$('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_COLLMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_COLLMNG03010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_COLLMNG03010_SEARCH_TO_DT').datepicker('setDate', defaultToDate);
		},
		grid: function() {
			itp_fn_jqgrid_resize(COLLMNG03010_GRID_ID, COLLMNG03010_GRID_LIST, 'lg');
			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_COLLMNG03010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_COLLMNG03010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_COLLMNG03010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_COLLMNG03010_CANCELLIST'		: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_COLLMNG03010_DELETE'			: modifyObj.button.del();		break;	// 발주 취소 버튼(예정)
						case 'ITP_BTN_COLLMNG03010_EXCEL'			: listObj.excelDown();			break;	// 엑셀 다운로드
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_COLLMNG03010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_COLLMNG03010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
            fn_show_btn_auth_array(['#ITP_BTN_COLLMNG03010_SRH', '#ITP_BTN_COLLMNG03010_NEWREG']);
            $('#ITP_AJAX_COLLMNG03010_LIST_CONTAINER').show();
            $('#ITP_FORM_COLLMNG03010_SEARCH #ITP_FORM_COLLMNG03010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
            if(fn_make_user_info.get('spplyId').length != 0){
            	$('#ITP_FORM_COLLMNG03010_SEARCH #ITP_FORM_COLLMNG03010_SEARCH_SPPLY_ID').val(fn_make_user_info.get('spplyId'));
            } else {
            	$('#ITP_FORM_COLLMNG03010_SEARCH #ITP_FORM_COLLMNG03010_SEARCH_SPPLY_ID').val(fn_make_user_info.get('userId'));
            }
            $('#ITP_BTN_COLLMNG03010_SRH').show();
            $('#ITP_BTN_COLLMNG03010_PRINT').show();
            $('#ITP_BTN_COLLMNG03010_SEARCH_WHS_ID').show();
            $('#ITP_BTN_COLLMNG03010_DELETE_WHS_ID').show();
            $('#ITP_BTN_COLLMNG03010_EXCEL').show();
        }
    },
    action: function() {
		var _this = this;
		$('#ITP_BTN_COLLMNG03010_PRINT').on('click', function() {
			_this.popup();
		});
	},
	popup: function() {
		var selectedIds = $(COLLMNG03010_GRID_ID).getGridParam('selarrrow');
		if(selectedIds.length > 1) { // 1이상 선택시
			alert("납품서 하나만 선택해주세요 (대표 납품서번호)") ;//출력
			return; //리턴
		}
		var selRowData ; //변수선언		
		for (var i=selectedIds.length-1; i>=0; i--) {		//1개만 선택시
			selRowData = $(COLLMNG03010_GRID_ID).jqGrid('getRowData', selectedIds[i]);		//cell 선택
		}
		if (selRowData == null) {
			itp_fn_modal_alert_ajax(ITP_MSG_LOCALE.message.ajax.noData)
			return;
		}
		const key = {brandId: selRowData.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_COLLMNG03010_param: {},
	itp_COLLMNG03010_search: false,
	button: {
		search: function() {
			listObj.itp_COLLMNG03010_search = true;
			let param = $('#ITP_FORM_COLLMNG03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            $(COLLMNG03010_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_COLLMNG03010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            var option = {
            	gridId: COLLMNG03010_GRID_ID,
            	colModel: gridColModel.list,
            	param: param,
            	url: DOMAIN + API_RTNMNG_INOUTMNG_SPPLY_PICK_GRID_LIST,
            	pager: COLLMNG03010_GRID_PAGER,
            	multiselect: true,
				multiboxonly : false,
				rownumbers: false,
				loadComplete: function(data) {
                    itp_fn_grid_load_complete(data, COLLMNG03010_GRID_ID, true, 'number', 'COLLMNG03010', listObj.itp_COLLMNG03010_search, listObj.empty, true, data.gridRecords, true);
				},
				onPaging: function(action) {
                    itp_fn_grid_paging(COLLMNG03010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
		},
		clearData: function() {
			$(COLLMNG03010_GRID_ID).jqGrid('clearGridData', true);
            $(COLLMNG03010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(COLLMNG03010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(COLLMNG03010_GRID_EMPTY).show();
		},
		unload: function() {
			$.jgrid.gridUnload(COLLMNG03010_GRID_ID);
		}
	},
	action: function() {
		var _this = this;
		// 출하창고 조회 버튼 클릭
		$('#ITP_BTN_COLLMNG03010_SEARCH_WHS_ID').on('click', function() {
			_this.popup();
		});
		// 출하창고 텍스트 삭제 버튼 클릭
		$('#ITP_BTN_COLLMNG03010_DELETE_WHS_ID').on('click', function() {
			$('#ITP_FORM_COLLMNG03010_SEARCH_WHS_NM').val('');
			$('#ITP_FORM_COLLMNG03010_SEARCH_WHS_ID').val('');
		});
	},
	popup: function() {
		var popFn = this.callback.create;
		
		const key = {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, key, 'S');
	},
	callback: {
		create: function(rowDataPop) {
			$('#ITP_FORM_COLLMNG03010_SEARCH_WHS_ID').val(rowDataPop.whsId);
			$('#ITP_FORM_COLLMNG03010_SEARCH_WHS_NM').val(rowDataPop.whsNm);
		}
	},
	excelDown: function() {
		var param = {
			'url': API_RTNMNG_INOUTMNG_SPPLY_PICK_GRID_LIST,
			'param': $('#ITP_FORM_COLLMNG03010_SEARCH').serializeObject(),
			'gridColumns': $(COLLMNG03010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '공급사 수거서현황.xlsx',
			'sheetName': '공급사 수거서리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};
