
$(document).ready(function () {
    LoadTable()
});
$('#plcamp_register').click(function () {
    debugger;
    LoadTable();
    $('.asidefilterclass').addClass('hidden');
    $('#asideCamp_Register').removeClass('hidden');
    $('.groupclass').addClass('hidden');
    $('#asideCamp_Registergroupclass').removeClass('hidden');
    $('.Filterclass').addClass('hidden');
    $('#asideCamp_RegisterFilterclass').removeClass('hidden');

});
$('#CR_filter').click(function () {
    debugger;
    var start_date = '', end_date = ''
    if (($('#CRFStart_date').val() !== "" && $('#CRFStart_date').val() !== undefined)) {
        var date = $('#CRFStart_date').val().split('/');
        start_date = date[2] + '-' + date[1] + '-' + date[0];

        var date2 = $('#CRFend_date').val().split('/');
        end_date = date2[2] + '-' + date2[1] + '-' + date2[0];
    }

    LoadTable($('#CRFcampname').val(), start_date, end_date);
})
$('#CR_clear').click(function () {
    $('#CRFcampname').val('');
    $('#CRFStart_date').val('');
    $('#CRFend_date').val('');
    LoadTable();
})
$('#save').click(function () {
    debugger;
    if ($.trim($('#campregname').val()) == "") {
        $('#Pcampregname').show();
        $('#Pcampregname').fadeOut(8000);
        $('#Pcampregname').html('Enter Camp Name');
        $('#campregname').focus();
        return false;
    }
    if ($.trim($('#start_date').val()) == "") {
        $('#Pstart_date').show();
        $('#Pstart_date').fadeOut(8000);
        $('#Pstart_date').html('Enter start date');
        $('#start_date').focus();
        return false;
    }
    if ($.trim($('#end_date').val()) == "") {
        $('#Pend_date').show();
        $('#Pend_date').fadeOut(8000);
        $('#Pend_date').html('Enter end date');
        $('#end_date').focus();
        return false;
    }
    var from_date = null, to_date = null;
    if ($('#start_date').val() != '') {
        var date1 = $('#start_date').val().split('/');
        from_date = date1[2] + '-' + date1[1] + '-' + date1[0];
    }
    if ($('#end_date').val() != '') {
        var date2 = $('#end_date').val().split('/');
        to_date = date2[2] + '-' + date2[1] + '-' + date2[0];
    }
    if ($('#save').html() == 'Update') {
        indicator_id = 'UP'
    }
    else {
        indicator_id = 'IS'
    }
    if ($('#Htcaid').val() == "") {
        campid = 0
    }
    else {
        campid = $('#Htcaid').val();
    }

    var submit = {
        Dml_Indicator: indicator_id,
        Camp_id: campid,
        Camp_name: $('#campregname').val(),
        Start_date: from_date,
        End_date: to_date,
        Address: $('#address').val(),
        Area: $('#area').val(),
        Team_members: $('#team_member').val(),
        Remark: $('#remark').val(),
        Hospital_id: $('#HospitalId').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCampRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data;
                if (data2.Table[0].Message == 'Inserted') {
                    toastr.success('Inserted Successfully');
                    setTimeout(function () {
                        location.reload();
                    }, 1000)
                    clearcamp()
                }
                else if (data2.Table[0].Message == 'Updated') {
                    toastr.success('Updated Successfully');
                    setTimeout(function () {
                        location.reload();
                    }, 1000)
                    clearcamp()
                }
                else {
                    toastr.success('Somthing Went Wrong');
                }
            }
            else {
                toastr.success('Somthing Went Wrong');
            }
        },
    });
});

function LoadTable(Fcampname, Fstart_date, Fend_date) {
    debugger;
    var values = {
        Dml_Indicator: "S",
        Hospital_id: $('#HospitalId').val(),
        Camp_name: Fcampname,
        Start_date: Fstart_date,
        End_date: Fend_date
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCampRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    $('#tbl_campregister tbody').empty();
                    $('#tbl_campregister').dataTable().fnClearTable();
                    $('#tbl_campregister').dataTable().fnDraw();
                    $('#tbl_campregister').dataTable().fnDestroy();

                    var j = 0, b = '', tr = '', edit = '', del = '', active_inactive = '', active = '', other_info = '';

                    b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++

                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="SM_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Campedit(this,"' + emp.camp_id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = ' <a  data-toggle="tooltip" title="Delete"  id="TPAdel_' + emp.camp_id + '" class="btn deleteicon-btn btn-xs"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form"  href="javascript:void(0);" onclick=Campdel(this,"' + emp.camp_id + '","D")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Campdel(this,"' + emp.camp_id + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true"></i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + ' ' + active + '</div></div>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Campdel(this,"' + emp.camp_id + '","ACT")><i class="fa fa-check-square-o mr-5" aria-hidden="true"></i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + ' ' + active + '</div></div>';
                        }
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.camp_name + '</td><td>' + emp.start_date + '</td><td >' + emp.end_date + '</td><td>' + emp.area + '</td><td >' + emp.address + '</td><td>' + active_inactive + '</td><td>' + other_info + '</td></tr>';

                    });
                    $('#tbl_campregister tbody').html(tr);
                    datatable('tbl_campregister', 'camp_register_list', 'Legal');
                }
            }
            else {
                console.log(data);
            }
        }
    });

}

function Campdel(event, val, Dml_Indicator) {
    debugger;

    var status = "";
    if (Dml_Indicator == 'ACT') {
        status = "Are You Sure, Do you Want to Active the Camp Register?";
    }
    else if (Dml_Indicator == 'INACT') {
        status = "Are You Sure, Do you Want to In-active the Camp Register?";
    }
    else if (val != '') {

        status = "Are You Sure, Do you want to Delete?";
    }
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
{

    closeButton: true,
    progressBar: true,
    allowHtml: true,
    timeOut: 20000000,

    onShown: function (toast) {
        $("#confirmationRevertYes").click(function () {
            debugger;

            var servicevalues = {
                Camp_id: val,
                Dml_Indicator: Dml_Indicator

            }


            $.ajax({
                async: true,
                type: 'POST',
                url: '/BloodBank/insert_update_deleteCampRegister',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(servicevalues),
                success: function (data) {
                    debugger;

                    var data2 = JSON.parse(data);


                    if (jQuery.isEmptyObject((data2.data))) {

                    }
                    else {

                        if (data2.msg == "Success") {
                            debugger;
                            data1 = data2.data;
                            if (Dml_Indicator == 'D') {
                                toastr.success("Deleted Successfully ");
                                LoadTable()

                            }
                            else if (Dml_Indicator == 'ACT') {
                                toastr.success("Activated Successfully");
                                LoadTable()
                            }
                            else if (Dml_Indicator == 'INACT') {
                                toastr.success("In-activated Successfully");
                                LoadTable()

                            }
                            else {
                                toastr.warning("Somthing Went Wrong");
                                return false;
                            }
                        }
                    }
                }
            });
        });
    }
});
}

function Campedit(event, Id) {
    debugger;
    $('#Htcaid').val(Id)

    $('#campregister_form').show();
    $('#campregister_table_div').hide();
    $('#divmaster').hide();
    $('#regtab').hide();
    $('#divcamp').show();

    var Edit = {
        Camp_id: Id,
        Dml_Indicator: "E"
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCampRegister',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table[0];

            $("#campregname").val(emp.camp_name);
            $("#start_date").val(emp.start_date);
            $("#end_date").val(emp.end_date);
            $("#address").val(emp.address);
            $("#area").val(emp.area);
            $("#team_member").val(emp.team_members);
            $("#remark").val(emp.remark);

            $('#save').html('Update');
        }
    })
}
function clearcamp() {
    $('#campregname').val(''),
    $('#start_date').val(''),
    $('#end_date').val(''),
    $('#area').val(''),
    $('#address').val(''),
    $('#team_member').val(''),
    $('#remark').val('')
};