jQuery(document).ready(function($) {
    //ESTIMATION BOX FIXED
    var w = $(window).width();
    var wBox = $('#estimation-box').width();
    if(w >= 541){
        $(window).scroll(function(e){
            windowTop = $(window).scrollTop();
            var boxWidth = $('#estimation-box').width();
            if(windowTop >= 200){
                $('#estimation-box').addClass('fixed-box-c').width(boxWidth);
            }else{
                $('#estimation-box').removeClass('fixed-box-c');
            }
        });
    }else{
        $('#estimation-box').css('right', -wBox+'px');
    }
    $('.show-box').on('click', function(e){
        $(this).hide();
        $('#estimation-box').css('right', '0px');
    });
    $('.close-box').on('click', function(e){
        $('#estimation-box').css('right', -wBox+'px');
        $('.show-box').show();
    });
    //TOGGLE BOX
    $('.btn-comment-box').on('click', function(e){
        var box = $(this).data('box');
        $(box).toggle();
    });
    //form-check-label ACTIVE CLASS & TOTAL
    $(document).on('click', '.form-check-input:not(.form-check-input-home)', function(e){
        const input = $(this);

        let total = 0;
        let name = $(this).data('name');
        let type = $(this).data('type');
        let pr = $(this).data('price');
        let id = $(this).data('id');
        let ch = 'unic';

        //var price = $(this).data('price');

        if($(this).is(':checked')){
            $(this).addClass('input-item-active');
        }else{
            $(this).removeClass('input-item-active');
        }

        if($(this).attr('type') == 'radio'){
            $(document).find('#item-line-'+id).remove();
            var wrap = $(this).data('wrap');
            $(wrap).find('.form-check-label').removeClass('active-opt');
        }else{
            ch = 'multiple';
            if($("#item-line-"+id).length >= 1) {
                var oldName = $(document).find('#item-line-'+id).data('name');
                var oldPrice = $(document).find('#item-line-'+id).data('price');
                $(document).find('#item-line-'+id).remove();
                if($(input).hasClass('input-item-active')){
                    name = oldName+', '+name;
                    pr += oldPrice;
                }else{
                    name = oldName.replace(', '+name, '').replace(name+', ', '');
                    pr = oldPrice-pr;
                }
            }
        }

        if($(this).is(':checked')){
            $(this).parent().parent().addClass('active-opt');
        }else{
            $(this).parent().parent().removeClass('active-opt');
        }

        $('.bd-total-c span').text(total);
        //total
        $('#cost-sections').find('.active-opt').each(function(e){
            var price = $(this).find('.form-check-input').data('price');
            total += price;
            $('.bd-total-c span').text(total);
            $('#total').val(total);
        });
        let newLine = '<li id="item-line-'+id+'" class="list-group-item d-flex justify-content-between align-items-start" data-name="'+name+'" data-type="'+type+'" data-price="'+pr+'" data-ch="'+ch+'">'+
            '<div class="ms-2 me-auto">'+
                '<div class="fw-bold item-title-t"><span>'+name+'</span> <small>'+type+'</small></div>'+
            '</div>'+
            '<span class="badge bg-primary rounded-pill">'+pr+'€</span>'+
        '</li>';
        $('.list-group-items').prepend(newLine);

        if($(input).attr('type') == 'checkbox'){
            if($(document).find('#item-line-'+id).data('price') == 0){
                $(document).find('#item-line-'+id).remove();
            }
        }
        $('#boxTotal').val($('#list-group-wrap').html());
    });
    //RANGE
    $('.form-range-input').on('input', function(e){
        var num = $(this).val();
        var item = $(this).data('item');
        $(item).text(num);
    });
    //LINK SESSION
    $(document).on('click', '.link-session-s', function(e){
        var r = confirm("Segur que vols continuar? Es borraran les dades actuals");
        if (r == true) {
            return true;
        } else {
            return false;
        }
    });
});