<?php
$page_title = "ওয়েবসাইট সেটিংস";
require_once 'includes/auth_check.php';
$message = '';

// Handle Settings Update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Update text settings
    $site_name = $_POST['site_name'];
    $conn->query("UPDATE settings SET setting_value = '$site_name' WHERE setting_name = 'site_name'");
    
    $auto_payment_status = isset($_POST['auto_payment_enabled']) ? '1' : '0';
    $conn->query("UPDATE settings SET setting_value = '$auto_payment_status' WHERE setting_name = 'auto_payment_enabled'");

    // Handle file uploads (Logo and Favicon)
    $upload_dir = "../uploads/site/";
    if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); }

    foreach(['site_logo', 'site_favicon'] as $file_key) {
        if (isset($_FILES[$file_key]) && $_FILES[$file_key]['error'] == 0) {
            $new_filename = $upload_dir . basename($_FILES[$file_key]['name']);
            $db_path = "uploads/site/" . basename($_FILES[$file_key]['name']);
            
            // Delete old file
            $old_file_path_res = $conn->query("SELECT setting_value FROM settings WHERE setting_name = '$file_key'")->fetch_assoc();
            if ($old_file_path_res && file_exists('../' . $old_file_path_res['setting_value'])) {
                unlink('../' . $old_file_path_res['setting_value']);
            }

            // Upload new file
            if (move_uploaded_file($_FILES[$file_key]['tmp_name'], $new_filename)) {
                $conn->query("UPDATE settings SET setting_value = '$db_path' WHERE setting_name = '$file_key'");
            }
        }
    }
    $message = '<div class="alert alert-success">সেটিংস সফলভাবে সেভ হয়েছে।</div>';
}

$settings_res = $conn->query("SELECT * FROM settings");
$settings = []; while($row = $settings_res->fetch_assoc()){ $settings[$row['setting_name']] = $row['setting_value']; }
include 'includes/header.php';
?>
<div class="container-fluid">
    <h2 class="mb-4">ওয়েবসাইট সেটিংস</h2>
    <?php echo $message; ?>
    <div class="card"><div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="mb-3">
                <label class="form-label">ওয়েবসাইটের নাম</label>
                <input type="text" name="site_name" class="form-control" value="<?php echo htmlspecialchars($settings['site_name']); ?>">
            </div>
            <div class="mb-3">
                <label class="form-label">সাইট লোগো (.png, .jpg)</label>
                <input type="file" name="site_logo" class="form-control">
                <small>বর্তমান লোগো: <img src="../<?php echo htmlspecialchars($settings['site_logo']); ?>" height="30" class="mt-2"></small>
            </div>
            <div class="mb-3">
                <label class="form-label">ফেভিকন (.ico, .png)</label>
                <input type="file" name="site_favicon" class="form-control">
                <small>বর্তমান ফেভিকন: <img src="../<?php echo htmlspecialchars($settings['site_favicon']); ?>" height="16" class="mt-2"></small>
            </div>
            <hr>
            <div class="form-check form-switch mb-3">
                <input class="form-check-input" type="checkbox" role="switch" id="autoPaymentSwitch" name="auto_payment_enabled" value="1" <?php if ($settings['auto_payment_enabled'] == '1') echo 'checked'; ?>>
                <label class="form-check-label" for="autoPaymentSwitch"><h4>স্বয়ংক্রিয় পেমেন্ট গেটওয়ে চালু করুন</h4></label>
            </div>
            <hr>
            <button type="submit" class="btn btn-primary">সেভ সেটিংস</button>
        </form>
    </div></div>
</div>
<?php include 'includes/footer.php'; $conn->close(); ?>