<template>
    <div class="app">
        <span class="material-icons">printer</span>
        <form action="#">
            <div class="form-item">
                <div class="input-group">
                    <label for="port">Puerto</label>
                    <input id="port" type="text" placeholder="Ej.: 8070" :disabled="!edit" v-model.number.lazy="port" required autofocus>
                </div>
            </div>
            <!-- <div class="form-item">
                <input id="pdf-always" type="checkbox" :disabled="!edit" v-model="alwaysPdf">
                <label for="pdf-always">Siempre imprimir en PDF</label>
            </div> -->
            <div class="form-item">
                <button type="submit" @click.self.prevent="handleForm">{{ edit ? 'Guardar' : 'Editar' }}</button>
            </div>
        </form>
        <div class="app-loading" v-show="isLoading">
            <div class="app-loading-ring">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
            <p>Espere...</p>
        </div>
    </div>
</template>

<script>
    import { ipcRenderer } from 'electron'

    export default {
        computed: {
            port: {
                get() {
                    return this.value
                },
                set(value) {
                    this.value = value
                }
            }
                
        },
        methods: {
            handleForm(e) {
                if (this.edit) {
                    this.toggleLoading()

                    ipcRenderer.sendSync('save-data', {
                        port: this.value,
                        alwaysPdf: this.alwaysPdf
                    })

                    ipcRenderer.send('restart-server')
                }

                this.edit = !this.edit
            },
            readSyncPort() {
                this.toggleLoading()

                const data = ipcRenderer.sendSync('read-data')
                this.value = data.port
                this.alwaysPdf = data.alwaysPdf

                this.toggleLoading()
            },
            handleServerRestarted() {
                ipcRenderer.on('server-restarted', () => {
                    this.toggleLoading()
                })

            },
            toggleLoading() {
                this.isLoading = !this.isLoading
            }
        },
        data() {
            return {
                edit: false,
                buttonText: 'Editar',
                value: 8070,
                alwaysPdf: true,
                isLoading: false
            }
        },
        mounted() {
            this.readSyncPort()
            this.handleServerRestarted()
        }
    }
</script>

<style>
    html {
        font-family: 'Roboto';
        background: #0288d1;
    }

    html, body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }

    .app {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        overflow: hidden;
    }

    .app span {
        width: 100px;
        height: 100px;
        color: #fff;
        text-align: center;
        font-size: 72pt;
        margin-bottom: 72px;
    }

    .app .form-item {
        width: 270px;
        height: 35px;
        margin-bottom: 25px;
    }

    .app .form-item .input-group {
        display: table;
        position: relative;
        width: 100%;
        border-collapse: separate;
        vertical-align: top;
    }

    .app .form-item .input-group label {
        position: relative;
        display: table-cell;
        width: 1%;
        white-space: nowrap;
        vertical-align: middle;
        background: #4caf50;
        color: #fff;
        font-size: 11pt;
        padding: 0 12px;
    }

    .app .form-item .input-group input[type="text"] {
        position: relative;
        display: table-cell;
        width: 90%;
        height: 35px;
        float: left;
        border-radius: 0;
        z-index: 2;
        border: none;
        font-size: 12pt;
        padding: 1px 10px;
        color: #424242;
    }

    .app .form-item label[for="pdf-always"] {
        font-size: 11pt;
        color: #fff;
    }

    .app .form-item button {
        width: 100%;
        height: 35px;
        background: #4caf50;
        border: none;
        color: #fff;
        font-size: 12pt;
    }

    .app .form-item button:hover {
        cursor: pointer;
        background: #4caf50;
    }

    .app .app-loading {
        display: flex;
        flex-direction: column;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        justify-content: center;
        align-items: center;
        background: rgba(0, 0, 0, 0.7);
        z-index: 10;
    }

    .app .app-loading .app-loading-ring {
        display: inline-block;
        position: relative;
        width: 64px;
        height: 64px;
    }

    .app .app-loading .app-loading-ring div {
        box-sizing: border-box;
        display: block;
        position: absolute;
        width: 51px;
        height: 51px;
        margin: 3px;
        border: 3px solid #fff;
        border-radius: 50%;
        animation: loading-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
        border-color: #fff transparent transparent transparent;
    }

    .app .app-loading .app-loading-ring div:nth-child(1) {
        animation-delay: -0.45s;
    }

    .app .app-loading .app-loading-ring div:nth-child(2) {
        animation-delay: -0.3s;
    }

    .app .app-loading .app-loading-ring div:nth-child(3) {
        animation-delay: -0.15s;
    }

    .app .app-loading p {
        color: #fff;
    }

    @keyframes loading-ring {
        0% {
            transform: rotate(0deg);
        }
        100% {
            transform: rotate(360deg);
        }
    }
</style>