$(document).ready(function () {

    $('#receptions').select2({tags: true});

    function update_table()
    {
        $('#receptions_table').empty();
        $.ajax({
            type: "GET",
            url: '/get_clinic_receptions/' + $('#clinic').val(),
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                for(i in data['receptions'])
                {
                    console.log(data['receptions'][i])
                    $('#receptions_table').append('<tr><td>'+data['receptions'][i]['name']+'</td><td><input class="reception-price" data-rt_id='+data['receptions'][i]['id']+' type="number", value=' +data['receptions'][i]['price']+ '></input></td><tr>')

                    $('.reception-price').change(function(){
                        update_price($(this).data('rt_id'), $(this).val())
                    })
                }
            }
        })
    }

    function update_price(rt_id, price){
        var message = {
                rt_id: rt_id,
                price: price
            }
        console.log(message)
        $.ajax({
            type: "POST",
            url: '/internal_route/update_price',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType\
            data: JSON.stringify(message),
            success: (data) => {
            }
        })
    }

    $('#clinic').change(function(){
        update_table();
    })

    $('#add_reception').click(function(){
        $('#receptions_table').empty();
        var message = {
                reception: $('#receptions').val(),
                price: $('#price').val(),
                clinic_id: $('#clinic').val(),
            }

        $.ajax({
            type: "POST",
            url: '/internal_route/add',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType\
            data: JSON.stringify(message),
            success: (data) => {
                update_table();
            }
        })
    })
});
