{% load i18n %}

{% block component %}
<v-data-table
    :headers="citems_headers"
    :items="items"
    :search="search"
    :group-by="group_by"
    :items-per-page="20"
    loading
    dense>
    <template v-slot:top>
        <v-toolbar flat>

            <v-text-field v-model="search" append-icon="mdi-magnify" label="Search" single-line hide-details></v-text-field>
            <v-divider class="mx-4" insert vertical></v-divider>

            <v-spacer></v-spacer>

            <v-dialog v-model="dialog" max-width="500px">
                <template v-slot:activator="{ on, attrs }">
                    <v-btn color="primary" dark class="mb-2" v-bind="attrs" v-on="on">
                        <v-icon small class="mr-2">mdi-plus</v-icon>
                        {% trans "New" %}
                    </v-btn>
                </template>

                <v-card>
                    <v-card-title>
                        <span class="text-h5"> [[ editedItem.id ]]</span>
                    </v-card-title>
                    <v-card-text>
                        <v-container>
                            <v-row>
                                <v-form ref="form" v-model="valid">

                                    <template v-for="field in editable_fields">

                                        <template v-if="field.field_widget == 'v-select'">
                                            <template v-if="field.choices">
                                                <v-select
                                                    v-model="editedItem[field.value]"
                                                    :items="field.choices"
                                                    :label="field.text"
                                                    :rules="[rules.required]"
                                                    item-text="text"
                                                    item-value="value"
                                                    persistent-hint>
                                                </v-select>
                                            </template>

                                            <template v-else>
                                                <v-select
                                                    v-model="editedItem[field.value]"
                                                    :items="items_relations[field.value]"
                                                    :label="field.text"
                                                    :rules="[rules.required]"
                                                    item-text="name"
                                                    item-value="id"
                                                    return-object
                                                    persistent-hint>
                                                    <template slot="item" slot-scope="data">
                                                        <template v-if="data.item.custom_identifier">
                                                            [[ data.item.custom_identifier ]] - [[ data.item.name ]]
                                                        </template>
                                                        <template v-else>
                                                            [[ data.item.name ]]
                                                        </template>
                                                    </template>
                                                    <template slot="selection" slot-scope="data">
                                                        [[ data.item.custom_identifier ]] - [[ data.item.name ]]
                                                    </template>
                                                </v-select>
                                            </template>
                                        </template>

                                        <template v-else-if="field?.dynamic_field_type">
                                            <DynField :item="editedItem" :field="field"></DynField>
                                        </template>

                                        <template v-else>
                                            <component :is="field.field_widget" v-model="editedItem[field.value]" :label="field.text"></component>
                                        </template>

                                    </template>

                                </v-form>
                            </v-row>
                        </v-container>
                    </v-card-text>

                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="blue darken-1" text @click="close">
                            <v-icon small class="mr-2">mdi-cancel</v-icon>
                            {% trans "Cancel" %}
                        </v-btn>
                        <v-btn color="blue darken-1" text @click="save">
                            <v-icon small class="mr-2">mdi-content-save-edit</v-icon>
                            {% trans "Save" %}
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

            <v-dialog v-model="dialogDelete" max-width="500px">
                <v-card>
                    <v-card-title class="text-h5">{% trans "Are you sure you want to delete this item?" %}</v-card-title>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="blue darken-1" text @click="closeDelete">
                            <v-icon small class="mr-2">mdi-cancel</v-icon>
                            {% trans "Cancel" %}
                        </v-btn>
                        <v-btn color="blue darken-1" text @click="deleteItemConfirm">
                            <v-icon small class="mr-2">mdi-delete-forever</v-icon>
                            {% trans "OK" %}
                        </v-btn>
                        <v-spacer></v-spacer>
                    </v-card-actions>
                </v-card>
            </v-dialog>
        </v-toolbar>
    </template>

    <template v-slot:item.id="{ item }">
        <v-btn plain @click="showItem(item)">
            [[ item.id.slice(0, 8) ]]...
        </v-btn>
    </template>

    <template v-slot:item.description="{ item }">
        <template v-if="item.description && item.description.length > 15">
            [[ item.description.slice(0, 15) ]]...
        </template>
        <template v-else>
            [[ item.description ]]
        </template>
    </template>

    <template v-slot:item.last_modified_at="{ item }">
        [[ formatDate(item.last_modified_at) ]]
    </template>

    <template v-slot:item.created_at="{ item }">
        [[ formatDate(item.created_at) ]]
    </template>

    <template v-slot:item.actions="{ item }">
        <v-icon v-if="show_url" small class="mr-2" @click="showItem(item)">mdi-eye</v-icon>
        <v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
        <v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
    </template>

    <template v-slot:no-data>
        <v-btn color="primary" @click="initialize">{% trans "Reset" %}</v-btn>
    </template>

</v-data-table>
{% endblock %}
