<div class="container-fluid">
    <div>
        <h3>Affiliates Settings</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Users</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Affiliates Settings</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-6 col-md-6">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">Affiliates Settings</h6>
                    <div class="affiliates-settings-alert"></div>
                    <form class="affiliates-settings" method="POST">
                        <div class="float-left">
                            <label for="affiliate_system" class="main-label">Affiliates System</label>
                            <br><small class="admin-info">Users will earn money by inviting users to your site.</small>
                        </div>
                        <div class="form-group float-right switcher">
                            <input type="hidden" name="affiliate_system" value="0" />
                            <input type="checkbox" name="affiliate_system" id="chck-affiliate_system" value="1" <?php echo ($music->config->affiliate_system == '1') ? 'checked': '';?>>
                            <label for="chck-affiliate_system" class="check-trail"><span class="check-handler"></span></label>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <label for="affiliate_type">Affiliate Method</label>
                        <br><small class="admin-info">When the user will earn? Choose the method.</small>
                        <div class="form-group">
                            <select class="form-control show-tick" id="affiliate_type" name="affiliate_type">
                              <option value="0" <?php if($music->config->affiliate_type == '0'){ echo "selected";} ?>>New user is registred</option>
                              <option value="1" <?php if($music->config->affiliate_type == '1'){ echo "selected";} ?>>New user is registred & bought a pro package</option>
                            </select>
                        </div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Amount</label>
                                <input type="text" name="amount_ref" class="form-control" value="<?php echo ($music->config->amount_ref);?>">
                                <small class="admin-info">The price you'll pay for each new referred user. Default 0.10</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Amount % </label>
                                <input type="number" name="amount_percent_ref" class="form-control" value="<?php echo ($music->config->amount_percent_ref);?>">
                                <small class="admin-info">The price you'll pay for each new referred user. After he join any pro package.</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
$(function() {
    $('.switcher input[type=checkbox]').click(function () {
        var configName = $(this).attr('name');
        var hash_id = $('input[name=hash_id]').val();
        var objData = {};
        if ($(this).is(":checked") === true) {
            objData[configName] = $(this).val();
        }
        else{
            if ($('input[name='+configName+']')[0]) {
                objData[configName] = $($('input[name='+configName+']')[0]).val();
            }
        }
        objData['hash_id'] = hash_id;
        $.post('{{CONFIG ajax_url}}/ap/save-settings', objData);
    });

    var setTimeOutColor = setTimeout(function (){});
    $('select').on('change', function() {
         clearTimeout(setTimeOutColor);
        var thisElement = $(this);
        var configName = thisElement.attr('name');
        var hash_id = $('input[name=hash_id]').val();
        var objData = {};
        objData[configName] = thisElement.val();
        objData['hash_id'] = hash_id;
        thisElement.addClass('warning');
        $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
            if (data.status == 200) {
                thisElement.removeClass('warning');
                thisElement.addClass('success');
            } else {
                thisElement.addClass('error');
            }
            var setTimeOutColor = setTimeout(function () {
                thisElement.removeClass('success');
                thisElement.removeClass('warning');
                thisElement.removeClass('error');
            }, 2000);
        });
    });
    $('input[type=color]').on('change', delay(function() {
            clearTimeout(setTimeOutColor);
            var thisElement = $(this);
            var configName = thisElement.attr('name');
            var hash_id = $('input[name=hash_id]').val();
            var objData = {};
            objData[configName] = this.value;
            objData['hash_id'] = hash_id;
            thisElement.addClass('warning');
            $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
                if (data.status == 200) {
                    thisElement.removeClass('warning');
                    thisElement.addClass('success');
                } else {
                    thisElement.addClass('error');
                }
                var setTimeOutColor = setTimeout(function () {
                    thisElement.removeClass('success');
                    thisElement.removeClass('warning');
                    thisElement.removeClass('error');
                }, 2000);
                //thisElement.focus();
            });
    }, 500));
    $('input[type=text], input[type=number], input[type=color] , textarea').on('keyup', delay(function() {
            clearTimeout(setTimeOutColor);
            var thisElement = $(this);
            var configName = thisElement.attr('name');
            var hash_id = $('input[name=hash_id]').val();
            var objData = {};
            objData[configName] = this.value;
            objData['hash_id'] = hash_id;
            thisElement.addClass('warning');
            $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
                if (data.status == 200) {
                    thisElement.removeClass('warning');
                    thisElement.addClass('success');
                } else {
                    thisElement.addClass('error');
                }
                var setTimeOutColor = setTimeout(function () {
                    thisElement.removeClass('success');
                    thisElement.removeClass('warning');
                    thisElement.removeClass('error');
                }, 2000);
                //thisElement.focus();
            });
    }, 500));
}); 
$(document).ready(function() {
    $('#amount_ref').focus(function() { $(this).select(); } );
    $('#amount_percent_ref').focus(function() { $(this).select(); } );
});
$(function() {

    //to ensure that value is Between 0-100
    $('#amount_percent_ref').keyup(function(e) {
        var num = $(this).val();
        if(isNaN(num)||num<0 ||num>100) {
            $('.affiliates-settings-alert').html('<div class="alert alert-danger">Only Enter Number Between 0-100</div>');
            setTimeout(function () {
                $('.affiliates-settings-alert').empty();
            }, 2000);
            $(this).val("0");
        }

        $('#amount_ref').val(0);
    });

    $('#amount_ref').keyup(function(e) {
        var num = $(this).val();
        if(isNaN(num)||num<0) {
            $('.affiliates-settings-alert').html('<div class="alert alert-danger">Only Enter Number</div>');
            setTimeout(function () {
                $('.affiliates-settings-alert').empty();
            }, 2000);
            $(this).val("0");
        }

        $('#amount_percent_ref').val(0);
    });

    //to hide and show inputs according to selected value
    // $('#affiliate_type').change(function(e){
    //     var selected_affiliate_type = $('#affiliate_type :selected').text();
    //     if( selected_affiliate_type == "New user is registred" ){
    //         $('.amount_ref').show();
    //         $('.amount_percent_ref').hide();
    //     }else if( selected_affiliate_type == "New user is registred & bought a pro package" ){
    //         $('.amount_ref').hide();
    //         $('.amount_percent_ref').show();
    //     }
    // });

    
});
</script>