<?php
/*
 * 개발 확인용 데모. 운영 전 삭제 또는 접근 제한.
 * AIWK 공통 header/footer 사용 예시.
 */
require_once dirname(__FILE__) . '/wallet_lib.php';

$msg = '';
$msg_type = 'info';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $act = isset($_POST['act']) ? $_POST['act'] : '';
        if ($act === 'charge') {
            $tx = wallet_credit_charge((int)$_POST['user_id'], (int)$_POST['amount'], 'demo charge', 'demo', 0);
            $msg = '충전 완료 tx_id=' . htmlspecialchars($tx, ENT_QUOTES, 'UTF-8');
            $msg_type = 'success';
        } elseif ($act === 'transfer') {
            $tx = wallet_transfer((int)$_POST['from_user_id'], (int)$_POST['to_user_id'], (int)$_POST['amount'], 'demo transfer', 'demo', 0);
            $msg = '전송 완료 tx_id=' . htmlspecialchars($tx, ENT_QUOTES, 'UTF-8');
            $msg_type = 'success';
        } elseif ($act === 'adjust') {
            $tx = wallet_admin_adjust((int)$_POST['user_id'], (int)$_POST['amount'], 'demo admin adjust');
            $msg = '관리자 조정 완료 tx_id=' . htmlspecialchars($tx, ENT_QUOTES, 'UTF-8');
            $msg_type = 'success';
        }
    } catch (Exception $e) {
        $msg = '오류: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
        $msg_type = 'danger';
    }
}

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

<div class="d-flex justify-content-between align-items-start gap-3 flex-wrap mb-4">
  <div>
    <h1 class="h3 mb-1">AIWK Wallet MVP Demo</h1>
    <p class="text-body-secondary mb-0">운영 전 삭제 또는 접근 제한 필요. 이 화면은 실제 장부를 변경합니다.</p>
  </div>
  <span class="badge text-bg-warning tw-px-3 tw-py-2">DEV ONLY</span>
</div>

<?php if ($msg) { ?>
<div class="alert alert-<?php echo $msg_type; ?>" role="alert"><?php echo $msg; ?></div>
<?php } ?>

<div class="row g-3">
  <div class="col-12 col-lg-4">
    <div class="card h-100 tw-rounded-2xl tw-shadow-lg">
      <div class="card-body">
        <h2 class="h5 card-title">충전</h2>
        <form method="post" class="vstack gap-2">
          <input type="hidden" name="act" value="charge">
          <label class="form-label mb-0">user_id</label>
          <input class="form-control" name="user_id" value="1">
          <label class="form-label mb-0">amount</label>
          <input class="form-control" name="amount" value="1000">
          <button class="btn btn-primary mt-2">충전</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12 col-lg-4">
    <div class="card h-100 tw-rounded-2xl tw-shadow-lg">
      <div class="card-body">
        <h2 class="h5 card-title">전송</h2>
        <form method="post" class="vstack gap-2">
          <input type="hidden" name="act" value="transfer">
          <label class="form-label mb-0">from</label>
          <input class="form-control" name="from_user_id" value="1">
          <label class="form-label mb-0">to</label>
          <input class="form-control" name="to_user_id" value="2">
          <label class="form-label mb-0">amount</label>
          <input class="form-control" name="amount" value="100">
          <button class="btn btn-success mt-2">전송</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12 col-lg-4">
    <div class="card h-100 tw-rounded-2xl tw-shadow-lg">
      <div class="card-body">
        <h2 class="h5 card-title">관리자 조정</h2>
        <form method="post" class="vstack gap-2">
          <input type="hidden" name="act" value="adjust">
          <label class="form-label mb-0">user_id</label>
          <input class="form-control" name="user_id" value="1">
          <label class="form-label mb-0">amount</label>
          <input class="form-control" name="amount" value="-10">
          <button class="btn btn-outline-danger mt-2">조정</button>
        </form>
      </div>
    </div>
  </div>
</div>

<div class="card aiwk-card-soft mt-4 tw-rounded-2xl tw-shadow-md">
  <div class="card-body">
    <h2 class="h5">잔액 조회 예시</h2>
    <div class="d-flex gap-2 flex-wrap">
      <a class="btn btn-sm btn-outline-primary" href="wallet_api.php?action=balance&user_id=1" target="_blank">user_id=1 잔액 JSON</a>
      <a class="btn btn-sm btn-outline-secondary" href="wallet_api.php?action=ledger&user_id=1" target="_blank">user_id=1 ledger JSON</a>
      <a class="btn btn-sm btn-outline-success" href="../sample/wallet_use_sample.php?user_id=1">다른 폴더 include 샘플</a>
    </div>
  </div>
</div>

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