<?php /* suppliers/view.php */ ?>

<!-- Header -->
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="fw-semibold mb-0">
      <i class="bi bi-truck me-2 text-primary"></i><?= e($supplier['supplier_name']) ?>
    </h5>
    <small class="text-muted ms-4">
      <?php if (!empty($supplier['contact_person'])): ?>
        Contact: <?= e($supplier['contact_person']) ?> &nbsp;·&nbsp;
      <?php endif; ?>
      <span class="badge <?= $supplier['status']==='active'?'badge-status-active':'badge-status-inactive' ?>">
        <?= ucfirst($supplier['status']) ?>
      </span>
    </small>
  </div>
  <div class="d-flex gap-2">
    <?php if (Auth::can('purchases.create')): ?>
    <a href="<?= APP_URL ?>/purchases/create" class="btn btn-sm btn-success">
      <i class="bi bi-cart-plus me-1"></i>New Purchase
    </a>
    <?php endif; ?>
    <?php if (Auth::can('suppliers.edit')): ?>
    <a href="<?= APP_URL ?>/suppliers/edit/<?= $supplier['id'] ?>"
       class="btn btn-sm btn-outline-primary">
      <i class="bi bi-pencil me-1"></i>Edit
    </a>
    <?php endif; ?>
    <a href="<?= APP_URL ?>/suppliers" class="btn btn-sm btn-outline-secondary">
      <i class="bi bi-arrow-left me-1"></i>Back
    </a>
  </div>
</div>

<!-- Stats Row -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-2">
    <div class="card text-center py-3">
      <div class="fw-bold fs-5 text-primary"><?= $stats['total_invoices'] ?></div>
      <small class="text-muted">Invoices</small>
    </div>
  </div>
  <div class="col-6 col-md-2">
    <div class="card text-center py-3">
      <div class="fw-bold"><?= currency($stats['total_purchased']) ?></div>
      <small class="text-muted">Total Purchased</small>
    </div>
  </div>
  <div class="col-6 col-md-2">
    <div class="card text-center py-3">
      <div class="fw-bold text-success"><?= currency($stats['total_paid']) ?></div>
      <small class="text-muted">Total Paid</small>
    </div>
  </div>
  <div class="col-6 col-md-2">
    <div class="card text-center py-3 <?= $stats['total_balance']>0?'border-danger':'' ?>">
      <div class="fw-bold text-danger"><?= currency($stats['total_balance']) ?></div>
      <small class="text-muted">Outstanding</small>
    </div>
  </div>
  <div class="col-6 col-md-2">
    <div class="card text-center py-3 <?= $stats['due_invoices']>0?'border-warning':'' ?>">
      <div class="fw-bold text-warning"><?= $stats['due_invoices'] ?></div>
      <small class="text-muted">Due Invoices</small>
    </div>
  </div>
  <div class="col-6 col-md-2">
    <div class="card text-center py-3 <?= count($pendingCheques)>0?'border-warning':'' ?>">
      <div class="fw-bold <?= count($pendingCheques)>0?'text-warning':'' ?>">
        <?= count($pendingCheques) ?>
      </div>
      <small class="text-muted">Pending Cheques</small>
    </div>
  </div>
</div>

<!-- Info + Balance Side by Side -->
<div class="row g-3 mb-3">

  <!-- Supplier Info -->
  <div class="col-md-5">
    <div class="card h-100">
      <div class="card-header bg-transparent fw-semibold small">
        <i class="bi bi-info-circle me-1"></i>Supplier Details
      </div>
      <div class="card-body">
        <dl class="row small mb-0">
          <?php if (!empty($supplier['contact_person'])): ?>
          <dt class="col-4 text-muted">Contact</dt>
          <dd class="col-8"><?= e($supplier['contact_person']) ?></dd>
          <?php endif; ?>

          <dt class="col-4 text-muted">Phone</dt>
          <dd class="col-8">
            <?php if (!empty($supplier['phone'])): ?>
            <a href="tel:<?= e($supplier['phone']) ?>" class="text-decoration-none">
              <?= e($supplier['phone']) ?>
            </a>
            <?php else: ?><span class="text-muted">—</span><?php endif; ?>
          </dd>

          <dt class="col-4 text-muted">Email</dt>
          <dd class="col-8">
            <?php if (!empty($supplier['email'])): ?>
            <a href="mailto:<?= e($supplier['email']) ?>" class="text-decoration-none">
              <?= e($supplier['email']) ?>
            </a>
            <?php else: ?><span class="text-muted">—</span><?php endif; ?>
          </dd>

          <?php if (!empty($supplier['address'])): ?>
          <dt class="col-4 text-muted">Address</dt>
          <dd class="col-8"><?= nl2br(e($supplier['address'])) ?></dd>
          <?php endif; ?>

          <dt class="col-4 text-muted">Opening Bal.</dt>
          <dd class="col-8"><?= currency($supplier['opening_balance'] ?? 0) ?></dd>
        </dl>
      </div>
    </div>
  </div>

  <!-- Balance + Pending Cheques -->
  <div class="col-md-7">
    <div class="card h-100 <?= $supplier['current_balance']>0?'border-danger':'' ?>">
      <div class="card-header bg-transparent fw-semibold small d-flex justify-content-between">
        <span><i class="bi bi-currency-exchange me-1"></i>Account Balance</span>
        <?php if ($supplier['current_balance'] > 0): ?>
        <a href="<?= APP_URL ?>/purchases?status=received&pmt=due&search=<?= urlencode($supplier['supplier_name']) ?>"
           class="btn btn-sm btn-outline-danger py-0 px-2 no-print">
          <i class="bi bi-eye me-1"></i>View Due Invoices
        </a>
        <?php endif; ?>
      </div>
      <div class="card-body">
        <div class="text-center py-2 mb-3">
          <?php if ($supplier['current_balance'] > 0): ?>
          <div class="text-muted small">Outstanding Balance</div>
          <div class="fw-bold display-6 text-danger"><?= currency($supplier['current_balance']) ?></div>
          <small class="text-muted">Amount owed to this supplier</small>
          <?php else: ?>
          <i class="bi bi-check-circle-fill text-success fs-2 d-block mb-1"></i>
          <div class="fw-semibold text-success">Account Cleared</div>
          <small class="text-muted">No outstanding balance</small>
          <?php endif; ?>
        </div>

        <?php if (!empty($pendingCheques)): ?>
        <div class="border-top pt-2">
          <div class="small fw-semibold text-warning mb-1">
            <i class="bi bi-hourglass me-1"></i>Pending Cheques
          </div>
          <?php foreach ($pendingCheques as $chq): ?>
          <div class="d-flex justify-content-between align-items-center small py-1 border-bottom">
            <div>
              <div class="fw-semibold">#<?= e($chq['cheque_no']) ?></div>
              <small class="text-muted">
                <?= e($chq['invoice_no']) ?>
                <?php if ($chq['cheque_date']): ?>
                  · <?= fDate($chq['cheque_date']) ?>
                <?php endif; ?>
                <?php if (!empty($chq['bank_name'])): ?>
                  · <?= e($chq['bank_name']) ?>
                <?php endif; ?>
              </small>
            </div>
            <div class="text-end">
              <div class="fw-bold text-warning"><?= currency($chq['amount']) ?></div>
              <a href="<?= APP_URL ?>/cheques?type=purchase&status=pending"
                 class="text-muted" style="font-size:.7rem">Manage →</a>
            </div>
          </div>
          <?php endforeach; ?>
          <div class="small text-end fw-bold text-warning mt-1">
            Total Pending: <?= currency(array_sum(array_column($pendingCheques,'amount'))) ?>
          </div>
        </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

<!-- Payment History -->
<?php if (!empty($payments)): ?>
<div class="card mb-3">
  <div class="card-header bg-transparent fw-semibold small">
    <i class="bi bi-clock-history me-1"></i>Payment History
    <span class="badge bg-secondary ms-1"><?= count($payments) ?></span>
  </div>
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-sm mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th>Invoice</th>
            <th>Date</th>
            <th>Method</th>
            <th>Reference</th>
            <th>Paid By</th>
            <th class="text-end">Amount</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($payments as $i => $pmt): ?>
          <tr>
            <td class="text-muted small"><?= $i+1 ?></td>
            <td>
              <a href="<?= APP_URL ?>/purchases/view/<?= $pmt['purchase_id'] ?>"
                 class="text-decoration-none fw-semibold">
                <?= e($pmt['invoice_no']) ?>
              </a>
            </td>
            <td><small><?= fDate($pmt['payment_date']) ?></small></td>
            <td>
              <span class="badge bg-secondary">
                <?= ucfirst(str_replace('_',' ',$pmt['payment_method'])) ?>
              </span>
            </td>
            <td class="text-muted small"><?= e($pmt['reference'] ?? '—') ?></td>
            <td><small><?= e($pmt['paid_by'] ?? '—') ?></small></td>
            <td class="text-end fw-semibold text-success"><?= currency($pmt['amount']) ?></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
        <tfoot class="table-secondary fw-bold">
          <tr>
            <td colspan="6">Total Paid</td>
            <td class="text-end text-success">
              <?= currency(array_sum(array_column($payments,'amount'))) ?>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>
</div>
<?php endif; ?>

<!-- Purchase History -->
<div class="card">
  <div class="card-header bg-transparent fw-semibold small d-flex justify-content-between align-items-center">
    <span><i class="bi bi-cart3 me-1"></i>Purchase History</span>
    <span class="text-muted fw-normal small"><?= count($purchases) ?> record(s)</span>
  </div>
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-sm table-hover mb-0">
        <thead class="table-dark">
          <tr>
            <th>#</th>
            <th>Invoice</th>
            <th>Date</th>
            <th class="text-end">Total</th>
            <th class="text-end">Paid</th>
            <th class="text-end">Balance</th>
            <th>Payment</th>
            <th>Status</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($purchases as $i => $p):
            $hasBalance = (float)$p['balance'] > 0;
          ?>
          <tr class="<?= $p['status']==='cancelled'?'table-secondary':($hasBalance?'table-warning bg-opacity-10':'') ?>">
            <td class="text-muted small"><?= $i + 1 ?></td>
            <td>
              <a href="<?= APP_URL ?>/purchases/view/<?= $p['id'] ?>"
                 class="fw-semibold text-decoration-none">
                <?= e($p['invoice_no']) ?>
              </a>
            </td>
            <td><small><?= fDate($p['purchase_date']) ?></small></td>
            <td class="text-end fw-semibold"><?= currency($p['total']) ?></td>
            <td class="text-end text-success"><?= currency($p['paid']) ?></td>
            <td class="text-end <?= $hasBalance?'text-danger fw-bold':'' ?>">
              <?= $hasBalance ? currency($p['balance']) : '—' ?>
            </td>
            <td>
              <?php $ps = $p['payment_status']; ?>
              <span class="badge <?= $ps==='paid'?'bg-success':($ps==='partial'?'bg-warning text-dark':'bg-danger') ?>">
                <?= ucfirst($ps) ?>
              </span>
            </td>
            <td>
              <?php $st = $p['status']; ?>
              <span class="badge <?= $st==='received'?'bg-success':($st==='cancelled'?'bg-secondary':'bg-info') ?>">
                <?= ucfirst($st) ?>
              </span>
            </td>
            <td>
              <div class="d-flex gap-1">
                <a href="<?= APP_URL ?>/purchases/view/<?= $p['id'] ?>"
                   class="btn btn-sm btn-outline-primary py-0 px-2" title="View">
                  <i class="bi bi-eye"></i>
                </a>
                <?php if ($hasBalance && $p['status']==='received' && Auth::can('purchases.edit')): ?>
                <a href="<?= APP_URL ?>/purchases/view/<?= $p['id'] ?>"
                   class="btn btn-sm btn-outline-warning py-0 px-2" title="Pay Balance">
                  <i class="bi bi-cash-coin"></i>
                </a>
                <?php endif; ?>
              </div>
            </td>
          </tr>
          <?php endforeach; ?>

          <?php if (empty($purchases)): ?>
          <tr>
            <td colspan="9" class="text-center text-muted py-4">
              <i class="bi bi-cart3 fs-2 d-block mb-1 opacity-25"></i>No purchases recorded yet.
            </td>
          </tr>
          <?php endif; ?>
        </tbody>
        <?php if (!empty($purchases)): ?>
        <tfoot class="table-secondary fw-bold">
          <tr>
            <td colspan="3">TOTAL (<?= count($purchases) ?> invoices)</td>
            <td class="text-end"><?= currency(array_sum(array_column($purchases,'total'))) ?></td>
            <td class="text-end text-success"><?= currency(array_sum(array_column($purchases,'paid'))) ?></td>
            <td class="text-end text-danger"><?= currency(array_sum(array_column($purchases,'balance'))) ?></td>
            <td colspan="3"></td>
          </tr>
        </tfoot>
        <?php endif; ?>
      </table>
    </div>
  </div>
</div>
