<template>
    <div class="p-d-flex p-flex-column p-ai-center" id="regex-create">
        <div class="p-formgroup-inline">
            <div class="p-field">
                <span class="p-float-label">
                    <InputText
                        v-model="name"
                        type="text"
                        class="p-inputtext-sm"
                        name="name"
                        id="name"/>
                    <label for="name">Enter searcher name</label>
                </span>
            </div>
            <Button
                label="Save"
                class="p-button-sm p-button-raised"
                @click="onSave"
                :disabled="!name || !pattern" />
        </div>

        <div class="regex-box">
            <div class="main">
                <pattern-box v-model="pattern"></pattern-box>

                <text-box :pattern="pattern"
                          :flags="flags">
                </text-box>

                <flags v-model="flags"></flags>
            </div>

            <aside>
                <side-bar></side-bar>
            </aside>
        </div>
    </div>
</template>

<script lang="ts">
import {Component, Vue} from "vue-property-decorator";
import TextBox from './TextBox.vue';
import PatternBox from './PatternBox.vue';
import Flags from './Flags.vue';
import SideBar from './SideBar.vue';

@Component({
    components: {
        TextBox,
        PatternBox,
        Flags,
        SideBar,
    },
})

export default class Create extends Vue {
    private name: string = '';
    private pattern: string = '';
    private flags: Array<string> = ['g', 'i'];

    async onSave() {
        try {
            const { data } = await (window as any).axios.post('/regex', {
                name: this.name,
                expression: this.pattern,
            });

            alert('Saved.');
        } catch (e) {
            console.log(e);
            console.log('Something went wrong.');
        }
    }
};
</script>

<style lang="scss" scoped>
#regex-create {
    margin-top: 25px;
}
</style>