<?php include_once('../header.php');
if (!check_login()) {
    header("Location: " . WEB_URL . "logout.php");
    die();
}
?>
<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["tenant_id"])) {
        $result = mysqli_query($link, "SELECT * FROM tenants WHERE tid = {$_POST["tenant_id"]}");;
        $tenant = mysqli_fetch_array($result);
        
        $assign_unit_url = WEB_URL . "tenant/addtenant.php?action=assign"; ?>
    <!-- Content Header (Page header) -->

    <section class="content-header">
    <ol class="breadcrumb">
        <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i><?= $_data['home_breadcam']; ?></a></li>
        <li class="active">Tenant Account Statement</li>
    </ol>
    </section>
    <!-- Main content -->
    <section class="content">
    <!-- Full Width boxes (Stat box) -->
    <div class="row">
    <div class="col-xs-12">
        <div class="box box-success mt-3">
        <div class="box-header">
            <h3 class="box-title"><?= $tenant["r_name"]; ?> 's Account</h3>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <table class="table sakotable table-bordered table-striped dt-responsive">
            <thead>
                <tr>
                <th>Date</th>
                <th>Description</th>
                <th>Debit $</th>
                <th>Credit $</th>
                <th>Balance $</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    $balance = 0;
        $tDebit = 0;
        $tCredit = 0;
        $result = mysqli_query($link, "SELECT * FROM tenant_accounts WHERE tid = {$_POST["tenant_id"]} order by created_at ASC");
        while ($row = mysqli_fetch_array($result)) {
            ?>
                <tr>
                <td><?= $row['created_at'] ?></td>
                <td><?= $row['description'] ?></td>
                <td>
    <?php 
        if ($row['trans_type'] == "credit") {
            echo number_format($row["transaction_amt"], 2); // Format to 2 decimal places
            $tDebit += $row["transaction_amt"]; 
        }
    ?>
</td>

                <td>
                    <?php 
                        if($row['trans_type'] == "debit"){
                            echo number_format($row["transaction_amt"], 2);
                            $tCredit += $row["transaction_amt"]; 
                        }
                    ?>
                </td>
                <td><?php
                    $balance = floatval($tDebit ?? 0) - floatval($tCredit ?? 0); 
                    echo number_format($balance, 2);  ?></td>
                </tr>
                <?php
        }
        mysqli_close($link);
        $link = null; ?>
            </tbody>
            <tfooter>
                <tr>
                    <td></td>
                    <th>Total</th>
                    <th><?= number_format($tDebit, 2)?></th>
                    <th><?= number_format($tCredit, 2) ?></th>
                    <th><?= number_format($balance, 2) ?></th>
                </tr>
            </tfooter>
            </table>
        </div>
        <!-- /.box-body -->
        </div>
        <!-- /.box -->
    </div>
    <!-- /.col -->
    </div>
    <!-- /.row -->
    <script type="text/javascript">
    function deleteTenant(Id){
    var iAnswer = confirm("<?= $_data['confirm']; ?>");
    if(iAnswer){
        window.location = '<?= WEB_URL; ?>tenant/tenantlist.php?id=' + Id;
    }
    }

    function ejectTenant(unit_assignment_id){
    var iAnswer = confirm("<?= $_data['confirm']; ?>");
    if(iAnswer){
        post("<?= WEB_URL; ?>tenant/eject_tenant.php", {assignment_id:unit_assignment_id});
        //window.location = '<?= WEB_URL; ?>tenant/eject_tenant.php?assignment_id=' + unit_assignment_id;
    }
    }
    
    $( document ).ready(function() {
        setTimeout(function() {
            $("#me").hide(300);
            $("#you").hide(300);
        }, 3000);
    });
    </script>
<?php include_once('../footer.php');
    }
    else{
        header("Location: " . WEB_URL . "tenant/tenantlist.php"); 
    }?>
