<script>
    function checkDelete(form){
        if(confirm('Anda akan menghapus data. Data yang di hapus tidak dapat dikembalikan'))
        {
            submitLink(form);
        }
    }
    
    function checkSubmit(data,form){
        if(data > 0){
            alert('Customer sudah ada dalam database.');
            return false;
        }
        submitLink(form);
    }
        
    function submitLink(form){
        var form = document.getElementById(form);
        form.submit();
    }

    function loadAppointments(date){
        $.get('/booking-show',{date: date}, function (data,status) {
            var result = '';
            var status = '';
            var myStatus = '';
            var myClass= '';
            var output = '';

            var appointments = data.appointments;
            var bookings = data.bookings;
    
            for(var i=0; i<appointments.length; i++){
                myStatus = 'AVAILABLE';
                myClass= 'available';
                checkedStatus= 'unchecked';
                
                var appointment = appointments[i];
                        
                var current = new Date();
                var requestDate = new Date(date);
    
                var selectedDate = requestDate.getFullYear() + "-" + (requestDate.getMonth()+1) + "-" + requestDate.getDate() + " " + appointment.appointment_time;
                var newSelectedDate = new Date(selectedDate);

                if(newSelectedDate.getTime() < current.getTime()){
                    myStatus = 'RESERVED';
                    status = 'disabled';
                    myClass = 'reserved';
                }
                        
                if(bookings != null){
                    for(var x=0; x<bookings.length; x++){
                        var booking = bookings[x];
                        if(booking.appointment_id == appointment.id){
                            if(booking.status == 1){
                                myStatus = 'RESERVED';
                                status = 'disabled';
                                myClass = 'reserved';
                            }
                            break;
                        }
                    }
                }
                        
                if((i + 1) %2 == 0){
                    output = output + ' | <input type="radio" name="time_sheet" value="' + appointments[i].id + '" ' + status + '><label class="' + myClass + '">&nbsp' + appointments[i].appointment_name + '</label></input><br>';
                }else{
                    output = output + '<input type="radio" name="time_sheet" value="' + appointments[i].id + '" ' + status + '><label class="' + myClass + '">&nbsp' + appointments[i].appointment_name + '</label></input>';
                }
                
                status='';
                myclass='';
                myStatus='';
            }
            $('#radio-appointments').html(output);
        });
    }

    function printDiv(divName) {
        var divToPrint = document.getElementById(divName);
        var htmlToPrint = '' +
            '<style type="text/css">' +
            'table th, table td {' +
            'border:1px solid #000;' +
            'padding:0.5em;' +
            '}' +
            '</style>';
                
        htmlToPrint += divToPrint.outerHTML;
            
        newWin = window.open("");
        newWin.document.write(htmlToPrint);
        newWin.print();
        newWin.close();
    }

</script>