<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "admin") {
    header("Location: ../auth/login.php");
    exit;
}

$error = "";
$success = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $partner_id = intval($_POST['partner_id']);
    $amount = floatval($_POST['amount']);

    if ($amount <= 0) {
        $error = "Transfer amount must be greater than zero.";
    } else {

        $stmt = $pdo->prepare("
            UPDATE partner_shops
            SET credit_balance = credit_balance + ?
            WHERE id = ?
        ");
        $stmt->execute([$amount, $partner_id]);

        $transaction_code = "TRX" . time();

        $stmt = $pdo->prepare("
            INSERT INTO transactions
            (
                transaction_code,
                from_type,
                from_id,
                to_type,
                to_id,
                amount,
                transaction_type,
                remarks
            )
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ");

        $stmt->execute([
            $transaction_code,
            'admin',
            $_SESSION['user_id'],
            'partner',
            $partner_id,
            $amount,
            'credit_transfer',
            'Admin credit transfer'
        ]);

        $success = "Credit transferred successfully.";
    }
}

$partners = $pdo->query("
    SELECT *
    FROM partner_shops
    ORDER BY shop_name
")->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Transfer Credit</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f8;
            margin: 0;
            padding: 30px;
        }

        .container {
            max-width: 800px;
            margin: auto;
        }

        .card {
            background: white;
            border-radius: 14px;
            padding: 28px;
            box-shadow: 0 4px 14px rgba(0,0,0,0.08);
        }

        h1 {
            margin-top: 0;
            color: #111827;
        }

        .subtitle {
            color: #6b7280;
            margin-bottom: 20px;
        }

        label {
            font-weight: bold;
            display: block;
            margin-bottom: 6px;
            color: #374151;
        }

        select,
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            box-sizing: border-box;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .success {
            color: #15803d;
            background: #dcfce7;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 15px;
        }

        .error {
            color: #b91c1c;
            background: #fee2e2;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 15px;
        }

        .btn {
            padding: 11px 16px;
            border-radius: 8px;
            border: none;
            text-decoration: none;
            cursor: pointer;
            display: inline-block;
        }

        .btn-primary {
            background: #2563eb;
            color: white;
        }

        .btn-gray {
            background: #6b7280;
            color: white;
        }

        .actions {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }

        .info-box {
            background: #eff6ff;
            padding: 14px;
            border-radius: 10px;
            margin-bottom: 20px;
            color: #1e3a8a;
        }
    </style>
</head>
<body>

<div class="container">

    <div class="card">

        <h1>Transfer Credit To Partner</h1>

        <p class="subtitle">
            Transfer Seven Digital credit to a partner shop.
        </p>

        <div class="info-box">
            Credit will be added directly to the partner's available balance.
        </div>

        <?php if($error): ?>
            <div class="error">
                <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>

        <?php if($success): ?>
            <div class="success">
                <?php echo htmlspecialchars($success); ?>
            </div>
        <?php endif; ?>

        <form method="POST">

            <div class="form-group">
                <label>Partner Shop</label>

                <select name="partner_id" required>

                    <?php foreach($partners as $partner): ?>

                        <option value="<?php echo $partner['id']; ?>">

                            <?php echo htmlspecialchars($partner['shop_name']); ?>

                            (Balance:
                            <?php echo number_format($partner['credit_balance'],2); ?>)

                        </option>

                    <?php endforeach; ?>

                </select>
            </div>

            <div class="form-group">
                <label>Credit Amount</label>

                <input
                    type="number"
                    step="0.01"
                    min="1"
                    name="amount"
                    required>
            </div>

            <div class="actions">

                <button class="btn btn-primary" type="submit">
                    Transfer Credit
                </button>

                <a class="btn btn-gray" href="dashboard.php">
                    Back to Dashboard
                </a>

            </div>

        </form>

    </div>

</div>

</body>
</html>
