
$(document).ready(function () {
    med_certificate_list();
    loadGeneralForms();
});


$('#BtnSubmit').click(function () {
    debugger;
    if ($('#txtName').val() == "") {
        $('#txtName').focus();
        $('#PtxtName').show();
        $('#PtxtName').fadeOut(8000);
        $('#PtxtName').html('Enter Patinet Name');
        return false;
    }
    if ($('#txtdiagnosis').val() == "") {
        $('#txtdiagnosis').focus();
        $('#Ptxtdiagnosis').show();
        $('#Ptxtdiagnosis').fadeOut(8000);
        $('#Ptxtdiagnosis').html('Enter the Diagnosis');
        return false;
    }
    if ($('#txtFdate').val() == "") {
        $('#txtFdate').focus();
        $('#PtxtFdate').show();
        $('#PtxtFdate').fadeOut(8000);
        $('#PtxtFdate').html('Select the from date');
        return false;
    }
    if ($('#txtDays').val() == "") {
        $('#txtDays').focus();
        $('#PtxtDays').show();
        $('#PtxtDays').fadeOut(8000);
        $('#PtxtDays').html('Enter the no of days');
        return false;
    }
    if ($('#txtEdate').val() == "") {
        $('#txtEdate').focus();
        $('#PtxtEdate').show();
        $('#PtxtEdate').fadeOut(8000);
        $('#PtxtEdate').html('Select the effect date');
        return false;
    }
    var Fdate = $('#txtFdate').val().split('/');
    Fdate = Fdate[2] + '-' + Fdate[1] + '-' + Fdate[0];
    var Edate = $('#txtEdate').val().split('/');
    Edate = Edate[2] + '-' + Edate[1] + '-' + Edate[0];

    var did = $('#BtnSubmit').attr("data-id");
    var navigation = $('#BtnSubmit').attr("data-nav");
    var name = $('#BtnSubmit').attr("data-name");
    var columntype = $('#BtnSubmit').attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             debugger;
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {

                     var medical = {
                         prefix: $('#selPrefix').val(),
                         name: $('#txtName').val(),
                         diagnosis: $('#txtdiagnosis').val(),
                         from_date: Fdate,
                         noof_days: $('#txtDays').val(),
                         effect_date: Edate,
                         created_by: $('#HiddenDoctorID').val(),
                         certificate_type: '1',
                         Dml_Indicator: 'I'
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Medical_Certificate/medical_certificate',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(medical),
                         success: function (data) {
                             var data = JSON.parse(data);
                             if (data.msg == "Success") {
                                 var data = data.data;
                                 if (jQuery.isEmptyObject((data))) {

                                 }
                                 else {
                                     if (Object.keys(data).length > 0) {
                                         //  window.location.href = '/Medical_Certificate/pdf?medid=' + data[0].medical_certificate_id + '&type=' + data[0].certificate_type;
                                         window.location.href = '/PatientDashboard/medical_certificate?medid=' + data[0].medical_certificate_id + '&type=' + data[0].certificate_type + '&Hrole_id=' + $('#hiddenRole_id').val();
                                     }
                                 }
                             }
                             else {
                                 console.log(data);
                             }
                         }
                     });
                 }
             }
         }
     });
});

$('#fit_certificate_tab').click(function () {
    debugger;
    loadcountry()
})


$('#F_BtnSubmit').click(function () {
    debugger;
    if ($('#F_txtName').val() == "") {
        $('#F_txtName').focus();
        $('#PF_txtName').show();
        $('#PF_txtName').fadeOut(8000);
        $('#PF_txtName').html('Enter Patinet Name');
        return false;
    }
    if ($('#F_txtFatherName').val() == "") {
        $('#F_txtFatherName').focus();
        $('#PF_txtFatherName').show();
        $('#PF_txtFatherName').fadeOut(8000);
        $('#PF_txtFatherName').html('Enter The Father Name');
        return false;
    }
    if ($('#F_Address').val() == "") {
        $('#F_Address').focus();
        $('#PF_Address').show();
        $('#PF_Address').fadeOut(8000);
        $('#PF_Address').html('Please Enter Line1 Address');
        return false;
    }

    if ($('#F_country').val() == "" || $('#F_country').val() == 0) {
        $('#F_country').focus();
        $('#Pcountry').show();
        $('#Pcountry').fadeOut(8000);
        $('#Pcountry').html('Select Country');
        return false;
    }

    if ($('#F_state').val() == "" || $('#F_state').val() == 0) {
        $('#F_state').focus();
        $('#Pstate').show();
        $('#Pstate').fadeOut(8000);
        $('#Pstate').html('Select State');
        return false;
    }

    if ($('#F_city').val() == "" || $('#F_city').val() == 0) {
        $('#F_city').focus();
        $('#Pcity').show();
        $('#Pcity').fadeOut(8000);
        $('#Pcity').html('Select City');
        return false;
    }

    if ($('#F_pin_code').val() == "" || $('#F_pin_code').val() == 0) {
        $('#F_pin_code').focus();
        $('#Ppin_code').show();
        $('#Ppin_code').fadeOut(8000);
        $('#Ppin_code').html('Enter the Pin Code');
        return false;
    }

    var did = $('#F_BtnSubmit').attr("data-id");
    var navigation = $('#F_BtnSubmit').attr("data-nav");
    var name = $('#F_BtnSubmit').attr("data-name");
    var columntype = $('#F_BtnSubmit').attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             debugger;
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {

                     var medical = {
                         Dml_Indicator: 'I',
                         prefix: $('#selPrefix').val(),
                         name: $('#F_txtName').val(),
                         fname: $('#F_txtFatherName').val(),
                         Address: $('#F_Address').val(),
                         certificate_type: '2',
                         created_by: $('#HiddenDoctorID').val(),
                         address_line_2: $('#F_address2').val(),
                         area: $('#F_area').val(),
                         country: $('#F_country').val(),
                         state: $('#F_state').val(),
                         city: $('#F_city').val(),
                         pin_code: $('#F_pin_code').val()
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Medical_Certificate/medical_certificate',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(medical),
                         success: function (data) {
                             var data = JSON.parse(data);
                             if (data.msg == "Success") {
                                 var data = data.data;
                                 if (jQuery.isEmptyObject((data))) {

                                 }
                                 else {
                                     if (Object.keys(data).length > 0) {
                                         window.location.href = '/PatientDashboard/medical_certificate?medid=' + data[0].medical_certificate_id + '&type=' + data[0].certificate_type + '&Hrole_id=' + $('#hiddenRole_id').val();
                                     }
                                 }
                             }
                             else {
                                 console.log(data);
                             }
                         }
                     });
                 }
             }
         }
     });
}); 

$('#death_certificate_tab').click(function () {
    debugger;
    loadcountry()
})

$('#D_BtnSubmit').click(function () {
    if ($('#D_txtName').val() == "") {
        $('#D_txtName').focus();
        $('#PD_txtName').show();
        $('#PD_txtName').fadeOut(8000);
        $('#PD_txtName').html('Enter Patient Name');
        return false;
    }
    if ($('#D_txtAge').val() == "") {
        $('#D_txtAge').focus();
        $('#PD_txtAge').show();
        $('#PD_txtAge').fadeOut(8000);
        $('#PD_txtAge').html('Enter the Age');
        return false;
    }
    if ($('#D_regno').val() == "") {
        $('#D_regno').focus();
        $('#D_regno').show();
        $('#PD_regno').fadeOut(8000);
        $('#PD_regno').html('Enter the Reg No');
        return false;
    }
    if ($('#D_txtFname').val() == "") {
        $('#D_txtFname').focus();
        $('#PD_txtFname').show();
        $('#PD_txtFname').fadeOut(8000);
        $('#PD_txtFname').html('Enter the Father Name');
        return false;
    }
    if ($('#D_txtMname').val() == "") {
        $('#D_txtMname').focus();
        $('#PD_txtMname').show();
        $('#PD_txtMname').fadeOut(8000);
        $('#PD_txtMname').html('Enter the Monther Name');
        return false;
    }

    if ($('#D_txtReligion').val() == "") {
        $('#D_txtReligion').focus();
        $('#PD_txtReligion').show();
        $('#PD_txtReligion').fadeOut(8000);
        $('#PD_txtReligion').html('Enter the Religion');
        return false;
    }
    if ($('#D_txtDTAdmission').val() == "") {
        $('#D_txtDTAdmission').focus();
        $('#PD_txtDTAdmission').show();
        $('#PD_txtDTAdmission').fadeOut(8000);
        $('#PD_txtDTAdmission').html('Enter the Date and Time of Admission');
        return false;
    }
    if ($('#D_txtDeath').val() == "") {
        $('#D_txtDeath').focus();
        $('#PD_txtDeath').show();
        $('#PD_txtDeath').fadeOut(8000);
        $('#PD_txtDeath').html('Enter Date and Time of Death');
        return false;
    }
    if ($('#D_txtCauseDeath').val() == "") {
        $('#D_txtCauseDeath').focus();
        $('#PD_txtCauseDeath').show();
        $('#PD_txtCauseDeath').fadeOut(8000);
        $('#PD_txtCauseDeath').html('Enter Cause of Death');
        return false;
    }

    if ($('#D_txtAddress').val() == "") {
        $('#D_txtAddress').focus();
        $('#PD_txtAddress').show();
        $('#PD_txtAddress').fadeOut(8000);
        $('#PD_txtAddress').html('Please Enter Line1 Address');
        return false;
    }

    if ($('#D_country').val() == "") {
        $('#D_country').focus();
        $('#PDcountry').show();
        $('#PDcountry').fadeOut(8000);
        $('#PDcountry').html('Select Country');
        return false;
    }

    if ($('#D_state').val() == "") {
        $('#D_state').focus();
        $('#PDstate').show();
        $('#PDstate').fadeOut(8000);
        $('#PDstate').html('Select State');
        return false;
    }

    if ($('#D_city').val() == "") {
        $('#D_city').focus();
        $('#PDcity').show();
        $('#PDcity').fadeOut(8000);
        $('#PDcity').html('Select City');
        return false;
    }

    if ($('#D_pin_code').val() == "") {
        $('#D_pin_code').focus();
        $('#PDpin_code').show();
        $('#PDpin_code').fadeOut(8000);
        $('#PDpin_code').html('Enter the Pin Code');
        return false;
    }

    var did = $('#D_BtnSubmit').attr("data-id");
    var navigation = $('#D_BtnSubmit').attr("data-nav");
    var name = $('#D_BtnSubmit').attr("data-name");
    var columntype = $('#D_BtnSubmit').attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             debugger;
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {

                     var medical = {
                         Dml_Indicator: 'I',
                         prefix: $('#selPrefix').val(),
                         name: $('#D_txtName').val(),
                         age: $('#D_txtAge').val(),
                         gender: $('#D_gender').val(),
                         regno: $('#D_regno').val(),
                         fname: $('#D_txtFname').val(),
                         mname: $('#D_txtMname').val(),
                         Address: $('#D_txtAddress').val(),
                         religion: $('#D_txtReligion').val(),
                         DTAdmission: $('#D_txtDTAdmission').val(),
                         DTDeath: $('#D_txtDeath').val(),
                         cause_death: $('#D_txtCauseDeath').val(),
                         certificate_type: '3',
                         created_by: $('#HiddenDoctorID').val(),
                         address_line_2: $('#D_address2').val(),
                         area: $('#D_area').val(),
                         country: $('#D_country').val(),
                         state: $('#D_state').val(),
                         city: $('#D_city').val(),
                         pin_code: $('#D_pin_code').val()
                     }

                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Medical_Certificate/medical_certificate',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(medical),
                         success: function (data) {
                             var data = JSON.parse(data);
                             if (data.msg == "Success") {
                                 var data = data.data;
                                 if (jQuery.isEmptyObject((data))) {

                                 }
                                 else {
                                     if (Object.keys(data).length > 0) {
                                         window.location.href = '/PatientDashboard/medical_certificate?medid=' + data[0].medical_certificate_id + '&type=' + data[0].certificate_type + '&Hrole_id=' + $('#hiddenRole_id').val();
                                     }
                                 }
                             }
                             else {
                                 console.log(data);
                             }
                         }
                     });
                 }
             }
         }
     });
});



function med_certificate_list() {
    $('#shade_slot').show();
    var DoctorId = $('#HiddenDoctorID').val();
    var Configuration = {
        created_by: DoctorId,
        Dml_Indicator: 'MCL'
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Medical_Certificate/medical_certificate_list',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Configuration),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                var name, value;
                var j = 0, j1 = 0, j2 = 0;
                tr = $('<table id="example_med_cert" class="table table-bordered table-striped text-center" />');
                var urcheck = $('#URight').val();
                tr.append("<thead><tr><th>S No</th><th>Name</th><th>Diagnosis</th><th>No Of Days</th><th>Effect Date</th><th>Issued Date</th><th>Print</th></tr></thead><tbody>");

                tr1 = $('<table id="example_med_cert1" class="table table-bordered table-striped text-center" />');
                var urcheck = $('#URight').val();
                tr1.append("<thead><tr><th>S No</th><th>Name</th><th>Father Name</th><th>Issued Date</th><th>Print</th></tr></thead><tbody>");

                tr2 = $('<table id="example_med_cert2" class="table table-bordered table-striped text-center" />');
                var urcheck = $('#URight').val();
                tr2.append("<thead><tr><th>S No</th><th>Name</th><th>Age/Gender</th><th>Date Of Admission</th><th>Date Of Death</th><th>Cause Of Death</th><th>Print</th></tr></thead><tbody>");
                $.each(data1.Table, function (i, emp) {
                    if (emp.certificate_type == 1) {

                        j++;

                        if (emp.prefix == 1) {
                            name = "Mr." + emp.name;
                        }
                        else if (emp.prefix == 2) {
                            name = "Mrs." + emp.name;
                        }
                        else if (emp.prefix == 3) {
                            name = "Miss." + emp.name;
                        }
                        else {
                        }

                        row_data = "<tr><td>" + j + "</td><td>" + name + "</td><td>" + emp.diagnosis + "</td><td>" + emp.noof_days + "</td><td>" + emp.effect_date + "</td><td>" + emp.created_on + "</td><td><button class='edit btn btn-primary  btn-sm btn-add' data-toggle='popover' title='Print Certificate' onclick=print_certificate(this) data-id=" + emp.medical_certificate_id + " data-name=" + emp.certificate_type + "><i class='fa fa-print' aria-hidden='true'></i></button></td></tr>";
                        tr.append(row_data);
                    }
                    else if (emp.certificate_type == 2) {
                        j1++;

                        if (emp.prefix == 1) {
                            name = "Mr." + emp.name;
                        }
                        else if (emp.prefix == 2) {
                            name = "Mrs." + emp.name;
                        }
                        else if (emp.prefix == 3) {
                            name = "Miss." + emp.name;
                        }
                        else {
                        }

                        row_data = "<tr><td>" + j1 + "</td><td>" + name + "</td><td>" + emp.fname + "</td><td>" + emp.created_on + "</td><td><button class='edit btn btn-primary  btn-sm btn-add' data-toggle='popover' title='Print Certificate' onclick=print_certificate(this) data-id=" + emp.medical_certificate_id + " data-name=" + emp.certificate_type + "><i class='fa fa-print' aria-hidden='true'></i></button></td></tr>";
                        tr1.append(row_data);
                    }
                    else if (emp.certificate_type == 3) {
                        j2++;

                        if (emp.prefix == 1) {
                            name = "Mr." + emp.name;
                        }
                        else if (emp.prefix == 2) {
                            name = "Mrs." + emp.name;
                        }
                        else if (emp.prefix == 3) {
                            name = "Miss." + emp.name;
                        }
                        else {
                        }
                        if (emp.gender == 1) {
                            value = emp.age + " / Male"
                        }
                        else {
                            value = emp.age + " / Female"
                        }

                        row_data = "<tr><td>" + j2 + "</td><td>" + name + "</td><td>" + value + "</td><td>" + emp.DTAdmission + "</td><td>" + emp.DTDeath + "</td><td>" + emp.cause_death + "</td><td><button class='edit btn btn-primary  btn-sm btn-add' data-toggle='popover' title='Print Certificate' onclick=print_certificate(this) data-id=" + emp.medical_certificate_id + " data-name=" + emp.certificate_type + "><i class='fa fa-print' aria-hidden='true'></i></button></td></tr>";
                        tr2.append(row_data);
                    }
                    else {

                    }
                });

                tr.append("</tbody>");
                tr1.append("</tbody>");
                tr2.append("</tbody>");
                $.when($('#tblmed_certificate').append(tr)).then($('#example_med_cert').DataTable({
                    // "columnDefs": [
                    //{
                    //    "type": "numeric-comma", targets: 3,
                    //}

                    // ]
                }));
                $.when($('#tblfitness_certificate').append(tr1)).then($('#example_med_cert1').DataTable({
                }));
                $.when($('#tbldeath_certificate').append(tr2)).then($('#example_med_cert2').DataTable({
                }));
            }
            else {
                console.log(data1);
            }
            $('#shade_slot').hide();
        },

    });
}


function print_certificate(e) {
    debugger;
    var medical_certificate_id = e.getAttribute("data-id")
    var certificate_type = e.getAttribute("data-name");
    window.location.href = '/PatientDashboard/medical_certificate?medid=' + medical_certificate_id + '&type=' + certificate_type + '&Hrole_id=' + $('#hiddenRole_id').val();
}

//load country
function loadcountry() {
    debugger;
    var country = {
        Dml_Indicator: "C"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#F_country').empty();
                    $('#D_country').empty();
                    var Country;
                    //var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#F_country').append(Country);
                    $('#F_country').change();
                    $('#D_country').append(Country);
                    $('#D_country').change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//get state 
$('#F_country').on('change', function () {
    debugger;
    getstate($('#F_country').val());
});

//get state 
$('#D_country').on('change', function () {
    debugger;
    getstate($('#D_country').val());
});

function getstate(ctry_id) {
    debugger;

    var state = {
        country_id: ctry_id,
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var state;
                    $('#F_state').empty();
                    $('#F_city').empty();
                    $('#D_state').empty();
                    $('#D_city').empty();
                    //var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        state = state + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#F_state').append(state);
                    $('#F_state').change();
                    $('#D_state').append(state);
                    $('#D_state').change();
                    //if (stateid != '' && stateid != undefined) {
                    //    $('#state').val(stateid);
                    //}

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#F_state').on('change', function () {
    getcity($('#F_state').val());
})
$('#D_state').on('change', function () {
    getcity($('#D_state').val());
})

function getcity(state_id) {
    debugger;
    
    var state = {
        state_id: state_id,
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var City;
                    $('#F_city').empty();
                    $('#D_city').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#F_city').append(default_opt + City);
                    $('#D_city').append(default_opt + City);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
};



//function loadGeneralForms(val) {
//    debugger;
//    $('#selectform').empty();
//    var forms = {
//        Dml_Indicator: "SGF",
//    }

//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/GetGeneralForms",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(forms),
//        success: function (data) {
//            debugger;
//            var data2 = JSON.parse(data);
//            if (jQuery.isEmptyObject((data2.data))) {

//            }
//            else {

//                if (data2.msg == "Success") {

//                    var data1 = data2.data.Table;

//                    var Form_Name;

//                    var default_opt = '<option value="0">--Select--</option>';
//                    $.each(data1, function (i, emp) {

//                        Form_Name = Form_Name + '<option value="' + emp.Formid + '" data-id="' + emp.is_multiple + '">' + emp.Form_Name + '</option>';

//                    });
//                    $('#selectform').append(default_opt + Form_Name);
//                    if (val != 0) {
//                        $('#selectform').val(val).prop('disabled', true);;
//                    }
//                }
//                else {
//                    console.log(data);
//                }
//            }
//        }
//    });

//}


//$('#selectform').change(function () {
//    debugger;
//    var selectid = $('#selectform').val()
//    selectedOptionText = $(this).find('option:selected').text();

//    $('#formShow_header').html(selectedOptionText);

//    $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + selectid + '?patientid=' + btoa(0) + '&visit_reportid=' + btoa(0) + '')


//})