/**
 * Advanced Form Integration - "mailerlite2" action component.
 * Auto-extracted from assets/js/script.js. Loaded on demand by
 * adfoinComponentLoader.loadPlatform("mailerlite2").
 */

Vue.component('mailerlite2', {
    props: ["trigger", "action", "fielddata"],
    data: function () {
        return {
            listLoading: false,
            fieldsLoading: false,
            credentialLoading: false,
            credentialsList: [],
            fields: [
                { type: 'text', value: 'email', title: 'Email', task: ['subscribe'], required: true },
                { type: 'text', value: 'status', title: 'Status', task: ['subscribe'], required: false, description: 'active | unsubscribed | unconfirmed | bounced | junk' },
                { type: 'text', value: 'ip_address', title: 'IP Address', task: ['subscribe'], required: false },
                { type: 'text', value: 'opted_in_at', title: 'Opted In At', task: ['subscribe'], required: false, description: 'Date in format yyyy-MM-dd HH:mm:ss' },
                { type: 'text', value: 'optin_ip', title: 'Opt-in IP', task: ['subscribe'], required: false },
                { type: 'text', value: 'resubscribe', title: 'Resubscribe', task: ['subscribe'], required: false, description: 'Set to "true" to resubscribe previously unsubscribed contacts' },
            ]

        }
    },
    methods: {
        getCredentials: function () {
            var that = this;
            adfoinHelpers.fetchCredentials(this, 'adfoin_get_mailerlite2_credentials', {
                loadingKey: 'credentialLoading',
                autoSelect: 'first',
                onLoaded: function () { that.getData(); }
            });
        },
        getData: function () {
            this.getLists();
            this.getCustomFields();
        },
        getLists: function () {
            adfoinHelpers.fetchToFielddata(this, 'adfoin_get_mailerlite2_list', {
                targetKey: 'list',
                loadingKey: 'listLoading'
            });
        },
        getCustomFields: function () {
            var that = this;

            if (!this.fielddata.credId) {
                this.fields = [
                    { type: 'text', value: 'email', title: 'Email', task: ['subscribe'], required: true },
                    { type: 'text', value: 'status', title: 'Status', task: ['subscribe'], required: false, description: 'active | unsubscribed | unconfirmed | bounced | junk' },
                    { type: 'text', value: 'ip_address', title: 'IP Address', task: ['subscribe'], required: false },
                    { type: 'text', value: 'opted_in_at', title: 'Opted In At', task: ['subscribe'], required: false, description: 'Date in format yyyy-MM-dd HH:mm:ss' },
                    { type: 'text', value: 'optin_ip', title: 'Opt-in IP', task: ['subscribe'], required: false },
                    { type: 'text', value: 'resubscribe', title: 'Resubscribe', task: ['subscribe'], required: false, description: 'Set to "true" to resubscribe previously unsubscribed contacts' },
                ];
                return;
            }

            this.fieldsLoading = true;
            this.fields = [
                { type: 'text', value: 'email', title: 'Email', task: ['subscribe'], required: true },
                { type: 'text', value: 'status', title: 'Status', task: ['subscribe'], required: false, description: 'active | unsubscribed | unconfirmed | bounced | junk' },
                { type: 'text', value: 'ip_address', title: 'IP Address', task: ['subscribe'], required: false },
            ];

            var customFieldData = {
                'action': 'adfoin_get_mailerlite2_custom_fields',
                'credId': this.fielddata.credId,
                '_nonce': adfoin.nonce
            };

            jQuery.post(ajaxurl, customFieldData, function (response) {

                if (response.success) {
                    if (response.data) {
                        response.data.map(function (single) {
                            that.fields.push({ type: 'text', value: single.key, title: single.value, task: ['subscribe'], required: false, description: single.description });
                        });

                        that.fieldsLoading = false;
                    }
                }
            });
        },
        handleAccountChange: function () {
            this.fielddata.listId = '';
            this.fielddata.list = {};
            this.getData();
        }
    },
    created: function () {

    },
    mounted: function () {
        var that = this;

        if (typeof this.fielddata.listId == 'undefined') {
            this.fielddata.listId = '';
        }

        if (typeof this.fielddata.credId == 'undefined') {
            this.fielddata.credId = '';
        }

        this.getCredentials();
    },
    template: '#mailerlite2-action-template'
});
