/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* Version                 : 2.1.8
* File                    : assets/js/rules/backend-rule.js
* File Version            : 1.0.6
* Created / Last Modified : 14 March 2016
* Author                  : Dot on Paper
* Copyright               : © 2012 Dot on Paper
* Website                 : http://www.dotonpaper.net
* Description             : Back end rule JavaScript class.
*/

var DOPBSPBackEndRule = new function(){
    'use strict';

    /*
     * Private variables.
     */
    var $ = jQuery.noConflict();

    /*
     * Public variables
     */
    this.ajaxRequestInProgress;
    this.ajaxRequestTimeout;

    /*
     * Constructor
     */
    this.__construct = function(){
    };

    /*
     * Display rule.
     * 
     * @param id (Number): rule ID
     * @param language (String): rule current editing language
     * @param clearRule (Boolean): clear rule extra data diplay
     */
    this.display = function(id,
                            language,
                            clearRule){
        var HTML = new Array();

        language = language === undefined
                ? ($('#DOPBSP-rule-language')
                .val() === undefined
                        ? ''
                        : $('#DOPBSP-rule-language')
                        .val())
                : language;
        clearRule = clearRule === undefined
                ? true
                : false;
        language = clearRule
                ? ''
                : language;

        if (clearRule){
            DOPBSPBackEnd.clearColumns(2);
        }
        DOPBSPBackEnd.toggleMessages('active',
                                     DOPBSPBackEnd.text('MESSAGES_LOADING'));

        $('#DOPBSP-column1 .dopbsp-column-content li')
        .removeClass('dopbsp-selected');
        $('#DOPBSP-rule-ID-'+id)
        .addClass('dopbsp-selected');
        $('#DOPBSP-rule-ID')
        .val(id);

        $.post(ajaxurl,
               {
                   action  : 'dopbsp_rule_display',
                   id      : id,
                   language: language,
                   nonce   : DOPBSP_user_nonce
               },
               function(data){
                   HTML.push('<a href="javascript:DOPBSPBackEnd.confirmation(\'RULES_DELETE_RULE_CONFIRMATION\', \'DOPBSPBackEndRule.delete('+id+')\')" class="dopbsp-button dopbsp-delete"><span class="dopbsp-info">'+DOPBSPBackEnd.text('RULES_DELETE_RULE_SUBMIT')+'</span></a>');
                   HTML.push('<a href="'+DOPBSP_CONFIG_HELP_DOCUMENTATION_URL+'" target="_blank" class="dopbsp-button dopbsp-help">');
                   HTML.push(' <span class="dopbsp-info dopbsp-help">');
                   HTML.push(DOPBSPBackEnd.text('RULES_ADD_RULE_HELP')+'<br /><br />');
                   HTML.push(DOPBSPBackEnd.text('HELP_VIEW_DOCUMENTATION'));
                   HTML.push(' </span>');
                   HTML.push('</a>');

                   $('#DOPBSP-column2 .dopbsp-column-header')
                   .html(HTML.join(''));
                   $('#DOPBSP-column2 .dopbsp-column-content')
                   .html(data);

                   $('#DOPBSP-rule-start_date')
                   .datepicker();
                   $('#DOPBSP-rule-end_date')
                   .datepicker();

                   DOPBSPBackEndRule.init();
                   DOPBSPBackEnd.toggleMessages('success',
                                                DOPBSPBackEnd.text('RULES_RULE_LOADED'));
               })
         .fail(function(data){
             DOPBSPBackEnd.toggleMessages('error',
                                          data.status+': '+data.statusText);
         });
    };

    /*
     * Initialize events and validations.
     */
    this.init = function(){
        /*
         * Number of rules.
         */
        $('#DOPBSP-rule-time_lapse_min')
        .unbind('input propertychange');
        $('#DOPBSP-rule-time_lapse_min')
        .bind('input propertychange',
              function(){
                  DOPPrototypes.cleanInput($(this),
                                           '0123456789.',
                                           '',
                                           '0');
              });

        /*
         * Price
         */
        $('#DOPBSP-rule-time_lapse_max')
        .unbind('input propertychange');
        $('#DOPBSP-rule-time_lapse_max')
        .bind('input propertychange',
              function(){
                  DOPPrototypes.cleanInput($(this),
                                           '0123456789.',
                                           '',
                                           '0');
              });
    };

    /*
     * Add rule.
     */
    this.add = function(){
        DOPBSPBackEnd.clearColumns(2);
        DOPBSPBackEnd.toggleMessages('active',
                                     DOPBSPBackEnd.text('RULES_ADD_RULE_ADDING'));

        $.post(ajaxurl,
               {
                   action: 'dopbsp_rule_add',
                   nonce : DOPBSP_user_nonce
               },
               function(data){
                   $('#DOPBSP-column1 .dopbsp-column-content')
                   .html(data);
                   DOPBSPBackEnd.toggleMessages('success',
                                                DOPBSPBackEnd.text('RULES_ADD_RULE_SUCCESS'));
               })
         .fail(function(data){
             DOPBSPBackEnd.toggleMessages('error',
                                          data.status+': '+data.statusText);
         });
    };

    /*
     * Edit rule.
     * 
     * @param id (Number): rule ID
     * @param type (String): field type
     * @param field (String): field name
     * @param value (String): field value
     * @param onBlur (Boolean): true if function has been called on blur event
     */
    this.edit = function(id,
                         type,
                         field,
                         value,
                         onBlur){
        onBlur = onBlur === undefined
                ? false
                : true;

        this.ajaxRequestInProgress !== undefined && !onBlur
                ? this.ajaxRequestInProgress.abort()
                : '';
        this.ajaxRequestTimeout !== undefined
                ? clearTimeout(this.ajaxRequestTimeout)
                : '';

        switch (field){
            case 'name':
                $('#DOPBSP-rule-ID-'+id+' .dopbsp-name')
                .html(value === ''
                              ? '&nbsp;'
                              : value);
                break;
        }

        if (onBlur
                || type === 'select'
                || type === 'switch'){
            if (!onBlur){
                DOPBSPBackEnd.toggleMessages('active-info',
                                             DOPBSPBackEnd.text('MESSAGES_SAVING'));
            }

            $.post(ajaxurl,
                   {
                       action  : 'dopbsp_rule_edit',
                       id      : id,
                       field   : field,
                       value   : value,
                       language: $('#DOPBSP-rule-language')
                       .val(),
                       nonce   : DOPBSP_user_nonce
                   },
                   function(data){
                       if (!onBlur){
                           DOPBSPBackEnd.toggleMessages('success',
                                                        DOPBSPBackEnd.text('MESSAGES_SAVING_SUCCESS'));
                       }
                   })
             .fail(function(data){
                 DOPBSPBackEnd.toggleMessages('error',
                                              data.status+': '+data.statusText);
             });
        }
        else{
            DOPBSPBackEnd.toggleMessages('active-info',
                                         DOPBSPBackEnd.text('MESSAGES_SAVING'));

            this.ajaxRequestTimeout = setTimeout(function(){
                                                     clearTimeout(this.ajaxRequestTimeout);

                                                     this.ajaxRequestInProgress = $.post(ajaxurl,
                                                                                         {
                                                                                             action  : 'dopbsp_rule_edit',
                                                                                             id      : id,
                                                                                             field   : field,
                                                                                             value   : value,
                                                                                             language: $('#DOPBSP-rule-language')
                                                                                             .val(),
                                                                                             nonce   : DOPBSP_user_nonce
                                                                                         },
                                                                                         function(data){
                                                                                             DOPBSPBackEnd.toggleMessages('success',
                                                                                                                          DOPBSPBackEnd.text('MESSAGES_SAVING_SUCCESS'));
                                                                                         })
                                                                                   .fail(function(data){
                                                                                       DOPBSPBackEnd.toggleMessages('error',
                                                                                                                    data.status+': '+data.statusText);
                                                                                   });
                                                 },
                                                 600);
        }
    };


    /*
     * Delete rule.
     * 
     * @param id (Number): rule ID
     */
    this.delete = function(id){
        DOPBSPBackEnd.toggleMessages('active',
                                     DOPBSPBackEnd.text('RULES_DELETE_RULE_DELETING'));

        $.post(ajaxurl,
               {
                   action: 'dopbsp_rule_delete',
                   id    : id,
                   nonce : DOPBSP_user_nonce
               },
               function(data){
                   DOPBSPBackEnd.clearColumns(2);

                   $('#DOPBSP-rule-ID-'+id)
                   .stop(true,
                         true)
                   .animate({'opacity': 0},
                            600,
                            function(){
                                $(this)
                                .remove();

                                if (data === '0'){
                                    $('#DOPBSP-column1 .dopbsp-column-content')
                                    .html('<ul><li class="dopbsp-no-data">'+DOPBSPBackEnd.text('RULES_NO_RULES')+'</li></ul>');
                                }
                                DOPBSPBackEnd.toggleMessages('success',
                                                             DOPBSPBackEnd.text('RULES_DELETE_RULE_SUCCESS'));
                            });
               })
         .fail(function(data){
             DOPBSPBackEnd.toggleMessages('error',
                                          data.status+': '+data.statusText);
         });
    };

    return this.__construct();
};