require(['config'], function() {
    require([
		'jquery.bootstrap.fileUpload',	//파일 업로드가 있는 페이지에서 로딩
		'xlsx' 		// 엑셀 다운로드
    ], function($) {
        pageObj.init();
    });
});

/********************************************************
 --------------------------------------------------------
 - Object 목록								            -
 --------------------------------------------------------
 * const gridColModel = {}	: 화면 Grid Object			*
 * let pageObj = {}			: 화면공통 Object			*
 * let listObj = {}			: 목록화면 Object			*
 * let viewObj = {}			: 상세화면 Object			*
 * let modifyObj = {}		: 수정화면 Object			*
 * let createObj = {}		: 신규화면 Object			*
 *******************************************************/

/*화면 변수*/
const STINFO04010_GRID_ID = '#ITP_STINFO04010_jqGrid';
const STINFO04010_GRID_LIST = '#ITP_STINFO04010_jqGrid_list';
const STINFO04010_GRID_PAGER = '#ITP_STINFO04010_jqGridPager';
const STINFO04010_GRID_EMPTY = '#ITP_STINFO04010_jqGridEmpty';

const STINFO04010_UPLOAD_GRID_ID = '#ITP_STINFO04010_UPLOAD_jqGrid';
const STINFO04010_UPLOAD_GRID_LIST = '#ITP_STINFO04010_UPLOAD_jqGrid_list';
const STINFO04010_UPLOAD_GRID_PAGER = '#ITP_STINFO04010_UPLOAD_jqGridPager';
const STINFO04010_UPLOAD_GRID_EMPTY = '#ITP_STINFO04010_UPLOAD_jqGridEmpty';

const STINFO04010_VIEW_GRID_ID = '#ITP_STINFO04010_VIEW_jqGrid';
const STINFO04010_VIEW_GRID_LIST = '#ITP_STINFO04010_VIEW_jqGrid_list';
const STINFO04010_VIEW_GRID_PAGER = '#ITP_STINFO04010_VIEW_jqGridPager';
const STINFO04010_VIEW_GRID_EMPTY = '#ITP_STINFO04010_VIEW_jqGridEmpty';

const STINFO04010_DETAIL_GRID_ID = '#ITP_STINFO04010_DETAIL_jqGrid';
const STINFO04010_DETAIL_GRID_LIST = '#ITP_STINFO04010_DETAIL_jqGrid_list';
const STINFO04010_DETAIL_GRID_PAGER = '#ITP_STINFO04010_DETAIL_jqGridPager';
const STINFO04010_DETAIL_GRID_EMPTY = '#ITP_STINFO04010_DETAIL_jqGridEmpty';

let ITP_FORM_STINFO04010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_STINFO04010_DETAIL_IS_WRITING = false;

let STINFO04010_UPLOAD_GRID_LAST_ROW_ID;

/*API URL*/
let API_DETAIL_GRID_LIST 	= '/api/item/detail-grid-list';	    // 목록
let API_DETAIL_INFO 		= '/api/item/info-item';			// 상세
let API_DETAIL_SAVE 		= '/api/item/save-item';			// 저장
let API_DETAIL_SAVE_LIST 	= '/api/item/save-item-list';		// 일괄저장
let API_ITEM_CLASS_LEVEL	= '/api/item/class-level-list';		//품목분류 level 검색
let API_GET_KEY			 	= '/api/session/get-key';

let SCREEN_MODE;

function checkNumberPoint(event) {
	if(event.key === '.' 
		|| event.key === '-'
    	|| event.key >= 0 && event.key <= 9) {
    	return true;
    }
    return false;
}

/*화면 Grid ColModel*/
const gridColModel = {
    list: [
        {
            index: 'BRAND_ID', name: 'brandId',
            label: ITP_MSG_LOCALE.label.brandId, //브랜드ID
            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',
            sortable: false, hidden: true
        },
        {
            index: 'ITEM_ID', name: 'itemId',
            label: ITP_MSG_LOCALE.label.itemId, //품목번호
            width: '14', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'ITEM_NM', name: 'itemNm',
            label: ITP_MSG_LOCALE.label.itemNm, //품목명
            width: '13', fixed: false, align: 'left',
            sortable: false, hidden: false, classes: 'ui-ellipsis'
        },
        {
            index: 'ITEM_TYPE_NM', name: 'itemTypeNm',
            label: ITP_MSG_LOCALE.label.itemTypeNm, //품목유형
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'ITEM_CLASS_NM', name: 'itemClassNm',
            label: ITP_MSG_LOCALE.label.itemClassNm, //품목분류
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: false, classes: 'ui-ellipsis'
        },
        {
            index: 'ST_CD', name: 'itemStNm',
            label: ITP_MSG_LOCALE.label.stNm, //상태
            width: '6', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'PODR_PSSBLDVSN_NM', name: 'podrPssblDvsnNm',
            label: ITP_MSG_LOCALE.label.podrPssblDvsnNm, //발주여부
            width: '7', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'UNIT', name: 'unit',
            label: ITP_MSG_LOCALE.label.unit, //기본단위
            width: '6', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'PCH_ITEM_YN', name: 'pchItemYn',
            label: ITP_MSG_LOCALE.label.pchItemYn, //구매품목여부
            width: '8', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'SALE_ITEM_YN', name: 'saleItemYn',
            label: ITP_MSG_LOCALE.label.saleItemYn, //판매품목여부
            width: '8', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
    ],
	upload: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'NORMAL_YN', name: 'normalYn',
			label: "정상여부",
			width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250,
			sortable: false, editable: false, edittype: 'text',
			editrules: {required: false}
		},
		{
			index: 'ERROR_MSG', name: 'errorMsg',
			label: "에러메시지",
			width: '15', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 300,
			sortable: false, editable: false, edittype: 'text',
			editrules: {required: false}
		},
        {
            index: 'ITEM_NM', name: 'itemNm',
            label: ITP_MSG_LOCALE.label.itemNm, //품목명
            width: '13', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
            sortable: false, hidden: false
        },
        {
            index: 'ITEM_TYPE_NM', name: 'itemTypeNm',
            label: ITP_MSG_LOCALE.label.itemTypeNm, //품목유형
            width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250,
            sortable: false, hidden: false
        },
		{
            index: 'IMP_DVSN_NM', name: 'impDvsnNm',
            label: '내외자구분', 
            width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250, 
			sortable: false, hidden: false
        },
		{
            index: 'PODR_PSSBL_DVSN_NM', name: 'podrPssblDvsnNm',
            label: '발주여부', 
            width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250, 
			sortable: false, hidden: false
        },
		{
            index: 'UNIT_NM', name: 'unitNm',
            label: '기본단위', 
            width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250, 
			sortable: false, hidden: false
        },
		{
            index: 'PCH_ITEM_YN', name: 'pchItemYn',
            label: '구매품목여부', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'SALE_ITEM_YN', name: 'saleItemYn',
            label: '판매품목여부', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'WEIGHT', name: 'weight',
            label: '중량 값', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'WEIGHT_UNIT_NM', name: 'weightUnitNm',
            label: '중량 단위', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'VOLUME', name: 'volume',
            label: '부피 값', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'VOLUME_UNIT_NM', name: 'volumeUnitNm',
            label: '부피 단위', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'WIDTH', name: 'width',
            label: '가로 값', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'WIDTH_UNIT_NM', name: 'widthUnitNm',
            label: '가로 단위', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'HEIGHT', name: 'height',
            label: '세로 값', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'HEIGHT_UNIT_NM', name: 'heightUnitNm',
            label: '세로 단위', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'HIGH', name: 'high',
            label: '높이 값', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'HIGH_UNIT_NM', name: 'highUnitNm',
            label: '높이 단위', 
            width: '6', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'EXPRY_YN', name: 'expryYn',
            label: '유통기한여부', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'EXPRY_PERIOD', name: 'expryPeriod',
            label: '유통가능기간', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'RTN_PSSBL_YN', name: 'rtnPssblYn',
            label: '반품가능여부', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'RTN_DVSN_NM', name: 'rtnDvsnNm',
            label: '반품구분', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        },
		{
            index: 'BARCODE', name: 'barcode',
            label: '바코드', 
            width: '10', fixed: false, align: 'center', 
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
        }		
    ],
    detail: [
        {
            index: 'VIEW_CD', name: 'viewCd',
            label: ITP_MSG_LOCALE.label.viewCd,
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: true
        },
        {
            index: 'AUTH_NO', name: 'authNo',
            label: ITP_MSG_LOCALE.label.permitCd,
            width: '20', fixed: false, align: 'center',
            sortable: false, editable: false, edittype: 'text',
            editrules: {required: true}
        },
        {
            index: 'AUTH_NM', name: 'authNm',
            label: ITP_MSG_LOCALE.label.permitName,
            width: '30', fixed: false, align: 'center',
            sortable: true, editable: false, edittype: 'text',
            editrules: {required: true}
        },
        {
            index: 'USE_YN_NM', name: 'useYnNm',
            label: ITP_MSG_LOCALE.label.status,
            width: '10', fixed: false, align: 'center',
            sortable: false, editable: false, edittype: 'text'
        },
        {
            index: 'ADD_DT', name: 'addDt',
            label: ITP_MSG_LOCALE.label.regDt,
            width: '10', fixed: false, align: 'center',
            sortable: false, editable: false, edittype: 'text'
        }
    ]
};

/*화면공통 Object*/
let pageObj = {
    init: function () {
        this.ui.init();
        this.event.init();
        // this.fileUpload();
    },
    ui: {
        init: function () {
            this.view();
            this.grid();
            this.ready();
        },
        view: function() {
            // 버튼 권한설정
            fn_proc_btn_auth('STINFO04010');

            // 공통코드 표시
            $('select').each(function() {
                if($(this).data('select-code')) {
                    fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
                }
            });

            itemClassLevel.make(1, 'LIST');
        },
        grid: function() {
            itp_fn_jqgrid_resize(STINFO04010_GRID_ID, STINFO04010_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_STINFO04010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
                        case 'ITP_BTN_STINFO04010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
                        case 'ITP_BTN_STINFO04010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
                        case 'ITP_BTN_STINFO04010_CANCELLIST'		: createObj.button.cancel(); 	break;	// 취소/목록 버튼
                        case 'ITP_BTN_STINFO04010_UPLOAD'			: listObj.button.upload(); 		break;	// 엑셀업로드 버튼
                        case 'ITP_BTN_STINFO04010_DELETE'			: 								break;	// 삭제 버튼
                        case 'ITP_BTN_STINFO04010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
                        case 'ITP_FORM_STINFO04010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
                        case 'ITP_BTN_STINFO04010_RESET_PW'			: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
                        case 'ITP_BTN_STINFO04010_TEMPLATE_DOWNLOAD'	: uploadObj.button.downTemplate();	break;	// 템플릿 다운로드
                        case 'ITP_BTN_STINFO04010_FILE_UPLOAD'		: uploadObj.button.uploadFile();	break;	// 파일 업로드
                    }
                });
            });

            $('body').on('click', function(e) {
                var gridId = STINFO04010_DETAIL_GRID_ID.replace('#', '');
                var rids = $(STINFO04010_DETAIL_GRID_ID).jqGrid('getDataIDs');
                var last_row_id = rids[rids.length - 1];
                itp_fn_grid_reset_selection(e, last_row_id, gridId, 'STINFO04010_DETAIL');
            });

        }
    },
    fileUpload: function() {
        //파일업로드(등록/수정화면)
        var id = 'STINFO04010';
        $('#ITP_' + id + '_DETAIL_UPLOAD').empty();
        const fileArgs = {
            'fileNo': $('#ITP_FORM_' + id + '_DETAIL_FILE_NO').val(),
            'viewCd': $('#ITP_FORM_' + id + '_DETAIL_VIEW_CD').val(),
            'showYn': $('#ITP_' + id + '_DETAIL_UPLOAD').attr('data-show-yn'),
            'fileId': '#ITP_FORM_' + id + '_DETAIL_FILE_NO' //파일번호를 서버에서 받아서 넣을 파일번호 인풋박스 아이디
        };
        itp_fn_set_file_upload('ITP_' + id + '_DETAIL_UPLOAD', 'affl', true, false, id, fileArgs);
    },
    switchScreen: function(mode) {
		SCREEN_MODE = mode;
	
        $('.itp_det_head').find('button[id^="ITP_BTN_STINFO04010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_STINFO04010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
            itp_fn_fire_window_resize();
            $('#ITP_AJAX_STINFO04010_LIST_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO04010_SRH', '#ITP_BTN_STINFO04010_NEWREG', '#ITP_BTN_STINFO04010_UPLOAD']);
            $('#ITP_FORM_STINFO04010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));

        } else if(mode == 'UPLOAD') { // 엑셀업로드
			$('#ITP_AJAX_STINFO04010_UPLOAD_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO04010_FILE_UPLOAD','#ITP_BTN_STINFO04010_TEMPLATE_DOWNLOAD','#ITP_BTN_STINFO04010_SAVE', '#ITP_BTN_STINFO04010_CANCELLIST']);
            $('#ITP_FORM_STINFO04010_UPLOAD_BRAND_ID').val(fn_make_user_info.get('brandId'));

        } else if(mode == 'ADD') { // 등록
            $('#ITP_AJAX_STINFO04010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO04010_CANCELLIST', '#ITP_BTN_STINFO04010_SAVE']);

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

            $('#ITP_FORM_STINFO04010_DETAIL_STORE_ID').removeAttr('readonly');
            $('#ITP_FORM_STINFO04010_DETAIL_DUP').removeAttr('disabled');

            // 브랜드그룹 정보 설정
            $('#ITP_FORM_STINFO04010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
            $('#ITP_FORM_STINFO04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_FORM_STINFO04010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
			//$('#ITP_FORM_STINFO04010_DETAIL .fnItemClassMgntNo').text('(자동부여)');
			$('#ITP_FORM_STINFO04010_DETAIL_OQ_BRAND_ID').val(fn_make_user_info.get('oqBrandId'));
        } else if(mode == 'MODIFY') { // 수정
            $('#ITP_AJAX_STINFO04010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO04010_CANCELLIST', '#ITP_BTN_STINFO04010_SAVE', '#ITP_BTN_STINFO04010_RESET_PW']);

            $('#ITP_FORM_STINFO04010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
            $('#ITP_FORM_STINFO04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
            $('#ITP_FORM_STINFO04010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
            $('#ITP_FORM_STINFO04010_DETAIL_STORE_ID').attr('readonly', true);
            $('#ITP_FORM_STINFO04010_DETAIL_DUP').attr('disabled', true);
            $('#ITP_FORM_STINFO04010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);

            $('#ITP_FORM_STINFO04010_DETAIL_SEARCH_AFFL_SHOP').hide();
            $('#ITP_FORM_STINFO04010_DETAIL_DELETE_AFFL_SHOP').hide();
        } else if(mode == 'VIEW') { // 보기
            $('#ITP_AJAX_STINFO04010_VIEW_CONTAINER').show();
            $('#ITP_FORM_STINFO04010_VIEW .fnBrandNm').text(fn_make_user_info.get('brandNm'));
            fn_show_btn_auth_array(['#ITP_BTN_STINFO04010_MODIFY', '#ITP_BTN_STINFO04010_CANCELLIST']);
        }
    }
};

/*목록화면 Object*/
let listObj = {
    init: function () {
        this.form();
        this.grid.init();
    },
    itp_STINFO04010_search: false,
    button: {
        search: function() {
            listObj.itp_STINFO04010_search = true;
            let param = $('#ITP_FORM_STINFO04010_SEARCH').serializeObject();
            console.log(JSON.stringify(param));
            param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            $(STINFO04010_GRID_ID).data('grid-param',param);
            $(STINFO04010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        },
        create: function() {
            if(!fn_make_user_info.isEmpty()) {
                createObj.init();
            }
        }, 
		upload: function() {
			if(!fn_make_user_info.isEmpty()) {
                uploadObj.init();
            }
		}
    },
    form: function() {
        $('#ITP_FORM_STINFO04010_SEARCH_ITEM_CLASS1').on('change', function() {
            ($(this).val() === '') ? itemClassLevel.clear(2): itemClassLevel.change(2);
        });
        $('#ITP_FORM_STINFO04010_SEARCH_ITEM_CLASS2').on('change', function() {
            ($(this).val() === '') ? itemClassLevel.clear(3): itemClassLevel.change(3);
        });
        $('#ITP_FORM_STINFO04010_SEARCH_ITEM_CLASS3').on('change', function() {
            ($(this).val() === '') ? itemClassLevel.clear(4): itemClassLevel.change(4);
        });
    },
    empty: {
        init: function() {
        },
        itp_STINFO04010_param: {},
        push: function() {
        },
        back: function() {
        }
    },
    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_STINFO04010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.sbrandId = fn_make_user_info.get('brandId');
            // param.sbrandId = sessionStorage.getItem('itp_brand_id') !== null ? sessionStorage.getItem('itp_brand_id') : 'BRD221000001';

            var option = {
                gridId: STINFO04010_GRID_ID,
                colModel: gridColModel.list,
                param: param,
                url: DOMAIN + API_DETAIL_GRID_LIST,
                pager: STINFO04010_GRID_PAGER,
                onCellSelect: function(rowId, cellIdx, cellValue) {
                    var cm = $(this).jqGrid('getGridParam', 'colModel');
                    var colNm = cm[cellIdx].name;
                    if (colNm == 'itemId') {
                        const selectVal = $(this).jqGrid('getCell', rowId, 'itemId');
                        const key = {brandId:fn_make_user_info.get('brandId'), itemId: selectVal, viewCd: 'R'};
                        var param = $.param(key);
                        viewObj.init(param);
                    }
                },
                loadComplete: function(data) {
                    console.log(data);
                    itp_fn_grid_load_complete(data, STINFO04010_GRID_ID, true, 'number', 'STINFO04010', listObj.itp_STINFO04010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(STINFO04010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(STINFO04010_GRID_ID).jqGrid('setCell', rowId, 'itemId', '', ITP_GRID_COL_STYLE.link);
                    });
                },
                onPaging: function(action) {
                    itp_fn_grid_paging(STINFO04010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
        },
        clearData : function() {
            $(STINFO04010_GRID_ID).jqGrid('clearGridData', true);
            $(STINFO04010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(STINFO04010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(STINFO04010_GRID_EMPTY).show();
        },
        unload : function() {
            $.jgrid.gridUnload(STINFO04010_GRID_ID);
        }
    }
};

/*상세화면 Object*/
let viewObj = {
    init: function (param) {
        this.load(param);
    },
    load: function(param) {
        fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
    },
    callback: function (result) {
        console.log(result);
        pageObj.switchScreen('VIEW');
        $('#ITP_FORM_STINFO04010_VIEW #ITP_FORM_STINFO04010_VIEW_ITEM_ID').val(result.itemId);
        $('#ITP_FORM_STINFO04010_VIEW .fnStCd').text(result.itemStNm);
        var itemClassNm = ""; //itemClassNm 값 없음
		
		if (result.itemClass1Nm != null && result.itemClass1Nm != "")//itemclass1Nm 값이 있을때
		{
			var itemClassNm = result.itemClass1Nm;  //itemClassNm 에 itemClass1Nm 값 넣음
		}
		
		if (result.itemClass2Nm != null && result.itemClass2Nm != "")//itemclass2Nm 값이 있을때
		{
			var itemClassNm = itemClassNm + " " + ">" + " " + result.itemClass2Nm;	//itemClassNm 에 itemClassNm + > + itemClass2Nm 값 넣음
		}
		
		if (result.itemClass3Nm != null && result.itemClass3Nm != "")//itemclass3Nm 값이 있을때
		{
			var itemClassNm = itemClassNm + " " + ">" + " " + result.itemClass3Nm;	//itemClassNm 에 itemClassNm + > + itemClass3Nm 값 넣음		
		}
		
		if (result.itemClass4Nm != null && result.itemClass4Nm != "")//itemclass4Nm 값이 있을때
		{
			var itemClassNm = itemClassNm + " " + ">" + " " + result.itemClass4Nm;	//itemClassNm 에 itemClassNm + > + itemClass4Nm 값 넣음

		}
        $('#ITP_FORM_STINFO04010_VIEW .fnItemClass1').text(result.itemClass1Nm);
        $('#ITP_FORM_STINFO04010_VIEW .fnItemClass2').text(result.itemClass2Nm);
        $('#ITP_FORM_STINFO04010_VIEW .fnItemClass3').text(result.itemClass3Nm);
        $('#ITP_FORM_STINFO04010_VIEW .fnItemClass4').text(result.itemClass4Nm);
        $('#ITP_FORM_STINFO04010_VIEW .fnItemClassMgntNo').text(result.itemClassMgntNo);
        $('#ITP_FORM_STINFO04010_VIEW .fnItemTypeNm').text(result.itemTypeNm);
        
        $('#ITP_FORM_STINFO04010_VIEW .fnItemNm').text(result.itemNm);
        let impDvsnNm;
        $('#ITP_FORM_STINFO04010_VIEW #ITP_FORM_STINFO04010_VIEW_IMP_DVSN').val(result.impDvsn);
        if (result.impDvsn == '') {
        	impDvsnNm = '';
		} else {
			impDvsnNm = $('#ITP_FORM_STINFO04010_VIEW_IMP_DVSN option:selected').text(); 
		}        
        $('#ITP_FORM_STINFO04010_VIEW .fnImpDvsn').text(impDvsnNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnPodrPssblDvsnNm').text(result.podrPssblDvsnNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnUnit').text(result.unit);
        $('#ITP_FORM_STINFO04010_VIEW .fnPchItemYn').text(result.pchItemYn);
        //$('#ITP_FORM_STINFO04010_VIEW .fnViewPchUnit').text(result.pchUnit);
        $('#ITP_FORM_STINFO04010_VIEW .fnSaleItemYn').text(result.saleItemYn);
        //$('#ITP_FORM_STINFO04010_VIEW .fnSaleUnit').text(result.saleUnit);
        $('#ITP_FORM_STINFO04010_VIEW .fnBarcode').text(result.barcode);
        //$('#ITP_FORM_STINFO04010_VIEW .fnStckUnit').text(result.stckUnit);
        $('#ITP_FORM_STINFO04010_VIEW .fnWeight').text(result.weight.toFixed(2));
        $('#ITP_FORM_STINFO04010_VIEW .fnWeightUnit').text(result.weightUnitNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnVolume').text(result.volume.toFixed(2));
        $('#ITP_FORM_STINFO04010_VIEW .fnVolumeUnit').text(result.volumeUnitNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnWidth').text(result.width.toFixed(2));
        $('#ITP_FORM_STINFO04010_VIEW .fnWidthUnit').text(result.widthUnitNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnHeight').text(result.height.toFixed(2));
        $('#ITP_FORM_STINFO04010_VIEW .fnHeightUnit').text(result.heightUnitNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnHigh').text(result.high.toFixed(2));
        $('#ITP_FORM_STINFO04010_VIEW .fnHighUnit').text(result.highUnitNm);
        $('#ITP_FORM_STINFO04010_VIEW .fnOneBoxQty').text(result.oneBoxQty);
        $('#ITP_FORM_STINFO04010_VIEW .fnExpryYn').text(result.expryYn);
        $('#ITP_FORM_STINFO04010_VIEW .fnExpryPeriod').text(result.expryPeriod);
        $('#ITP_FORM_STINFO04010_VIEW .fnRtnPssblYn').text(result.rtnPssblYn);
        $('#ITP_FORM_STINFO04010_VIEW .fnRtnDvsnNm').text(result.rtnDvsnNm);
		$('#ITP_FORM_STINFO04010_VIEW .fnOqMenuCd').text(result.oqMenuCd);
		$('#ITP_FORM_STINFO04010_VIEWL #ITP_FORM_STINFO04010_VIEW_OQ_BRAND_ID').val(result.oqBrandId);
        $('#ITP_FORM_STINFO04010_VIEW_FILE_NO').val(result.itemImageFileNo);
        /*
        itp_fn_attach_file.view('ITP_FORM_STINFO04010_VIEW_FILE', result.imageFileNoList
            , 'form-control itp_input', 'border:none; top:12px; left:12px; height:100px; width:125px; position:absolute;');
        */
       fileUploadInfo4010()
    }
};

/*수정화면 Object*/
let modifyObj = {
    init: function () {
    },
    button: {
        modify: function (param) {
            itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO04010_DETAIL');
            const selectVal = $('#ITP_FORM_STINFO04010_VIEW_ITEM_ID').val();
            const key = {brandId:fn_make_user_info.get('brandId'), itemId: selectVal, viewCd: 'R'};
            var param = $.param(key);
            modifyObj.load(param);
        }
    },
    load: function(param) {
        fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
    },
    callback: function (result) {
        console.log(result);
        pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_VIEW_CD').val('U');
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ITEM_ID').val(result.itemId);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ITEM_ST_CD').val(result.itemStCd);
        $('#ITP_FORM_STINFO04010_DETAIL .fnItemClassMgntNo').text(result.itemId);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ITEM_TYPE').val(result.itemType);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ITEM_NM').val(result.itemNm);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_IMP_DVSN').val(result.impDvsn);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_PODR_PSSBL_DVSN').val(result.podrPssblDvsn);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_UNIT').val(result.unit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_PCH_ITEM_YN').val(result.pchItemYn);
        //$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_PCH_UNIT').val(result.pchUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_SALE_ITEM_YN').val(result.saleItemYn);
        //$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_SALE_UNIT').val(result.saleUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_BARCODE').val(result.barcode);
        //$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_STCK_UNIT').val(result.stckUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_WEIGHT').val(result.weight);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_WEIGHT_UNIT').val(result.weightUnit);
        $('#ITP_FORM_STINFO04010_DETAIL .item_unit').text(result.unit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_VOLUME').val(result.volume);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_VOLUME_UNIT').val(result.volumeUnit);        
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_WIDTH').val(result.width);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_WIDTH_UNIT').val(result.widthUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_HEIGHT').val(result.height);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_HEIGHT_UNIT').val(result.heightUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_HIGH').val(result.high);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_HIGH_UNIT').val(result.highUnit);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ONE_BOX_QTY').val(result.oneBoxQty);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_EXPRY_YN').val(result.expryYn);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_EXPRY_PERIOD').val(result.expryPeriod);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_RTN_PSSBL_YN').val(result.rtnPssblYn);
        $('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_RTN_DVSN').val(result.rtnDvsn);
        // $.each(result.imageFileNoList, function(index, item) {
        //     $('#ITP_FORM_STINFO04010_VIEW .ITEM_IMAGE').append('<img src=" + item.filePath +" />');
        // });
		$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_OQ_MENU_CD').val(result.oqMenuCd);
		$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_OQ_BRAND_ID').val(result.oqBrandId);
        $('#ITP_FORM_OPER04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_FILE_NO').val(result.itemImageFileNo);
        
        pageObj.fileUpload();
        itemClassLevel.data.push(result.itemClass1);
        itemClassLevel.data.push(result.itemClass2);
        itemClassLevel.data.push(result.itemClass3);
        itemClassLevel.data.push(result.itemClass4);
        itemClassLevel.make(1, 'MODIFY');
    }
};

/* 엑셀업로드 화면 */
let uploadObj = {
	init: function () {
		this.grid.init();
		this.change();
	}, 
	button: {
        downTemplate: function() {
            var lidx = window.location.href.lastIndexOf('/');
			window.location.assign(window.location.href.substring(0, lidx) + '/excel_template/item_upload_v1.xlsx');
        },
        uploadFile: function() {
        	uploadObj.grid.clearData();
			// 파일선택
			$('#ITP_FORM_STINFO04010_UPLOAD_EXCEL_UPLOAD_SELECT_FILE').click();
        }, 
		makeExcelDataList: function(excelData) {
			var processArray = [];
			
			excelData.shift();
			excelData.shift();
			excelData.some(function(data) {
				var processData = {};				
				processData.itemNm 	= data.A.toString(); 		
				processData.itemTypeNm 	= data.B.toString(); 		
				processData.impDvsnNm 			= data.C.toString(); 	
				processData.podrPssblDvsnNm 	= data.D.toString(); 	
				processData.unitNm 		= data.E.toString(); 	
				processData.pchItemYn 	= data.F.toString(); 	
				processData.saleItemYn 	= data.G.toString(); 	
				processData.weight 			= data.H.toString(); 	
				processData.weightUnitNm 	= data.I.toString(); 	
				processData.volume 			= data.J.toString(); 	
				processData.volumeUnitNm 	= data.K.toString(); 	
				processData.width 	= data.L.toString(); 	
				processData.widthUnitNm 	= data.M.toString(); 	
				processData.height 	= data.N.toString(); 	
				processData.heightUnitNm 	= data.O.toString(); 	
				processData.high 	= data.P.toString(); 	
				processData.highUnitNm 	= data.Q.toString(); 	
				processData.expryYn 	= data.R.toString(); 	
				processData.expryPeriod 	= data.S.toString(); 	
				processData.rtnPssblYn 	= data.T.toString(); 	
				processData.rtnDvsnNm 	= data.U.toString(); 	
				processData.barcode 	= data.V.toString(); 	
				processArray.push(processData);
			});
			
			$.each(processArray, function(key, value) {
				
				var normalYn = 'Y';
				var errorMsg = '';
			
				// console.log(JSON.stringify(value));
				// 코드값 체크
				if("소모품:자산용".indexOf(value.itemTypeNm) < 0) {
					normalYn = 'N';
					errorMsg += '품목유형;';
				} 
				if("국내:수입".indexOf(value.impDvsnNm) < 0) {
					normalYn = 'N';
					errorMsg += '내외자구분;';
				}
				if("발주가능:발주불가".indexOf(value.podrPssblDvsnNm) < 0) {
					normalYn = 'N';
					errorMsg += '발주여부;';
				}
				if("부피:개수:박스".indexOf(value.unitNm) < 0) {
					normalYn = 'N';
					errorMsg += '기본단위;';
				}
				if("Y:N".indexOf(value.pchItemYn) < 0) {
					normalYn = 'N';
					errorMsg += '구매품목여부;';
				}
				if("Y:N".indexOf(value.saleItemYn) < 0) {
					normalYn = 'N';
					errorMsg += '판매품목여부;';
				}
				if("kg:g:mg:ton".indexOf(value.weightUnitNm) < 0) {
					normalYn = 'N';
					errorMsg += '중량단위;';
				}
				if("cm³:m³:L:ml".indexOf(value.volumeUnitNm) < 0) {
					normalYn = 'N';
					errorMsg += '부피단위;';
				}
				if("cm:m:mm:km:inch:ft:yd".indexOf(value.widthUnitNm) < 0) {
					normalYn = 'N';
					errorMsg += '가로단위;';
				}
				if("cm:m:mm:km:inch:ft:yd".indexOf(value.heightUnitNm) < 0) {
					normalYn = 'N';
					errorMsg += '세로단위;';
				}
				if("cm:m:mm:km:inch:ft:yd".indexOf(value.highUnitNm) < 0) {
					normalYn = 'N';
					errorMsg += '높이단위;';
				}
				if("Y:N".indexOf(value.expryYn) < 0) {
					normalYn = 'N';
					errorMsg += '유통기한여부;';
				}
				if("Y:N".indexOf(value.rtnPssblYn) < 0) {
					normalYn = 'N';
					errorMsg += '반품가능여부;';
				}
				if("실물반품:폐기반품".indexOf(value.rtnDvsnNm) < 0) {
					normalYn = 'N';
					errorMsg += '반품구분;';
				}
								
				// 행추가
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('addRow', {position: 'last'});
				var rids = $(STINFO04010_UPLOAD_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'viewCd', 'C');					
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'normalYn', normalYn); // 정상여부
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'errorMsg', errorMsg); 
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'itemNm', value.itemNm);				
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'itemTypeNm', value.itemTypeNm);				
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'impDvsnNm', value.impDvsnNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'podrPssblDvsnNm', value.podrPssblDvsnNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'unitNm', value.unitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'pchItemYn', value.pchItemYn);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'saleItemYn', value.saleItemYn);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'weight', value.weight);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'weightUnitNm', value.weightUnitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'volume', value.volume);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'volumeUnitNm', value.volumeUnitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'width', value.width);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'widthUnitNm', value.widthUnitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'height', value.height);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'heightUnitNm', value.heightUnitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'high', value.high);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'highUnitNm', value.highUnitNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'expryYn', value.expryYn);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'expryPeriod', value.expryPeriod);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'rtnPssblYn', value.rtnPssblYn);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'rtnDvsnNm', value.rtnDvsnNm);	
				$(STINFO04010_UPLOAD_GRID_ID).jqGrid('setCell', last_row_id, 'barcode', value.barcode);	
		 	});
			$(STINFO04010_UPLOAD_GRID_EMPTY).hide();
							
		}
    }, 
	change: function() {
		// 파일선택
		$(document).on('change', '#ITP_FORM_STINFO04010_UPLOAD_EXCEL_UPLOAD_SELECT_FILE', function(obj) {						
			var input = obj.target;
		    var reader = new FileReader();
			var rABS = !!reader.readAsBinaryString;
		    reader.onload = function(){
		        var fileData = reader.result;
		        var wb = XLSX.read(fileData, {type : rABS ? 'binary' : 'array'});
		        var sheetNameList = wb.SheetNames; // 시트 이름 목록 가져오기 
		        var firstSheetName = sheetNameList[0]; // 첫번째 시트명
		        var firstSheet = wb.Sheets[firstSheetName]; // 첫번째 시트 
		        var rowObj =XLSX.utils.sheet_to_json(firstSheet, {header:"A", defval: ""});

				console.log(JSON.stringify(rowObj));
				// console.log(rowObj.length);
				
				$('#ITP_FORM_STINFO04010_UPLOAD_EXCEL_UPLOAD_SELECT_FILE').val('');
				if(rowObj.length < 3) {
					itp_fn_modal_alert_ajax('엑셀파일에 품목 데이터를 기술하세요.');
					return;
				} else if(rowObj.length > 1001) {
					itp_fn_modal_alert_ajax('엑셀 일괄처리는 최대 1000건까지 가능합니다.');
					return;
				}				
				
				uploadObj.button.makeExcelDataList(rowObj);				
		    };
		    if(rABS) reader.readAsBinaryString(input.files[0]); 
			else reader.readAsArrayBuffer(input.files[0]);			
		});
	}, 
	grid: {
        init: function () {
            this.search();
        },
        colModel: gridColModel.upload,
        search: function() {
            pageObj.switchScreen('UPLOAD');
            this.unload();
            this.load();
        },
        load: function() {
            let param = $('#ITP_FORM_STINFO04010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.sbrandId = fn_make_user_info.get('brandId');
			param.uploadYn = "Y"; // 엑셀업로드 여부

            var option = {
                gridId: STINFO04010_UPLOAD_GRID_ID,
                colModel: gridColModel.upload,
                param: param,
                url: DOMAIN + API_DETAIL_GRID_LIST,
                pager: STINFO04010_UPLOAD_GRID_PAGER,
                onCellSelect: function(rowId, cellIdx, cellValue) {
					// STINFO04010_UPLOAD_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowId, cellIdx, STINFO04010_UPLOAD_GRID_LAST_ROW_ID, STINFO04010_UPLOAD_GRID_ID, ['itemId']);
                },
                loadComplete: function(data) {
                    // console.log(data);
                    itp_fn_grid_load_complete(data, STINFO04010_UPLOAD_GRID_ID, true, 'number', 'STINFO04010', listObj.itp_STINFO04010_search, listObj.empty, true, data.gridRecords, true);                    
                },
                onPaging: function(action) {
                    itp_fn_grid_paging(STINFO04010_UPLOAD_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
        },
        clearData : function() {
            $(STINFO04010_UPLOAD_GRID_ID).jqGrid('clearGridData', true);
            $(STINFO04010_UPLOAD_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(STINFO04010_UPLOAD_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(STINFO04010_UPLOAD_GRID_EMPTY).show();
        },
        unload : function() {
            $.jgrid.gridUnload(STINFO04010_UPLOAD_GRID_ID);
        }
    }
}

/*신규화면 Object*/
let createObj = {
    init: function () {
		pageObj.switchScreen('ADD');
		this.load();
        pageObj.fileUpload();
        itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO04010_DETAIL');
        this.button.init();
        itemClassLevel.make(1, 'ADD');
    },
	load: function() {
		// console.log("====================");
		var param = {keyType:"It"};
        fn_ajax_call(API_GET_KEY, param, this.callback, 'GET');
    },
	callback: function (result) {
		$('.fnBrandId').text(result.keyData);	
		$('#ITP_FORM_STINFO04010_DETAIL .fnItemClassMgntNo').text(result.keyData);	
		$('#ITP_FORM_STINFO04010_DETAIL #ITP_FORM_STINFO04010_DETAIL_ITEM_ID').val(result.keyData);
		
	},
    button: {
        init: function() {
            $('#ITP_FORM_STINFO04010_DETAIL_ITEM_CLASS1').off('change').on('change', function() {
                ($(this).val() === '') ? itemClassLevel.clear(2): itemClassLevel.change(2);
            });
            $('#ITP_FORM_STINFO04010_DETAIL_ITEM_CLASS2').off('change').on('change', function() {
                ($(this).val() === '') ? itemClassLevel.clear(3): itemClassLevel.change(3);
            });
            $('#ITP_FORM_STINFO04010_DETAIL_ITEM_CLASS3').off('change').on('change', function() {
                ($(this).val() === '') ? itemClassLevel.clear(4): itemClassLevel.change(4);
            });
        },
        cancel: function() {
        	let childrens = $('#ITP_FORM_STINFO04010_VIEW_FILE').children();
        	childrens.remove();
            listObj.grid.search();
        },
        save: function() {
			if(SCREEN_MODE == 'UPLOAD') { // 엑셀 일괄저장
				// 그리드 체크
				var reccount = $(STINFO04010_UPLOAD_GRID_ID).getGridParam("reccount");
				if(reccount < 1) {
					return;
				}
				let check = true;
				const rowData = $(STINFO04010_UPLOAD_GRID_ID).getRowData();
				$.each(rowData, function(key, value) {
					if(check) {
						if(value.normalYn == 'N') {
							check = false;
						}							
					}							
				});
				if(!check) {
					itp_fn_modal_alert_ajax('비정상 데이터가 포함되어 있습니다.');
					return;
				}
				
				var modalFn = {
					callBack: () => {						
						const formId = '#ITP_FORM_STINFO04010_UPLOAD';						
						
						let gridInsertData = [];						
						$.each(rowData, function(key, value) {
							gridInsertData.push(value);
						});
						
						let param = $(formId).serializeObject();
						param.list = gridInsertData;
						
						var searhFn = function() {
		                    ITP_FORM_STINFO04010_DETAIL_IS_WRITING = false;
		                    listObj.grid.search();
		                };
		                fn_ajax_call(API_DETAIL_SAVE_LIST, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('저장 하시겠습니까?', modalFn);				
				
			} else {
				const formId = '#ITP_FORM_STINFO04010_DETAIL';
	            itp_fn_form_clear_validate(null, formId);
	
	            if (itp_fn_form_event.isValid(formId)) {
	                let param = $(formId).serializeObject();
	                // console.log(JSON.stringify(param));
	                var searhFn = function() {
	                    ITP_FORM_STINFO04010_DETAIL_IS_WRITING = false;
	                    listObj.grid.search();
	                };
	                fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
	            }	
			}
        }
    }
};

var itemClassLevel = {
    level: 1,
    levelID: '',
    data: [],
    keys: function() {
        var keys = {sBrandId:fn_make_user_info.get('brandId'), sItemLevel: this.level};
        for(var i=1; i<=this.level; i++) {
            var key = 'sItemClass' + i;
            var id = '#' + this.levelID + i;
            keys[key] = $(id + ' option:selected').val();
        }
        return keys;
    },
    make: function(level, mode) {
        this.level = level;
        this.levelID = (mode === 'LIST' ? 'ITP_FORM_STINFO04010_SEARCH_ITEM_CLASS' : 'ITP_FORM_STINFO04010_DETAIL_ITEM_CLASS');
        if(mode !== 'MODIFY') {
            this.data.length = 0;
            this.clear(1);
        }
        this.call($.param(this.keys()), mode === 'MODIFY');
    },
    change: function(level) {
        this.level = level;
        this.clear(level);
        this.call($.param(this.keys()), false);
    },
    call: function(param, isModify) {
        var _this = this;
        var callbackFn = function(result) {
            var id = _this.levelID + _this.level;
            fn_make_select_item_class(result.searchList, id, true, _this.emptyText());
            if(isModify) {
                $('#' + id).val(_this.data[ _this.level - 1]);
                if(_this.level < _this.data.length) {
                    if(_this.data[_this.level] !== '') {
                        _this.level++;
                        _this.call( $.param(_this.keys()), isModify);
                    }
                }
            }
        };
        fn_ajax_call(API_ITEM_CLASS_LEVEL, param, callbackFn, 'GET');
    },
    emptyText: function() {
        return '품목분류Level' + this.level;
    },
    clear: function(level) {
        for(var i=level; i<5; i++) {
            $('#' + (this.levelID + i)).find('option:not(:first)').remove();
        }
    }
};


function fileUploadInfo4010() {
	//파일업로드(등록/수정화면)
	var uid='#ITP_FORM_STINFO04010_VIEW_FILE';
	var fid='#ITP_FORM_STINFO04010_VIEW_FILE_NO'; 
    console.log("xx file no xxx" ,$(fid).val())
	$(uid).empty();  
	$(uid).bootstrapFileUpload({
		url: DOMAIN + '/api/file/upload',
		inputName: 'uploadFile',
		folderName: 'affl',
		fileNumber:  $(fid).val(),
		fileInput: fid,
		fileTypes: ITP_UPLOAD_DEFAULTS.fileTypes,
		viewCode: 'R',
		formMethod: 'post',
		multiFile: true,
		multiUpload: false, 
		maxFiles: 5,
		showThumb: true,
		thumbWidth: 80,
  		thumbHeight: 80,
		debug: false, 
		screen_id: 'ITP_STINFO04010_VIEW_FILE',
		showYn: false,
		onInit: function() {			 
		}
	});
    setTimeout(function() {
        $('#ITP_STINFO04010_VIEW_FILE_UPLOAD_FORM').hide();
        $(uid).css("width", "100%");
        // $(uid+' .btn').hide();
    },20);
}