define('modules/smart-assistant/views/case/modals/add-memory', ['views/modal'], function (ModalView) {

    return ModalView.extend({

        backdrop: true,
        cssName: 'add-memory',

        header: false,

        templateContent: '' +
            '<div style="direction: rtl; text-align: right;">' +
            '<h4 style="margin-top: 0;">{{translate \'Add Memory\' scope=\'SmartAssistant\'}}</h4>' +
            '<div class="form-group">' +
            '  <label>{{translate \'name\' category=\'fields\' scope=\'CaseMemory\'}}</label>' +
            '  <input type="text" class="form-control field-name" maxlength="255">' +
            '</div>' +
            '<div class="form-group">' +
            '  <label>{{translate \'category\' category=\'fields\' scope=\'CaseMemory\'}}</label>' +
            '  <select class="form-control field-category">' +
            '    <option value="key_facts">{{translateOption \'key_facts\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="strategy">{{translateOption \'strategy\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="decisions">{{translateOption \'decisions\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="contacts_notes">{{translateOption \'contacts_notes\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="timeline">{{translateOption \'timeline\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="documents_notes">{{translateOption \'documents_notes\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="billing_notes">{{translateOption \'billing_notes\' field=\'category\' scope=\'CaseMemory\'}}</option>' +
            '  </select>' +
            '</div>' +
            '<div class="form-group">' +
            '  <label>{{translate \'importance\' category=\'fields\' scope=\'CaseMemory\'}}</label>' +
            '  <select class="form-control field-importance">' +
            '    <option value="low">{{translateOption \'low\' field=\'importance\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="normal" selected>{{translateOption \'normal\' field=\'importance\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="high">{{translateOption \'high\' field=\'importance\' scope=\'CaseMemory\'}}</option>' +
            '    <option value="critical">{{translateOption \'critical\' field=\'importance\' scope=\'CaseMemory\'}}</option>' +
            '  </select>' +
            '</div>' +
            '<div class="form-group">' +
            '  <label>{{translate \'content\' category=\'fields\' scope=\'CaseMemory\'}}</label>' +
            '  <textarea class="form-control field-content" rows="4" style="direction: rtl;"></textarea>' +
            '</div>' +
            '</div>',

        buttonList: [
            {
                name: 'save',
                label: 'Save',
                style: 'primary',
            },
            {
                name: 'cancel',
                label: 'Cancel',
            },
        ],

        setup: function () {
            this.caseId = this.options.caseId;
            this.headerHtml = '<span class="fas fa-brain" style="margin-left: 6px;"></span> ' +
                this.translate('Add Memory', 'labels', 'SmartAssistant');
        },

        actionSave: function () {
            var name = this.$el.find('.field-name').val().trim();
            var content = this.$el.find('.field-content').val().trim();
            var category = this.$el.find('.field-category').val();
            var importance = this.$el.find('.field-importance').val();

            if (!content) {
                Espo.Ui.error('Content is required');
                return;
            }

            if (!name) {
                name = content.substring(0, 100);
            }

            var self = this;

            this.disableButton('save');

            Espo.Ajax.postRequest('SmartAssistant/action/saveMemory', {
                caseId: this.caseId,
                name: name,
                content: content,
                category: category,
                importance: importance,
            }).then(function () {
                Espo.Ui.success(self.translate('Memory saved', 'labels', 'SmartAssistant'));
                self.trigger('saved');
            }).catch(function () {
                Espo.Ui.error('Error saving memory');
                self.enableButton('save');
            });
        },
    });
});
