<?php
/*
 * 다른 폴더에서 wallet 기능을 가져다 쓰는 샘플.
 * 위치 예시: /var/www/html/aiwk/sample/wallet_use_sample.php
 * 핵심:
 *   require_once dirname(__FILE__) . '/../wallet/wallet_lib.php';
 */
require_once dirname(__FILE__) . '/../wallet/wallet_lib.php';

$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 1;
$balances = array();
$ledger = array();
$error = '';

try {
    $balances = wallet_get_balances($user_id);
    $ledger = wallet_ledger_list($user_id, 10);
} catch (Exception $e) {
    $error = $e->getMessage();
}

$AIWK_PAGE = array(
    'title' => 'AIWK Wallet Include Sample',
    'active_menu' => 'sample',
    'fluid' => false,
    'use_tailwind' => true
);
require_once dirname(__FILE__) . '/../aiwk_header.php';
?>

<div class="mb-4">
  <h1 class="h3 mb-1">다른 폴더에서 Wallet 사용 샘플</h1>
  <p class="text-body-secondary mb-0">이 파일은 <code>/sample</code> 폴더에 있지만 <code>/wallet/wallet_lib.php</code>를 include해서 잔액과 ledger를 읽습니다.</p>
</div>

<?php if ($error) { ?>
<div class="alert alert-danger"><?php echo aiwk_h($error); ?></div>
<?php } else { ?>

<form method="get" class="row g-2 align-items-end mb-3">
  <div class="col-12 col-sm-6 col-md-3">
    <label class="form-label">user_id</label>
    <input class="form-control" name="user_id" value="<?php echo aiwk_h($user_id); ?>">
  </div>
  <div class="col-12 col-sm-auto">
    <button class="btn btn-primary">조회</button>
  </div>
</form>

<div class="row g-3 mb-4">
  <div class="col-6 col-lg-3">
    <div class="card aiwk-card-soft tw-rounded-2xl tw-shadow-md">
      <div class="card-body">
        <div class="text-body-secondary small">AVAILABLE</div>
        <div class="aiwk-stat-value"><?php echo aiwk_h($balances['AVAILABLE']); ?></div>
      </div>
    </div>
  </div>
  <div class="col-6 col-lg-3">
    <div class="card aiwk-card-soft tw-rounded-2xl tw-shadow-md">
      <div class="card-body">
        <div class="text-body-secondary small">PAID</div>
        <div class="aiwk-stat-value"><?php echo aiwk_h($balances['PAID']); ?></div>
      </div>
    </div>
  </div>
  <div class="col-6 col-lg-3">
    <div class="card aiwk-card-soft tw-rounded-2xl tw-shadow-md">
      <div class="card-body">
        <div class="text-body-secondary small">BONUS</div>
        <div class="aiwk-stat-value"><?php echo aiwk_h($balances['BONUS']); ?></div>
      </div>
    </div>
  </div>
  <div class="col-6 col-lg-3">
    <div class="card aiwk-card-soft tw-rounded-2xl tw-shadow-md">
      <div class="card-body">
        <div class="text-body-secondary small">LOCKED</div>
        <div class="aiwk-stat-value"><?php echo aiwk_h($balances['LOCKED']); ?></div>
      </div>
    </div>
  </div>
</div>

<div class="card tw-rounded-2xl tw-shadow-lg">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span>최근 Ledger 10건</span>
    <a class="btn btn-sm btn-outline-secondary" href="../wallet/wallet_demo.php">Wallet Demo</a>
  </div>
  <div class="table-responsive">
    <table class="table table-sm table-hover mb-0 align-middle">
      <thead>
        <tr>
          <th>ID</th>
          <th>Type</th>
          <th>From</th>
          <th>To</th>
          <th class="text-end">Amount</th>
          <th>Memo</th>
          <th>Time</th>
        </tr>
      </thead>
      <tbody>
      <?php foreach ($ledger as $r) { ?>
        <tr>
          <td><?php echo aiwk_h($r['ledger_id']); ?></td>
          <td><span class="badge text-bg-secondary"><?php echo aiwk_h($r['tx_type']); ?></span></td>
          <td><?php echo aiwk_h($r['from_user_id']); ?></td>
          <td><?php echo aiwk_h($r['to_user_id']); ?></td>
          <td class="text-end aiwk-mono"><?php echo aiwk_h($r['amount']); ?></td>
          <td><?php echo aiwk_h($r['memo']); ?></td>
          <td class="text-nowrap small text-body-secondary"><?php echo aiwk_h($r['created_at']); ?></td>
        </tr>
      <?php } ?>
      </tbody>
    </table>
  </div>
</div>

<?php } ?>

<?php require_once dirname(__FILE__) . '/../aiwk_footer.php'; ?>
