<template>
    <v-menu
        v-model="menu.visible"
        :position-x="0.5 * (app.width - width)"
        :position-y="0.5 * (app.height - height)"
        absolute
        offset-y
        :close-on-content-click="false"
        :close-on-click="false"
    >
        <v-card tile flat>
            <v-card-title class="py-1">
                {{ __('Asignare cursuri') }}
            </v-card-title>

            <v-divider></v-divider>

            <curs-asigneaza   
                ref="AssignCursInput"          
                :app="app"
                :width="width"
                :height="height"
                :target="target"
                @input="OnInputAssign"
            />
            
            <v-divider></v-divider>

            <v-card-actions>
                <cs-btn 
                    caption="Asignează"
                    icon="mdi-content-save"
                    color="primary"
                    :disabled="! IsValid()"
                    @click="OnClickAssign"
                />

                <v-spacer></v-spacer>
                
                <cs-btn 
                    caption="Renunță"
                    icon="mdi-close"
                    @click="OnClickCancel"
                />
            </v-card-actions>
        </v-card>
    </v-menu>
</template>

<script>
    export default {
        props: {
            app: {required: true},
            menu: {required: true},
            width: {default: 800},
            height: {default: 600},
            target: {default: null},
        },

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

        methods: {

            IsValid() {
                if( ! this.activeRecord )
                {
                    return false;
                }

                if( ! this.activeRecord.date_from || ! this.activeRecord.date_to)
                {
                    return false;
                }

                if(this.activeRecord.materiale_trimise.lenght == 0)
                {
                    return false;
                }

                if(this.activeRecord.customers[this.app.payload.customer_id].length == 0)
                {
                    return false;
                }

                return true;
            },

            OnClickAssign() {
                this.app.requests.Assigncursuri
                    .ResetData()
                    .SetData(this.activeRecord)
                    .Request([
                        response => {
                            this.app.classes.Cursuri.GetCustomerusercursuricounter();
                            this.OnClickCancel();
                        }
                    ]) 
            },

            OnInputAssign(e) {
                this.activeRecord = {...e};
            },

            OnClickCancel() {
                this.menu.visible = false;
                this.menu.curs = null;
            }
        },

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

        name: 'CursAsigneazaMenu'
    }
</script>