$(".alert-dismissible").fadeOut(0);
var table;


$(document).ready(function () {
table = $('#datatable-fixedheader').DataTable({
processing: true,
serverSide: true,
ordering: true,
autoWidth: false,
pageLength: 10,
order: [],
ajax: {
url: BASEURL + 'book_an_appointment/ajax_list',
type: 'POST'
},
columnDefs: [
{ targets: [0,4,5,6,7,8,9], orderable: false }
]
});
});


function reload_table(){
table.ajax.reload(null,false);
}


function category_active(id){
$.post(BASEURL+'book_an_appointment/category_active/'+id,function(){reload_table();});
}


function category_deactive(id){
$.post(BASEURL+'book_an_appointment/category_deactive/'+id,function(){reload_table();});
}


function reject_btn(id){
$('[name="ap_id"]').val(id);
$('#remarks').val('');
$('#modal2').modal('show');
}

function confirm_ap(id){
    if(confirm('Confirm this appointment?')){
        $.post(
            BASEURL + 'book_an_appointment/confirm_appointment/' + id,
            function(){
                reload_table();
            }
        );
    }
}

function view_details(id){
    $.get(BASEURL+'book_an_appointment/get_details/'+id,function(res){
        if(res.status){

            $('#v_name').text(res.data.patient_name);
            $('#v_mobile').text(res.data.patient_mobile);
            $('#v_email').text(res.data.patient_email);
            $('#v_age').text(res.data.patient_age);
            $('#v_date').text(res.data.appontment_date);
            $('#v_time').text(res.data.appontment_time);
            $('#v_doctor').text(res.data.doctor_name);
            $('#v_remarks').text(res.data.remarks ?? '-');

            let cls='status-pending', txt='Pending';
            if(res.data.status==1){cls='status-confirm';txt='Confirmed'}
            if(res.data.status==2){cls='status-reject';txt='Rejected'}

            $('#v_status')
              .removeClass()
              .addClass('status-badge '+cls)
              .text(txt);

            $('#viewModal').modal('show');
        }
    },'json');
}

function reject_appointment(){
if($('#remarks').val()===''){ $('#err_editor').html('Required'); return; }


$.ajax({
url: BASEURL+'Book_An_Appointment/reject_appointment',
type:'POST',
data:$('#reject_ap_form').serialize(),
dataType:'json',
success:function(){ $('#modal2').modal('hide'); reload_table(); }
});
}


$(document).on('click','.delete_btn',function(){
var id=$(this).data('id');
if(confirm('Delete record?')){
$.post(BASEURL+'book_an_appointment/ajax_delete/'+id,function(){reload_table();});
}
});