<?php
/*
 * Billing Payment - Online payment page for customer portal
 */

session_start();

date_default_timezone_set('Asia/Jakarta');

require_once(__DIR__ . '/../include/db_config.php');
require_once(__DIR__ . '/../lib/BillingService.class.php');
require_once(__DIR__ . '/../lib/PublicPayment.class.php');

if (!isset($_SESSION['billing_portal_customer_id']) || (int)$_SESSION['billing_portal_customer_id'] <= 0) {
    header('Location: billing_login.php');
    exit;
}

// Check if we have an invoice to pay
if (!isset($_SESSION['billing_payment_invoice'])) {
    header('Location: billing_portal.php');
    exit;
}

$invoice = $_SESSION['billing_payment_invoice'];
$activeCustomerId = (int)$_SESSION['billing_portal_customer_id'];

try {
    $billingService = new BillingService();
    $customer = $billingService->getCustomerById($activeCustomerId);
    
    if (!$customer || (int)$customer['id'] !== (int)$invoice['customer_id']) {
        unset($_SESSION['billing_payment_invoice']);
        $_SESSION['billing_portal_flash_error'] = 'Data pelanggan tidak valid.';
        header('Location: billing_portal.php');
        exit;
    }
    
    $profile = $billingService->getProfileById((int)$customer['profile_id']);
    
    // Get active payment gateway
    $payment = new PublicPayment();
    $gateway = $payment->getActiveGateway();
    
    $methods = [];
    if ($gateway) {
        $methods = $payment->getPaymentMethods((float)$invoice['amount']);
    }

    $manualMethods = [
        [
            'method_code' => 'cash',
            'method_name' => 'Tunai',
            'method_type' => 'manual',
            'admin_fee_type' => 'fixed',
            'admin_fee_value' => 0,
        ],
        [
            'method_code' => 'bank_transfer',
            'method_name' => 'Transfer/Manual',
            'method_type' => 'manual',
            'admin_fee_type' => 'fixed',
            'admin_fee_value' => 0,
        ],
        [
            'method_code' => 'manual_ewallet',
            'method_name' => 'E-Wallet/Manual',
            'method_type' => 'manual',
            'admin_fee_type' => 'fixed',
            'admin_fee_value' => 0,
        ],
    ];

    $methods = array_merge($manualMethods, $methods);
    
    if (empty($methods)) {
        throw new Exception('Tidak ada metode pembayaran yang tersedia untuk jumlah tagihan ini.');
    }
    
    // Group methods by type
    $grouped_methods = [];
    foreach ($methods as $method) {
        $type = $method['method_type'];
        if (!isset($grouped_methods[$type])) {
            $grouped_methods[$type] = [];
        }
        $grouped_methods[$type][] = $method;
    }
    
    $type_labels = [
        'manual' => 'Manual',
        'qris' => 'QRIS',
        'va' => 'Virtual Account',
        'ewallet' => 'E-Wallet',
        'retail' => 'Retail Store'
    ];
    
    $type_icons = [
        'manual' => 'fa-handshake-o',
        'qris' => 'fa-qrcode',
        'va' => 'fa-bank',
        'ewallet' => 'fa-mobile',
        'retail' => 'fa-shopping-cart'
    ];
    
} catch (Throwable $e) {
    $_SESSION['billing_portal_flash_error'] = 'Terjadi kesalahan: ' . htmlspecialchars($e->getMessage());
    header('Location: billing_portal.php');
    exit;
}

$theme = 'default';
$themecolor = '#3a4149';
if (file_exists(__DIR__ . '/../include/theme.php')) {
    include(__DIR__ . '/../include/theme.php');
}

// Handle payment method selection
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['payment_method'])) {
    $payment_method = $_POST['payment_method'];
    
    try {
        // Validate payment method
        $selected_method = null;
        foreach ($methods as $method) {
            if ($method['method_code'] === $payment_method) {
                $selected_method = $method;
                break;
            }
        }
        
        if (!$selected_method) {
            throw new Exception('Metode pembayaran tidak valid.');
        }
        
        // Calculate admin fee
        $admin_fee = 0;
        if ($selected_method['admin_fee_type'] == 'percentage' || $selected_method['admin_fee_type'] == 'percent') {
            $admin_fee = ceil(((float)$invoice['amount'] * (float)$selected_method['admin_fee_value']) / 100);
        } else {
            $admin_fee = (float)$selected_method['admin_fee_value'];
        }
        
        $total_amount = (float)$invoice['amount'] + $admin_fee;

        if (in_array($selected_method['method_code'], ['cash', 'bank_transfer', 'manual_ewallet'], true)) {
            $manualReference = 'MANUAL-' . $invoice['id'] . '-' . date('YmdHis');
            $billingService->updateInvoice((int)$invoice['id'], [
                'payment_channel' => $selected_method['method_code'],
                'reference_number' => $manualReference,
            ]);

            $_SESSION['billing_payment_info'] = [
                'invoice_id' => $invoice['id'],
                'method_code' => $selected_method['method_code'],
                'method_name' => $selected_method['method_name'],
                'payment_reference' => $manualReference,
                'payment_url' => '',
                'qr_url' => '',
                'virtual_account' => '',
                'amount' => (float)$invoice['amount'],
                'admin_fee' => 0,
                'total_amount' => (float)$invoice['amount'],
                'expired_at' => date('Y-m-d H:i:s', strtotime('+2 days')),
            ];

            header('Location: billing_payment_instructions.php');
            exit;
        }

        if (!$gateway) {
            throw new Exception('Payment gateway tidak aktif. Silakan pilih metode manual.');
        }

        $payment_data = [
            'payment_method' => $payment_method,
            'amount' => $total_amount,
            'customer_name' => $customer['name'],
            'customer_phone' => $customer['phone'] ?? '',
            'customer_email' => $customer['email'] ?? '',
            'product_name' => 'Pembayaran Tagihan ' . ($profile['profile_name'] ?? 'Internet'),
            'callback_url' => (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/api/billing_payment_callback.php',
            'return_url' => (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/public/billing_payment_status.php'
        ];

        $result = $payment->createPayment($payment_data);

        if ($result['success']) {
            $_SESSION['billing_payment_info'] = [
                'invoice_id' => $invoice['id'],
                'method_code' => $selected_method['method_code'],
                'payment_reference' => $result['payment_reference'],
                'payment_url' => $result['payment_url'],
                'qr_url' => $result['qr_url'],
                'virtual_account' => $result['virtual_account'],
                'amount' => (float)$invoice['amount'],
                'admin_fee' => $admin_fee,
                'total_amount' => $total_amount,
                'method_name' => $selected_method['method_name'],
                'expired_at' => $result['expired_at']
            ];

            if (!empty($result['payment_url'])) {
                header('Location: ' . $result['payment_url']);
                exit;
            }

            header('Location: billing_payment_instructions.php');
            exit;
        }

        throw new Exception($result['message'] ?? 'Gagal membuat pembayaran.');
        
    } catch (Exception $e) {
        $_SESSION['billing_portal_flash_error'] = 'Pembayaran gagal: ' . htmlspecialchars($e->getMessage());
        header('Location: billing_payment.php');
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pembayaran Tagihan - <?= htmlspecialchars($customer['name']); ?></title>
    <meta name="theme-color" content="<?= $themecolor; ?>" />
    <link rel="stylesheet" type="text/css" href="../css/font-awesome/css/font-awesome.min.css" />
    <?php $themeAsset = ($theme === 'darkgreen') ? 'dark' : $theme; ?>
    <link rel="stylesheet" href="../css/mikrobil-ui.<?= $themeAsset; ?>.min.css">
    <link rel="stylesheet" href="../css/theme-darkgreen.css">
    <link rel="icon" href="../img/favicon.svg" type="image/svg+xml" sizes="any" />
    <link rel="icon" href="../img/favicon.png" type="image/png" sizes="32x32" />
    <link rel="apple-touch-icon" href="../img/favicon.png" />
    <style>
        :root {
            --dg-text: #f8fafc;
            --dg-muted: #94a3b8;
            --dg-panel: rgba(15, 23, 42, 0.78);
            --dg-panel-strong: rgba(15, 23, 42, 0.92);
            --dg-border: rgba(148, 163, 184, 0.18);
            --dg-border-strong: rgba(148, 163, 184, 0.28);
            --dg-accent: #93c5fd;
            --dg-accent-2: #1e293b;
            --dg-accent-3: #334155;
            --dg-col-bg: rgba(51, 65, 85, 0.35);
            --primary-color: #334155;
            --primary-dark: rgba(51, 65, 85, 0.85);
            --info-color: #334155;
            --bg-color: radial-gradient(1100px 650px at 18% 8%, rgba(51, 65, 85, 0.18), transparent 60%), radial-gradient(900px 500px at 88% 12%, rgba(30, 41, 59, 0.14), transparent 60%), linear-gradient(135deg, #050816, #0f172a 35%, #111827);
        }
        body { 
            background: var(--bg-color); 
            font-family: 'Source Sans Pro', Arial, sans-serif; 
            color: var(--dg-text);
        }
        .wrapper { 
            max-width: 940px; 
            margin: 0 auto; 
            padding: 20px; 
        }
        .card { 
            background: var(--dg-panel); 
            border-radius: 14px; 
            box-shadow: var(--dg-shadow); 
            border: 1px solid var(--dg-border);
            margin-bottom: 20px; 
        }
        .card-header { 
            padding: 16px 20px; 
            border-bottom: 1px solid var(--dg-border); 
            display:flex; 
            flex-wrap:wrap; 
            align-items:center; 
            justify-content:space-between; 
            gap:10px; 
        }
        .card-header .header-info { 
            flex:1 1 auto; 
        }
        .logout-link { 
            background: rgba(15, 23, 42, 0.35); 
            border-radius:12px; 
            padding:8px 14px; 
            font-size:13px; 
            color: var(--dg-text); 
            text-decoration:none; 
            border:1px solid var(--dg-border); 
            transition: all 0.2s; 
            font-weight:600; 
            display:inline-flex; 
            align-items:center; 
            gap:6px; 
        }
        .logout-link:hover { 
            background: rgba(51, 65, 85, 0.18); 
            color:#fff; 
        }
        .card-body { 
            padding: 20px; 
        }
        .summary-grid { 
            display: grid; 
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); 
            gap: 15px; 
            margin-bottom: 20px; 
        }
        .box { 
            border-radius: 4px; 
            padding: 18px; 
            color: #fff; 
            min-height: 110px; 
        }
        .box h2 { 
            margin: 0 0 6px; 
            font-size: 26px; 
        }
        .badge-status { 
            display: inline-block; 
            padding: 4px 10px; 
            border-radius: 999px; 
            font-size: 12px; 
        }
        .badge-status.paid { 
            background: #d1fae5; 
            color: #065f46; 
        }
        .badge-status.unpaid { 
            background: #fee2e2; 
            color: #991b1b; 
        }
        .badge-status.overdue { 
            background: #fde68a; 
            color: #92400e; 
        }
        .invoice-table { 
            width: 100%; 
            border-collapse: collapse; 
        }
        .invoice-table th,
        .invoice-table td { 
            border: 1px solid var(--dg-border); 
            padding: 10px; 
            font-size: 13px; 
        }
        .invoice-table th { 
            background: rgba(15, 23, 42, 0.35); 
            font-weight: 600; 
        }
        .form-control[disabled] { 
            background: rgba(15, 23, 42, 0.25); 
        }
        .btn { 
            cursor: pointer; 
        }
        .alert {
            padding: 12px 16px;
            border-radius: 10px;
            margin-bottom: 18px;
            border: 1px solid var(--dg-border);
            background: var(--dg-panel);
            line-height: 1.55;
            position: relative;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            max-width: min(100%, 560px);
            box-sizing: border-box;
            flex-wrap: wrap;
        }
        .alert::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 4px;
            border-radius: 10px 0 0 10px;
        }
        .alert-info {
            color: var(--dg-text);
        }
        .alert-info::before {
            background: var(--info-color);
        }
        .alert-warning {
            color: #92400e;
        }
        .alert-warning::before {
            background: #d97706;
        }
        .alert-success {
            color: #166534;
        }
        .alert-success::before {
            background: #22c55e;
        }
        .alert-error {
            color: #991b1b;
        }
        .alert-error::before {
            background: #dc2626;
        }
        .payment-section {
            background: var(--dg-panel);
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: var(--dg-shadow);
            border: 1px solid var(--dg-border);
        }
        
        .payment-section h4 {
            color: var(--dg-accent);
            margin-bottom: 15px;
            font-size: 1.2rem;
            font-weight: 600;
            display: flex;
            align-items: center;
        }
        
        .payment-section h4 i {
            margin-right: 8px;
            width: 20px;
        }
        
        .payment-methods-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 15px;
        }
        
        @media (max-width: 768px) {
            .payment-methods-grid {
                grid-template-columns: repeat(2, 1fr);
                gap: 10px;
            }
            
            .wrapper {
                padding: 0 10px;
            }
            
            .payment-section {
                padding: 15px;
                margin-bottom: 15px;
            }
        }
        
        @media (max-width: 480px) {
            .payment-methods-grid {
                grid-template-columns: 1fr;
            }
        }
        
        .payment-method {
            border: 1px solid var(--dg-border-strong);
            border-radius: 8px;
            padding: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: rgba(15, 23, 42, 0.35);
            color: var(--dg-text);
            min-height: 60px;
        }
        
        .payment-method:hover {
            border-color: var(--primary-color);
            background: rgba(51, 65, 85, 0.18);
            transform: translateY(-1px);
            box-shadow: 0 10px 24px rgba(0, 0, 0, 0.25);
        }
        
        .payment-method.selected {
            border-color: var(--primary-color);
            background: rgba(51, 65, 85, 0.26);
            box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
        }
        
        .payment-method input[type="radio"] {
            margin-right: 10px;
            transform: scale(1.2);
        }
        
        .payment-method .method-info {
            display: flex;
            align-items: center;
            flex: 1;
            gap: 12px;
        }
        
        .payment-method .method-icon {
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(51, 65, 85, 0.18);
            border-radius: 8px;
            flex-shrink: 0;
        }
        
        .payment-method .method-details {
            flex: 1;
        }
        
        .payment-method input[type="radio"] {
            margin: 0;
            transform: scale(1.3);
            accent-color: var(--primary-color);
        }
        
        .btn-pay {
            width: 100%;
            padding: 15px;
            font-size: 1.1rem;
            font-weight: 600;
            background: linear-gradient(135deg, var(--dg-accent-2) 0%, var(--dg-accent-3) 100%);
            color: white;
            border: none;
            border-radius: 10px;
            margin-top: 20px;
            transition: all 0.3s ease;
        }
        
        .btn-pay:hover:not(:disabled) {
            background: linear-gradient(135deg, #334155 0%, #475569 100%);
            transform: translateY(-2px);
            box-shadow: 0 14px 30px rgba(0, 0, 0, 0.35);
        }
        
        .btn-pay:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            background: #ccc;
        }
        
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: var(--dg-accent);
            text-decoration: none;
        }
        
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="wrapper">
    <a href="billing_portal.php" class="back-link">
        <i class="fa fa-arrow-left"></i> Kembali ke Portal
    </a>
    
    <div class="card">
        <div class="card-header">
            <div class="header-info">
                <h2><i class="fa fa-credit-card"></i> Pembayaran Tagihan</h2>
                <p style="margin:0; color:var(--dg-muted);">Pelanggan: <?= htmlspecialchars($customer['name']); ?></p>
                <p style="margin:0; color:var(--dg-muted); font-size:13px;">Nomor layanan: <?= htmlspecialchars($customer['service_number']); ?></p>
            </div>
        </div>
        <div class="card-body">
            <?php if (isset($_SESSION['billing_portal_flash_error'])): ?>
                <div class="alert alert-error">
                    <strong>Error:</strong> <?= htmlspecialchars($_SESSION['billing_portal_flash_error']); ?>
                </div>
                <?php unset($_SESSION['billing_portal_flash_error']); ?>
            <?php endif; ?>
            
            <div class="summary-grid">
                <div class="box" style="background:linear-gradient(135deg, #1e293b 0%, #334155 100%);">
                    <h2><?= htmlspecialchars($profile['profile_name'] ?? 'Tidak diketahui'); ?></h2>
                    <p style="margin:0;">Harga: Rp <?= number_format($profile['price_monthly'] ?? 0, 0, ',', '.'); ?>/bulan</p>
                </div>
                <div class="box" style="background:#00a65a;">
                    <h2>Rp <?= number_format($invoice['amount'], 0, ',', '.'); ?></h2>
                    <p style="margin:0;">Jumlah Tagihan</p>
                </div>
                <div class="box" style="background:#f39c12;">
                    <h2><?= htmlspecialchars(date('d M Y', strtotime($invoice['due_date']))); ?></h2>
                    <p style="margin:0;">Jatuh Tempo</p>
                </div>
                <div class="box" style="background:#dd4b39;">
                    <h2><?= htmlspecialchars($invoice['period']); ?></h2>
                    <p style="margin:0;">Periode Tagihan</p>
                </div>
            </div>
            
            <div class="alert alert-info">
                <strong>Informasi:</strong> Pilih metode pembayaran yang tersedia di bawah ini untuk menyelesaikan pembayaran tagihan Anda.
            </div>
        </div>
    </div>
    
    <!-- Payment Methods -->
    <form method="POST" action="" id="paymentForm">
        <?php foreach ($grouped_methods as $type => $type_methods): ?>
        <div class="payment-section">
            <h4>
                <i class="fa <?= $type_icons[$type] ?? 'fa-credit-card'; ?>"></i>
                <?= $type_labels[$type] ?? ucfirst($type); ?>
            </h4>
            
            <div class="payment-methods-grid">
                <?php foreach ($type_methods as $method): ?>
                <label class="payment-method">
                    <div class="method-info">
                        <input type="radio" name="payment_method" value="<?= htmlspecialchars($method['method_code']); ?>" required>
                        <div class="method-icon">
                            <i class="fa <?= $type_icons[$type] ?? 'fa-credit-card'; ?>" style="font-size: 24px; color: var(--dg-accent);"></i>
                        </div>
                        <div class="method-details">
                            <div class="method-name" style="font-weight: 600; color: var(--dg-text); margin-bottom: 4px;">
                                <?= htmlspecialchars($method['method_name']); ?>
                            </div>
                            <div class="method-fee" style="font-size: 12px; color: var(--dg-muted);">
                                <?php if ($method['admin_fee_value'] > 0): ?>
                                    <?php if ($method['admin_fee_type'] == 'percentage'): ?>
                                        +<?= $method['admin_fee_value']; ?>%
                                    <?php else: ?>
                                        +Rp <?= number_format($method['admin_fee_value'], 0, ',', '.'); ?>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span style="color: #28a745;">Gratis</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </label>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endforeach; ?>
        
        <button type="submit" class="btn-pay" id="btnPay" disabled>
            <i class="fa fa-lock"></i> Lanjutkan Pembayaran
        </button>
    </form>
</div>

<script>
// Handle payment method selection
document.querySelectorAll('.payment-method').forEach(method => {
    method.addEventListener('click', function() {
        // Remove selected class from all methods
        document.querySelectorAll('.payment-method').forEach(m => m.classList.remove('selected'));
        
        // Add selected class to clicked method
        this.classList.add('selected');
        
        // Check the radio button
        this.querySelector('input[type="radio"]').checked = true;
        
        // Enable pay button
        document.getElementById('btnPay').disabled = false;
    });
});

// Form submission
document.getElementById('paymentForm').addEventListener('submit', function() {
    const btn = document.getElementById('btnPay');
    btn.disabled = true;
    btn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Memproses...';
});
</script>

</body>
</html>
