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


// 화면변수
const VENDINFO03010_GRID_ID = '#ITP_VENDINFO03010_jqGrid';
const VENDINFO03010_GRID_LIST = '#ITP_VENDINFO03010_jqGrid_list';
const VENDINFO03010_GRID_PAGER = '#ITP_VENDINFO03010_jqGridPager';
const VENDINFO03010_GRID_EMPTY = '#ITP_VENDINFO03010_jqGridEmpty';


// API URL
let API_ITEMUNIT_SPPLY_GRID_LIST = '/api/itemunit/spply-grid-list';


// 화면 grid ColModel
const gridColModel = {
	list:[
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, 	//품목번호
			width: '10', 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: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, 		//기본단위
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CURR', name: 'curr',
			label: ITP_MSG_LOCALE.label.curr, 		//통화
			width: '4', 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: 'CONT_STT_DT', name: 'contSttDt',
			label: ITP_MSG_LOCALE.label.contSttDt, 	//시작일
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_END_DT', name: 'contEndDt',
			label: ITP_MSG_LOCALE.label.contEndDt, 	//종료일
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'LEAD_TIME', name: 'leadTime',
			label: ITP_MSG_LOCALE.label.leadTime, 	//리드타임 (Day)
			width: '7', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_ST_NM', name: 'unitStNm',
			label: ITP_MSG_LOCALE.label.status, 	//상태
			width: '4', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SPPLY_ITME_ID', name: 'spplyItemId',
			label: ITP_MSG_LOCALE.label.spplyItemId, //공급사품번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	]
}


/*화면공통 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('VENDINFO03010');
			// 공통코드 표시
			$('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_VENDINFO03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_VENDINFO03010_SEARCH_FROM_DT').datepicker('setDate', now);
			$('#ITP_FORM_VENDINFO03010_SEARCH_TO_DT').datepicker('setDate', defaultToDate);
		},
		grid: function() {
			itp_fn_jqgrid_resize(VENDINFO03010_GRID_ID, VENDINFO03010_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_VENDINFO03010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_VENDINFO03010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_VENDINFO03010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_VENDINFO03010_CANCELLIST'			: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_VENDINFO03010_DELETE'				: 								break;	// 삭제 버튼
						case 'ITP_BTN_VENDINFO03010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_BTN_VENDINFO03010_EXCEL'				: listObj.excelDown(); 		break;	// 엑셀 다운로드
					}
				});
			});
		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_VENDINFO03010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_VENDINFO03010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === 'LIST') { // 목록
			$('#ITP_AJAX_VENDINFO03010_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_VENDINFO03010_SRH', '#ITP_BTN_VENDINFO03010_NEWREG']);
			$('#ITP_FORM_VENDINFO03010_SEARCH #ITP_FORM_VENDINFO03010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_FORM_VENDINFO03010_SEARCH #ITP_FORM_VENDINFO03010_SEARCH_SPPLY_ID').val(fn_make_user_info.get('spplyId'));

			$('#ITP_BTN_VENDINFO03010_EXCEL').show();
		}
	}
}


/*목록화면 Object*/
let listObj = {
    init: function () {
        this.grid.init();
    },
    itp_VENDINFO03010_param: {},
    itp_VENDINFO03010_search: false,
    button: {
        search: function() {
            listObj.itp_VENDINFO03010_search = true;
            let param = $('#ITP_FORM_VENDINFO03010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            $(VENDINFO03010_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_VENDINFO03010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;            
            var option = {
                gridId: VENDINFO03010_GRID_ID,
                colModel: gridColModel.list,
                param: param,
                url: DOMAIN + API_ITEMUNIT_SPPLY_GRID_LIST,
                pager: VENDINFO03010_GRID_PAGER,
                rownumbers: false,
                loadComplete: function(data) {
                    itp_fn_grid_load_complete(data, VENDINFO03010_GRID_ID, true, 'number', 'VENDINFO03010', listObj.itp_VENDINFO03010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(VENDINFO03010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(VENDINFO03010_GRID_ID).jqGrid('setCell', rowId, 'spplyId', '', ITP_GRID_COL_STYLE.link);
                    });
                },
                onPaging: function(action) {
                    itp_fn_grid_paging(VENDINFO03010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
        },
        clearData : function() {
            $(VENDINFO03010_GRID_ID).jqGrid('clearGridData', true);
            $(VENDINFO03010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(VENDINFO03010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(VENDINFO03010_GRID_EMPTY).show();
        },
        unload : function() {
            $.jgrid.gridUnload(VENDINFO03010_GRID_ID);
        }
    },
	excelDown: function() {
		var param = {
			'url': API_ITEMUNIT_SPPLY_GRID_LIST,
			'param': $('#ITP_FORM_VENDINFO03010_SEARCH').serializeObject(),
			'gridColumns': $(VENDINFO03010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '공급사 단가정보.xlsx',
			'sheetName': '공급사 단가리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};
