<?php
// hps_supplier_statement.php
require "db.php";

/* -----------------------------
   HELPERS
------------------------------*/
function esc($v){
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

/* -----------------------------
   INPUTS
------------------------------*/
$supplier_id = (int)($_GET['supplier_id'] ?? 0);
$from = $_GET['from'] ?? '';
$to   = $_GET['to'] ?? '';

if ($supplier_id <= 0) {
    die("Supplier not selected");
}

/* -----------------------------
   SUPPLIER INFO
------------------------------*/
$supplier = mysqli_fetch_assoc(mysqli_query($conn,"
    SELECT * FROM hps_suppliers WHERE id=$supplier_id
"));

if (!$supplier) die("Invalid supplier");

/* -----------------------------
   OPENING BALANCE
------------------------------*/
$opening = 0;
if ($from) {
    $ob = mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT SUM(dr - cr) bal
        FROM hps_supplier_ledger
        WHERE supplier_id=$supplier_id
          AND trans_date < '$from'
    "));
    $opening = $ob['bal'] ?? 0;
}

/* -----------------------------
   LEDGER ENTRIES
------------------------------*/
$cond = "WHERE supplier_id=$supplier_id";
if ($from) $cond .= " AND trans_date >= '$from'";
if ($to)   $cond .= " AND trans_date <= '$to'";

$rows = mysqli_query($conn,"
    SELECT *
    FROM hps_supplier_ledger
    $cond
    ORDER BY trans_date, id
");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Supplier Statement</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
@media print {
    .no-print { display:none }
    body { background:#fff }
}

body { background:#f1f3f5 }

.statement {
    background:#fff;
    max-width:1000px;
    margin:30px auto;
    padding:30px;
    border-radius:12px;
    box-shadow:0 8px 25px rgba(0,0,0,.08);
}

.header {
    border-bottom:2px solid #000;
    padding-bottom:10px;
    margin-bottom:15px;
}

.company-logo {
    font-size:22px;
    font-weight:bold;
}

.table th {
    background:#000;
    color:#fff;
}

.balance-pos { color:#dc3545; font-weight:bold }
.balance-neg { color:#198754; font-weight:bold }
</style>
</head>
<body>

<div class="statement">

<!-- HEADER -->
<div class="row header">
<div class="col-6">
<div class="company-logo">YOUR COMPANY NAME</div>
<div class="small text-muted">
Address Line 1<br>
City · Phone · Email
</div>
</div>

<div class="col-6 text-end">
<h5 class="mb-1">SUPPLIER STATEMENT</h5>
<div class="small">
From: <b><?=esc($from ?: 'Beginning')?></b><br>
To: <b><?=esc($to ?: date('Y-m-d'))?></b>
</div>
</div>
</div>

<!-- SUPPLIER INFO -->
<div class="row mb-3">
<div class="col-12">
<b>Supplier:</b> <?=esc($supplier['name'])?><br>
<b>Mobile:</b> <?=esc($supplier['mobile'])?><br>
<b>CNIC:</b> <?=esc($supplier['cnic'])?><br>
<b>Address:</b> <?=esc($supplier['address'])?>
</div>
</div>

<!-- LEDGER TABLE -->
<table class="table table-bordered table-sm">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th class="text-end">Debit</th>
<th class="text-end">Credit</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>

<?php
$running = $opening;
if ($from):
?>
<tr class="table-secondary fw-bold">
<td><?=date('d-M-Y',strtotime($from))?></td>
<td>Opening Balance</td>
<td></td>
<td></td>
<td class="text-end <?= $running>0?'balance-pos':'balance-neg' ?>">
<?=number_format($running,2)?>
</td>
</tr>
<?php endif; ?>

<?php while($r=mysqli_fetch_assoc($rows)):
    $running += ($r['dr'] - $r['cr']);
?>
<tr>
<td><?=date('d-M-Y',strtotime($r['trans_date']))?></td>
<td><?=esc($r['description'])?></td>
<td class="text-end"><?= $r['dr']>0 ? number_format($r['dr'],2) : '' ?></td>
<td class="text-end"><?= $r['cr']>0 ? number_format($r['cr'],2) : '' ?></td>
<td class="text-end <?= $running>0?'balance-pos':'balance-neg' ?>">
<?=number_format($running,2)?>
</td>
</tr>
<?php endwhile; ?>

</tbody>
</table>

<!-- FOOTER -->
<div class="row mt-3">
<div class="col-6">
<b>Final Balance:</b>
<span class="<?= $running>0?'balance-pos':'balance-neg' ?>">
<?=number_format($running,2)?>
</span>
</div>

<div class="col-6 text-end no-print">
<button onclick="window.print()" class="btn btn-secondary btn-sm">
Print Statement
</button>
</div>
</div>

</div>

</body>
</html>
