$(document).ready(function () {
    $("body").on("blur", ".flatno", fillName);
    $("body").on("change", "#party", fillFlat);
    $("body").on('change', '#flatno', getflatdtl);
    $("body").on("blur", ".tranname", ChkDupParty);


    function ChkDupParty() {
        var party = $(".tranname").val();
        if (party != '') {
            $.post('chkdup_party.php', {party: party}, function (data) {
                if (data == 1) {
                    alert("Member Already Exist");
                    $(".tranname").val("");
                    return false;
                }

            });
        }
    }


    $(".ledgdiv").hide();
    $(".secondrymodal").hide();

    $(".proceed").click(function () {
        $(".secondrymodal").show();
        $(".primarymodal").hide();

        var fdate = $('.mfrmdt').val(),
                tdate = $('.todate').val();

        $('.fdate').val(fdate);
        $('.tdate').val(tdate);
    });

    $("#myModal").on('shown.bs.modal', function () {
        $(".secondrymodal").hide();
        $(".primarymodal").show();
    });

    function fillName() {
        var flatno = $(".flatno").val();
        if (flatno != '') {
            $.post('get_flatno.php', {flatno: flatno}, function (data) {
                $("#party").val(data.accode);
                $("#party").trigger('change');

            }, 'json');
        }
    }


    function  getflatdtl() {
        var flatno = $(".flatno").val();

        if (flatno != "") {
            $.post("getflatdtl.php", {flatno: flatno}, function (data) {

                if (data.flat == 1) {
                    $(".name").val(data.name);
                    $(".name2").val(data.name2);
                    $(".block").val(data.block);
                    $(".wing").val(data.wing);
                    $(".floor").val(data.floor);
                    $(".unit").val(data.unit);
                    $(".area").val(data.area);
                }
            });
        }

    }

    function fillFlat() {
        var party = $("#party").val();
        if (party != '') {
            $.post('get_party.php', {party: party}, function (data) {
                $(".flatno").val(data.flatno);
                $(".flatno").trigger('change');

            }, 'json');

            if (confirm('Do You Want To View Ledger ?')) {
                $('.legderparty').val(party);
                $('.LedgerModal').modal('show');
            }
        }
    }


    $(".myTable").find('.allchk').change(function () {
        $(".mcheck").prop('checked', $(this).prop('checked'));
    });


    $('.proceed').click(function () {
        var frmdate = $(".fdate").val(),
                todate = $(".tdate").val(),
                defbank = $(".defbank").val(),
                narr = $(".narr").val(),
                data = {frmdate: frmdate, todate: todate, defbank: defbank, narr: narr};
        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: 'bankrecp_mult.php',
            data: data,
            success: function (response) {
                $(".mprint").empty();
                $(".mprint").html(response);
            },
            error: function (request, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    });
});