<?php 
include 'config.php'; 
if($_SESSION['user_role'] != 'SuperAdmin') exit();

$id = (int)$_GET['id'];
$pay = $conn->query("SELECT p.*, a.agency_name FROM saas_payments p JOIN agencies a ON p.agency_id = a.id WHERE p.id = $id")->fetch_assoc();

if(!$pay) die("Record not found.");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Payment</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Poppins', sans-serif; background: #f8f9fc; padding: 50px; }
        .card { background: #fff; max-width: 500px; margin: auto; padding: 30px; border-radius: 15px; box-shadow: 0 10px 25px rgba(0,0,0,0.05); }
        input, select { width: 100%; padding: 12px; margin: 10px 0; border: 1px solid #ddd; border-radius: 10px; }
        .btn { background: #4e73df; color: white; padding: 15px; border: none; width: 100%; border-radius: 10px; font-weight: 600; cursor: pointer; }
    </style>
</head>
<body>
    <div class="card">
        <h2>Edit Payment Record</h2>
        <p>Agency: <b><?php echo $pay['agency_name']; ?></b></p>
        <hr style="margin:20px 0; border:0; border-top:1px solid #eee;">
        
        <form action="handle_saas_payment.php" method="POST">
            <input type="hidden" name="action" value="update">
            <input type="hidden" name="payment_id" value="<?php echo $id; ?>">
            <input type="hidden" name="agency_id" value="<?php echo $pay['agency_id']; ?>">
            <input type="hidden" name="old_amount" value="<?php echo $pay['amount_paid']; ?>">
            
            <label>Amount (PKR)</label>
            <input type="number" name="amount" value="<?php echo $pay['amount_paid']; ?>" required>
            
            <label>Payment Method</label>
            <select name="method">
                <option <?php if($pay['payment_method'] == 'Bank Transfer') echo 'selected'; ?>>Bank Transfer</option>
                <option <?php if($pay['payment_method'] == 'JazzCash / EasyPaisa') echo 'selected'; ?>>JazzCash / EasyPaisa</option>
                <option <?php if($pay['payment_method'] == 'Cash') echo 'selected'; ?>>Cash</option>
            </select>

            <label>Notes</label>
            <input type="text" name="notes" value="<?php echo $pay['notes']; ?>" required>
            
            <button type="submit" class="btn">Update & Recalculate Balance</button>
            <a href="agency_ledger.php?id=<?php echo $pay['agency_id']; ?>" style="display:block; text-align:center; margin-top:15px; color:#64748b; text-decoration:none;">Cancel</a>
        </form>
    </div>
</body>
</html>