<template>
    <v-menu
        v-model="menu.visible"
        :position-x="menu.x"
        :position-y="menu.y"
        absolute
        offset-y
        :close-on-content-click="false"
        :close-on-click="false"
    >
        <v-card
            tile
            flat
            :width="width"
        >
            <v-card-title>
                {{ __('Atașează fișiere la curs') }}
            </v-card-title>

            <v-divider></v-divider>

            <v-card-text>
                <v-file-input
                    dense
                    multiple
                    :label="__('Selectați unul sau mai multe fișiere')"
                    v-model="files"
                >
                </v-file-input>
            </v-card-text>

            <v-divider></v-divider>

            <v-card-actions>
                <cs-btn 
                    caption="Atașează"
                    icon="mdi-content-save"
                    color="primary"
                    :loading="app.requests.Attachfiles.processing"
                    :disabled="files.length == 0"
                    @click="OnClickSave"
                />

                <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: 400},
            height: {default: 300},
        },

        data: () => ({
            files: [],
        }),

        methods: {
            OnClickSave() {
                this.app.requests.Attachfiles.ResetData().SetData({
                    files: this.files,
                    customer_curs_id: this.app.payload.customer_curs_id,
                    customer_id: this.app.payload.customer_id,
                }).Request([
                    response => {
                        if( !! this.app.datatables.Cursuriatasate )
                        {
                            this.app.datatables.Cursuriatasate.activeRow.item.files_count = (1 * this.app.datatables.Cursuriatasate.activeRow.item.files_count + 1 * this.files.length);
                        }
                        if( !! this.app.dataSession.Cursuriatasate )
                        {
                            this.app.dataSession.Cursuriatasate.GetByCustomerId(this.app.payload.customer_id);
                        }
                        this.app.dataSession.Customercursurifiles.GetByCustomerCursId(this.app.payload.customer_curs_id);
                        this.menu.visible = false;
                        this.files = [];
                    }
                ])
            },

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

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

        name: 'UploadFileMenu'
    }
</script>