define('custom:views/c-dokumente/record/panels/c-a-i-collections', 
['views/record/panels/relationship'], function (Dep) {

    return Dep.extend({

        setup: function () {
            Dep.prototype.setup.call(this);
        },

        getRowActionList: function (model, acl) {
            var list = Dep.prototype.getRowActionList.call(this, model, acl);

            if (acl.edit) {
                list.unshift({
                    action: 'editLinkData',
                    label: 'Edit Link Data',
                    data: {
                        id: model.id
                    }
                });
            }

            return list;
        },

        actionEditLinkData: function (data) {
            var id = data.id;
            
            this.notify('Loading...');

            var url = 'CAICollectionCDokumente?where[0][type]=and&where[0][value][0][type]=equals&where[0][value][0][attribute]=cDokumenteId&where[0][value][0][value]=' + this.model.id + '&where[0][value][1][type]=equals&where[0][value][1][attribute]=cAICollectionsId&where[0][value][1][value]=' + id + '&maxSize=1';

            Espo.Ajax.getRequest(url).then(function (response) {
                this.notify(false);

                if (response.list && response.list.length > 0) {
                    var junctionRecord = response.list[0];

                    this.createView('dialog', 'views/modals/edit', {
                        scope: 'CAICollectionCDokumente',
                        id: junctionRecord.id
                    }, function (view) {
                        view.render();

                        this.listenToOnce(view, 'after:save', function () {
                            this.clearView('dialog');
                            this.actionRefresh();
                        }, this);
                    }.bind(this));
                } else {
                    this.notify('Junction record not found', 'error');
                }
            }.bind(this)).catch(function () {
                this.notify('Error loading link data', 'error');
            }.bind(this));
        }

    });
});
