<?php
// Session aur structural components ke liye sidebar ko top par include kiya hai
$sidebar_path = "/home/tameeralmadina/public_html/TAM/sidebar";
if (file_exists($sidebar_path)) {
    include $sidebar_path;
} elseif (file_exists($sidebar_path . ".php")) {
    include $sidebar_path . ".php";
}

include "db.php";

if(isset($_POST['submit'])){
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $contact = mysqli_real_escape_string($conn, $_POST['contact']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $business_name = mysqli_real_escape_string($conn, $_POST['business_name']);
    $opening_balance = mysqli_real_escape_string($conn, $_POST['opening_balance']);

    $sql = "INSERT INTO customers (name, contact, email, address, business_name, opening_balance)
            VALUES ('$name', '$contact', '$email', '$address', '$business_name', '$opening_balance')";

    if($conn->query($sql)){
        $success = "Customer added successfully!";
    } else {
        $error = "Error: " . $conn->error;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Customer - Tameer Al Madina</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: #F4F6F9;
    font-family: 'Poppins', sans-serif;
    min-height: 100vh;
}

.main-layout {
    display: flex;
    width: 100%;
    min-height: 100vh;
    position: relative;
}

aside, .sidebar, [class*="sidebar"] {
    width: 260px;
    min-width: 260px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transition: all 0.3s ease;
}

.content-wrapper {
    flex: 1;
    margin-left: 260px;
    display: flex;
    flex-direction: column;
    background: #F4F6F9;
    min-width: 0;
    width: calc(100% - 260px);
    transition: all 0.3s ease;
}

.top-navbar {
    height: 60px;
    background: #FFFFFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30px;
    border-bottom: 1px solid #EAEAEA;
}

.navbar-left-flex {
    display: flex;
    align-items: center;
    gap: 15px;
}

.mobile-menu-toggle {
    display: none;
    font-size: 20px;
    color: #0A192F;
    cursor: pointer;
    background: #F4F6F9;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    border: 1px solid #E2E8F0;
}

.search-container {
    position: relative;
    width: 300px;
}

.search-container input {
    width: 100%;
    padding: 8px 15px 8px 40px;
    background: #F8F9FA;
    border: 1px solid #E2E8F0;
    border-radius: 8px;
    font-size: 13px;
    outline: none;
}

.search-container i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #A0AEC0;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 20px;
}

.profile-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Updated Vector User Profile Icon Styling */
.profile-info i.profile-icon {
    font-size: 26px;
    color: #4A5568;
    background: #F4F6F9;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #E2E8F0;
}

.profile-text h4 {
    font-size: 13px;
    color: #1A202C;
    font-weight: 600;
}

.profile-text p {
    font-size: 11px;
    color: #718096;
}

.page-container {
    padding: 20px 30px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.form-card {
    background: #FFFFFF;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    width: 100%;
    display: flex;
    flex-direction: column;
    flex: 1;
}

form {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.form-banner {
    background: linear-gradient(135deg, #0A192F 0%, #172A45 100%);
    padding: 15px 30px;
    color: #FFFFFF;
    border-bottom: 4px solid #FF6B00;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.banner-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.banner-icon {
    background: rgba(255, 255, 255, 0.1);
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-size: 16px;
}

.banner-text h2 {
    font-size: 18px;
    font-weight: 600;
}

.banner-text p {
    font-size: 11px;
    color: #A0AEC0;
}

.banner-right-text {
    font-size: 18px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.15);
}

.section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 18px 30px 10px 30px;
    font-size: 14px;
    font-weight: 600;
    color: #0A192F;
}

.section-header i {
    color: #FF6B00;
    font-size: 16px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr); 
    gap: 15px 25px;
    padding: 0 30px;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    font-size: 12px;
    font-weight: 500;
    color: #2D3748;
    margin-bottom: 6px;
}

.required {
    color: #E53E3E;
    margin-left: 3px;
}

.input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.input-wrapper i {
    position: absolute;
    left: 15px;
    color: #A0AEC0;
    font-size: 13px;
}

.input-wrapper input {
    width: 100%;
    height: 38px;
    border: 1px solid #E2E8F0;
    border-radius: 6px;
    padding: 5px 15px 5px 40px;
    font-size: 13px;
    color: #4A5568;
}

.input-wrapper textarea {
    width: 100%;
    height: 38px; 
    border: 1px solid #E2E8F0;
    border-radius: 6px;
    padding: 8px 15px 8px 40px;
    font-size: 13px;
    color: #4A5568;
    resize: none;
}

.input-wrapper input:focus,
.input-wrapper textarea:focus {
    border-color: #FF6B00;
    box-shadow: 0 0 0 2px rgba(255, 107, 0, 0.1);
    outline: none;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 20px 30px 25px 30px;
    margin-top: auto; 
    border-top: 1px solid #F0F2F5;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 22px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
}

.btn-cancel { background: #EDF2F7; color: #4A5568; }
.btn-submit { background: #FF6B00; color: #FFFFFF; }

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    z-index: 998;
}

.alert-box {
    margin: 10px 30px 0 30px;
    padding: 8px 15px;
    border-radius: 6px;
    font-size: 12px;
}
.alert-success { background: #C6F6D5; color: #22543D; border-left: 4px solid #38A169; }
.alert-error { background: #FED7D7; color: #742A2A; border-left: 4px solid #E53E3E; }

@media (min-width: 992px) {
    body { overflow: hidden; }
    .page-container { height: calc(100vh - 60px); }
}

@media (max-width: 1024px) and (min-width: 769px) {
    aside, .sidebar, [class*="sidebar"] {
        width: 240px;
        min-width: 240px;
    }
    .content-wrapper {
        margin-left: 240px;
        width: calc(100% - 240px);
    }
    .form-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
    body { overflow-y: auto !important; }
    
    .mobile-menu-toggle {
        display: inline-flex;
    }

    aside, .sidebar, [class*="sidebar"] {
        left: -260px !important;
    }

    aside.active, .sidebar.active, [class*="sidebar"].active {
        left: 0 !important;
    }

    .content-wrapper {
        margin-left: 0 !important;
        width: 100% !important;
    }

    .top-navbar { padding: 0 15px; }
    .search-container { width: 140px; }
    .page-container { padding: 10px; }
    .form-banner { padding: 12px 15px; }
    .banner-right-text { display: none; }
    .section-header { padding: 15px 15px 8px 15px; }

    .form-grid {
        grid-template-columns: 1fr !important;
        padding: 0 15px;
        gap: 12px;
    }
    
    .form-actions {
        padding: 15px;
        justify-content: space-between;
    }
    .btn {
        flex: 1;
        justify-content: center;
    }
}
</style>
</head>
<body>

<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleMobileSidebar()"></div>

<div class="main-layout">
    
    <?php 
    if (file_exists($sidebar_path)) {
        include $sidebar_path;
    } elseif (file_exists($sidebar_path . ".php")) {
        include $sidebar_path . ".php";
    }
    ?>

    <div class="content-wrapper">
        
        <!-- Top Navbar -->
        <div class="top-navbar">
            <div class="navbar-left-flex">
                <div class="mobile-menu-toggle" onclick="toggleMobileSidebar()">
                    <i class="fa-solid fa-bars"></i>
                </div>
                <div class="search-container">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <input type="text" placeholder="Search here...">
                </div>
            </div>
            
            <div class="user-profile">
                <div class="profile-info">
                    <!-- Clean Vector Icon replaces the placeholder image source -->
                    <i class="fa-solid fa-circle-user profile-icon"></i>
                    <div class="profile-text">
                        <h4>Rubab Admin</h4>
                        <p>Administrator</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Form Panel Wrapper -->
        <div class="page-container">
            <div class="form-card">
                
                <div class="form-banner">
                    <div class="banner-left">
                        <div class="banner-icon">
                            <i class="fa-regular fa-user"></i>
                        </div>
                        <div class="banner-text">
                            <h2>Add Customer</h2>
                            <p>Manage customer information and account details</p>
                        </div>
                    </div>
                    <div class="banner-right-text">Tameer Al Madina</div>
                </div>

                <?php if(isset($success)): ?>
                    <div class='alert-box alert-success'><i class="fa-solid fa-circle-check"></i> <?php echo $success; ?></div>
                <?php endif; ?>
                
                <?php if(isset($error)): ?>
                    <div class='alert-box alert-error'><i class="fa-solid fa-circle-exclamation"></i> <?php echo $error; ?></div>
                <?php endif; ?>

                <form method="POST">
                    
                    <div>
                        <div class="section-header">
                            <i class="fa-solid fa-circle-user"></i> Basic Information
                        </div>

                        <div class="form-grid">
                            <div class="form-group">
                                <label>Customer Name<span class="required">*</span></label>
                                <div class="input-wrapper">
                                    <i class="fa-regular fa-user"></i>
                                    <input type="text" name="name" placeholder="Enter customer name" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Contact</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-phone"></i>
                                    <input type="text" name="contact" placeholder="Enter contact number">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Email</label>
                                <div class="input-wrapper">
                                    <i class="fa-regular fa-envelope"></i>
                                    <input type="email" name="email" placeholder="Enter email address">
                                </div>
                            </div>
                        </div>

                        <div class="section-header">
                            <i class="fa-solid fa-location-dot"></i> Business & Financial Info
                        </div>

                        <div class="form-grid">
                            <div class="form-group">
                                <label>Business Name</label>
                                <div class="input-wrapper">
                                    <i class="fa-regular fa-building"></i>
                                    <input type="text" name="business_name" placeholder="Enter business name">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Opening Balance (OMR)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-money-bill-wave"></i>
                                    <input type="number" step="0.001" name="opening_balance" value="0.000" placeholder="0.000">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Address</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-location-pin"></i>
                                    <textarea name="address" placeholder="Enter address"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="button" onclick="window.history.back();" class="btn btn-cancel">
                            <i class="fa-solid fa-xmark"></i> Cancel
                        </button>
                        <button type="submit" name="submit" class="btn btn-submit">
                            <i class="fa-regular fa-floppy-disk"></i> Save Customer
                        </button>
                    </div>

                </form>

            </div>
        </div>
    </div>
</div>

<script>
function toggleMobileSidebar() {
    const sidebarElements = document.querySelectorAll('aside, .sidebar, [class*="sidebar"]');
    const overlay = document.getElementById('sidebarOverlay');
    
    sidebarElements.forEach(sidebar => {
        sidebar.classList.toggle('active');
    });
    
    if(sidebarElements[0].classList.contains('active')) {
        overlay.style.display = 'block';
    } else {
        overlay.style.display = 'none';
    }
}
</script>

</body>
</html>