//check for report page
if ($('#reportPage').val() == 1)
{
    $(document).ready(function () {
        $(".bp_mask").inputmask("999/999", {
            autoUnmask: true});
    });
    var hosturl = $('#hosturl').val();
    var tdate = $("#tdate").val();
    var controllName = $("#controllName").val();
    ///////////////Expected Delivery
    $("#fromdate").live('change', function () {

        var fromdate = $(this).val();
        var todate = $("#todate").val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {fromdate: fromdate, todate: todate}, function (res)
        {
            $("#expectedDiv").html("");
            $("#expectedDiv").append(res);

        });

    });

    $("#todate").live('change', function () {

        var todate = $(this).val();
        var fromdate = $("#fromdate").val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {fromdate: fromdate, todate: todate}, function (res)
        {
            $("#expectedDiv").html("");
            $("#expectedDiv").append(res);

        });

    });

    $(".searchall").live('change', function () {
        var patientid = $("#patientids").val();
        var todate = $("#todates").val();
        var fromdate = $("#fromdates").val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {patientid: patientid, fromdate: fromdate, todate: todate}, function (res)
        {
            $("#expectedDiv").html("");
            $("#expectedDiv").append(res);

        });

    });

    $("#printe").live('click', function () {

        var todate = $("#todate").val();
        var fromdate = $("#fromdate").val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint', {fromdate: fromdate, todate: todate}, function (res)
        {
            $("#expectedPrint").html(res);
            $.print("#expectedPrint");
            $("#expectedPrint").html('');

        });

    });

//////////////////////////Financial Report

    $('.searchFin').select2({
        width: '170px',
        allowClear: true,
        minimumInputLength: 3,
        formatNoMatches: function (term) {
            /* customize the no matches output */
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {
        var newitem = $('#newddTerm').val();

    });

    $('body').on('keyup change', '.finSearch', function () {

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var value = $("#patientName").val();
        var hvalue = $("#husband").val();
        var address = $("#patAddress").val();

        if ($(this).attr('id') == "husband")
            value = '';
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {address: address, fdate: fdate, todate: todate, value: value, hvalue: hvalue}, function (res)
        {
            $("#financialDiv").html("");
            $("#financialDiv").append(res);
        });

    });

    $("#printfi").live('click', function () {

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var value = $("#patientName").val();
        var hvalue = $("#husband").val();
        var address = $("#patAddress").val();
        var branch_id = $("#branch_id").val();

        if ($(this).attr('id') == "husband")
            value = '';
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint'
                , {address: address, fdate: fdate, todate: todate, value: value, hvalue: hvalue, branch_id: branch_id}, function (res)
        {
            $("#financialPrint").html(res);
            $.print("#financialPrint");
            $("#financialPrint").html('');

        });

    });

    $("#exportExcel").live('click', function () {
        var patientId = $('#patientid').val();
        if (patientId != 'undefined' && patientId != null && patientId != "")
        {
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=getexcel', {patientId: patientId}, function (res)
            {
                $("#filesExcel").html("");
                $("#filesExcel").html(res);
                $("#ExelSheetsD").modal('show');

            });
        } else
        {
            alert('Please Choose patient first ...........');
        }

    });

    /////////////////Complete Report
    $(".patientReport").live("change", function ()
    {
        var patientId = $(this).val();
        $("#patientid").val(patientId);
        $("#pNo").select2('val', patientId);
        $("#pName").select2('val', patientId);
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {patientId: patientId}, function (res)
        {
            $("#CompleteReportDiv").html("");
            $("#CompleteReportDiv").append(res);

            $(".bp_mask").inputmask("999/999", {
                autoUnmask: true});
            $("#printc").attr('data-id', patientId);
        });

    });

    $('.removeMsg').live('click', function () {
        $(this).parent().parent().remove();
    });

    $('#printc').live('click', function () {


        var patientId = $(this).attr('data-id');

        // window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=showprint&id=" + id+"&patientid="+patientId;
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint', {patientid: patientId}, function (data)
        {
            $("#cprint").html(data);

            $(".bp_mask").inputmask("999/999", {
                autoUnmask: true});



            if ($("#reportNote").val() == "")
            {
                $("#pnotDiv").hide();
            } else
            {
                $("#preportNote").html($("#reportNote").val());
                $("#pnotDiv").show();
            }

            $.print("#cprint");
            $("#cprint").html('');
            $("#pnotDiv").show();
        });
    });

    $('#printCom').live('click', function () {
        var fage = $("#fromage").val();
        var toage = $("#toage").val();
        var diagnosis = new Array();
        var diagnosisant = new Array();
        var operation = $("#operation").val();
        var termination = $("#termination").val();

        $('#diagnosis :selected').each(function (i, selected) {
            diagnosis[i] = $(selected).val();
        });

        $('#diagnosisant :selected').each(function (i, selected) {
            diagnosisant[i] = $(selected).val();
        });
        if ($(this).attr("id") == "diagnosis" && $.inArray("0", diagnosis) != -1)
        {
            $("#diagnosis option:selected").removeAttr("selected");
            $("#diagnosis").select2();
        }
        if ($(this).attr("id") == "diagnosisant" && $.inArray("0", diagnosisant) != -1)
        {
            $("#diagnosisant option:selected").removeAttr("selected");
            $("#diagnosisant").select2();
        }
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint', {fage: fage, toage: toage, diagnosis: JSON.stringify(diagnosis), diagnosisant: JSON.stringify(diagnosisant), operation: operation, termination: termination}, function (res)
        {
            $("#comPrint").html(res);
            $.print("#comPrint");
            $("#comPrint").html('');
        });
    });

    $(".comSearch").live('change', function () {

        var fage = $("#fromage").val();
        var toage = $("#toage").val();
        var diagnosis = new Array();
        var diagnosisant = new Array();
        var operation = $("#operation").val();
        var termination = $("#termination").val();

        $('#diagnosis :selected').each(function (i, selected) {
            diagnosis[i] = $(selected).val();
        });

        $('#diagnosisant :selected').each(function (i, selected) {
            diagnosisant[i] = $(selected).val();
        });
        if ($(this).attr("id") == "diagnosis" && $.inArray("0", diagnosis) != -1)
        {
            $("#diagnosis option:selected").removeAttr("selected");
            $("#diagnosis").select2();
        }
        if ($(this).attr("id") == "diagnosisant" && $.inArray("0", diagnosisant) != -1)
        {
            $("#diagnosisant option:selected").removeAttr("selected");
            $("#diagnosisant").select2();
        }
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {fage: fage, toage: toage, diagnosis: JSON.stringify(diagnosis), diagnosisant: JSON.stringify(diagnosisant), operation: operation, termination: termination}, function (res)
        {
            $("#combinedDiv").html("");
            $("#combinedDiv").append(res);
        });

    });

    $('body').on('click', '#finSearchBtn', function () {

        $("#finSearchBtn").attr('disabled', true);

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var value = $("#patientName").val();
        var hvalue = $("#husband").val();
        var address = $("#patAddress").val();
        var branch_id = $("#branch_id").val();

        if ($(this).attr('id') == "husband")
            value = '';
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search'
                , {address: address, fdate: fdate, todate: todate, value: value, hvalue: hvalue, branch_id: branch_id}, function (res)
        {
            $("#financialDiv").html("");
            $("#financialDiv").append(res);
            $("#finSearchBtn").attr('disabled', false);
        });

    });

    //###########################################################

    $('body').on('click', '#paymentSearchBtn', function () {

        $("#paymentSearchBtn").attr('disabled', true);

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var value = $("#patientName").val();
        var hvalue = $("#husband").val();
        var address = $("#patAddress").val();

        if ($(this).attr('id') == "husband")
            value = '';
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=searchPayment',
                {address: address, fdate: fdate, todate: todate, value: value, hvalue: hvalue}, function (res)
        {
            $("#financialDiv").html("");
            $("#financialDiv").append(res);
            $("#paymentSearchBtn").attr('disabled', false);
        });

    });

    $('body').on('click', '#printPayment', function () {

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var value = $("#patientName").val();
        var hvalue = $("#husband").val();
        var address = $("#patAddress").val();

        if ($(this).attr('id') == "husband")
            value = '';
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=printPayment',
                {address: address, fdate: fdate, todate: todate, value: value, hvalue: hvalue}, function (res)
        {
            $("#financialPrint").html(res);
            $.print("#financialPrint");
            $("#financialPrint").html('');
        });

    });

//###########################################################
}


if ($('#terminationreport').val() == 1)
{
    $('#printf').live('click', function () {
        $("#printd").css('display', 'block');
        $.print("#printd");
        $("#printd").css('display', 'none');
    });
    var hosturl = $('#hosturl').val();
    var controllName = $("#controllName").val();
    $('.all').on('change', '#terminationType', function () {
        var value = $(this).val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {termtype: value}, function (res)
        {
            $("#expectedDiv").html("");
            $("#expectedDiv").append(res);

        });
    });
    //$('#repTable').dataTable();

}


if ($('#risktypePage').val() == 1)
{
    $('#printf').live('click', function () {
        var value = $('#risktype').val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint', {risktype: value}, function (res)
        {
            $("#riskPrint").html(res);
            $.print("#riskPrint");
            $("#riskPrint").html('');
        });
    });
    var hosturl = $('#hosturl').val();
    var controllName = $("#controllName").val();
    $('.all').on('change', '#risktype', function () {
        var value = $(this).val();
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=search', {risktype: value}, function (res)
        {
            $("#expectedDiv").html("");
            $("#expectedDiv").append(res);

        });
    });
    //$('#repTable').dataTable();

}


if ($('#drugUsereport').val() == 1) {

    var hosturl = $('#hosturl').val();
    var controllName = $("#controllName").val();

    $('#drugCatId').select2();
    $('#drugId').select2();

    $('body').on('change', '.dcatReport', function () {
        var value = $(this).val();
        $.post(hosturl + '/controllers/financialreport.php?ac=drugByCat', {catId: value}, function (res) {
            $('#drugId').html(res);
            $('#drugId').select2("val", "");
        });
    });

    $("#drugId").live("change", function ()
    {
        var drugId = $(this).val();
        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();

        $.post(hosturl + '/controllers/' + controllName + '.php?ac=drugsearch', {drugId: drugId, fdate: fdate, todate: todate}, function (res)
        {
            $("#DrugsDiv").html("");
            $("#DrugsDiv").append(res);
        });
    });

    $("#printDrug").live('click', function () {

        var fdate = $("#findate").val();
        var todate = $("#tofindate").val();
        var drugId = $("#drugId").val();

        $.post(hosturl + '/controllers/' + controllName + '.php?ac=drugprint', {drugId: drugId, fdate: fdate, todate: todate}, function (res)
        {
            $("#drugPrint").html(res);
            $.print("#drugPrint");
            $("#drugPrint").html('');

        });

    });

}


var awcustomjs = function () {

    var initcontrolpropr = function () {

        var table = $('#controllpropr');

        // begin: third table
        table.dataTable({
            // Internationalisation. For more info refer to http://datatables.net/manual/i18n

            // Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
            // setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js).
            // So when dropdowns used the scrollable div should be removed.
            //"dom": "<'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",

            //"bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.
            dom: "Bfrtip",
            buttons: ['copy', 'csv', 'excel', 'pdf', 'print', 'pageLength'],
            "dom": "<'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",

            "lengthMenu": [
                [15, 20, -1],
                [15, 20, "All"] // change per page values here
            ],
            // set the initial value
            "pageLength": 15,
            "language": {
                "lengthMenu": " _MENU_",
                "aria": {
                    "sortAscending": ": Used to order Ascending",
                    "sortDescending": ": Used to order Descending"
                },
                "emptyTable": "NO data",
                "info": "Show _START_ From _END_ To _TOTAL_ item",
                "infoFiltered": "(Filter is _MAX_ from total)",
                "lengthMenu": "_MENU_",
                "search": "",
                "zeroRecords": "No search data"
            },
            "columnDefs": [{// set default column settings
                    'orderable': true,
                    'targets': [0]
                }],
            "order": [
                [4, "asc"]
            ] // set first column as a default sort by asc
        });
        var tableWrapper = jQuery('#controllprop_wrapper');

        tableWrapper.find('.dataTables_length select').select2(); // initialize select2 dropdown
    }

    return {
        //main function to initiate the module
        init: function () {
            initcontrolpropr();
        }
    };
}();
