<style>
    @media print {
        body * {
            visibility: hidden;
        }

        .print-container,
        .print-container * {
            visibility: visible;
        }
    }
</style>



<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <!-- <h1 class="m-0">Deactive Member </h1> -->
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="#">Books</a></li>
                    <li class="breadcrumb-item active">GL</li>
                </ol>
            </div>
        </div>
    </div>
</div>




<section class="content">
    <div class="container-fluid">




        <div class="row">

            <section class="col-lg-12 connectedSortable">

                <div class="card">
                    <div class="card-header " style="background-color:#31708e;color:white">
                        <h3 class="card-title">
                            Cash book
                        </h3>
                        <div class="card-tools">
                            <ul class="nav nav-pills ml-auto">
                            </ul>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="tab-content p-0">

                            <?php if (isset($_SESSION['mem_add']) && $_SESSION['mem_add'] != "") { ?>
                                <div class="alert alert-success">
                                    <strong><?php echo $_SESSION['mem_add']; ?></strong>
                                </div>
                            <?php $_SESSION['mem_add'] = "";
                            } ?>

                            <form method="post" action="<?php echo base_url(); ?>index.php/Demand/general_ledger" id="member_add_" enctype='multipart/form-data'>
                                <div class="row">
                                    <!-- <div class="col-sm-3">
                                        <div>
                                            Date
                                        </div>
                                        <input type="date" name="added_on" id="added_on_id" value="<?php if ($added_on != "") {
                                                                                                        echo date("Y-m-d", strtotime($added_on));
                                                                                                    } else {
                                                                                                        echo date("Y-m-d");
                                                                                                    } ?>" onchange="disable_month_year();" class="form-control ">
                                    </div> -->
                                    <div class="col-sm-3">
                                        <div>Month</div>
                                        <select name="month" id="month_id" class="form-control ">
                                            <option value="">Select Month</option>
                                            <option <?php if ($month == "January") {
                                                        echo "selected";
                                                    } ?> value="January">January</option>
                                            <option <?php if ($month == "February") {
                                                        echo "selected";
                                                    } ?> value="February">February</option>
                                            <option <?php if ($month == "March") {
                                                        echo "selected";
                                                    } ?> value="March">March</option>
                                            <option <?php if ($month == "April") {
                                                        echo "selected";
                                                    } ?> value="April">April</option>
                                            <option <?php if ($month == "May") {
                                                        echo "selected";
                                                    } ?> value="May">May</option>
                                            <option <?php if ($month == "June") {
                                                        echo "selected";
                                                    } ?> value="June">June</option>
                                            <option <?php if ($month == "July") {
                                                        echo "selected";
                                                    } ?> value="July">July</option>
                                            <option <?php if ($month == "August") {
                                                        echo "selected";
                                                    } ?> value="August">August</option>
                                            <option <?php if ($month == "September") {
                                                        echo "selected";
                                                    } ?> value="September">September</option>
                                            <option <?php if ($month == "October") {
                                                        echo "selected";
                                                    } ?> value="October">October</option>
                                            <option <?php if ($month == "November") {
                                                        echo "selected";
                                                    } ?> value="November">November</option>
                                            <option <?php if ($month == "December") {
                                                        echo "selected";
                                                    } ?> value="December">December</option>
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <div>Year</div>
                                        <?php $y = date('Y'); ?>
                                        <select name="year" id="year_id" class="form-control">
                                            <option value="">Select Year</option>
                                            <?php for ($j = $y; $j >= 2022; $j--) { ?>
                                                <option <?php if ($j == $year) {
                                                            echo "selected";
                                                        } ?> value="<?php echo $j; ?>"><?php echo $j; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-3">
                                        <div>Category</div>
                                        <select name="category" id="category_id" class="form-control ">
                                            <option value="">Select Category</option>
                                            <?php
                                            $category_name = "";
                                            foreach ($category_array as $caste) { ?>
                                                <option <?php if ($category_val == $caste['id']) {
                                                            $category_name = $caste['category'];
                                                            echo "selected";
                                                        } ?> value="<?php echo $caste['id']; ?>"><?php echo $caste['category']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <div>Bank</div>
                                        <select name="bank" id="bank_id" onchange="disable_category();" class="form-control ">
                                            <option value="">Select Bank</option>
                                            <?php
                                            $bank_name = "";
                                            foreach ($banks as $bank) { ?>
                                                <option <?php if ($bank_val == $bank['bank']) {
                                                            $bank_name = $bank['bank'];
                                                            echo "selected";
                                                        } ?> value="<?php echo $bank['bank']; ?>"><?php echo $bank['bank']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-1 mt-4"><button type="submit" class="btn btn-primary">Submit</button></div>
                                </div>
                            </form>

                            <?php $sno = 0;
                            $member_ft = 0;

                            ?>

                            <?php if (isset($cashbook) && count($cashbook) > 0) { ?>
                                <div class="print-container">
                                    <br>
                                    <center>
                                        <h1> <?php if ($month != "" &&  $year != "") {
                                                    echo $month . ' - ' . $year;
                                                } else if ($year != "") {
                                                    echo $year;
                                                } else if ($month != "") {
                                                    echo $month;
                                                } else {
                                                    echo date("d-m-Y", strtotime($added_on));
                                                } ?></h1>
                                    </center>
                                    <div class="row">
                                        <div class="col-sm-2"></div>
                                        <div class="col-sm-8" style="padding-right: 0px;">
                                            <div class="tile">
                                                <div class="tile-body">
                                                    <br><br>
                                                    <center>
                                                        <h3>GL - <?php echo $category_name . $bank_name; ?></h3>
                                                    </center><br>

                                                    <table id="" class="table table-striped table-bordered " style="width:100%">
                                                        <thead>
                                                            <tr>
                                                                <th class="text-center">S.No.</th>
                                                                <th class="text-center">Date</th>
                                                                <th class="text-center">Heading</th>
                                                                <th class="text-center">Income</th>
                                                                <th class="text-center">Expense</th>
                                                                <th class="text-center">Total</th>
                                                            </tr>
                                                        </thead>
                                                        <!-- Here's where we iterate through our $articles query object, printing out article info -->
                                                        <tbody>
                                                            <?php $sno = 1;
                                                            $total_income = 0;
                                                            $total_expese = 0;
                                                            $amount = 0;
                                                            ?>
                                                            <?php
                                                            foreach ($cashbook as $key => $cb) {
                                                                $datecount = 1;
                                                                foreach ($cb as $heading => $pay) {
                                                            ?>
                                                                    <tr>
                                                                        <td class="text-center">
                                                                            <?= $sno ?>
                                                                        </td>
                                                                        <td class="text-center">
                                                                            <?php if ($datecount == 1) {
                                                                                echo $key;
                                                                            } ?>
                                                                        </td>
                                                                        <td class="text-center">
                                                                            <?= $heading; ?>
                                                                        </td>
                                                                        <td class="text-center">
                                                                            <?= $pay['income']; ?>
                                                                        </td>
                                                                        <td class="text-center">
                                                                            <?= $pay['expense']; ?>
                                                                        </td>
                                                                        <td class="text-center">
                                                                            <?php echo abs($pay['income'] - $pay['expense']);
                                                                            $total_income += $pay['income'];
                                                                            $total_expese += $pay['expense'];
                                                                            $amount += ($pay['income'] - $pay['expense']);
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                    <?php
                                                                    $datecount++;
                                                                    $sno++; ?>
                                                            <?php
                                                                }
                                                            } ?>

                                                            <tr>
                                                                <td class="text-center">

                                                                </td>
                                                                <td class="text-center">

                                                                </td>
                                                                <td class="text-center">

                                                                </td>
                                                                <td class="text-center">
                                                                    <b> <?php echo   abs($total_income); ?></b>
                                                                </td>
                                                                <td class="text-center">
                                                                    <b> <?php echo  abs($total_expese); ?></b>
                                                                </td>
                                                                <td class="text-center">
                                                                    <b> <?php echo abs($amount); ?></b>
                                                                </td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <center>
                                    <form>
                                        <input type="submit" onclick="window.print(); " class="btn btn-primary" value="Print" />
                                    </form>
                                </center>
                            <?php } ?>
                        </div>
                    </div>
                </div>
        </div>
</section>
</div>
</div>
</section>
<script>
    function disable_category() {
        var bank_id = $("#bank_id").val();
        if (bank_id != "") {
            $("#category_id").val("");
            $('#category_id').prop('disabled', 'disabled');
            $("#category_id").attr("readonly", true);
        } else {
            $('#category_id').prop('disabled', false);
            $("#category_id").attr("readonly", false);
        }
    }
</script>