<?php
$this->load->library("BIBAccounts");
$BIBAccounts = new BIBAccounts();
?>
<?php
$IncomeAccounts = $this->acc->getAccountsByGroup(3);
$ExpenseAccounts = $this->acc->getAccountsByGroup(4);
$IncomesTotal = $this->acc->getAccountBalanceByGroup(3);
$ExpensesTotal = $this->acc->getAccountBalanceByGroup(4);
$PL = $IncomesTotal - $ExpensesTotal;

?>
<style>
      table,
    td,
    tr {
        padding: 0px;
        margin: 0px;
    }
    .group-name {
        width: 40%;
    }
    .group-total {
        width: 10%;
    }
    .table-gray {
        background-color: #eee;
    }
    .table td,
    .table th {
        padding: 0px;
        margin: 0px;
    }
    .bg-darker {
        background-color: #ddd;
    }
    td .table {
        border: 0px !important;
        margin-bottom: 0rem;
    }
    table td:first-child {
        padding-left: 5px;
    }
    .table-bordered.vertical-borders td,
    .table-bordered.vertical-borders th {
        border-left: 1px solid #dee2e6;
        border-right: 1px solid #dee2e6;
    }
    .table-bordered.vertical-borders thead th {
        padding: 0.25rem;
        border-top: none;
        border-bottom: 2px solid #dee2e6;
    }
    .table-bordered.vertical-borders tfoot th {
        padding: 0.25rem;
        border-top: 2px solid #dee2e6;
        border-bottom: none;
    }
.currency{
    font-weight: normal !important;
}
.underline{
    text-decoration: underline;
}
    .form-control {
        display: block;
        width: 100%;
        height: calc(1.5rem + 1px);
        padding: 0.375rem 0.75rem;
        font-size: 1rem;
        font-weight: 400;
        line-height: 1.5;
        color: #495057;
        background-color: #fff;
        background-clip: padding-box;
        border: 1px solid #ced4da;
        border-radius: 0.25rem;
        box-shadow: inset 0 0 0 transparent;
        transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;
    }
</style>





<div class="content-wrapper">
    
    <section class="content">
        <div class="container-fluid">
            <div class="card card-primary ">

            <div class="card-header">
                <h3 class="card-title">Receipts And Payments <?php //echo $pageTitle; ?> </h3>

                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-plus"></i>
                  </button>
                </div>
                <!-- /.card-tools -->
              </div>


             
                <div class="card-body">
                <form method="post" action="" id="FilterForm">
                        <div class="row align-items-end">
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="fromDate"><?php myLang("Starting Period"); ?></label>
                                    <input type="text" class="form-control nepaliDatePicker" name="fromDate" value="<?php echo (isset($_POST['fromDate'])) ? $_POST['fromDate'] : NepaliDate($this->session->FiscalYear->fiscalyear_from); ?>" id="fromDate" aria-describedby="helpId_fromDate" placeholder="Starting Period">
                                </div>
                            </div>
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="toDate"><?php myLang("Ending Period"); ?></label>
                                    <input type="text" class="form-control nepaliDatePicker" name="toDate" value="<?php echo (isset($_POST['toDate'])) ? $_POST['toDate'] : NepaliDate($this->session->FiscalYear->fiscalyear_to); ?>" id="toDate" aria-describedby="helpId_toDate" placeholder="Ending Period">
                                </div>
                            </div>
                            <div class="col-1 text-center">
                                <div class="form-group">
                                    <label for="showOB"><?php myLang("Opening"); ?></label>
                                    <input type="checkbox" class="form-control " name="showOB" id="showOB" aria-describedby="helpId_showOB" placeholder="Show Opening Balance" <?php echo (isset($_POST['showOB'])) ? "CHECKED" : ""; ?>>
                                </div>
                            </div>
                            <div class="col-1 text-center">
                                <div class="form-group">
                                    <label for="showClosing"><?php myLang("Closing"); ?></label>
                                    <input type="checkbox" class="form-control " name="showClosing" id="showClosing" aria-describedby="helpId_showClosing" placeholder="Show Closing Balance" <?php echo (isset($_POST['showClosing'])) ? "CHECKED" : ""; ?>>
                                </div>
                            </div>
                            <div class="col-1 text-center">
                                <div class="form-group">
                                    <label for="showPeriod"><?php myLang("Period"); ?></label>
                                    <input type="checkbox" class="form-control " name="showPeriod" id="showPeriod" aria-describedby="helpId_showPeriod" placeholder="Show Periodic Balance" <?php echo (isset($_POST['showPeriod'])) ? "CHECKED" : "CHECKED"; ?>>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group d-flex justify-content-end">
                                    <button class="btn btn-secondary toggle-collapse mr-2" id="toggleShowAll">Toggle Collapse</button>
                                    <button type="submit" class="btn btn-primary mr-2">Filter</button>
                                    <button type="submit" class="btn btn-success mr-2">Export</button>
                                    <button type="button" class="btn btn-warning" onclick="resetForm()">Reset</button>
                                </div>
                            </div>
                        </div>
                      
                    </form>

                    <script>
                        function resetForm() {
                            document.getElementById("fromDate").value = "2078-4-1";
                            document.getElementById("toDate").value = "2079-3-32";
                            document.getElementById("showOB").checked = false;
                            document.getElementById("showClosing").checked = false;
                            document.getElementById("FilterForm").submit();
                        }
                    </script>

                </div>

            </div>
            <div class="card">
              
                <div class="card-body table-responsive p-0">
                    <?php $IncomesExpenses = $BIBAccounts->prepareIncomeExpenses() ?>
                    <table class="table table-head-fixed table-bordered  g-0">
                        <thead>
                            <tr>
                                <th class="" width="35%"><?php myLang("Receipts"); ?></th>
                                <th class="text-right" width="15%"></th>
                                <th class="text-right" width="15%"><?php myLang("Amount"); ?></th>
                                <th class="" width="35%"><?php echo myLang("Payments"); ?></th>
                                <th class="text-right" width="15%"></th>
                                <th class="text-right" width="15%"><?php myLang("Amount"); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td colspan=3 class="p-0">
                                    <table class="table g-0">
                                        <?php $total1 = 0;
                                        foreach ($IncomesExpenses['Incomes'] as $Incomes) { ?>

                                            <?php if ($Incomes['account'] == "GROUP TOTAL") : ?>
                                                <!--tr>
                                                    <td class="" width="70%"><b><?php echo $Incomes['group']; ?></b></td>
                                                    <td width="15%"></td>
                                                    <td class="text-right" width="15%"><u><?php echo myCurrency($Incomes['amount']); ?></u></td>

                                                </tr-->
                                            <?php elseif ($Incomes['account'] == "CATEGORY TOTAL") : ?>
                                                <tr>
                                                    <td class="" width="70%"><b><?php echo $Incomes['category']; ?></b></td>
                                                    <td class="text-right" width="15%"><?php echo myCurrency($Incomes['amount']); ?></td>
                                                    <td width="15%"></td>
                                                </tr>
                                            <?php else : ?>
                                                <?php $total1 += $Incomes['amount']; ?>
                                                <tr>
                                                    <td class="" width="70%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $Incomes['account']; ?></td>
                                                    <td width="15%"></td>
                                                    <td class="text-right" width="15%"><?php echo myCurrency($Incomes['amount']); ?></td>

                                                </tr>
                                            <?php endif; ?>

                                        <?php }; ?>

                                    </table>
                                </td>
                                <td colspan=3 class="p-0">
                                    <table class="table g-0">
                                        <?php $total = 0;
                                        foreach ($IncomesExpenses['Expenses'] as $Expenses) {
                                            if ($Expenses['account'] == "GROUP TOTAL") : ?>
                                                <!--tr>
                                                    <td class="" width="70%"><b><?php echo $Expenses['group']; ?></b></td>
                                                    <td width="15%"></td>
                                                    <td class="text-right" width="15%"><u><?php echo myCurrency($Expenses['amount']); ?></u></td>


                                                </tr-->
                                            <?php elseif ($Expenses['account'] == "CATEGORY TOTAL") : ?>
                                                <tr>
                                                    <td class="" width="70%"><b><?php echo $Expenses['category']; ?></b></td>
                                                    <td class="text-right" width="15%"><?php echo myCurrency($Expenses['amount']); ?></td>
                                                    <td width="15%"></td>

                                                </tr>

                                            <?php else : ?>
                                                <?php $total += $Expenses['amount']; ?>
                                                <tr>
                                                    <td class="" width="70%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $Expenses['account']; ?></td>
                                                    <td width="15%"></td>
                                                    <td class="text-right" width="15%"><?php echo myCurrency($Expenses['amount']); ?></td>


                                                </tr>
                                            <?php endif; ?>



                                        <?php }; ?>

                                    </table>
                                </td>
                            </tr>
                            <?php $balance_fi = $total1 - $total; ?>
                            <?php if ($balance_fi < 0) :  $total1 +=$balance_fi; ?>
                                <tr>
                                    <th><?php echo myLang("Closing Balance"); ?></th>
                                    <th></th>
                                    <td><span class="text-red"><?php echo myCurrency($balance_fi); ?></span></td>
                                    <th></th>
                                    <th></th>
                                    <td></td>
                                </tr>
                            <?php elseif ($balance_fi > 0) : $total +=$balance_fi;  ?>
                                <tr>
                                    <th></th>
                                    <th></th>
                                    <td></td>
                                    <th><?php echo myLang("Closing Balance"); ?></th>
                                    <th></th>
                                    <td><?php echo myCurrency($balance_fi); ?></td>
                                </tr>

                            <?php endif; ?>

                            <tr>
                                <th>Total</th>
                                <th></th>
                                <td><?php echo myCurrency($total1); ?></td>
                                <th>Total</th>
                                <th></th>
                                <td><?php echo myCurrency($total); ?></td>
                            </tr>
                        </tbody>



                    </table>


                </div>
            </div>


        </div><!-- /.container-fluid -->
    </section>