$(document).ready(function () {
    table_package();
    $('.Consulting').hide();
    $('.Lab_Panel').hide();
    $('.Pharmacy_Details').hide();
    $('.Package_Category').hide();
    $('.Vaccination').hide();
});

$('#package_type').change(function () {
    debugger;
    var values = $('#package_type').val();
    var opt = '';
    $('#Package_category').empty();
    if (values == 1) {
        opt = opt + '<option value="0">--Select--</option><option value="3">Doctor Home Visit</option><option value="4">Nurse Home Visit</option><option value="5">Physio Home Visit</option><option value="7">Multi Package Creation</option>';
        $('#Package_category').html(opt);
    }
    else if (values == 2) {
        opt = opt + '<option value="0">--Select--</option><option value="1">Health Package </option><option value="2">Master Health Check up</option>';
        $('#Package_category').html(opt);
    }
    else {
        $('#Package_category').html('<option value="0">--Select--</option>');
    }
});
function category() {
    var values = $('#package_type').val();
    var opt = '';
    $('#Package_category').empty();
    if (values == 1) {
        opt = opt + '<option value="0">--Select--</option><option value="3">Doctor Home Visit</option><option value="4">Nurse Home Visit</option><option value="5">Physio Home Visit</option><option value="7">Multi Package Creation</option>';
        $('#Package_category').html(opt);
    }
    else if (values == 2) {
        opt = opt + '<option value="0">--Select--</option><option value="1">Health Package </option><option value="2">Master Health Check up</option>';
        $('#Package_category').html(opt);
    }
    else {
        $('#Package_category').html('<option value="0">--Select--</option>');
    }
}

$('#package_Never_Expiry').change(function () {
    if ($('#package_Never_Expiry').prop("checked") == true) {
        $('#package_validity_date').val('');
        $("#package_validity_date").prop("disabled", true);
    }
    else {
        $("#package_validity_date").prop("disabled", false);
    }
})
function expiry()
{
    if ($('#package_Never_Expiry').prop("checked") == true) {
        $('#package_validity_date').val('');
        $("#package_validity_date").prop("disabled", true);
    }
    else {
        $("#package_validity_date").prop("disabled", false);
    }
}

$('#consult_flag').change(function () {
    if($('#consult_flag').val() == "1")
    {
        if ($('#btnAddPackage').html() == "Save Package")
        {
            $('#no_of_consult,#con_walkin,#consut_video,#consult_chat').attr("disabled", false);
        }  
    }
    else
    {
        if ($('#btnAddPackage').html() == "Save Package")
        {
            $('#no_of_consult').val('');
            $('#con_walkin,#consut_video,#consult_chat').prop("checked", 0);
            $('#no_of_consult,#con_walkin,#consut_video,#consult_chat').attr("disabled", true);
        }  
    }
});


$('#btnAddPackage').click(function () {
    debugger;
    if ($.trim($('#package_type').val()) == "" || $('#package_type').val()=='0') {
        $('#Ppackage_type').show();
        $('#Ppackage_type').fadeOut(8000);
        $('#Ppackage_type').html('Enter Package Type');
        $('#Ppackage_type').focus();
        $('#Ppackage_type').val('');
        return false;
    }
    if ($.trim($('#Package_category').val()) == "" || $('#Package_category').val() == '0') {
        $('#Ppackage_category').show();
        $('#Ppackage_category').fadeOut(8000);
        $('#Ppackage_category').html('Enter Package Category');
        $('#Ppackage_category').focus();
        $('#Ppackage_category').val('');
        return false;
    }
    if ($.trim($('#packagename').val()).length == "") {
        $('#Ppackagename').show();
        $('#Ppackagename').fadeOut(8000);
        $('#Ppackagename').html('Enter Package Name');
        $('#packagename').focus();
        $('#packagename').val('');
        return false;
    }
    var chpackage_Never_Expiry = 0;
    if ($('#package_Never_Expiry').prop("checked") == true) {
        chpackage_Never_Expiry = 1;
    }
    if ($.trim($('#package_validity_date').val()).length == "" && chpackage_Never_Expiry == 0) {
        $('#Ppackage_validity_date').show();
        $('#Ppackage_validity_date').fadeOut(8000);
        $('#Ppackage_validity_date').html('Select Package Validity Date');
        $('#package_validity_date').focus();
        $('#package_validity_date').val('');
        return false;
    }
    if ($.trim($('#package_expire_date').val()).length == "") {
        $('#Ppackage_expire_date').show();
        $('#Ppackage_expire_date').fadeOut(8000);
        $('#Ppackage_expire_date').html('Enter Expiry Days');
        $('#package_expire_date').focus();
        $('#package_expire_date').val('');
        return false;
    }
    if ($.trim($('#packageprice').val()).length == "") {
        $('#Ppackageprice').show();
        $('#Ppackageprice').fadeOut(8000);
        $('#Ppackageprice').html('Enter Package Price');
        $('#packageprice').focus();
        $('#packageprice').val('');
        return false;
    }
    if ($.trim($('#DPackagehighlights').val()).length == "") {
        $('#PDPackagehighlights').show();
        $('#PDPackagehighlights').fadeOut(8000);
        $('#PDPackagehighlights').html('Enter Package Highlights');
        $('#DPackagehighlights').focus();
        $('#DPackagehighlights').val('');
        return false;
    }
    if ($.trim($('#DPackagedescription').val()).length == "") {
        $('#PDPackagedescription').show();
        $('#PDPackagedescription').fadeOut(8000);
        $('#PDPackagedescription').html('Enter Package Description');
        $('#DPackagedescription').focus();
        $('#DPackagedescription').val('');
        return false;
    }
    var SelPackage_category = $('#Package_category').val();
    if (SelPackage_category == 1 || SelPackage_category == 2) {

        if ($('#consult_flag').val() == "") {
            $('#Pconsult_flag').show();
            $('#Pconsult_flag').fadeOut(8000);
            $('#Pconsult_flag').html('Select Consulting Type');
            $('#consult_flag').focus();
            $('#consult_flag').val('');
            return false;
        }
        if ($.trim($('#no_of_consult').val()).length > 0) {
            if (($('#con_walkin').prop("checked") == true) || ($('#consut_video').prop("checked") == true) || ($('#consult_chat').prop("checked") == true)) {
            }
            else {
                $('#Pno_of_consult1').show();
                $('#Pno_of_consult1').fadeOut(8000);
                $('#Pno_of_consult1').html('Select Any One of Appointment Type');
                $('#no_of_consult').focus();
                return false;
            }
        }
        if ($.trim($('#no_of_types_lab').val()).length > 0) {
            if ($.trim($('#panelname').val()).length > 0 && ($.trim($('#panel_cost').html()).length > 0)) {
            }
            else {
                $('#Ppanelname').show();
                $('#Ppanelname').fadeOut(8000);
                $('#Ppanelname').html('Enter Panel Name');
                $('#panelname').focus();
                $('#panelname').val('');
                return false;
            }
        }
        if ($.trim($('#panelname').val()).length > 0 && ($.trim($('#panel_cost').html()).length > 0)) {
            if ($.trim($('#no_of_types_lab').val()).length > 0) {
            }
            else {
                $('#Pno_of_types_lab').show();
                $('#Pno_of_types_lab').fadeOut(8000);
                $('#Pno_of_types_lab').html('Enter the Number of Test');
                $('#no_of_types_lab').focus();
                $('#no_of_types_lab').val('');
                return false;
            }
        }
        if ($.trim($('#phar_discount').val()).length > 0) {
            if ($.trim($('#noofFreeDelphar').val()).length == "") {
                $('#PnoofFreeDelphar').show();
                $('#PnoofFreeDelphar').fadeOut(8000);
                $('#PnoofFreeDelphar').html('Enter No of Free Deliveries');
                $('#noofFreeDelphar').focus();
                $('#noofFreeDelphar').val('');
                return false;
            }
        }
        if (($.trim($('#noofFreeDelphar').val()).length == 0) && ($.trim($('#no_of_types_lab').val()).length == 0) && ($.trim($('#no_of_consult').val()).length == 0)) {
            $('#Pno_of_consult1').show();
            $('#Pno_of_consult1').fadeOut(8000);
            $('#Pno_of_consult1').html('Enter No of free consultings');
            $('#no_of_consult').focus();
            $('#PnoofFreeDelphar').show();
            $('#PnoofFreeDelphar').fadeOut(8000);
            $('#PnoofFreeDelphar').html('Enter No of Free Deliveries');
            $('#noofFreeDelphar').focus();
            $('#Pno_of_types_lab').show();
            $('#Pno_of_types_lab').fadeOut(8000);
            $('#Pno_of_types_lab').html('Enter the Number of Test');
            $('#no_of_types_lab').focus();
            return false;

        }
    }
    else {
        if ($('#noofvisit').val() == "") {
            $('#Pnoofvisit').show();
            $('#Pnoofvisit').fadeOut(8000);
            $('#Pnoofvisit').html('Select Consulting Type');
            $('#noofvisit').focus();
            $('#noofvisit').val('');
            return false;
        }
    }
    if (($.trim($('#packagename,#package_validity_date,#packageprice,#package_expire_date').val()).length) > 0)
    {
        package_details_insert_update_delete();
    }

});

$('#packageimage').change(function () {
    packageimageupload();
});


function packageimageupload() {
    var uploadpath = $('#packageimage').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (window.FormData !== undefined) {
        var fileUpload = $("#packageimage").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }
        fileData.append('username', 'Manas');
        $.ajax({
            url: '/PackageDetails/UploadPackageImage',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                $.each(result, function (i, emp) {
                    $('#photolog').empty();
                    var d, n, drloimage;
                    d = new Date();
                    n = d.getTime();
                    drloimage = $('#hdnfilepath1').val() + $('#HospitalId').val() + '/Package/' + emp + '?' + n;
                    $('#photolog').append('<a href=" ' + drloimage + '" target="_blank">Image</a>');
                    $('#packimageid').val(emp);
                });
            }
        });
    }
}

function package_details_insert_update_delete() {
    debugger;
    var lab_panels = [];
    var Package_Category = [];
    var object = {
        hightight: "",
        description: "",
        inclusion: [],
        exclusion: [],
        salesText: "",
        tmc: []
    }


    var k = 0;
    var visited_day = '', Description = '';
    $('#tblnoofvisit tr').each(function (i, row) {
        $(this).find('input').each(function () {
            k++;
            if (k == 1) {
                visited_day = visited_day + $(this).val() + "$";
            }
            else {
                Description = Description + $(this).val() + "$";
                k = 0;
            }
        })
    });
    $('.exclusion_name').each(function () {
        var name1 = $(this).text();
        object.exclusion.push(name1);
    });
    $('.inclusion_name').each(function () {
        var name2 = $(this).text();
        object.inclusion.push(name2);
    });
    $('.tc_name').each(function () {
        var name3 = $(this).text();
        object.tmc.push(name3);
    });

    var discount_type = '', video = 2, walkin = 2, chat = 2, home_colection = 2, indicator = '';
    if ($('#con_walkin').prop("checked") == true) {
        walkin = 1;
    }
    if ($('#consut_video').prop("checked") == true) {
        video = 1;
    }
    if ($('#consult_chat').prop("checked") == true) {
        chat = 1;
    }
    if ($('#Chkhmecollected').prop("checked") == true) {
        home_colection = 1;
    }
    if ($('#Chkhmecollected').prop("checked") == true) {
        home_colection = 1;
    }
    if ($.trim($('#DPackagehighlights').val()).length > 0) {
        object.hightight = $('#DPackagehighlights').val();
    }
    if ($.trim($('#DPackagedescription').val()).length > 0) {
        object.description = $('#DPackagedescription').val();
    }
    if ($.trim($('#DPackagedescriptionsalestext').val()).length > 0) {
        object.salesText = $('#DPackagedescriptionsalestext').val();
    }
    if ($('#btnAddPackage').html() == "Update Package") {
        if ($('#package_type').val() == '1' && $('#Package_category').val() == '7') {
            indicator = "MPDU";
        }
        else {
            indicator = "PDU";
        }
    }
    else {
        if ($('#package_type').val() == '1' && $('#Package_category').val() == '7') {
            indicator = "MPDI";
        }
        else {
            indicator = "PDI";
        }
    }

    //if ($('#btnAddPackage').html() == "Update Package") {
    //    indicator = "PDU";
    //}
    //else {
    //    indicator = "PDI";
    //}
    var package_Never_Expiry = 0;
    if ($('#package_Never_Expiry').prop("checked") == true) {
        package_Never_Expiry = 1;
    }

    var package_data = {
        package_name: $('#packagename').val(),
        package_id: $('#package_id').val(),
        package_image_url: $('#packimageid').val(),
        package_validity_date: $('#package_validity_date').val(),
        validity_period_date: $('#package_expire_date').val(),
        price: $('#packageprice').val(),
        discount: $('#package_discount').val(),
        discount_type: $("select").val(),
        appointment_status: $('#consult_flag').val(),
        no_of_consutation: $('#no_of_consult').val(),
        video: video,
        walkin: walkin,
        chat: chat,
        phar_discount: $('#phar_discount').val(),
        phar_discount_type: $('select.pharma').val(),
        phar_no_of_free_delivery: $('#noofFreeDelphar').val(),
        lab_panel_id: $('#panelname').val().split('/')[0],
        lab_panel_cost: $('#panel_cost').html(),
        lab_no_of_package: $('#no_of_types_lab').val(),
        lab_home_collection: home_colection,
        lab_test_advice: $('#testadvice').val(),
        highlight_description_type: [],
        highlight_description: JSON.stringify(object),
        indicator_id: indicator,
        doctor_id: $('#HiddenDoctorID').val(),
        lab_panels: (JSON.stringify(lab_panels)),
        package_home_care_description: Description,
        visited_day: visited_day,
        never_expiry: package_Never_Expiry,
        package_type: $('#package_type').val(),
        package_category: $('#Package_category').val(),
        vaccination_name: $('#txtVaccination').val(),

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var b = data1.Table;
                    if (b[0].msg == "already") {
                        toastr.warning("Package Already Exists");
                        
                    }
                    else {
                        //alert("Package Saved Successfully");
                        swal({
                            title: "Good job!",
                            text: "You clicked the button!",
                            icon: "success",
                            button: "Aww yiss!",
                        });
                        location.reload();
                    }
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}


$('#btnAddinclusions').click(function () {
    if ($('#DPackageinclusion').val().length > 0) {
        var input = '';
        var val = $('#DPackageinclusion').val();
        $('#DPackageinclusion').val(input);
        var id = "inclusiontr" + $('#tableinclusion')[0].rows.length;
        row_data = "<tr id=" + id + "><td class='inclusion_name'>" + val + "</td><td><span class='btn deleteicon-btn btn-xs' id='delPackage' onclick=delPackinclusion('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td></tr>";
        $('#tableinclusion').append(row_data);
    }
    else {

    }
});

$('#btnAddexclusions').click(function () {
    if ($('#DPackageexclusion').val().length > 0) {
        var input = '';
        var val = $('#DPackageexclusion').val();
        $('#DPackageexclusion').val(input);
        var id = "exclusiontr" + $('#tableexclusion')[0].rows.length;
        row_data = "<tr  id=" + id + "><td class='exclusion_name'>" + val + "</td><td><span class='btn deleteicon-btn btn-xs' id='delPackexclusion' onclick=delPackexclusion('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td></tr>";
        $('#tableexclusion').append(row_data);
    }
    else {

    }
});

$('#btnDPackagetc').click(function () {
    if ($('#DPackagetc').val().length > 0) {
        var input = '';
        var val = $('#DPackagetc').val();
        $('#DPackagetc').val(input);
        var id = "tnctr" + $('#termsandconditions')[0].rows.length;
        row_data = "<tr id=" + id + "><td class='tc_name'>" + val + "</td><td><span class='btn deleteicon-btn btn-xs' id='delPacktc' onclick=delPacktc('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td>";
        $('#termsandconditions').append(row_data);
    }
    else { }
});


function delPackinclusion(id) {
    var check = confirm("Are you sure?");
    if (check) $("#" + id).remove()
}

function delPackexclusion(id) {
    var check = confirm("Are you sure?");
    if (check) $("#" + id).remove()
}

function delPacktc(id) {
    var check = confirm("Are you sure?");
    if (check) $("#" + id).remove()
}

function table_package() {
    var package_data = {
        indicator_id: "PDST"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table class="table table-bordered table-striped table-responsive" id="package_list_table" />');
                    tr.append("<thead><tr><th>S No</th><th>Package Id</th><th>Package Name</th><th>Package Valid Date</th><th>Price(Rs)</th><th>Status</th><th>Actions</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                // else if (data.length > 0) {
                else{
                    $('#package_list').empty();
                    var j = 0;

                    var b = data1.Table;
                    tr = $('<table class="table table-bordered table-striped table-responsive" id="package_list_table" />');
                    tr.append("<thead><tr><th>S No</th><th>Package Id</th><th>Package Name</th><th>Package Valid Date</th><th>Price(Rs)</th><th>Status</th><th>Actions</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        j++;
                        var package_validity_date = "-", validity_period_days = "-";
                        if (emp.package_validity_date == null || emp.package_validity_date == '') {
                            package_validity_date = "-"
                        }
                        else {
                            package_validity_date = emp.package_validity_date;
                        }
                        if (emp.package_status == "0") {
                            validity_period_days = "Inactive";
                        }
                        else {
                            validity_period_days = "Active";
                        }
                        if (emp.package_status == "1") {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.package_id + "</td><td>" + emp.package_name + "</td><td>" + package_validity_date + "</td><td>" + emp.price + "</td><td>" + validity_period_days + "</td><td><button class='btn addicon-btn btn-xs mr-20' onclick=package_edit('" + emp.package_id + "')><i class='fa fa-eye' aria-hidden='true'></i></button><button class='btn deleteicon-btn btn-xs' onclick=package_delete('" + emp.package_id + "')><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";
                            tr.append(row_data);
                        }
                        else {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.package_id + "</td><td>" + emp.package_name + "</td><td>" + package_validity_date + "</td><td>" + emp.price + "</td><td>" + validity_period_days + "</td><td><span></span><span></span></td></tr>";
                            tr.append(row_data);
                        }
                    });
                    tr.append("</tbody>");
                    $.when($('#package_list').append(tr)).then($('#package_list_table').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                            "responsive": true
                        }]
                    }));
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}

function package_delete(val) {
    var r;
    r = confirm("Are you sure you want to delete this package ?");
    if (r == true) {
        var package_data = {
            indicator_id: "PDD",
            package_id: val
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/PackageDetails/package_ins_upd_del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    location.reload();
                }
                else {
                    console.log(data);
                }
            },
        });
    }
    else
    {
        toastr.success("Thank you!!!");
    }
}


function package_edit(val) {
    $("#radioBtn > a.active").removeClass("active").addClass("not-active");
    $('html, body').animate({
        scrollTop: $("#packagenameHead").offset().top
    }, 2000);

    var package_data = {
        indicator_id: "PDS",
        package_id: val
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {

                }
                    //else if (data.length > 0) {
                else {
                    $("#btnAddPackage").hide();
                    var package_details, highlight_details, cosult_details, lab_deatils, pharmadetails, cv;
                    console.log(data1);
                    package_details = data1.Table;
                    highlight_details = data1.Table1;
                    cosult_details = data1.Table2;
                    lab_deatils = data1.Table3;
                    pharmadetails = data1.Table4;
                    homevisit = data1.Table5;
                    $.each(package_details, function (i, emp) {
                        var package_validity_date = emp.package_validity_date;
                        var validity_period_date = emp.validity_period_days;
                        if (emp.never_expiry == 1) {
                            $("#package_Never_Expiry").prop("checked", true)
                            expiry();
                        }
                        else {
                            $('#package_validity_date').val(package_validity_date.split('T')[0]);
                        }
                        $('#package_expire_date').val(validity_period_date.split('T')[0]);
                        $('#packagename').val(emp.package_name),
                        $('#packageprice').val(emp.price);
                        $('#package_discount').val(emp.discount);
                        $("select").val(emp.discount_type);
                        $('#package_id').val(emp.package_id);
                        $('#photolog').empty();
                        category();
                        $('#Package_category').val(emp.package_category);
                        package_category();
                        if (emp.package_image_url == null || emp.package_image_url == "") {
                        }
                        else {
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            drloimage = $('#hdnfilepath1').val() + $('#HospitalId').val() + '/Package/' + emp.package_image_url + '?' + n;
                            $('#photolog').append('<a href= "' + drloimage + '" target="_blank">Image</a>');
                            $('#packimageid').val(emp.package_image_url);
                        }
                    });

                    $.each(highlight_details, function (i, emp) {
                        cv = JSON.parse(emp.description);
                        $('#DPackagehighlights').val(cv.hightight);
                        $('#DPackagedescription').val(cv.description);
                        $('#DPackagedescriptionsalestext').val(cv.salesText);
                        $('#tableinclusion,#tableexclusion,#termsandconditions').empty();
                        $('a[data-toggle=consult_flag][data-title="' + emp.appointment_status + '"]').removeClass('not-active');
                        $.each(cv.inclusion, function (j, emp1) {
                            var id = "inclusiontr" + $('#tableinclusion')[0].rows.length;
                            row_data = "<tr  id=" + id + "><td class='inclusion_name'>" + emp1 + "</td><td><span class='btn btn-action del-pd btn-xs' id='delPackage'  onclick=delPackinclusion('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td></tr>";
                            $('#tableinclusion').append(row_data);
                        });
                        $.each(cv.exclusion, function (j, emp1) {
                            var id = "exclusiontr" + $('#tableinclusion')[0].rows.length;
                            row_data = "<tr  id=" + id + "><td class='exclusion_name'>" + emp1 + "</td><td><span class='btn btn-action del-pd btn-xs' id='delPackexclusion'  onclick=delPackexclusion('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td></tr>";
                            $('#tableexclusion').append(row_data);
                        });
                        $.each(cv.tmc, function (j, emp1) {
                            var id = "tnctr" + $('#termsandconditions')[0].rows.length;
                            row_data = "<tr id=" + id + "><td class='tc_name'>" + emp1 + "</td><td><span class='btn btn-action del-pd btn-xs' id='delPacktc'  onclick=editPacktc('" + id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></span></td>";
                            $('#termsandconditions').append(row_data);
                        });
                    });

                    $.each(cosult_details, function (i, emp) {
                        $('a[data-toggle=consult_flag][data-title="' + emp.appointment_status + '"]').removeClass('not-active');
                        $('a[data-toggle=consult_flag][data-title="' + emp.appointment_status + '"]').addClass('active');
                        $('#consult_flag').val(emp.appointment_status);
                        if (emp.no_of_consultation == null || emp.no_of_consultation == "0" || emp.no_of_consultation == "") {
                            $('#no_of_consult').val('');
                        }
                        else {
                            $('#no_of_consult').val(emp.no_of_consultation);
                        }
                        if (emp.walkin_allowed == "1") $('#con_walkin').prop("checked", emp.walkin_allowed);
                        if (emp.video_allowed == "1") $('#consut_video').prop("checked", emp.video_allowed);
                        if (emp.chat_allowed == "1") $('#consult_chat').prop("checked", emp.chat_allowed);
                    });

                    $.each(lab_deatils, function (i, emp1) {
                        if (emp1.panel_name == null || emp1.panel_name == "" || emp1.panel_name == "-/-") {
                            $('#panelname').val("");
                        }
                        else {
                            $('#panelname').val(emp1.panel_name);
                        }
                        if (emp1.panel_cost == null || emp1.panel_cost == "") {
                            $('#panel_cost').html("");
                        }
                        else {
                            $('#panel_cost').html(emp1.panel_cost);
                        }
                        if (emp1.no_of_package == null || emp1.no_of_package == "0") {
                            $('#no_of_types_lab').val("");
                        }
                        else {
                            $('#no_of_types_lab').val(emp1.no_of_package);
                        }
                        $('#testadvice').val(emp1.test_advice);
                        if (emp1.home_collection_offered == "1") $('#Chkhmecollected').prop("checked", emp1.home_collection_offered);
                    });

                    $.each(pharmadetails, function (i, emp1) {
                        if (emp1.discount == "0" || emp1.discount == null || emp1.discount == "") {
                            $('#phar_discount').val("");
                        }
                        else {
                            $('#phar_discount').val(emp1.discount);
                        }
                        $('select.pharma').val(emp1.discount_type);
                        if (emp1.no_of_free_delivery == null || emp1.no_of_free_delivery == "0" || emp1.no_of_free_delivery == "") {
                            $('#noofFreeDelphar').val("");
                        }
                        else {
                            $('#noofFreeDelphar').val(emp1.no_of_free_delivery);
                        }
                    });
                    $("#tblnoofvisit").empty();
                    var table = '<table class="table table-bordered table-striped dataTable"><tr><th>Visit no</th><th>Visited day</th><th>Description</th></tr>';
                    var j = 0;
                    $.each(homevisit, function (i, emp) {
                        j++;
                        table = table + '<tr><td>' + j + '</td><td><input class="form-control tblPackage_Category" type="tel" id="visited_day_' + j + '" autocomplete="off" value="' + emp.visited_day + '" disabled/></td><td><input class="form-control tblPackage_Category" type="tel" id="Description_' + j + '" value="' + emp.package_home_care_description + '" autocomplete="off" disabled/></td></tr>';
                    });
                    $("#noofvisit").val(j);
                    $('#tblnoofvisit').html(table + '</table>');
                    $('#package_expire_date,#packageprice,#package_discount,#pack_dist_type,#radioBtn,#no_of_consult,#con_walkin,#consut_video,#consult_chat,#no_of_types_lab,#no_of_types_lab,#Chkhmecollected,#phar_discount,#phar_discount_type,#noofFreeDelphar,#panelname,#noofvisit,#package_type,#Package_category,#packagename,#package_validity_date,#package_Never_Expiry,#packageimage,#DPackagehighlights,#DPackagedescription,#DPackageinclusion,#DPackageexclusion,#DPackagedescriptionsalestext,#DPackagetc,#testadvice').attr("disabled", true);
                    $('#consult_flag').attr("disabled", true);
                    $('#package_Never_Expiry').prop("disabled", true);
                    $('#btnAddPackage').html("Update Package");
                    $('#collapseOneinclusions').collapse('show');
                    $('#collapseOneexclusions').collapse('show');
                    $('#collapseOneTermsandConfitions').collapse('show');
                    $('#labpackage_highlights').collapse('show');
                    $('#labconsulting-section').collapse('show');
                    $('#labpane-section').collapse('show');
                    $('#pharmacy-details').collapse('show');
                    $('a[data-toggle=consult_flag][data-title=1]').addClass('disabled');
                    $('a[data-toggle=consult_flag][data-title=2]').addClass('disabled');
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}



$('#panelname').on('input', function () {
    $('#panelname').autocomplete({
        source: function (request, response) {
            $.ajax({
                async: true,
                url: "/PackageDetails/AutoComplete_PanelName?rep_list=" + $('#panelname').val(),
                type: "POST",
                dataType: "json",
                data: { Prefix: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.patient_name, value: item.patient_name };
                    }));
                }
            });
        }
    });
});


$('#panelname').change(function () {
    var lab_panel_id = $('#panelname').val();
    var package_data = {
        indicator_id: "LNAC",
        lab_panel_id: lab_panel_id.split('/')[0]
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
           var data1 = JSON.parse(data);
           if (data1.msg == "Success") {
               var data1 = data1.data;
               if (jQuery.isEmptyObject((data1))) {
               }
               else {
                   var data1, lab_amount;

                   lab_amount = data1.Table;
                   $.each(lab_amount, function (i, emp1) {
                       $('#panel_cost').html(emp1.panel_cost);
                   });
               }
           }
           else {
               console.log(data1);
           }
        }
    });
});

$("#Package_category").change(function () {
    debugger;
    if ($('#Package_category').val() == 1 || $('#Package_category').val() == 2) {
        //$('.Package_Highlights').show();
        $('.Consulting').show();
        $('.Lab_Panel').show();
        $('.Pharmacy_Details').show();
        $('.Package_Category').hide();
        $('.Vaccination').hide();
    }
    if ($('#Package_category').val() == 5 || $('#Package_category').val() == 3 || $('#Package_category').val() == 4) {
        //$('.Package_Highlights').hide();
        $('.Consulting').hide();
        $('.Lab_Panel').hide();
        $('.Pharmacy_Details').hide();
        $('.Package_Category').show();
        $('.Vaccination').hide();
        if ($('#Package_category').val() == 5) { $('.Package_Category_Name').html('Physio'); }
        if ($('#Package_category').val() == 3) { $('.Package_Category_Name').html('Doctor Home Visit'); }
        if ($('#Package_category').val() == 4) { $('.Package_Category_Name').html('Nurse Home Visit'); }
    }
    else if ($('#Package_category').val() == 6) {
        //$('.Package_Highlights').hide();
        $('.Consulting').hide();
        $('.Lab_Panel').hide();
        $('.Pharmacy_Details').hide();
        $('.Package_Category').hide();
        $('.Vaccination').show();
    }
    else if ($('#Package_category').val() == 7) {
        //$('.Package_Highlights').hide();
        $('.Consulting').show();
        $('.Lab_Panel').show();
        $('.Pharmacy_Details').show();
        $('.Package_Category').show();
        $('.Package_Category_Name').html('Package Category');
        $('.Vaccination').show();
    }
});
function package_category() {
    if ($('#Package_category').val() == 1 || $('#Package_category').val() == 2) {
        //$('.Package_Highlights').show();
        $('.Consulting').show();
        $('.Lab_Panel').show();
        $('.Pharmacy_Details').show();
        $('.Package_Category').hide();
        $('.Vaccination').hide();
    }
    if ($('#Package_category').val() == 5 || $('#Package_category').val() == 3 || $('#Package_category').val() == 4) {
        //$('.Package_Highlights').hide();
        $('.Consulting').hide();
        $('.Lab_Panel').hide();
        $('.Pharmacy_Details').hide();
        $('.Package_Category').show();
        $('.Vaccination').hide();
        if ($('#Package_category').val() == 5) { $('.Package_Category_Name').html('Physio'); }
        if ($('#Package_category').val() == 3) { $('.Package_Category_Name').html('Doctor Home Visit'); }
        if ($('#Package_category').val() == 4) { $('.Package_Category_Name').html('Nurse Home Visit'); }
    }
    else if ($('#Package_category').val() == 6) {
        //$('.Package_Highlights').hide();
        $('.Consulting').hide();
        $('.Lab_Panel').hide();
        $('.Pharmacy_Details').hide();
        $('.Package_Category').hide();
        $('.Vaccination').show();
    }
    else if ($('#Package_category').val() == 7) {
        //$('.Package_Highlights').hide();
        $('.Consulting').show();
        $('.Lab_Panel').show();
        $('.Pharmacy_Details').show();
        $('.Package_Category').show();
        $('.Package_Category_Name').html('Package Category');
        $('.Vaccination').show();
    }
}

$('#noofvisit').change(function () {
    var noofvisit = $('#noofvisit').val();
    if (noofvisit > 40) {
        toastr.warning('Enter the Visit Number Less than 10');
        $('#noofvisit').val('');
        return false;
    }
    var table = '<table class="table table-bordered table-striped dataTable"><tr><th>Visit no</th><th>Visited day</th><th>Description</th></tr>';
    var j = 0;
    for (var i = 0; i < noofvisit; i++) {
        j++;
        var val = '';
        if (j == 1) { val = 0; }
        debugger;
        table = table + '<tr><td>' + j + '</td><td><input class="form-control tblPackage_Category" type="tel" id="visited_day_' + j + '" autocomplete="off" value="' + val + '"/></td><td><input class="form-control tblPackage_Category" type="tel" id="Description_' + j + '" autocomplete="off"/></td></tr>';
    }
    $('#tblnoofvisit').html(table + '</table>');
});