<?php
require_once '../config.php';
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php");
    exit;
}

$success = '';
$error = '';

// Ambil Tarif Terkini
$tarif = $pdo->query("SELECT * FROM tarif ORDER BY id DESC LIMIT 1")->fetch();
$harga_m3 = $tarif['harga_m3'] ?? 0;
$biaya_abonemen = $tarif['biaya_abonemen'] ?? 0;
$biaya_pasang = $tarif['biaya_pasang_baru'] ?? 0;

// Handle Update Tarif
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $h_m3 = (float)str_replace(',', '.', $_POST['harga_m3']);
    $b_abon = (float)str_replace(',', '.', $_POST['biaya_abonemen']);
    $b_pasang = (float)str_replace(',', '.', $_POST['biaya_pasang_baru']);

    if ($h_m3 < 0 || $b_abon < 0 || $b_pasang < 0) {
        $error = "Tarif tidak boleh bernilai negatif!";
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO tarif (harga_m3, biaya_abonemen, biaya_pasang_baru) VALUES (?, ?, ?)");
            $stmt->execute([$h_m3, $b_abon, $b_pasang]);
            $success = "Tarif berhasil diperbarui! Tagihan berikutnya akan menggunakan tarif ini.";
            
            // Refresh data
            $tarif = $pdo->query("SELECT * FROM tarif ORDER BY id DESC LIMIT 1")->fetch();
            $harga_m3 = $tarif['harga_m3'];
            $biaya_abonemen = $tarif['biaya_abonemen'];
            $biaya_pasang = $tarif['biaya_pasang_baru'];
        } catch (PDOException $e) {
            $error = "Gagal menyimpan tarif: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Manajemen Tarif - SPAM</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body { background:#f4f7fe; font-family:'Segoe UI'; }
.bg-header{
    background: linear-gradient(135deg, #3A7CA5 0%, #2C5F7D 100%);
    color:white;
    padding:18px;
    border-bottom-left-radius:20px;
    border-bottom-right-radius:20px;
}
.card-soft {
    background: white; border: none; border-radius: 20px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.05); padding: 20px; margin-bottom: 15px;
}
.form-control {
    border-radius: 12px; padding: 12px 15px; border: 1px solid #e0e0e0; background: #f8f9fa;
}
.form-control:focus {
    border-color: #3A7CA5; box-shadow: 0 0 0 0.25rem rgba(58, 124, 165, 0.25); background: white;
}
.form-label { font-weight: 700; font-size: 0.85rem; color: #4a5568; }
.btn-sage {
    background: linear-gradient(135deg, #3A7CA5 0%, #2C5F7D 100%);
    color: white; border: none; border-radius: 12px; padding: 12px; font-weight: 700; width: 100%;
}
.tarif-display {
    background: #f0f7ff; border-radius: 12px; padding: 15px; margin-bottom: 10px;
    display: flex; justify-content: space-between; align-items: center;
}
.tarif-label { font-size: 0.9rem; color: #5a6270; }
.tarif-value { font-size: 1.2rem; font-weight: 800; color: #3A7CA5; }
</style>
</head>
<body>

<div class="bg-header shadow-sm">
    <div class="container d-flex justify-content-between align-items-center">
        <a href="dashboard.php" class="text-white"><i class="fa fa-arrow-left"></i></a>
        <h5 class="mb-0 fw-bold">Manajemen Tarif</h5>
        <div style="width: 20px;"></div>
    </div>
</div>

<div class="container pb-5 mt-3">
    <?php if ($success): ?>
        <div class="alert alert-success" style="font-size:0.85rem; border-radius:10px;"><?= $success ?></div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="alert alert-danger" style="font-size:0.85rem; border-radius:10px;"><?= $error ?></div>
    <?php endif; ?>

    <!-- Tampilan Tarif Saat Ini -->
    <div class="card-soft">
        <h6 class="fw-bold mb-3"><i class="fa fa-tag me-2 text-primary"></i>Tarif Berlaku Saat Ini</h6>
        <div class="tarif-display">
            <span class="tarif-label"><i class="fa fa-droplet me-1"></i> Harga per m³</span>
            <span class="tarif-value"><?= format_rupiah($harga_m3) ?></span>
        </div>
        <div class="tarif-display">
            <span class="tarif-label"><i class="fa fa-file-contract me-1"></i> Biaya Abonemen</span>
            <span class="tarif-value"><?= format_rupiah($biaya_abonemen) ?></span>
        </div>
        <div class="tarif-display" style="background: #fff8f0;">
            <span class="tarif-label"><i class="fa fa-plug me-1"></i> Biaya Pasang Baru</span>
            <span class="tarif-value" style="color: #E67E22;"><?= format_rupiah($biaya_pasang) ?></span>
        </div>
    </div>

    <!-- Form Update Tarif -->
    <div class="card-soft">
        <h6 class="fw-bold mb-3"><i class="fa fa-pen-to-square me-2 text-primary"></i>Perbarui Tarif</h6>
        <form method="POST">
            <div class="mb-3">
                <label class="form-label">Harga per m³ (Rupiah)</label>
                <input type="number" name="harga_m3" class="form-control" required value="<?= $harga_m3 ?>">
            </div>
            <div class="mb-3">
                <label class="form-label">Biaya Abonemen Bulanan (Rupiah)</label>
                <input type="number" name="biaya_abonemen" class="form-control" required value="<?= $biaya_abonemen ?>">
            </div>
            <div class="mb-4">
                <label class="form-label">Biaya Pasang Baru (Rupiah)</label>
                <input type="number" name="biaya_pasang_baru" class="form-control" required value="<?= $biaya_pasang ?>">
            </div>
            <button type="submit" class="btn btn-sage">
                <i class="fa fa-save me-2"></i> Simpan Tarif Baru
            </button>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>