<?php
include('header.php');

// Only admin can manage fees
if ($_SESSION['usertype'] != 'admin') {
    echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>';
    echo '<script>Swal.fire({ title: "Access Denied!", text: "Only admin can manage fees.", icon: "error" }).then(() => { window.location.href = "index"; });</script>';
    exit;
}

$msg = '';
$msgtype = '';

// Update fees
if (isset($_POST['updatefees'])) {
    $pan_fee = mysqli_real_escape_string($conn, trim($_POST['pan_fee']));
    $aadhar_fee = mysqli_real_escape_string($conn, trim($_POST['aadhar_fee']));
    $delhi_birth_fee = mysqli_real_escape_string($conn, trim($_POST['delhi_birth_fee']));

    if (!is_numeric($pan_fee) || !is_numeric($aadhar_fee) || !is_numeric($delhi_birth_fee) || $pan_fee < 0 || $aadhar_fee < 0 || $delhi_birth_fee < 0) {
        $msg = 'Please enter valid fee amounts';
        $msgtype = 'error';
    } else {
        date_default_timezone_set('Asia/Kolkata');
        $now = date("Y-m-d H:i:s");

        $q1 = mysqli_query($conn, "INSERT INTO fee_settings (service_name, fee_amount, updated_at) VALUES ('pan_print', '$pan_fee', '$now') ON DUPLICATE KEY UPDATE fee_amount='$pan_fee', updated_at='$now'");
        $q2 = mysqli_query($conn, "INSERT INTO fee_settings (service_name, fee_amount, updated_at) VALUES ('aadhar_print', '$aadhar_fee', '$now') ON DUPLICATE KEY UPDATE fee_amount='$aadhar_fee', updated_at='$now'");
        $q3 = mysqli_query($conn, "INSERT INTO fee_settings (service_name, fee_amount, updated_at) VALUES ('delhi_birth', '$delhi_birth_fee', '$now') ON DUPLICATE KEY UPDATE fee_amount='$delhi_birth_fee', updated_at='$now'");

        if ($q1 && $q2 && $q3) {
            $msg = 'Fees updated successfully!';
            $msgtype = 'success';
        } else {
            $msg = 'Failed to update fees. Please try again.';
            $msgtype = 'error';
        }
    }
}

// Fetch current fees
$pan_fee_val = 10;
$aadhar_fee_val = 20;
$delhi_birth_fee_val = 30;

$res = mysqli_query($conn, "SELECT service_name, fee_amount FROM fee_settings WHERE service_name IN ('pan_print','aadhar_print','delhi_birth')");
if ($res) {
    while ($row = mysqli_fetch_assoc($res)) {
        if ($row['service_name'] == 'pan_print') $pan_fee_val = $row['fee_amount'];
        if ($row['service_name'] == 'aadhar_print') $aadhar_fee_val = $row['fee_amount'];
        if ($row['service_name'] == 'delhi_birth') $delhi_birth_fee_val = $row['fee_amount'];
    }
}
?>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<?php if (!empty($msg)) { ?>
<script>
    Swal.fire({ title: "<?php echo $msgtype == 'success' ? 'Success!' : 'Error!'; ?>", text: "<?php echo $msg; ?>", icon: "<?php echo $msgtype; ?>" });
</script>
<?php } ?>

<style>
.fee-card {
    max-width: 700px;
    margin: 25px auto;
    background: linear-gradient(135deg, #ffffff 0%, #f8f9ff 100%);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    overflow: hidden;
}
.fee-header {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    color: #fff;
    padding: 20px 30px;
    display: flex;
    align-items: center;
    gap: 15px;
}
.fee-header i { font-size: 28px; }
.fee-header h4 { margin: 0; font-weight: 600; letter-spacing: 0.5px; }
.fee-body { padding: 30px; }
.fee-item {
    background: #fff;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    transition: all 0.3s ease;
}
.fee-item:hover { border-color: #2a5298; box-shadow: 0 4px 15px rgba(42,82,152,0.1); }
.fee-item-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 15px;
}
.fee-icon {
    width: 50px;
    height: 50px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    color: #fff;
}
.fee-icon.pan { background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); }
.fee-icon.aadhar { background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%); }
.fee-icon.delhi { background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%); }
.fee-item-header h5 { margin: 0; font-weight: 700; color: #344767; }
.fee-item-header small { color: #94a3b8; }
.fee-input {
    border: 2px solid #e2e8f0;
    border-radius: 10px;
    padding: 12px 16px;
    font-size: 18px;
    font-weight: 700;
    width: 100%;
    transition: all 0.3s ease;
    background: #f8fafc;
}
.fee-input:focus { border-color: #2a5298; box-shadow: 0 0 0 3px rgba(42,82,152,0.15); outline: none; background: #fff; }
.btn-save {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    color: #fff;
    border: none;
    padding: 12px 40px;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 100%;
}
.btn-save:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(30,60,114,0.4); color: #fff; }
.rupee-prefix {
    position: relative;
}
.rupee-prefix span {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 18px;
    font-weight: 700;
    color: #94a3b8;
}
.rupee-prefix .fee-input { padding-left: 35px; }
</style>

<div class="fee-card">
    <div class="fee-header">
        <i class="fas fa-cogs"></i>
        <div>
            <h4>Fee Management</h4>
            <small style="opacity:0.8;">Update service charges for Pan, Aadhar, and Delhi Birth</small>
        </div>
    </div>
    <div class="fee-body">
        <form method="post" action="">
            <!-- Pan Print Fee -->
            <div class="fee-item">
                <div class="fee-item-header">
                    <div class="fee-icon pan"><i class="fas fa-id-card"></i></div>
                    <div>
                        <h5>Pan Print Fee</h5>
                        <small>Set the charge for Pan Card service</small>
                    </div>
                </div>
                <div class="rupee-prefix">
                    <span>&#8377;</span>
                    <input type="number" name="pan_fee" class="fee-input" value="<?php echo $pan_fee_val; ?>" min="0" step="1" required>
                </div>
            </div>

            <!-- Aadhar Print Fee -->
            <div class="fee-item">
                <div class="fee-item-header">
                    <div class="fee-icon aadhar"><i class="fas fa-address-card"></i></div>
                    <div>
                        <h5>Aadhar Print Fee</h5>
                        <small>Set the charge for Aadhar Card service</small>
                    </div>
                </div>
                <div class="rupee-prefix">
                    <span>&#8377;</span>
                    <input type="number" name="aadhar_fee" class="fee-input" value="<?php echo $aadhar_fee_val; ?>" min="0" step="1" required>
                </div>
            </div>

            <!-- Delhi Birth Fee -->
            <div class="fee-item">
                <div class="fee-item-header">
                    <div class="fee-icon delhi"><i class="fas fa-baby"></i></div>
                    <div>
                        <h5>Delhi Birth Fee</h5>
                        <small>Set the charge for Delhi Birth application</small>
                    </div>
                </div>
                <div class="rupee-prefix">
                    <span>&#8377;</span>
                    <input type="number" name="delhi_birth_fee" class="fee-input" value="<?php echo $delhi_birth_fee_val; ?>" min="0" step="1" required>
                </div>
            </div>

            <button type="submit" name="updatefees" class="btn-save">
                <i class="fas fa-save"></i> Update Fees
            </button>
        </form>
    </div>
</div>

<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script src="dist/js/adminlte.js"></script>
</body>
</html>
