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

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

/*화면 변수*/
const OPER06010_GRID_ID = '#ITP_OPER06010_jqGrid';
const OPER06010_GRID_LIST = '#ITP_OPER06010_jqGrid_list';
const OPER06010_GRID_PAGER = '#ITP_OPER06010_jqGridPager';
const OPER06010_GRID_EMPTY = '#ITP_OPER06010_jqGridEmpty';

const OPER06010_VIEW_GRID_ID = '#ITP_OPER06010_VIEW_jqGrid';
const OPER06010_VIEW_GRID_LIST = '#ITP_OPER06010_VIEW_jqGrid_list';
const OPER06010_VIEW_GRID_PAGER = '#ITP_OPER06010_VIEW_jqGridPager';
const OPER06010_VIEW_GRID_EMPTY = '#ITP_OPER06010_VIEW_jqGridEmpty';

const OPER06010_DETAIL_GRID_ID = '#ITP_OPER06010_DETAIL_jqGrid';
const OPER06010_DETAIL_GRID_LIST = '#ITP_OPER06010_DETAIL_jqGrid_list';
const OPER06010_DETAIL_GRID_PAGER = '#ITP_OPER06010_DETAIL_jqGridPager';
const OPER06010_DETAIL_GRID_EMPTY = '#ITP_OPER06010_DETAIL_jqGridEmpty';

/*API URL*/
let API_DETAIL_GRID_LIST    = '/api/brand/detail-grid-list';
let API_DETAIL_INFO         = '/api/brand/info-brand';
let API_DETAIL_SAVE         = '/api/brand/save-brand';
let BRAND_POP_GRID_LIST 	= '/api/brand/pop-grid-list';
let API_GET_KEY			 	= '/api/session/get-key';

let ITP_FORM_OPER06010_DETAIL_IS_WRITING = false;

/*화면 Grid Object*/
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: 'AFFL_SHOP_ID', name: 'afflShopId',
            label: ITP_MSG_LOCALE.label.afflShopId,
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: true
        },
        {
            index: 'AFFL_SHOP_NM', name: 'afflShopNm',
            label: ITP_MSG_LOCALE.label.afflShopNm, //브랜드그룹
            width: '10', fixed: false, align: 'left',
            sortable: false, hidden: false, classes: 'ui-ellipsis'
        },
        {
            index: 'BRAND_ID', name: 'brandId',
            label: ITP_MSG_LOCALE.label.brandId,
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'BRAND_NM', name: 'brandNm',
            label: ITP_MSG_LOCALE.label.brandNm, //브랜드명
            width: '13', fixed: false, align: 'left',
            sortable: false, hidden: false, classes: 'ui-ellipsis'
        },
        {
            index: 'ST_NM', name: 'stNm',
            label: ITP_MSG_LOCALE.label.stNm, //상태
            width: '10', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'BRAND_TYPE_NM', name: 'brandTypeNm',
            label: ITP_MSG_LOCALE.label.brandTypeNm, //브랜드 타입
            width: '13', fixed: false, align: 'center',
            sortable: false, hidden: false
        },
        {
            index: 'REG_DT', name: 'regDt',
            label: ITP_MSG_LOCALE.label.regDt, //등록일
            width: '10', 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('OPER06010');

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

            itp_fn_form_event.onKeyup('#ITP_FORM_OPER06010_DETAIL');

            // form alert message 주입
            $('input[data-alert-required="ONLYCHK"]').each(function() {
                if($(this).data('alert-msg') === 'address') {
                    $(this).data('alert-msg', ITP_MSG_LOCALE.message.form.emptyAddr);
                }
            });

            itp_fn_form_event.onKeyup('#ITP_FORM_OPER03010_DETAIL');
            //pg사아이디 선택시 보이게함
            $('#ITP_FORM_OPER06010_DETAIL_PG_ID').on('change', function() {
                if($(this).val() === '') {
                    $('#ITP_FORM_OPER06010_DETAIL .itp_form_pg').hide();
                } else {
                    $('#ITP_FORM_OPER06010_DETAIL .itp_form_pg').show();
                }
            });
        },
        grid: function() { 
        },
        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_OPER06010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
                        case 'ITP_BTN_OPER06010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
                        case 'ITP_BTN_OPER06010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
                        case 'ITP_BTN_OPER06010_CANCELLIST'			: createObj.button.cancel(); 	break;	// 취소/목록 버튼
                        case 'ITP_BTN_OPER06010_DELETE'				: 								break;	// 삭제 버튼
                        case 'ITP_BTN_OPER06010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
                        case 'ITP_FORM_OPER06010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
                        case 'ITP_BTN_OPER06010_RESET_PW'			: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
                        case 'ITP_FORM_OPER06010_DETAIL_ADRES_SCH'	: createObj.button.address();	break;	// 신규등록 주소 찾기
                        case 'ITP_BTN_OPER06010_DETAIL_ADDROW'		: afflShopObj.button.addRow();	break;	// 상세:추가버튼
                        case 'ITP_BTN_OPER06010_DETAIL_DELROW'		: afflShopObj.button.delRow();	break;	// 상세:삭제버튼
                    }
                });
            });
        }
    },
	action: function () {
	        var _this = this;
	        // 브랜드그룹 조회 버튼 클릭
			$('#ITP_BTN_OPER06010_SEARCH_AFFL_SHOP').on('click', function() {
				console.log("ITP_BTN_OPER06010_SEARCH_AFFL_SHOP click");
	            _this.popup('L');
	        });
	        // 브랜드그룹 텍스트 삭제 버튼 클릭
	        $('#ITP_BTN_OPER06010_DELETE_AFFL_SHOP').on('click', function() {
	            $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_ID').val('');
	            $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_NM').val('');
	            $('select#ITP_FORM_OPER06010_SEARCH_BRAND_ID option').remove();
	        });
	},
	popup: function (view) {
        // 팝업
        var popFn = function(rowDataPop) {
            if(rowDataPop.AFFL_SHOP) {
                $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
                $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
            }
            if(rowDataPop.AFFL_BRAND) {
                fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_OPER06010_SEARCH_BRAND_ID', true, '선택');
            }
        };
        fn_call_popup('biz', 'BIZPOP_AFFL_SHOP', '#ITP_ASIDE', popFn, null, 'S');
    },
    fileUpload: function() {
        //파일업로드(수정화면)
        var id = 'OPER06010';
        $('#ITP_' + id + '_DETAIL_UPLOAD').empty();
        $('#ITP_' + id + '_DETAIL_LOGO_FILE').empty();
        var removeFn = (param) => {
            $('#ITP_' + id + '_DETAIL_LOGO_FILE').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', 'brand', false, false, id, fileArgs, removeFn);
    },
    switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_OPER06010_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_OPER06010').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
            itp_fn_fire_window_resize();
            fn_show_btn_auth_array(['#ITP_BTN_OPER06010_SRH', '#ITP_BTN_OPER06010_NEWREG']);
            $('#ITP_AJAX_OPER06010_LIST_CONTAINER').show();
            
        } else if(mode == 'ADD') { // 등록
            $('#ITP_AJAX_OPER06010_DETAIL_CONTAINER').show();
            
			
            fn_show_btn_auth_array(['#ITP_BTN_OPER06010_CANCELLIST', '#ITP_BTN_OPER06010_SAVE']);

            
            $('#ITP_FORM_OPER06010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
                $(this).attr('id') === 'ITP_FORM_OPER06010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
                if(elem.type === 'select') {
                    $(this).val('').prop('selected', true);
                }
            });
            $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_FILE_NO').val('');
            $('#ITP_FORM_OPER06010_DETAIL_BRAND_ID').removeAttr('readonly');
            $('#ITP_FORM_OPER06010_DETAIL_BRAND_NM').removeAttr('readonly');
            $('#ITP_FORM_OPER06010_DETAIL_BSNS_REG_NO').removeAttr('readonly');
            $('#ITP_FORM_OPER06010_DETAIL_CORP_REG_NO').removeAttr('readonly');
            // $('#ITP_FORM_OPER06010_DETAIL_SEARCH_AFFL_SHOP').removeAttr('disabled');
            // $('#ITP_FORM_OPER06010_DETAIL_DELETE_AFFL_SHOP').removeAttr('disabled');
            $('#ITP_FORM_OPER06010_DETAIL_SEARCH_AFFL_SHOP').show();
            $('#ITP_FORM_OPER06010_DETAIL_DELETE_AFFL_SHOP').show();
			$('#ITP_FORM_OPER06010_DETAIL .itp_form_info').hide();
			$('#ITP_FORM_OPER06010_DETAIL .itp_form_change').show();

            $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
            $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));

            $('.fnBrandId').text('(자동부여)');

            $('#ITP_FORM_OPER06010_DETAIL_PG_ID').trigger('change');
        } else if(mode == 'MODIFY') { // 수정
            $('#ITP_AJAX_OPER06010_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_OPER06010_CANCELLIST', '#ITP_BTN_OPER06010_SAVE']);

            $('#ITP_FORM_OPER06010_DETAIL_BRAND_ID').attr('readonly', true);
            $('#ITP_FORM_OPER06010_DETAIL_BRAND_NM').attr('readonly', true);
            $('#ITP_FORM_OPER06010_DETAIL_BSNS_REG_NO').attr('readonly', true);
            $('#ITP_FORM_OPER06010_DETAIL_CORP_REG_NO').attr('readonly', true);
            // $('#ITP_FORM_OPER06010_DETAIL_SEARCH_AFFL_SHOP').attr('disabled', true);
            // $('#ITP_FORM_OPER06010_DETAIL_DELETE_AFFL_SHOP').attr('disabled', true);
            $('#ITP_FORM_OPER06010_DETAIL_SEARCH_AFFL_SHOP').hide();
            $('#ITP_FORM_OPER06010_DETAIL_DELETE_AFFL_SHOP').hide();
			$('#ITP_FORM_OPER06010_DETAIL .itp_form_info').show();
			$('#ITP_FORM_OPER06010_DETAIL .itp_form_change').hide();

            $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
        } else if(mode == 'VIEW') { // 보기
            $('#ITP_AJAX_OPER06010_VIEW_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_OPER06010_MODIFY', '#ITP_BTN_OPER06010_CANCELLIST']);
        }
    }
};

/*목록화면 Object*/
let listObj = {
    init: function () {
        afflShopObj.init();
        this.grid.init();
    },
    itp_OPER06010_param: {},
    itp_OPER06010_search: false,
    button: {
        search: function() {
            listObj.itp_OPER06010_search = true;
            let param = $('#ITP_FORM_OPER06010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;
            param.gridPage = $.jgrid.defaults.page;
            console.log(JSON.stringify(param));
            $(OPER06010_GRID_ID).data('grid-param',param);
            $(OPER06010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
        },
        create: function() {
			console.log(fn_make_user_info.get('authTpCd'));
			if (fn_make_user_info.get('authTpCd') != '30') {
				itp_fn_modal_alert('브랜드 그룹담당자만 신규등록 가능합니다.');
				return;
			}
			
            if($('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val() === '') {
                itp_fn_modal_alert('브랜드그룹 정보가 존재하지 않습니다.');
                return;
            }
            pageObj.switchScreen('ADD');
            itp_fn_form_clear_validate(null, '#ITP_FORM_OPER06010_DETAIL');
            // 권한정보 리스트 조회
            createObj.init();
        }
    },
    empty: {
        init: function() {
        },
        push: function() {
        },
        back: function() {
        }
    },
    grid: {
        init: function () {
            // 데이터 없을때
            itp_fn_jqgrid_resize(OPER06010_GRID_ID, OPER06010_GRID_LIST, 'lg');
            this.search();
        },
        colModel: gridColModel.list,
        search: function() {
            pageObj.switchScreen('LIST');
            this.unload();
            this.load();
        },
        load: function() {
            let param = $('#ITP_FORM_OPER06010_SEARCH').serializeObject();
            param.gridSize = $.jgrid.defaults.rowNum;

            var option = {
                gridId: OPER06010_GRID_ID,
                colModel: gridColModel.list,
                param: param,
                url: DOMAIN + API_DETAIL_GRID_LIST,
                pager: OPER06010_GRID_PAGER,
                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, OPER06010_GRID_ID, true, 'number', 'OPER06010', listObj.itp_OPER06010_search, listObj.empty, true, data.gridRecords, true);
                    var ids = $(OPER06010_GRID_ID).getDataIDs();
                    $.each(ids, function(idx, rowId) {
                        $(OPER06010_GRID_ID).jqGrid('setCell', rowId, 'brandId', '', ITP_GRID_COL_STYLE.link);
                    });
                },
                onPaging: function(action) {
                    itp_fn_grid_paging(OPER06010_GRID_ID, action, param);
                }
            };
            itp_fn_grid_make_remote(option);
        },
        clearData : function() {
            $(OPER06010_GRID_ID).jqGrid('clearGridData', true);
            $(OPER06010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
            $(OPER06010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
            $(OPER06010_GRID_EMPTY).show();
        },
        unload : function() {
            $.jgrid.gridUnload(OPER06010_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_OPER06010_VIEW #ITP_FORM_OPER06010_VIEW_AFFL_SHOP_ID').val(result.afflShopId);
        $('#ITP_FORM_OPER06010_VIEW #ITP_FORM_OPER06010_VIEW_BRAND_ID').val(result.brandId);
        $('#ITP_FORM_OPER06010_VIEW .fnBrandUnitUnqNo').text(result.brandId);
        $('#ITP_FORM_OPER06010_VIEW .fnStCd').text(result.stNm);
        $('#ITP_FORM_OPER06010_VIEW .fnBrandNm').text(result.brandNm);
        $('#ITP_FORM_OPER06010_VIEW .fnBrandType').text(result.brandTypeNm);
        $('#ITP_FORM_OPER06010_VIEW .fnOwnerNm').text(result.ownerNm);
        $('#ITP_FORM_OPER06010_VIEW .fnTelNo').text(result.telNo);
        $('#ITP_FORM_OPER06010_VIEW .fnFaxNo').text(result.faxNo);
        $('#ITP_FORM_OPER06010_VIEW .fnEmalAddr').text(result.emalAddr);
        $('#ITP_FORM_OPER06010_VIEW .fnDpstBnkCd').text(result.bnkCdNm);
        $('#ITP_FORM_OPER06010_VIEW .fnRcvAcctNo').text(result.acctNo);
        $('#ITP_FORM_OPER06010_VIEW #ITP_FORM_OPER06010_VIEW_DPST_BNK_CD').val(result.bnkCd);
        let bnkCdNm = $('#ITP_FORM_OPER06010_VIEW_DPST_BNK_CD option:selected').text();
        $('#ITP_FORM_OPER06010_VIEW .fnBnkCdNm').text(bnkCdNm);
        $('#ITP_FORM_OPER06010_VIEW .fnAcctNo').text(result.acctNo);
        $('#ITP_FORM_OPER06010_VIEW .fnOwacNm').text(result.owacNm);
        $('#ITP_FORM_OPER06010_VIEW .fnZipNo').text(result.zipNo);
        $('#ITP_FORM_OPER06010_VIEW .fnLoadAddr').text(result.addr1);
        $('#ITP_FORM_OPER06010_VIEW .fnDtlAddr').text(result.addr2);
        $('#ITP_FORM_OPER06010_VIEW .fnRegId').text(result.sysRegId);
        $('#ITP_FORM_OPER06010_VIEW .fnRegDt').text(result.sysRegDttm);
        $('#ITP_FORM_OPER06010_VIEW .fnChgId').text(result.sysChgId);
        $('#ITP_FORM_OPER06010_VIEW .fnChgDt').text(result.sysChgDttm);

		$('#ITP_FORM_OPER06010_VIEW .fnBsnsRegNo').text(result.bsnsRegNo);
		$('#ITP_FORM_OPER06010_VIEW .fnCorpRegNo').text(result.corpRegNo);
		$('#ITP_FORM_OPER06010_VIEW .fnOqBrandId').text(result.oqBrandId);
		
        // $('#ITP_FORM_OPER06010_VIEW .fnBrandLogo').text(result.lastLoginDt);

        $('#ITP_FORM_OPER06010_VIEW .fnPgId').text(result.pgId);
        $('#ITP_FORM_OPER06010_VIEW .fnPgAfflId').text(result.pgAfflId);
        $('#ITP_FORM_OPER06010_VIEW .fnPgKey').text(result.pgKey);

        pageObj.fileUpload();
        /*
        itp_fn_attach_file.view('ITP_OPER06010_VIEW_LOGO_FILE', result.fileLogNoList
            , 'form-control itp_input', 'border:none; top:12px; left:12px; height:100px; width:125px; position:absolute;');
        */
        fileUploadOper6010(result.fileLogNoList);
        let locate = result.addr1 + ' ' + result.addr2;
        kakaoMap.init('ITP_FORM_OPER06010_VIEW', locate, result.latX, result.longY);

    }
};


function fileUploadOper6010(arr) {
	//파일업로드(등록/수정화면)
	var uid='#ITP_OPER06010_VIEW_LOGO_FILE'; 
    $(uid).empty();
	if(Array.isArray(arr) && arr.length ) {
		var sid='ITP_OPER06010_LOGO';
		var fileNo=arr[0].fileNo;
		$(uid).bootstrapFileUpload({
			url: DOMAIN + '/api/file/upload',
			inputName: 'uploadFile',
			folderName: 'affl',
			fileNumber:  fileNo,
			fileInput: null,
			fileTypes: ITP_UPLOAD_DEFAULTS.fileTypes,
			viewCode: 'R',
			formMethod: 'post',
			multiFile: true,
			multiUpload: false,
			maxSize: 100,
			maxFiles: 5,
			showThumb: true,
			thumbWidth: 80,
			thumbHeight: 80,
			debug: true, 
			screen_id: sid,
			showYn: false 
		});
		setTimeout(function() {
			$('#'+sid+'_UPLOAD_FORM').hide();
		},20);
	}
}

/*수정화면 Object*/
let modifyObj = {
    button: {
        modify: function () {
            itp_fn_form_clear_validate(null, '#ITP_FORM_OPER06010_DETAIL');
            const brandIdVal = $('#ITP_FORM_OPER06010_VIEW_BRAND_ID').val();
            const key = {brandId: brandIdVal, 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_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_VIEW_CD').val('U');

        $('#ITP_FORM_OPER06010_DETAIL .fnAfflShopId').text(result.fnAfflShopId);
        $('#ITP_FORM_OPER06010_DETAIL .fnBrandId').text(result.brandId);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val(result.afflShopId);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_NM').val(result.afflShopNm);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_ID').val(result.brandId);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_ST_CD').val(result.stCd);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_NM').val(result.brandNm);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_TYPE').val(result.brandType);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BSNS_REG_NO').val(result.bsnsRegNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_CORP_REG_NO').val(result.corpRegNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_OWNER_NM').val(result.ownerNm);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_TEL_NO').val(result.telNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_FAX_NO').val(result.faxNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_EMAL_ADDR').val(result.emalAddr);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_DPST_BNK_CD').val(result.bnkCd);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_RCV_ACCT_NO').val(result.acctNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_OWAC_NM').val(result.owacNm);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_ZIPNO').val(result.zipNo);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_ADRES').val(result.addr1);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_ADRES_DTL').val(result.addr2);

        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_PG_ID').val(result.pgId);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_PG_AFFL_ID').val(result.pgAfflId);
        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_PG_KEY').val(result.pgKey);

		$('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_OQ_BRAND_ID').val(result.oqBrandId);

		$('#ITP_FORM_OPER06010_DETAIL .fnRegId').text(result.sysRegId);
        $('#ITP_FORM_OPER06010_DETAIL .fnRegDt').text(result.sysRegDttm);
        $('#ITP_FORM_OPER06010_DETAIL .fnChgId').text(result.sysChgId);
        $('#ITP_FORM_OPER06010_DETAIL .fnChgDt').text(result.sysChgDttm);

        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_PG_ID').trigger('change');

        $('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_FILE_NO').val(result.brandLogoFileNo);
        pageObj.fileUpload();
        itp_fn_attach_file.view('ITP_OPER06010_DETAIL_LOGO_FILE', result.fileLogNoList
            , 'form-control itp_input', 'border:none; top:12px; left:12px; height:100px; width:125px; position:absolute;');

        let locate = result.addr1 + ' ' + result.addr2;
        kakaoMap.init('ITP_FORM_OPER06010_DETAIL', locate, result.latX, result.longY);

    }
};

/*신규화면 Object*/
let createObj = {
    init: function () {
		this.load();
        this.button.init();
        pageObj.fileUpload();
        kakaoMap.init('ITP_FORM_OPER06010_DETAIL', '');
    },
	load: function() {
		var param = {keyType:"Br"};
        fn_ajax_call(API_GET_KEY, param, this.callback, 'GET');
    },
	callback: function (result) {
		$('.fnBrandId').text(result.keyData);		
		$('#ITP_FORM_OPER06010_DETAIL #ITP_FORM_OPER06010_DETAIL_BRAND_ID ').val(result.keyData);
		$('#ITP_FORM_OPER06010_DETAIL_PG_ID').on('change', function() {
			console.log(this.value == 'KICC');
			if (this.value == 'KICC') {
				$('#ITP_FORM_OPER06010_DETAIL_PG_AFFL_ID').attr('readonly', false);
			} else {
				$('#ITP_FORM_OPER06010_DETAIL_PG_AFFL_ID').attr('readonly', true);
			}
		})
	},
    button: {
        init: function() {
            // 브랜드그룹 조회 버튼 클릭
            $('#ITP_FORM_OPER06010_DETAIL_SEARCH_AFFL_SHOP').one('click', function() {
                createObj.popup();
            });
            // 브랜드그룹 텍스트 삭제 버튼 클릭
            $('#ITP_FORM_OPER06010_DETAIL_DELETE_AFFL_SHOP').one('click', function() {
                $('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val('');
                $('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_NM').val('');
            });
        },
        address: function() {
            kakaoMap.address('ITP_FORM_OPER06010_DETAIL');
        },
        save: function() {
            const formId = '#ITP_FORM_OPER06010_DETAIL';
            itp_fn_form_event.removeChar('#ITP_FORM_OPER06010_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_OPER06010_DETAIL_IS_WRITING = false;
                    listObj.grid.search();
                };
                fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
            }
        },
        cancel: function () {
            listObj.grid.search();
        }
    },
    popup: function() {
        var popFn = function(rowDataPop) {
            // console.log(rowDataPop);
            if(rowDataPop.AFFL_SHOP) {
                $('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
                $('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
            }
        };
        fn_call_popup('biz', 'BIZPOP_AFFL_SHOP', '#ITP_ASIDE', popFn, null, 'S');
    }
};

/*브랜드그룹 Object*/
let afflShopObj = {
    init: function () {
		console.log(fn_make_user_info.get('authTpCd'));
        if(fn_make_user_info.get('authTpCd') === '30') {
			this.afflInfo.setAfflShop();
        }
    },
    afflInfo: {
        setAfflShop: function() {
            var _this = this;
            this.afflShopId 	= fn_make_user_info.get('afflShopId');
            this.afflShopNm 	= fn_make_user_info.get('afflShopNm');
            $('#ITP_FORM_OPER06010_DETAIL_AFFL_SHOP_ID').val(_this.afflShopId);
            $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_ID').val(_this.afflShopId);
            $('#ITP_FORM_OPER06010_SEARCH_AFFL_SHOP_NM').val(_this.afflShopNm);
            $('#ITP_BTN_OPER06010_SEARCH_AFFL_SHOP').attr('disabled', true);
            $('#ITP_BTN_OPER06010_DELETE_AFFL_SHOP').attr('disabled', true);

            var callbackFn = (result) => {
                var brandList = [];
                $.each(result.gridRows, function(i, item) {
                    brandList.push({'brandId': item.brandId, 'brandNm': item.brandNm});
                });
                fn_make_select_brand(brandList, 'ITP_FORM_OPER06010_SEARCH_BRAND_ID', true, '선택');
            };
            var param = {'afflShopId': _this.afflShopId};
            fn_ajax_call(BRAND_POP_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
        }
    }
};
