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: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, 		//기본단위
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'STDD_QTY', name: 'stddQty',
			label: ITP_MSG_LOCALE.label.stddQty, 	//기준수량
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CURR', name: 'curr',
			label: ITP_MSG_LOCALE.label.curr, 		//통화
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, 	//단가
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_STT_DT', name: 'contSttDt',
			label: ITP_MSG_LOCALE.label.contSttDt, 	//시작일
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_END_DT', name: 'contEndDt',
			label: ITP_MSG_LOCALE.label.contEndDt, 	//종료일
			width: '8', 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: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'REG_DT', name: 'regDt',
			label: ITP_MSG_LOCALE.label.regDt, 		//등록일
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_ST_NM', name: 'unitStNm',
			label: ITP_MSG_LOCALE.label.status, 	//상태
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SPPLY_ITME_ID', name: 'spplyItemId',
			label: ITP_MSG_LOCALE.label.spplyItemId, //공급사품번
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	],
	datail:[]
}


/*화면공통 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'));
				}
			});
				// 조회일자 지정
			$('#ITP_TAB_VENDINFO03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
		},
		grid: function() {
			itp_fn_jqgrid_resize(VENDINFO03010_GRID_ID, VENDINFO03010_GRID_LIST, 'lg');
			listObj.empty.init();
			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_FORM_VENDINFO03010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
						case 'ITP_BTN_VENDINFO03010_RESET_PW'			: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
						case 'ITP_BTN_VENDINFO03010_DETAIL_ADDROW'		: pageObj.grid.button.addRow();	break;	// 상세:추가버튼
						case 'ITP_BTN_VENDINFO03010_DETAIL_DELROW'		: pageObj.grid.button.delRow();	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']);
		}/* else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_VENDINFO03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_VENDINFO03010_CANCELLIST', '#ITP_BTN_VENDINFO03010_SAVE']);

			$('#ITP_FORM_VENDINFO03010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_VENDINFO03010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});

			$('#ITP_FORM_VENDINFO03010_DETAIL_USER_ID').removeAttr('readonly');
			$('#ITP_FORM_VENDINFO03010_DETAIL_DUP').removeAttr('disabled');
			$('#ITP_FORM_VENDINFO03010_DETAIL_SEARCH_BRAND_ID option').remove();
			$('#ITP_FORM_VENDINFO03010_DETAIL_SEARCH_AFFL_SHOP').show();
			$('#ITP_FORM_VENDINFO03010_DETAIL_DELETE_AFFL_SHOP').show();
		} else if(mode === 'MODIFY') { // 수정
			$('#ITP_AJAX_VENDINFO03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_VENDINFO03010_CANCELLIST', '#ITP_BTN_VENDINFO03010_SAVE', '#ITP_BTN_VENDINFO03010_RESET_PW']);

			$('#ITP_FORM_VENDINFO03010_DETAIL_USER_ID').attr('readonly', true);
			$('#ITP_FORM_VENDINFO03010_DETAIL_DUP').attr('disabled', true);
			$('#ITP_FORM_VENDINFO03010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);
			$('#ITP_FORM_VENDINFO03010_DETAIL_USER_NM').attr('readonly', true);
			$('#ITP_FORM_VENDINFO03010_DETAIL_SEARCH_AFFL_SHOP').hide();
			$('#ITP_FORM_VENDINFO03010_DETAIL_DELETE_AFFL_SHOP').hide();
		} else if(mode === 'VIEW') { // 보기
			$('#ITP_AJAX_VENDINFO03010_VIEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_VENDINFO03010_MODIFY', '#ITP_BTN_VENDINFO03010_CANCELLIST']);
		}*/
	}
}


/*목록화면 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;
            param.sbrandId = LOGIN_USER_INFO.brandId;
            param.sspplyId = LOGIN_USER_INFO.spplyId;
            $(VENDINFO03010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        }/*,
        create: function() {
            pageObj.switchScreen('ADD');
            itp_fn_form_clear_validate(null, '#ITP_FORM_OPER06010_DETAIL');
            // 권한정보 리스트 조회
            createObj.init();
        }*/
    },
    empty: {
        init: function() {
            var _this = this;
            this.push();
            $(VENDINFO03010_GRID_EMPTY).on('click', function() {
                _this.back();
                listObj.itp_VENDINFO03010_param.gridSize = $.jgrid.defaults.rowNum;
                $(VENDINFO03010_GRID_ID).setGridParam({'postData': JSON.stringify(listObj.itp_VENDINFO03010_param)}).trigger('reloadGrid');
            });
        },
        push: function() {
            let param = $('#ITP_FORM_VENDINFO03010_SEARCH').serializeObject();
            param.sbrandId = LOGIN_USER_INFO.brandId;
            param.sspplyId = LOGIN_USER_INFO.spplyId;
            listObj.itp_VENDINFO03010_param = param;

        },
        back: function() {
            $('#ITP_FORM_VENDINFO03010_SEARCH_CONT_STT_DT').val(listObj.itp_VENDINFO03010_param.contSttDt);
            $('#ITP_FORM_VENDINFO03010_SEARCH_CONT_END_DT').val(listObj.itp_VENDINFO03010_param.contEndDt);
            $('#ITP_FORM_VENDINFO03010_SERVICE_ST_CD').val(listObj.itp_VENDINFO03010_param.stCd);
            $('#ITP_FORM_VENDINFO03010_SEARCH_ITEM_NM').val(listObj.itp_VENDINFO03010_param.itemNm);
        }
    },
    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_VENDINFO03010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;            
            param.sbrandId = LOGIN_USER_INFO.brandId;
            param.sspplyId = LOGIN_USER_INFO.spplyId;
            console.log(param);
            var option = {
                gridId: VENDINFO03010_GRID_ID,
                colModel: gridColModel.list,
                param: param,
                url: DOMAIN + API_ITEMUNIT_SPPLY_GRID_LIST,
                pager: VENDINFO03010_GRID_PAGER,
                rownumbers: false,
                /*onCellSelect: function(rowId, cellIdx, cellValue) {
                    var cm = $(this).jqGrid('getGridParam', 'colModel');
                    var colNm = cm[cellIdx].name;
                    if (colNm === 'brandId') {
                        const brandIdVal = $(this).jqGrid('getCell', rowId, 'brandId');
                        const key = {brandId: brandIdVal, viewCd: 'R'};
                        var param = $.param(key);
                        viewObj.init(param);
                    }
                },*/
                loadComplete: function(data) {
                    console.log(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, 'itemId', '', 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);
        }
    }
};

console.log(CONN_KEY);