<?php
require_once 'config/database.php';
secureSessionStart();
requireLogin();

$conn = getConnection();
$success = '';
$error = '';

// Get current company details
$company = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM com_details WHERE id = 1"));

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_company'])) {
    $name = sanitize($conn, $_POST['name']);
    $web_name = sanitize($conn, $_POST['web_name']);
    $address = sanitize($conn, $_POST['address']);
    $email = sanitize($conn, $_POST['email']);
    $mobile = sanitize($conn, $_POST['mobile']);
    $copyright = sanitize($conn, $_POST['copyright_text']);
    $insta = sanitize($conn, $_POST['insta_link']);
    $facebook = sanitize($conn, $_POST['facebook_link']);
    $youtube = sanitize($conn, $_POST['youtube_link']);
    
    $stmt = mysqli_prepare($conn, "UPDATE com_details SET 
        name = ?, web_name = ?, address = ?, email = ?, mobile = ?, 
        copyright_text = ?, insta_link = ?, facebook_link = ?, youtube_link = ? 
        WHERE id = 1");
    
    mysqli_stmt_bind_param($stmt, "sssssssss", $name, $web_name, $address, $email, $mobile, $copyright, $insta, $facebook, $youtube);
    
    if (mysqli_stmt_execute($stmt)) {
        $success = "Company details updated successfully!";
        // Refresh data
        $company = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM com_details WHERE id = 1"));
    } else {
        $error = "Error updating company details!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Company Settings - MLM System</title>
    
    <!-- Google Fonts -->
    <link href="https://fonts.gstatic.com" rel="preconnect">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

    <!-- Vendor CSS Files -->
    <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
    <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
    <link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">

    <!-- Template Main CSS File -->
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/header.php'; ?>
    <?php include 'includes/sidebar.php'; ?>

    <main id="main" class="main">
        <div class="pagetitle">
            <h1>Company Settings</h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                    <li class="breadcrumb-item active">Company Settings</li>
                </ol>
            </nav>
        </div>

        <section class="section">
            <div class="row">
                <div class="col-lg-12">
                    <?php if ($success): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <?php echo $success; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($error): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <?php echo $error; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">Company Information</h5>
                            
                            <form method="POST" class="row g-3">
                                <div class="col-md-6">
                                    <label for="name" class="form-label">Company Name</label>
                                    <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($company['name']); ?>" required>
                                </div>
                                
                                <div class="col-md-6">
                                    <label for="web_name" class="form-label">Website Name</label>
                                    <input type="text" class="form-control" id="web_name" name="web_name" value="<?php echo htmlspecialchars($company['web_name']); ?>" required>
                                </div>
                                
                                <div class="col-12">
                                    <label for="address" class="form-label">Address</label>
                                    <textarea class="form-control" id="address" name="address" rows="3" required><?php echo htmlspecialchars($company['address']); ?></textarea>
                                </div>
                                
                                <div class="col-md-6">
                                    <label for="email" class="form-label">Email</label>
                                    <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($company['email']); ?>" required>
                                </div>
                                
                                <div class="col-md-6">
                                    <label for="mobile" class="form-label">Mobile</label>
                                    <input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo htmlspecialchars($company['mobile']); ?>" required pattern="[0-9]{10}">
                                </div>
                                
                                <div class="col-12">
                                    <label for="copyright_text" class="form-label">Copyright Text</label>
                                    <input type="text" class="form-control" id="copyright_text" name="copyright_text" value="<?php echo htmlspecialchars($company['copyright_text']); ?>" required>
                                </div>
                                
                                <div class="col-md-4">
                                    <label for="insta_link" class="form-label">Instagram Link</label>
                                    <input type="url" class="form-control" id="insta_link" name="insta_link" value="<?php echo htmlspecialchars($company['insta_link']); ?>">
                                </div>
                                
                                <div class="col-md-4">
                                    <label for="facebook_link" class="form-label">Facebook Link</label>
                                    <input type="url" class="form-control" id="facebook_link" name="facebook_link" value="<?php echo htmlspecialchars($company['facebook_link']); ?>">
                                </div>
                                
                                <div class="col-md-4">
                                    <label for="youtube_link" class="form-label">YouTube Link</label>
                                    <input type="url" class="form-control" id="youtube_link" name="youtube_link" value="<?php echo htmlspecialchars($company['youtube_link']); ?>">
                                </div>
                                
                                <div class="text-center">
                                    <button type="submit" name="update_company" class="btn btn-primary">Update Company Details</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <?php include 'includes/footer.php'; ?>

    <!-- Vendor JS Files -->
    <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
<?php mysqli_close($conn); ?>