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

$id_edit = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$success = false;
$error = '';

// Ambil data pelanggan
$stmt = $pdo->prepare("SELECT * FROM pelanggan WHERE id = ?");
$stmt->execute([$id_edit]);
$p = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$p) {
    header("Location: manajemen_pelanggan.php");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = trim($_POST['nama']);
    $alamat = trim($_POST['alamat']);
    $no_hp = trim($_POST['no_hp']);
    $username = trim($_POST['username']);
    $password = $_POST['password'];

    if (empty($nama) || empty($alamat)) {
        $error = "Nama dan Alamat wajib diisi!";
    } else {
        try {
            if (!empty($password)) {
                $hashed_pass = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE pelanggan SET nama=?, alamat=?, no_hp=?, username=?, password=? WHERE id=?");
                $stmt->execute([$nama, $alamat, $no_hp, $username, $hashed_pass, $id_edit]);
            } else {
                $stmt = $pdo->prepare("UPDATE pelanggan SET nama=?, alamat=?, no_hp=?, username=? WHERE id=?");
                $stmt->execute([$nama, $alamat, $no_hp, $username, $id_edit]);
            }
            $success = true;
            // Refresh data
            $stmt = $pdo->prepare("SELECT * FROM pelanggan WHERE id = ?");
            $stmt->execute([$id_edit]);
            $p = $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            $error = "Gagal memperbarui data: " . $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>Edit Pelanggan - 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;
}
.form-card {
    background: white;
    border-radius: 20px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.05);
    padding: 20px;
    margin-top: -20px;
}
.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%;
}
.qr-box {
    background: #f8f9fa;
    border-radius: 16px;
    padding: 20px;
    text-align: center;
    border: 2px dashed #3A7CA5;
    margin-top: 20px;
}
.qr-box img { max-width: 200px; height: auto; }
@media print {
    body * { visibility: hidden; }
    #printArea, #printArea * { visibility: visible; }
    #printArea { position: absolute; left: 0; top: 0; width: 100%; }
}
</style>
</head>
<body>

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

<div class="container pb-5">
    <div class="form-card">
        <?php if ($success): ?>
            <div class="alert alert-success" style="font-size:0.85rem; border-radius:10px;">
                <i class="fa fa-check-circle me-1"></i> Data pelanggan berhasil diperbarui!
            </div>
        <?php endif; ?>
        <?php if ($error): ?>
            <div class="alert alert-danger" style="font-size:0.85rem; border-radius:10px;"><?= $error ?></div>
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label">ID Meteran (Tidak dapat diubah)</label>
                <input type="text" class="form-control bg-light" value="<?= htmlspecialchars($p['id_meteran']) ?>" readonly>
            </div>
            <div class="mb-3">
                <label class="form-label">Nama Pelanggan</label>
                <input type="text" name="nama" class="form-control" required value="<?= htmlspecialchars($p['nama']) ?>">
            </div>
            <div class="mb-3">
                <label class="form-label">Alamat Lengkap</label>
                <textarea name="alamat" class="form-control" rows="2" required><?= htmlspecialchars($p['alamat']) ?></textarea>
            </div>
            <div class="mb-3">
                <label class="form-label">Nomor HP / WhatsApp</label>
                <input type="text" name="no_hp" class="form-control" value="<?= htmlspecialchars($p['no_hp']) ?>">
            </div>
            
            <hr class="my-4">
            <p class="small text-muted mb-2"><i class="fa fa-info-circle me-1"></i> Akun Login Pelanggan (Kosongkan password jika tidak ingin mengubah)</p>
            
            <div class="mb-3">
                <label class="form-label">Username Pelanggan</label>
                <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($p['username']) ?>">
            </div>
            <div class="mb-4">
                <label class="form-label">Password Baru</label>
                <input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak ingin diganti">
            </div>

            <button type="submit" class="btn btn-sage">
                <i class="fa fa-save me-2"></i> Simpan Perubahan
            </button>
        </form>

        <!-- Area Cetak Ulang Barcode -->
        <div id="printArea">
            <div class="qr-box">
                <h5 class="fw-bold text-sage mb-2" style="color:#3A7CA5;"><?= htmlspecialchars($p['id_meteran']) ?></h5>
                <img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=<?= urlencode($p['id_meteran']) ?>" alt="QR Code">
                <p class="small text-muted mt-2 mb-0"><?= htmlspecialchars($p['nama']) ?></p>
                <p class="small text-muted"><?= htmlspecialchars($p['alamat']) ?></p>
            </div>
        </div>
        <button onclick="window.print()" class="btn btn-outline-secondary w-100 mt-3" style="border-radius:12px;">
            <i class="fa fa-print me-2"></i> Cetak Ulang Barcode
        </button>
    </div>
</div>

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