<template>
    <!-- <div>   
     <a href="#" @click="fnPopup()"> CheckPlus 안심본인인증 Click</a>
     <br>
     <a href="#" @click="fnPopupTest('s')"> CheckPlus 안심본인인증 테스트 Click(성공)</a>
     <br>
     <a href="#" @click="fnPopupTest('f')"> CheckPlus 안심본인인증 테스트 Click(실패)</a>
   </div>         -->
    <div class="gd-3">
        <button class="large-btn darkg-border-btn" @click="fnPopup()" title="본인인증" v-if="service == 'prov'">본인인증</button>     
        <!-- <button class="large-btn darkg-border-btn" @click="fnPopupTest('s')" v-if="service == 'prov'"> 본인인증 테스트</button> -->

        <button class="large-btn darkg-border-btn" @click="fnPopupTest('s')" v-if="service == 'devs'"> 본인인증</button>
        <button class="large-btn darkg-border-btn" @click="fnPopupTest('f')" v-if="service == 'devf'"> 본인인증</button>
    </div>
</template>

<script>

import axios from 'axios';

export default {
    props: {
        service: {
            type: String, // 문자열로 제한
            default: 'prov' // 필수로 넣어야함
        }
    },
    data() {
        return {
            niceData: { sEncData: '' },
            testData: '{"sName" : "홍길동", "sBirthDate" : "20200525", "sMobileNo" : "01054878878" , "sMobileCo" : "LGT" , "sErrorCode" : "" , "sDupInfo" : "중복확인 DI값(64Byte)"}',
            testDataf: '{"sName" : "홍길동", "sBirthDate" : "20200525", "sMobileNo" : "01054878878" , "sMobileCo" : "LGT" , "sErrorCode" : "1111", "sDupInfo" : "중복확인 DI값(64Byte)"}',
            windowRef: null,
        }
    },
    methods: {
        fnPopup: function () {
            let vm = this;
            axios({
                url: '/nice/getNiceData.json',
                method: 'post',
                hearder: {
                    'Content-Type': "application/json; charset=UTF-8",
                },
            }).then(function (response) {
                vm.niceData = response.data;
                vm.niceData.sEncData = response.data.sEncData;
                vm.windowRef = null;
                vm.windowRef = window.open('', 'popupChk', 'width=500, height=550, top=100, left=100, fullscreen=no, menubar=no, status=no, toolbar=no, titlebar=yes, location=no, scrollbar=no');

                let form = document.createElement("form");
                form.setAttribute("method", "POST");
                form.setAttribute("action", "https://nice.checkplus.co.kr/CheckPlusSafeModel/checkplus.cb");
                form.setAttribute("target", "popupChk");

                let m = document.createElement("input");
                m.setAttribute("type", "hidden");
                m.setAttribute("name", "m");
                m.setAttribute("value", "checkplusService");
                form.appendChild(m);

                let EncodeData = document.createElement("input");
                EncodeData.setAttribute("type", "hidden");
                EncodeData.setAttribute("name", "EncodeData");
                EncodeData.setAttribute("value", vm.niceData.sEncData);
                form.appendChild(EncodeData);

                let recvMethodType = document.createElement("input");
                recvMethodType.setAttribute("type", "hidden");
                recvMethodType.setAttribute("name", "recvMethodType");
                recvMethodType.setAttribute("value", "get");
                form.appendChild(recvMethodType);
                document.body.appendChild(form);
                form.submit();

                if (vm.windowRef != null) {
                    vm.windowRef.addEventListener('beforeunload', vm.evtClose);
                } else {
                    alert("window.open fail!!!");
                }

                window.addEventListener("message", vm.recvEvtFromChild, false);

            }).catch(function (error) {
                alert('시스템 오류. 관리자에게 문의하세요');
            });
        },

        // 테스트 데이터 
        fnPopupTest: function (test) {
            let vm = this;

            this.windowRef = window.open('', 'popupChk', 'width=500, height=550, top=100, left=100, fullscreen=no, menubar=no, status=no, toolbar=no, titlebar=yes, location=no, scrollbar=no');

        
            let form = document.createElement("form");
            form.setAttribute("method", "GET");
            form.setAttribute("action", "/cmmn/NiceTestPopup.page");
            form.setAttribute("target", "popupChk");

            let m = document.createElement("input");
            m.setAttribute("type", "hidden");
            m.setAttribute("name", "m");
            m.setAttribute("value", "checkplusService");
            form.appendChild(m);

            let EncodeData = document.createElement("input");
            EncodeData.setAttribute("type", "hidden");
            EncodeData.setAttribute("name", "EncodeData");
            if (test == 'f') {
                EncodeData.setAttribute("value", this.testDataf);
            } else {
                EncodeData.setAttribute("value", this.testData);
            }

            form.appendChild(EncodeData);

            let recvMethodType = document.createElement("input");
            recvMethodType.setAttribute("type", "hidden");
            recvMethodType.setAttribute("name", "recvMethodType");
            recvMethodType.setAttribute("value", "get");
            form.appendChild(recvMethodType);
            document.body.appendChild(form);
        
            form.submit();


            if (this.windowRef != null) {
                this.windowRef.addEventListener('beforeunload', this.evtClose);
            } else {
                alert("window.open fail!!!");
            }

            window.addEventListener("message", this.recvEvtFromChild, false);

        },

        // 팝업 닫기 이벤트 
        evtClose: function () {
            if (this.windowRef) {
                this.windowRef.close();
                this.windowRef = null;
                this.$emit('revData', 'close', null);
            }
        },

        // 내용 전달  
        recvEvtFromChild: function (evt) {
            if (evt.data == null) {

                return;
            }
            let recvObj = JSON.parse(evt.data);
            this.$emit('revData', 'request', recvObj);
        },
    },
    watch: {

    },
    computed: {

    },
    components: {

    },
    mounted() {

    },
    unmounted() {

    },
}
</script>
