// JavaScript Document
$(document).ready(function (e) {
//###########################################################################################################################################################
//######################################### reports controller   ##############################################################################################
//###########################################################################################################################################################
    $(".searchPatients").live('click', function (e) {
        var clientArea = $('#clientArea').val();
        var insuranceCompany = $('#insuranceCompany').val();
        var branchId = $('#branchId').val();
        var clientId = $('#clientId').val();
        var searchDateFrom = $('#searchDateFrom').val();
        //var searchDateTo = $('#searchDateTo').val();

        if (clientArea != -1 && insuranceCompany != -1)
        {
            $('#clientAreaError').hide();
            $('#insuranceCompanyError').hide();
            $.post('reportsajax.php?do=patientsearch', {clientId: clientId, clientArea: clientArea, insuranceCompany: insuranceCompany, branchId: branchId, searchDateFrom: searchDateFrom}, function (rslt) {
                $('#searchResult').html('');
                $('#searchResult').html(rslt);
            });
        }
        if (clientArea == -1)
        {
            $('#clientAreaError').show();
        }

        if (insuranceCompany == -1)
        {
            $('#insuranceCompanyError').show();
        }
    });



    $(".searchCompany").live('click', function (e) {
        var clientArea = $('#clientArea').val();
        var insuranceCompany = $('#insuranceCompany').val();
        var branchId = $('#branchId').val();
        var searchDateFrom = $('#searchDateFrom').val();
        //var searchDateTo = $('#searchDateTo').val();

        if (insuranceCompany != -1)
        {
            $('#insuranceCompanyError').hide();
            $.post('reportsajax.php?do=companysearch', {clientArea: clientArea, insuranceCompany: insuranceCompany, branchId: branchId, searchDateFrom: searchDateFrom}, function (rslt) {
                $('#searchResult').html('');
                $('#searchResult').html(rslt);
            });
        }
        else
        {
            $('#insuranceCompanyError').show();
        }
    });


    $(".searchInsCompany").live('click', function (e) {
        var insuranceCompany = $('#insuranceCompany').val();
        var branchId = $('#branchId').val();
        var searchDateFrom = $('#searchDateFrom').val();
        //var searchDateTo = $('#searchDateTo').val();

        $.post('reportsajax.php?do=inscompanysearch', {insuranceCompany: insuranceCompany, branchId: branchId, searchDateFrom: searchDateFrom}, function (rslt) {
            $('#searchResult').html('');
            $('#searchResult').html(rslt);
        });

    });


    $(".bindtype").change(function (e) {
        if ($(this).val() == 1)
        {
            $('#storageDiv').show();
            $('#bankDiv').hide();
        }
        else
        {
            $('#bankDiv').show();
            $('#storageDiv').hide();
        }



    });

    $(".bindSettlement").live('change', function (e) {
        var itr = $(this).attr('data-itr');
        if ($(this).val() == 1)
        {
            $('#saveDiv' + itr).show();
            $('#masroufDiv' + itr).hide();
            $('#save' + itr).select2("val", "-1");
            $('#expensestype' + itr).select2("val", "-1");
        }
        else
        {
            $('#masroufDiv' + itr).show();
            $('#saveDiv' + itr).hide();
            $('#save' + itr).select2("val", "-1");
            $('#expensestype' + itr).select2("val", "-1");
        }



    });


    $(".searchPayMethod").live('click', function (e) {
        var searchDateFrom = $('#fromDate').val();
        var searchDateTo = $('#toDate').val();
        var branchid = $('#branchid').val();
        $.post('reportsajax.php?do=paymethodsearch', {searchDateFrom: searchDateFrom, searchDateTo: searchDateTo, branchid: branchid}, function (rslt) {
            $('#searchResult').html('');
            $('#searchResult').html(rslt);
        });
    });




    //###########################################################################################################################################################
    $("#employee").live('change', function (e) {

        var employeeId = $(this).val();

        if (employeeId == -1)
        {
            $("#bindValueP").html('');
            $("#bindValueP").html(0);

            $("#bindSettlementValueP").html('');
            $("#bindSettlementValueP").html(0);

            $("#bindValueNetP").html('');
            $("#bindValueNetP").html(0);

            $("#bindValue").val(0);
            $("#bindSettlementValue").val(0);
            $("#bindValueNet").val(0);

            $('#valueError').hide();
            $('#employeeError').show();
        }
        else
        {
            $('#employeeError').hide();
            $.post('reportsajax.php?do=getbindvalue', {employeeId: employeeId}, function (rslt) {
                var obj = JSON.parse(rslt);
                //alert(rslt);
                $("#bindValueP").html('');
                $("#bindValueP").html(obj.sumBind);

                $("#bindSettlementValueP").html('');
                $("#bindSettlementValueP").html(obj.sumSettlement);

                $("#bindValueNetP").html('');
                $("#bindValueNetP").html(obj.sumNet);

                $("#bindValue").val(obj.sumBind);
                $("#bindSettlementValue").val(obj.sumSettlement);
                $("#bindValueNet").val(obj.sumNet);
            });

            var value = $('#value').val();
            var bindValueNet = $('#bindValueNet').val();

            if (Number(value) <= Number(bindValueNet))
            {
                $('#valueError').hide();
            }
            else
            {
                $('#valueError').show();
            }
        }
    });


    $(".searchBinds").live('change', function (e) {
        var employeeId = $('#employee').val();
        $.post('reportsajax.php?do=bindssearch', {employeeId: employeeId}, function (rslt) {
            // alert(rslt);
            $('#bindSearchResult').html('');
            $('#bindSearchResult').html(rslt);
        });
    });



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

        var bankId = $(this).val();

        if (bankId == -1)
        {
            $("#account").html('');
            $("#account").html('<option value="-1"> اختر </option>');
        }
        else
        {
            $.post('reportsajax.php?do=getbankaccounts', {bankId: bankId}, function (rslt) {
                //alert(rslt);
                $('#account').html('');
                $('#account').html(rslt);
            });
        }
    });



    //###########################################################################################################################################################
    $(".saveBindSettle").live('click', function (e) {
        // var value = $('#value').val();
        var bindValueNet = $('#bindValueNet').val();
        var employeeId = $('#employee').val();
        // var bindSettlement = $('#bindSettlement').val();
        var valid = 1;
        var itr = $('#itr').val();
        var bindSettlement;
        var save;
        var expense;
        var value;
        var valueSum = 0;


        if (itr == 1 && $('#value1').val() == '' && $('#save1').val() == -1 && $('#expensestype1').val() == -1 && $('#bindSettlement1').val() == -1)
        {
            $('#valueError0').html('على الأقل ادخل تسوية واحدة ');
            $('#valueError0').show();
            valid = 0;
        }
        else
        {


            if (employeeId == -1)
            {
                $('#employeeError').show();
                valid = 0;
            }
            else
            {
                $('#employeeError').hide();
            }
///// ###   start from here .............................................add itr .............

            for (var i = 1; i <= itr; i++)
            {
                bindSettlement = $('#bindSettlement' + i).val();
                save = $('#save' + i).val();
                expense = $('#expensestype' + i).val();
                value = $('#value' + i).val();
                if (value == '' && save == -1 && expense == -1 && bindSettlement == -1)
                {

                }
                else
                {

                    if (bindSettlement == 1 && save == -1)
                    {
                        $('#saveError' + i).show();
                        valid = 0;
                    }
                    else
                    {
                        $('#saveError' + i).hide();
                    }

                    if (bindSettlement == 0 && expense == -1)
                    {
                        $('#expenseError' + i).show();
                        valid = 0;
                    }
                    else
                    {
                        $('#expenseError' + i).hide();
                    }

                    if (value == '')
                    {
                        $('#valueError' + i).html('ادخل القيمة أولا ');
                        $('#valueError' + i).show();
                        valid = 0;
                    }
                    else
                    {
                        valueSum = Number(valueSum) + Number(value);
                        $('#valueError' + i).html('');
                        $('#valueError' + i).hide();
                    }


                }
            }


            if (Number(valueSum) <= Number(bindValueNet))
            {
                $('#valueError0').html('');
                $('#valueError0').hide();

            }
            else
            {
                $('#valueError0').html('قيمة التسوية أكبر من مجموع قيم العهد الخاصة بالموظف ');
                $('#valueError0').show();
                valid = 0;
            }


            if (valid == 1)
            {
                //########## submit form
                $('#bindSettleForm').submit();
            }
        }
    });


    $(".searchBindSettle").live('change', function (e) {
        var employeeId = $('#employee').val();
        $.post('reportsajax.php?do=bindsettlesearch', {employeeId: employeeId}, function (rslt) {
            // alert(rslt);
            $('#bindSettleSearchResult').html('');
            $('#bindSettleSearchResult').html(rslt);
        });
    });


    $(".searchBindReport").live('change', function (e) {
        var employeeId = $('#employee').val();
        var fromDate = $('#fromDate').val();
        var toDate = $('#toDate').val();
        $.post('reportsajax.php?do=bindreportsearch', {employeeId: employeeId, fromDate: fromDate, toDate: toDate}, function (rslt) {
            // alert(rslt);
            $('#reportSearchResult').html('');
            $('#reportSearchResult').html('<div class="clearfix"></div>');
            $('#reportSearchResult').html(rslt);
        });
    });


    $("#addNewBindS").click(function (e) {
        var itr = $('#itr').val();
        var newItr = Number(itr) + 1;
        $('#itr').val(newItr);

        $.post('reportsajax.php?do=addnewsettle', {newItr: newItr}, function (rslt) {
            $('#bindSettleTable').append(rslt);
        });

    });
});