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

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

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

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

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

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

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

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

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

        $.post(ajaxurl,
               {
                   action  : 'dopbsp_discount_display',
                   id      : id,
                   language: language,
                   nonce   : DOPBSP_user_nonce
               },
               function(data){
                   HTML.push('<a href="javascript:DOPBSPBackEnd.confirmation(\'DISCOUNTS_DELETE_DISCOUNT_CONFIRMATION\', \'DOPBSPBackEndDiscount.delete('+id+')\')" class="dopbsp-button dopbsp-delete"><span class="dopbsp-info">'+DOPBSPBackEnd.text('DISCOUNTS_DELETE_DISCOUNT_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('DISCOUNTS_DISCOUNT_ADD_ITEM_HELP')+'<br /><br />');
                   HTML.push(DOPBSPBackEnd.text('DISCOUNTS_DISCOUNT_EDIT_ITEM_HELP')+'<br /><br />');
                   HTML.push(DOPBSPBackEnd.text('DISCOUNTS_DISCOUNT_DELETE_ITEM_HELP')+'<br /><br />');
                   HTML.push(DOPBSPBackEnd.text('DISCOUNTS_DISCOUNT_SORT_ITEM_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);

                   DOPBSPBackEndDiscountItems.init();
                   DOPBSPBackEndDiscountItem.init();
                   DOPBSPBackEndDiscountItemRules.init();
                   DOPBSPBackEndDiscountItemRule.init();
                   DOPBSPBackEnd.toggleMessages('success',
                                                DOPBSPBackEnd.text('DISCOUNTS_DISCOUNT_LOADED'));
               })
         .fail(function(data){
             DOPBSPBackEnd.toggleMessages('error',
                                          data.status+': '+data.statusText);
         });
    };

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

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

    /*
     * Edit discount.
     * 
     * @param id (Number): discount 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-discount-ID-'+id+' .dopbsp-name')
                .html(value === ''
                              ? '&nbsp;'
                              : value);
                break;
        }

        switch (type){
            case 'switch':
                value = $('#DOPBSP-discount-'+field+'-'+id)
                .is(':checked')
                        ? 'true'
                        : 'false';
                break;
        }

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

            $.post(ajaxurl,
                   {
                       action: 'dopbsp_discount_edit',
                       id    : id,
                       field : field,
                       value : value,
                       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_discount_edit',
                                                                                             id    : id,
                                                                                             field : field,
                                                                                             value : value,
                                                                                             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 discount.
     * 
     * @param id (Number): discount ID
     */
    this.delete = function(id){
        DOPBSPBackEnd.toggleMessages('active',
                                     DOPBSPBackEnd.text('DISCOUNTS_DELETE_DISCOUNT_DELETING'));

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

                   $('#DOPBSP-discount-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('DISCOUNTS_NO_DISCOUNTS')+'</li></ul>');
                                }
                                DOPBSPBackEnd.toggleMessages('success',
                                                             DOPBSPBackEnd.text('DISCOUNTS_DELETE_DISCOUNT_SUCCESS'));
                            });
               })
         .fail(function(data){
             DOPBSPBackEnd.toggleMessages('error',
                                          data.status+': '+data.statusText);
         });
    };

    return this.__construct();
};