/**
 * Advanced Form Integration — "successai" action component.
 * Loaded on demand by adfoinComponentLoader.loadPlatform("successai").
 */

Vue.component('successai', {
    props: ["trigger", "action", "fielddata"],
    data: function () {
        return {
            credLoading: false,
            fieldsLoading: false,
            credentialsList: [],
            fields: []
        };
    },
    methods: {
        getCredentials: function () {
            var that = this;
            this.credLoading = true;
            jQuery.post(ajaxurl, {
                action: 'adfoin_get_successai_credentials',
                _nonce: adfoin.nonce
            }, function (response) {
                that.credLoading = false;
                if (response && response.success && Array.isArray(response.data)) {
                    that.credentialsList = response.data;
                }
            }).fail(function () {
                that.credLoading = false;
            });
        },
        loadFields: function () {
            var that = this;
            this.fieldsLoading = true;
            jQuery.post(ajaxurl, {
                action: 'adfoin_get_successai_fields',
                _nonce: adfoin.nonce
            }, function (response) {
                that.fieldsLoading = false;
                if (response && response.success && Array.isArray(response.data)) {
                    that.fields = response.data.map(function (single) {
                        return {
                            type: single.type || 'text',
                            value: single.key,
                            title: single.value,
                            task: ['add_lead'],
                            required: !!single.required
                        };
                    });
                } else {
                    that.fields = [];
                }
            }).fail(function () {
                that.fields = [];
                that.fieldsLoading = false;
            });
        }
    },
    mounted: function () {
        var defaults = {
            credId:   '',
            campaign: ''
        };
        var that = this;
        Object.keys(defaults).forEach(function (k) {
            if (typeof that.fielddata[k] === 'undefined') {
                that.$set(that.fielddata, k, defaults[k]);
            }
        });
        this.getCredentials();
        this.loadFields();
    },
    template: '#successai-action-template'
});
