<template>
    <v-menu
        v-model="menu.visible"
        :position-x="menu.x"
        :position-y="menu.y"
        absolute
        offset-y
        right
        :close-on-content-click="false"
        :close-on-click="false"
    >
        <v-card tile flat width="480">
            <v-card-title>
                <span class="text-dark-blue">
                    {{ __('Editare registru') }}
                </span>
            </v-card-title>

            <v-divider></v-divider>

            <v-card-text>

                <v-row dense>
                    <v-col cols="12">
                        <v-text-field
                            :label="__('Denumire')"
                            dense
                            outlined
                            hide-details="auto"
                            v-model="activeRecord.name"
                        >
                        </v-text-field>
                    </v-col>

                    <v-col cols="12">

                        <v-checkbox
                            dense
                            hide-details
                            v-model="activeRecord.allow_upload_row_files"
                            :true-value="1"
                            :false-value="0"
                            color="purple darken-3"
                            class="my-0 py-0"
                        >
                            <template #label>
                                <span class="text-caption">
                                    {{__('Încarcarea de fișiere la nivel de rând')}}
                                </span>
                            </template>
                        </v-checkbox>

                        <v-checkbox
                            dense
                            hide-details
                            v-model="activeRecord.has_departamente_column"
                            :true-value="1"
                            :false-value="0"
                            color="purple darken-3"
                            class="my-0 py-0"
                        >
                            <template #label>
                                <span class="text-caption">
                                    {{__('Departament')}}
                                </span>
                            </template>
                        </v-checkbox>

                        <v-checkbox
                            dense
                            hide-details
                            v-model="activeRecord.has_status_column"
                            :true-value="1"
                            :false-value="0"
                            color="purple darken-3"
                            class="my-0 py-0"
                        >
                            <template #label>
                                <span class="text-caption">
                                    {{__('Vizibilitate (public | protected)')}}
                                </span>
                            </template>
                        </v-checkbox>

                        <v-checkbox
                            dense
                            hide-details
                            v-model="activeRecord.has_stare_column"
                            :true-value="1"
                            :false-value="0"
                            color="purple darken-3"
                            class="my-0 py-0"
                        >
                            <template #label>
                                <span class="text-caption">
                                    {{__('Status (new | approved | edited | na)')}}
                                </span>
                            </template>
                        </v-checkbox>

                        <v-checkbox
                            dense
                            hide-details
                            v-model="activeRecord.allow_versions"
                            :true-value="1"
                            :false-value="0"
                            color="purple darken-3"
                            class="my-0 py-0"
                        >
                            <template #label>
                                <span class="text-caption">
                                    {{__('Permite versionare')}}
                                </span>
                            </template>
                        </v-checkbox>
                
                    </v-col>
                </v-row>

            </v-card-text>

            <v-divider></v-divider>

            <v-card-actions>
                <cs-btn 
                    icon="mdi-content-save"
                    :caption="'Salvează'"
                    outlined
                    color="purple"
                    :disabled="false"
                    :loading="false"
                    @click="Update"
                />

                <v-spacer></v-spacer>

                <cs-btn 
                    icon="mdi-close"
                    caption="Renunță"
                    outlined
                    @click="menu.visible = false"
                />
            </v-card-actions>
        </v-card>
    </v-menu>
</template>

<script>
    export default {
        props: {
            app: {required: true},
            menu: {required: true},
        },

        data: () => ({
            t: null, 
        }),

        computed: {
            activeRecord() {
                return this.menu.activeRecord;
            }
        },

        methods: {
            Update() {
                if(this.t)
                {
                    clearTimeout(this.t);
                }

                this.t = setTimeout( () => {
                    this.app.requests.Updateregistru
                    .ResetData()
                        .SetData({
                            ...this.activeRecord,
                            description: this.activeRecord.description ? this.activeRecord.description : '-', 
                        })
                        .Request([
                            response => {
                                // let selected_id = this.activeRecord.id;
                                this.app.dataSession.Registre.Post();
                                let i = setInterval( () => {
                                    if(! this.app.dataSession.Registre.processing)
                                    {
                                        clearInterval(i);
                                        // let registru = _.find(this.app.dataSession.Registre.items, item => item.id == selected_id)
                                        // this.controller.selected_registru = registru;
                                        this.controller.RefreshColumns();
                                        this.menu.visible = false
                                    }
                                }, 50);
                            }
                        ])
                }, 50)

                
            }
        },

        components: {
            CsBtn: require('@CsVueComp/buttons/CsBtn').default,
        },

        name: 'RegistruOptionsFormMenu',
    }
</script>