<?php include("includes/db_config.php");
// Get party ID from request (ensure proper validation)
$party_id = isset($_GET['party_id']) ? mysqli_real_escape_string($conn, $_GET['party_id']) : 0;

// Fetch party details
$partyQuery = "SELECT party_name, party_phone_no FROM ah_party WHERE id = '$party_id'";
$partyResult = mysqli_query($conn, $partyQuery);
$party = mysqli_fetch_assoc($partyResult);

// Fetch transactions for the party
$transactionQuery = "
    SELECT ti.id, ti.type, ti.bill_no, ti.bill_date, ti.total_amt, ti.received_amount,ti.balance 
    FROM tbl_ah_items ti 
    WHERE ti.customer_id = '$party_id' 
    ORDER BY ti.bill_date DESC
";
$transactionResult = mysqli_query($conn, $transactionQuery);
 ?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?>
    <style>
        .modal-body .form-label{font-weight:600;}.viewptdt .form-label{font-weight:600;}.viewptdt label{margin-top:10px;}
    </style>
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">

            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">                    
                    <div class="row">
                        <div class="col-12">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                     <div class="col-md-10">
                                        (<strong>Party Name:</strong> <?= htmlspecialchars($party['party_name']); ?>, 
                                             <strong>Contact No.:</strong> +91-<?= htmlspecialchars($party['party_phone_no']); ?>)
                                    </div>
                                    <div class="col-md-2">
                                        <center><a href="all-party.php"><button class="btn btn-success btn-md"><i class="fa fa-arrow-left"></i> Back</button></a></center>
                                    </div>
                                 </div>
                                <div class="portlet-body table-responsive">
                                    <table id="datatable-1" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                 <th>Type</th>
                                                <th>Bill No</th>
                                                <th>Bill Date</th>
                                                 <th class="text-center">Debit</th>
                                                 <th class="text-center">Credit</th>
                                                 <th class="text-center">Balance</th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>  
                                            <?php 
                                            $i = 1;
                                            $totalBalance = 0; 
                                            while ($row = mysqli_fetch_assoc($transactionResult)) { 
                                                $totalBalance += $row['balance']; ?>
                                                <tr>
                                                    <td><?= $i++; ?></td>
                                                    <td><?= htmlspecialchars($row['type']); ?></td>
                                                    <td><?= htmlspecialchars($row['bill_no']); ?></td>
                                                    <td><?= $row['bill_date']; ?></td>
                                                    <td class="text-center" >₹ <?= number_format($row['total_amt'], 2); ?></td>
                                                    <td class="text-center">₹ <?= number_format($row['received_amount'], 2); ?></td>
                                                     <td class="text-center">₹ <?= number_format($row['balance'], 2); ?></td>
                                                </tr>
                                            <?php } ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th colspan="5"></th>
                                                <th><b>Total Balance :</b></th>
                                                <th class="text-center">₹ <?= number_format($totalBalance, 2); ?></th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div> 
            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
    <script type="text/javascript">
      function delete_ptype_by_ID(id) {
        if (confirm('Do You Want to Deleting This \nContinue anyway?')) {
            window.location.href = 'delete_sales.php?id=' + id;
        }
    }
    </script>
</body>

</html>-