ItemDetail = {
    template: "#ItemDetail",
    router_path: "/ItemDetail/:id",
    delimiters: ["[[", "]]"],

    data: function() {
        return {
            dialog: false,
        }
    },

    computed: {
        object: function() {
            return this.$store.state.items.items.find(i => i.id == this.$route.params.id)
        },

        linked_properties: function() {
            return this.$store.state.linkedProperties.items.filter(lp => lp.item == this.$route.params.id)
        },

        linked_properties_headers: function() {
            return this.$store.state.linkedProperties.headers
        },

        properties: function() {
            return this.$store.state.properties.items.filter(p => this.linked_properties.map(e => e.property).includes(p.id))
        },

        properties_headers: function() {
            return this.$store.state.properties.headers
        },

        children: function() {
            return this.$store.state.relations.items.filter(p => p.parent == this.$route.params.id)
        },

        parents: function() {
            return this.$store.state.relations.items.filter(p => p.child == this.$route.params.id)
        },

        children_headers: function() {
            return this.$store.state.relations.headers
        },

        headers: function() {
            return this.$store.state.items.headers
        },

        all_items: function() {
            return this.$store.state.items.items
        },

        all_properties: function() {
            return this.$store.state.properties.items
        },

        history: function() {
            return this.$store.state.history.items
        },
    },

    methods: {

        async showHistory () {
            const response = await this.$http.get(Urls["items:history"](this.$route.params.id))

            this.$store.state.history.headers = response.data.headers
            this.$store.dispatch("history/setItems", { self: this, items: response.data.history })
            this.dialog = true
        },

        showType () {
            this.$router.push({ name: "ItemTypeDetail", params: { id: this.object.type }})
        },

        closeDialog () {
            this.dialog = false
            this.$store.state.history.items = []
        },

        async deleteVersion(version) {
            const response = await this.$http.delete(Urls["items:history.edit"](this.$route.params.id, version.history_id))

            Swal.fire({title: "{{_('Version successfully deleted') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})

            this.closeDialog()
        },

        async useVersion(version) {
            const response = await this.$http.post(Urls["items:history.edit"](this.$route.params.id, version.history_id))
            const efields = this.$store.getters["items/encryptedFields"]
            const new_item = await this.decryptObject(efields, response.data.object)

            this.$store.commit("items/editItem", new_item)

            Swal.fire({title: "{{_('Version successfully restored') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})

            this.closeDialog()
        },

        linkedPropertyEdition (method, item) {
            return this.object_edit("items:linked.property.edit", "items:linked.property.create", 'linkedProperties', method, item)
        },

        relationPropertiesEdition (method, item) {
            return this.object_edit("items:relation.property.edit", "items:relation.property.create", 'relationProperties', method, item)
        },

        relationEdition (method, item) {
            return this.object_edit("items:relation.edit", "items:relation.create", 'relations', method, item)
        },

        async deleteLinkedProperty (item) {
            await this.linkedPropertyEdition("delete", item)
            this.$store.commit("linkedProperties/removeItem", item.id)
        },

        async createLinkedProperty (item) {
            item.item = this.$route.params.id
            const new_item = await this.linkedPropertyEdition("post", item)
            this.$store.commit("linkedProperties/addItem", new_item)
        },

        async editLinkedProperty (item) {
            item.item = this.$route.params.id
            const new_item = await this.linkedPropertyEdition("post", item)
            this.$store.commit("linkedProperties/editItem", new_item)
        },

        async deleteProperty (item) {
            console.log(item)
        },

        async createProperty (item) {
            console.log(item)
        },

        async editProperty (item) {
            console.log(item)
        },

        async deleteRelation (item) {
            await this.relationEdition("delete", item)
            this.$store.commit("relations/removeItem", item.id)
        },

        async createRelation (item) {
            const new_item = await this.relationEdition("post", item)
            this.$store.commit("relations/addItem", new_item)
        },

        async editRelation (item) {
            const new_item = await this.relationEdition("post", item)
            this.$store.commit("relations/editItem", new_item)
        },

    }
}
