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			*
 *******************************************************/

/*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 ITP_FORM_STINFO01011_DETAIL_IS_WRITING = false;

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

            // 공통코드 표시
            $('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_STINFO01011_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);
                }
            });
        },
        ready: function() {
            viewObj.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_STINFO01011_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
                        case 'ITP_BTN_STINFO01011_CANCELLIST'			: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
                        case 'ITP_BTN_STINFO01011_SAVE'				    : modifyObj.button.save(); 		break;	// 저장 버튼
                        case 'ITP_FORM_STINFO01011_DETAIL_ADRES_SCH'	: modifyObj.button.address();	break;	// 신규등록 주소 찾기
                    }
                });
            });
        }
    },
    fileUpload: function() {
        //파일업로드(수정화면)
        var id = 'STINFO01011';
        $('#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', true, false, id, fileArgs, removeFn);
    },
    switchScreen: function(mode) {
        $('.itp_det_head').find('button[id^="ITP_BTN_STINFO01011_"]').each(function(i) {
            $(this).hide();
        });
        $('#ITP_TAB_STINFO01011').find('div[id$="_CONTAINER"]').each(function(i) {
            $(this).hide();
        });
        if(mode == 'LIST') { // 목록
            fn_show_btn_auth_array(['#ITP_BTN_STINFO01011_SRH', '#ITP_BTN_STINFO01011_NEWREG']);
            $('#ITP_AJAX_STINFO01011_LIST_CONTAINER').show();
        } else if(mode == 'ADD') { // 등록
            $('#ITP_AJAX_STINFO01011_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO01011_CANCELLIST', '#ITP_BTN_STINFO01011_SAVE']);

            $('#ITP_FORM_STINFO01011_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
                $(this).attr('id') === 'ITP_FORM_STINFO01011_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
                if(elem.type === 'select') {
                    $(this).val('').prop('selected', true);
                }
            });
            $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_FILE_NO').val('');
            $('#ITP_FORM_STINFO01011_DETAIL_BRAND_ID').removeAttr('readonly');
            $('#ITP_FORM_STINFO01011_DETAIL_BRAND_NM').removeAttr('readonly');
            $('#ITP_FORM_STINFO01011_DETAIL_BSNS_REG_NO').removeAttr('readonly');
            $('#ITP_FORM_STINFO01011_DETAIL_CORP_REG_NO').removeAttr('readonly');
            // $('#ITP_FORM_STINFO01011_DETAIL_SEARCH_AFFL_SHOP').removeAttr('disabled');
            // $('#ITP_FORM_STINFO01011_DETAIL_DELETE_AFFL_SHOP').removeAttr('disabled');
            $('#ITP_FORM_STINFO01011_DETAIL_SEARCH_AFFL_SHOP').show();
            $('#ITP_FORM_STINFO01011_DETAIL_DELETE_AFFL_SHOP').show();
            $('#ITP_FORM_STINFO01011_DETAIL .itp_form_info').hide();
            $('#ITP_FORM_STINFO01011_DETAIL .itp_form_change').show();

            $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));

            $('.fnBrandId').text('(자동부여)');
        } else if(mode == 'MODIFY') { // 수정
            $('#ITP_AJAX_STINFO01011_DETAIL_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO01011_CANCELLIST', '#ITP_BTN_STINFO01011_SAVE']);

            $('#ITP_FORM_STINFO01011_DETAIL_BRAND_ID').attr('readonly', true);
            $('#ITP_FORM_STINFO01011_DETAIL_BRAND_NM').attr('readonly', true);
            $('#ITP_FORM_STINFO01011_DETAIL_BSNS_REG_NO').attr('readonly', true);
            $('#ITP_FORM_STINFO01011_DETAIL_CORP_REG_NO').attr('readonly', true);
            // $('#ITP_FORM_STINFO01011_DETAIL_SEARCH_AFFL_SHOP').attr('disabled', true);
            // $('#ITP_FORM_STINFO01011_DETAIL_DELETE_AFFL_SHOP').attr('disabled', true);
            $('#ITP_FORM_STINFO01011_DETAIL_SEARCH_AFFL_SHOP').hide();
            $('#ITP_FORM_STINFO01011_DETAIL_DELETE_AFFL_SHOP').hide();
            $('#ITP_FORM_STINFO01011_DETAIL .itp_form_info').show();
            $('#ITP_FORM_STINFO01011_DETAIL .itp_form_change').hide();

            $('#ITP_BTN_STINFO01011_RESET_PW').show();
        } else if(mode == 'VIEW') { // 보기
            $('#ITP_AJAX_STINFO01011_VIEW_CONTAINER').show();
            fn_show_btn_auth_array(['#ITP_BTN_STINFO01011_MODIFY']);
        }
    }
};

/*상세화면 Object*/
let viewObj = {
    init: function (param) {
        var param = {brandId: fn_make_user_info.get('brandId'), viewCd: 'R'};
        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_STINFO01011_VIEW #ITP_FORM_STINFO01011_VIEW_AFFL_SHOP_ID').val(result.afflShopId);
        $('#ITP_FORM_STINFO01011_VIEW #ITP_FORM_STINFO01011_VIEW_BRAND_ID').val(result.brandId);
        $('#ITP_FORM_STINFO01011_VIEW .fnBrandUnitUnqNo').text(result.brandId);
        $('#ITP_FORM_STINFO01011_VIEW .fnStCd').text(result.stNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnBrandNm').text(result.brandNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnBrandType').text(result.brandTypeNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnOwnerNm').text(result.ownerNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnTelNo').text(result.telNo);
        $('#ITP_FORM_STINFO01011_VIEW .fnFaxNo').text(result.faxNo);
        $('#ITP_FORM_STINFO01011_VIEW .fnEmalAddr').text(result.emalAddr);
        $('#ITP_FORM_STINFO01011_VIEW .fnDpstBnkCd').text(result.bnkCdNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnRcvAcctNo').text(result.acctNo);
        $('#ITP_FORM_STINFO01011_VIEW #ITP_FORM_STINFO01011_VIEW_DPST_BNK_CD').val(result.bnkCd);
        let bnkCdNm = $('#ITP_FORM_STINFO01011_VIEW_DPST_BNK_CD option:selected').text();        
        $('#ITP_FORM_STINFO01011_VIEW .fnBnkCd').text(bnkCdNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnAcctNo').text(result.acctNo);
        $('#ITP_FORM_STINFO01011_VIEW .fnOwacNm').text(result.owacNm);
        $('#ITP_FORM_STINFO01011_VIEW .fnZipNo').text(result.zipNo);
        $('#ITP_FORM_STINFO01011_VIEW .fnLoadAddr').text(result.addr1);
        $('#ITP_FORM_STINFO01011_VIEW .fnDtlAddr').text(result.addr2);
        $('#ITP_FORM_STINFO01011_VIEW .fnRegId').text(result.sysRegId);
        $('#ITP_FORM_STINFO01011_VIEW .fnRegDt').text(result.sysRegDttm);
        $('#ITP_FORM_STINFO01011_VIEW .fnChgId').text(result.sysChgId);
        $('#ITP_FORM_STINFO01011_VIEW .fnChgDt').text(result.sysChgDttm);
        
        $('#ITP_FORM_STINFO01011_VIEW .fnBsnsRegNo').text(result.bsnsRegNo);
		$('#ITP_FORM_STINFO01011_VIEW .fnCorpRegNo').text(result.corpRegNo);
		$('#ITP_FORM_STINFO01011_VIEW .fnOqBrandId').text(result.oqBrandId);

        // $('#ITP_FORM_STINFO01011_VIEW .fnBrandLogo').text(result.lastLoginDt);

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

        pageObj.fileUpload();
        itp_fn_attach_file.view('ITP_STINFO01011_VIEW_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_STINFO01011_VIEW', locate, result.latX, result.longY);

    }
};

/*수정화면 Object*/
let modifyObj = {
    button: {
        modify: function () {
            itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO01011_DETAIL');
            const brandIdVal = $('#ITP_FORM_STINFO01011_VIEW_BRAND_ID').val();
            const key = {brandId: brandIdVal, viewCd: 'R'};
            var param = $.param(key);
            modifyObj.load(param);
        },
        address: function() {
            kakaoMap.address('ITP_FORM_STINFO01011_DETAIL');
        },
        save: function() {
            const formId = '#ITP_FORM_STINFO01011_DETAIL';
            itp_fn_form_event.removeChar('#ITP_FORM_STINFO01011_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_STINFO01011_DETAIL_IS_WRITING = false;
                    viewObj.init();
                };
                fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
            }
        },
        cancel: function () {
            viewObj.init();
        }
    },
    load: function(param) {
        var param = {brandId: fn_make_user_info.get('brandId'), viewCd: 'R'};
        fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
    },
    callback: function (result) {
        console.log(result);
        pageObj.switchScreen('MODIFY');
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_VIEW_CD').val('U');

        $('#ITP_FORM_STINFO01011_DETAIL .fnAfflShopId').text(result.fnAfflShopId);
        $('#ITP_FORM_STINFO01011_DETAIL .fnBrandId').text(result.brandId);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_AFFL_SHOP_ID').val(result.afflShopId);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_AFFL_SHOP_NM').val(result.afflShopNm);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_BRAND_ID').val(result.brandId);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_ST_CD').val(result.stCd);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_BRAND_NM').val(result.brandNm);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_BRAND_TYPE').val(result.brandType);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_BSNS_REG_NO').val(result.bsnsRegNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_CORP_REG_NO').val(result.corpRegNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_OWNER_NM').val(result.ownerNm);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_TEL_NO').val(result.telNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_FAX_NO').val(result.faxNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_EMAL_ADDR').val(result.emalAddr);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_DPST_BNK_CD').val(result.bnkCd);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_RCV_ACCT_NO').val(result.acctNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_OWAC_NM').val(result.owacNm);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_ZIPNO').val(result.zipNo);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_ADRES').val(result.addr1);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_ADRES_DTL').val(result.addr2);

        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_PG_ID').val(result.pgId);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_PG_AFFL_ID').val(result.pgAfflId);
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_PG_KEY').val(result.pgKey);

        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_OQ_BRAND_ID').val(result.oqBrandId);
        
        $('#ITP_FORM_STINFO01011_DETAIL .fnRegId').text(result.sysRegId);
        $('#ITP_FORM_STINFO01011_DETAIL .fnRegDt').text(result.sysRegDttm);
        $('#ITP_FORM_STINFO01011_DETAIL .fnChgId').text(result.sysChgId);
        $('#ITP_FORM_STINFO01011_DETAIL .fnChgDt').text(result.sysChgDttm);

        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_PG_ID').trigger('change');
        
        $('#ITP_FORM_STINFO01011_DETAIL #ITP_FORM_STINFO01011_DETAIL_FILE_NO').val(result.brandLogoFileNo);
        pageObj.fileUpload();
        itp_fn_attach_file.view('ITP_STINFO01011_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_STINFO01011_DETAIL', locate, result.latX, result.longY);

    }
};

/*브랜드그룹 Object*/
let afflShopObj = {
    init: function () {
        this.action();
        if(fn_make_user_info.get('authTpCd') === '30') {
            this.afflInfo.setAfflShop();
        }
    },
    action: function () {
        var _this = this;
        // 브랜드그룹 조회 버튼 클릭
        $('#ITP_BTN_STINFO01011_SEARCH_AFFL_SHOP').one('click', function() {
            _this.popup('L');
        });
        // 브랜드그룹 텍스트 삭제 버튼 클릭
        $('#ITP_BTN_STINFO01011_DELETE_AFFL_SHOP').one('click', function() {
            $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_ID').val('');
            $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_NM').val('');
            $('select#ITP_FORM_STINFO01011_SEARCH_BRAND_ID option').remove();
        });
    },
    afflInfo: {
        setAfflShop: function() {
            var _this = this;
            this.afflShopId 	= fn_make_user_info.get('afflShopId');
            this.afflShopNm 	= fn_make_user_info.get('afflShopNm');
            $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_ID').val(_this.afflShopId);
            $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_NM').val(_this.afflShopNm);
            $('#ITP_BTN_STINFO01011_SEARCH_AFFL_SHOP').attr('disabled', true);
            $('#ITP_BTN_STINFO01011_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_STINFO01011_SEARCH_BRAND_ID', true, '선택');
            };
            var param = {'afflShopId': _this.afflShopId};
            fn_ajax_call(BRAND_POP_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
        }
    },
    popup: function (view) {
        // 팝업
        var popFn = function(rowDataPop) {
            // console.log(rowDataPop);
            if(rowDataPop.AFFL_SHOP) {
                $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
                $('#ITP_FORM_STINFO01011_SEARCH_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
            }
            if(rowDataPop.AFFL_BRAND) {
                fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_STINFO01011_SEARCH_BRAND_ID', true, '선택');
            }
        };
        fn_call_popup('biz', 'BIZPOP_AFFL_SHOP', '#ITP_ASIDE', popFn, null, 'S');
    }
};
