$(document).ready(function(){

    /*var companyChangedTo = $('#company_changed_to').val();

    if(companyChangedTo !== -1){

    }*/

    var companySelectValue;

    $("#company-select").bind("click", function(e) {
        companySelectValue = $.trim($(this).val());
    }).bind("change", function(e) {
        var ifConfirmed = (companySelectValue === '') ? 1 : confirm("Da li ste sigurni da zelite da promenite firmu kojoj pripada kartica?\nSva parking mesta dodeljena karticibice izgubljena");

        if (ifConfirmed) {
            var curValue = $(this).val();
            if(curValue !== ''){
                // Ajax call
                $.ajax({
                    type: 'GET',
                    url: '/ajax-getcompanyparkings',
                    data: { cid: curValue },
                    dataType: 'json',
                    success: function (data) {
                        if ($.trim(data)){

                            var html = '<div class="fakelabel">Ima pravo parkiranja na parkinzima:</div><br>\n';

                            $.each(data, function(id, name) {

                               html += '<input class="form-check-label" id="parkings['+id+']" name="parkings['+id+']" value="'+id+'" type="checkbox">\n';
                               html += '<label for="parkings['+id+']" class="custom-label noselect"> '+name+' &nbsp; </label>\n';

                            });


                        } else{
                            var html = '<div class="fakelabel">Ima pravo parkiranja na parkinzima:</div><br>Odabrana firma nema zakupljenih mesta\n';
                        }

                        $('#parkings-permissions').html(html);

                    }
                });

            } else {
                $('#parkings-permissions').html('<div class="fakelabel">Ima pravo parkiranja na parkinzima:</div><br>Odabrana firma nema zakupljenih mesta\n');
            }

        } else {
            $(this).val(companySelectValue);
        }
    });



    var formField = '<div class="col-xs-12 col-sm-6 col-md-4 pbb vehicleff">\n' +
                    '   <div class="borderdiv">\n' +
                    '       <table width="100%" cellspacing="3" cellpadding="3">\n' +
                    '           <tr>\n' +
                    '               <th>Tip Vozila</th>\n' +
                    '               <th>Registarski broj</th>\n' +
                    '               <td rowspan="2"><i title="Ukloni vozilo" style="color:#900; font-size: 16px; cursor: pointer;" class="fa fa-trash rvf"></i></td>\n'+
                    '           </tr>\n' +
                    '           <tr>\n' +
                    '               <td>\n' +
                    '                   <input class="form-control" name="nv[vehicle_type][]" value="" type="text">\n' +
                    '               </td>\n' +
                    '               <td>\n' +
                    '                   <input class="form-control" name="nv[licence_plates][]" value="" type="text" style="text-transform:uppercase;">\n' +
                    '               </td>\n' +
                    '           </tr>\n' +
                    '       </table>\n' +
                    '   </div>\n' +
                    '</div>';

    $('#addwehiclefield').click( function () {

        if(confirm('Da li zelite da dodate novo polje za unos vozila')){


            if($('.vehicleff').length){

                $('.vehicleff').last().after(formField);
            } else {
                $('#vehiclesforms').html(formField);
            }

            var max = 0;

            $('#vehiclesforms .pbb').each(function () {
                if($(this).height() > max){max = $(this).height()}
            });
            $('#vehiclesforms .pbb').height(max);

    }

    });


    $(document).on('click', '.rvf', function () {

        if(confirm('Da li stvarno zelite da uklonite vozilo ?')){
           $(this).closest('.vehicleff').slideUp().remove();
        }
    });


    $(document).on('click', '#cardsubmit', function () {

        var vehicles  = $('.vehicleff').length;
        var companyId = $('#company-select').val();
        if(vehicles && companyId){
            $('#card-form').submit();
        } else {
            var msg = 'Ispravite greske:\n\n';
            if(!companyId){ msg += 'Odaberite firmu kojoj kartica pripada\n\n';}
            if(!vehicles) { msg += 'Unesite bar jedno vozilo\n\n';}
            alert(msg);

        }
    });



});