/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
const USER_TYPE_SUPER_ADMIN      = 1;
const USER_TYPE_ADMIN            = 2;
const USER_TYPE_DISTRIBUTOR      = 3;
const USER_TYPE_USER             = 4;
const USER_TYPE_SUPERVISOR       = 5;
const USER_TYPE_SITE_ENGINEERING = 6;

$( function() {
    $( '.js-validation-error' ).each(function() {
        $( this ).prevAll( '.form-control' ).addClass( 'is-invalid' );
        $( this ).prevAll().find( 'input[type=radio]' ).addClass( 'is-invalid' );
        $( this ).prevAll().find( 'input[type=checkbox]' ).addClass( 'is-invalid' );
        $( this ).prevAll().find( '.datetimepicker-input' ).addClass( 'is-invalid' );
        $( this ).addClass( 'display-block' );

    });

    $( '.js-select2' ).select2();

    $( '.hide' ).hide();

    $( '.show' ).show();

    $( '.js-alert' ).delay(7000).slideUp(200, function() {
        $( this ).alert( 'close' );
    });

    $( '[data-toggle="tooltip"]' ).tooltip();

    bsCustomFileInput.init();

    // $( '.js-toggle-switch' ).bootstrapToggle();

    $( '.js-summernote' ).summernote( {
        height: 250
    } );
});

function showAlertMessage( strAlert, strAlertMessage, strElement = '.js-show-alert-message', intDelayMessage = 7000 ) {

    var strIcon = '';
    if ( 'success' == strAlert) {
        strIcon = 'check';
    } else if ( 'danger' == strAlert) {
        strIcon = 'ban';
    } else if ( 'warning' == strAlert) {
        strIcon = 'exclamation-triangle';
    } else if ( 'info' == strAlert) {
        strIcon = 'info';
    }

    if( false == validateValue( strElement ) ) {
        strElement = '.js-show-alert-message';
    }

    if ( '' != strIcon) {
        var strHtml = '<div class="alert alert-' + strAlert + ' alert-dismissible js-alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><i class="icon fas fa-' + strIcon + '"></i> ' + strAlertMessage + '</div>';
        $( 'html, body' ).animate({ scrollTop: 0 }, 'slow' );
        $( strElement ).html( '' );
        $( strElement ).html(strHtml);
        $( '.js-alert' ).fadeIn().delay(intDelayMessage).fadeOut(function() {
            $( this ).remove();
        });
    }
}

function validateEmail( strEmail ) {
    
    var strEmailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if( strEmailFormat.test( strEmail ) ) {
        return true;
    } else {
        return false;
    }
}

function validateMobileNumber( intMobileNumber ) {
    var strNumberFormat = /^\d{10}$/;
    if( strNumberFormat.test( intMobileNumber ) ) {
        return true;
    } else {
        return false;
    }

}

function validateAmount( ths ) {
    
    if( /\D/g.test( ths.value ) ) {
        // Filter non-digits from input value.
        ths.value = ths.value.replace( /\D/g, '' );
    }
}

function validateArray( arrmixValue ) {
    if( Array.isArray( arrmixValue ) && 0 < arrmixValue.length ) {
        return true;
    }

    return false;
}

function validateString( strValue ) {

    if( true == validateValue( strValue ) && 'string' == typeof strValue ) {
        return true;
    }

    return false;
}

function validateInt(  intValue ) {

    if ( true == validateValue( intValue ) && true == ( /^\d+$/.test( intValue ) ) ) {
        return true;
    }

    return false;
}

function validateValue( mixValue ) {

    if ( '' != mixValue && null != mixValue && undefined != mixValue ) {
        return true;
    }

    return false;
}

function addValidationErrorInputField( strElement, strErrorMessage = '' ) {
    if ( '' == strErrorMessage ) {
        strErrorMessage = 'The ' + $(strElement).prev().text().replace(":", "" ) + ' field is required.';
    }

    $( strElement ).after( '<span class="error invalid-feedback js-validation-error">' + strErrorMessage + '</span>' );
    $( strElement ).addClass( 'is-invalid' );
}

function addValidationErrorSelect2Field( strElement, strErrorMessage = '' ) {
    if ( '' == strErrorMessage ) {
        strErrorMessage = 'The ' + $(strElement).prev().text().replace(":", "" ) + ' field is required.';
    }
    $( strElement ).next( '.select2-container' ).after( '<span class="error invalid-feedback js-validation-error">' + strErrorMessage + '</span>' );
    $( strElement ).addClass( 'is-invalid' );
}

function addValidationErrorRadioField( strElement, strErrorMessage = '' ) {
    /**
     * The JS code will work for this type of radio-button
     * 
        <label class="control-label label-required" for="js-gender-label">Message</label><br>
        <div class="custom-control custom-radio d-inline">
            <input class="custom-control-input custom-control-input-success custom-control-input-outline js-radio js-message-type" type="radio" id="js-message-type-read" name="message_type" value="read">
            <label for="js-message-type-read" class="custom-control-label">Read</label>
        </div>
        <div class="custom-control custom-radio d-inline">
            <input class="custom-control-input custom-control-input-success custom-control-input-outline js-radio js-message-type" type="radio" id="js-message-type-write" name="message_type" value="write">
            <label for="js-message-type-write" class="custom-control-label">Write</label>
        </div>
    */
    if( false == validateValue( strErrorMessage ) ) {
        strErrorMessage = 'The ' + $(strElement).parent().prev().prev( 'label' ).text().replace(":", "" ) + ' field is required.';
    }

    $( strElement ).parent().next( '.custom-radio:last' ).after( '<span class="error invalid-feedback js-validation-error">' + strErrorMessage + '</span>' );
    $( strElement ).parent().next( '.custom-radio:last' ).addClass( 'is-invalid' );
    $( strElement ).addClass( 'is-invalid' );
}

function addOnlyIsInvalidClass( strElement ) {
    $( strElement ).addClass( 'is-invalid' );
}

function removeValidationErrors() {
    $( '.js-validation-error' ).remove();
    $( '.form-control' ).removeClass( 'is-invalid' );
    $( '.custom-control-input' ).removeClass( 'is-invalid' );
    $( '.custom-radio' ).removeClass( 'is-invalid' );
    $( '.custom-file-input' ).removeClass( 'is-invalid' );
}

function resetDeleteConfirmationModal() {
    $("#js-modal-body-hidden-id" ).val(0);
    $("#js-modal-body-delete-confirm-header-name" ).html( '' );
    $( '#js-modal-body-delete-confirmation-extra-message' ).html( '' );
}

function showDeleteConfirmationModal( strHeaderName, intHiddenId, strExtraMessage = '', intExtraId = null ) {
    $( "#js-modal-body-delete-confirm-header-name" ).html( strHeaderName );
    $( "#js-modal-body-delete-confirm-hidden-id" ).val( intHiddenId );
    $( "#js-modal-body-delete-confirm-hidden-extra-id" ).val( intExtraId );
    $( '#js-modal-body-delete-confirmation-extra-message' ).html( strExtraMessage );
    $( "#js-modal-delete-confirmation" ).modal( 'show' );
}

function getCityListByStateId( intStateId, intHiddenCityId = '', objElement = '#js-city-id' ) {

    $.ajax({
        type: "POST",
        url: getBaseUrl() + 'fetch-city-list-by-state-id',
        data: { 'state_id': intStateId, 'hidden_city_id': intHiddenCityId },
        dataType: "html",
        success: function( objResult ) {
            var strHtml = $.parseJSON( objResult );
            $( objElement ).html( '<option disabled selected> Select City</option>' );
            $( objElement ).append( strHtml );
        }
    });
}