<?php
include "db.php";

// Get supplier ID from URL
if(!isset($_GET['id'])) { 
    echo "<p style='color:red; padding:20px;'>No Supplier ID provided.</p>"; 
    exit; 
}
$id = intval($_GET['id']);

// Fetch supplier data
$result = $conn->query("SELECT * FROM suppliers WHERE id = $id");
if($result->num_rows == 0) { 
    echo "<p style='color:red; padding:20px;'>Supplier not found.</p>"; 
    exit; 
}
$supplier = $result->fetch_assoc();

// Update on form submit
if(isset($_POST['submit'])){
    // Sanitize input inputs
    $name = $conn->real_escape_string($_POST['name']);
    $contact = $conn->real_escape_string($_POST['contact']);
    $email = $conn->real_escape_string($_POST['email']);
    $address = $conn->real_escape_string($_POST['address']);
    $business_name = $conn->real_escape_string($_POST['business_name']);
    $opening_balance = $conn->real_escape_string($_POST['opening_balance']);

    $sql = "UPDATE suppliers SET 
                name='$name', 
                contact='$contact', 
                email='$email',
                address='$address', 
                business_name='$business_name', 
                opening_balance='$opening_balance'
            WHERE id=$id";

    if($conn->query($sql)){
        $success = "Supplier configuration updated successfully!";
        // Refresh structural variables
        $supplier = $conn->query("SELECT * FROM suppliers WHERE id=$id")->fetch_assoc();
    } else { 
        $error = "Database 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>Edit Supplier Account - 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;
    overflow-y: auto;
}

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

/* Master Fixed Left Sidebar Layout */
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;
}

/* System Top Headers Section */
.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;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    border: 1px solid #E2E8F0;
}

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

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

.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 Fluid Workspace Content */
.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;
}

.form-banner {
    background: linear-gradient(135deg, #0A192F 0%, #172A45 100%);
    padding: 15px 30px;
    color: #FFFFFF;
    border-bottom: 4px solid #0d6efd;
}

.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;
}

/* Response Status Banners */
.alert-msg {
    padding: 12px 25px;
    font-size: 13px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid transparent;
}

.alert-success {
    background: #E6F4EA;
    color: #137333;
}

.alert-error {
    background: #FCE8E6;
    color: #C5221F;
}

/* Grid Layout Form Elements */
.form-body-wrapper {
    padding: 30px;
}

.form-grid-2x {
    display: grid;
    grid-template-columns: repeat(2, 11fr);
    gap: 20px;
    margin-bottom: 20px;
}

.input-group-block {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.input-group-block.full-width {
    grid-column: span 2;
}

.input-group-block label {
    font-size: 12px;
    font-weight: 600;
    color: #4A5568;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.input-group-block input[type="text"],
.input-group-block input[type="email"],
.input-group-block input[type="number"],
.input-group-block textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #CBD5E1;
    border-radius: 6px;
    background: #FFFFFF;
    color: #1A202C;
    font-size: 13px;
    font-family: 'Poppins', sans-serif;
    outline: none;
    transition: all 0.2s;
}

.input-group-block input:focus,
.input-group-block textarea:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.1);
}

.input-group-block textarea {
    min-height: 90px;
    resize: vertical;
}

/* Action Control Row */
.action-controls-panel {
    border-top: 1px solid #E2E8F0;
    padding-top: 20px;
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

.btn-base {
    padding: 10px 24px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: background 0.15s ease;
    border: none;
}

.btn-submit {
    background: #0d6efd;
    color: #FFFFFF;
    box-shadow: 0 2px 6px rgba(13, 110, 253, 0.15);
}

.btn-submit:hover {
    background: #0a58ca;
}

.btn-cancel {
    background: #EDF2F7;
    color: #4A5568;
    border: 1px solid #E2E8F0;
}

.btn-cancel:hover {
    background: #E2E8F0;
}

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

@media (max-width: 768px) {
    .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; }
    .page-container { padding: 12px; }
    .form-body-wrapper { padding: 20px; }
    .form-grid-2x { grid-template-columns: 100%; gap: 15px; }
    .input-group-block.full-width { grid-column: span 1; }
    .action-controls-panel { flex-direction: column-reverse; }
    .btn-base { width: 100%; justify-content: center; }
}
</style>
</head>
<body>

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

<div class="main-layout">
    
    <?php 
    $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";
    }
    ?>

    <div class="content-wrapper">
        
        <div class="top-navbar">
            <div class="navbar-left-flex">
                <div class="mobile-menu-toggle" onclick="toggleMobileSidebar()">
                    <i class="fa-solid fa-bars"></i>
                </div>
                <span style="font-weight: 500; font-size: 14px; color: #4A5568;">Procurement / Suppliers</span>
            </div>
            
            <div class="user-profile">
                <div class="profile-info">
                    <i class="fa-solid fa-circle-user profile-icon"></i>
                    <div class="profile-text">
                        <h4>Admin</h4>
                        <p>Administrator</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="page-container">
            <div class="form-card">
                
                <div class="form-banner">
                    <div class="banner-left">
                        <div class="banner-icon">
                            <i class="fa-solid fa-truck-field-un"></i>
                        </div>
                        <div class="banner-text">
                            <h2>Edit Supplier Profile</h2>
                            <p>Manage raw material providers, purchase opening statements, logistics info, and corporate details</p>
                        </div>
                    </div>
                </div>

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

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

                <form method="POST" class="form-body-wrapper">
                    <div class="form-grid-2x">
                        
                        <div class="input-group-block">
                            <label>Supplier Agent Name *</label>
                            <input type="text" name="name" value="<?php echo htmlspecialchars($supplier['name']); ?>" required placeholder="Enter primary focal person name">
                        </div>

                        <div class="input-group-block">
                            <label>Business / Mill Name</label>
                            <input type="text" name="business_name" value="<?php echo htmlspecialchars($supplier['business_name']); ?>" placeholder="e.g. Madina Steel Mills">
                        </div>

                        <div class="input-group-block">
                            <label>Contact Number</label>
                            <input type="text" name="contact" value="<?php echo htmlspecialchars($supplier['contact']); ?>" placeholder="e.g. 03007654321">
                        </div>

                        <div class="input-group-block">
                            <label>Email Address</label>
                            <input type="email" name="email" value="<?php echo htmlspecialchars($supplier['email']); ?>" placeholder="supplier@domain.com">
                        </div>

                        <div class="input-group-block">
                            <label>Opening Balance Payable (OMR)</label>
                            <input type="number" step="0.001" name="opening_balance" value="<?php echo $supplier['opening_balance']; ?>" placeholder="0.000">
                        </div>

                        <div class="input-group-block full-width">
                            <label>Factory / Office Address</label>
                            <textarea name="address" placeholder="Complete structural physical site address details..."><?php echo htmlspecialchars($supplier['address']); ?></textarea>
                        </div>

                    </div>

                    <div class="action-controls-panel">
                        <a href="ledger.php?type=supplier&id=<?php echo $id; ?>" class="btn-base btn-cancel">
                            <i class="fa-solid fa-chevron-left"></i> View Ledger
                        </a>
                        <button type="submit" name="submit" class="btn-base btn-submit">
                            <i class="fa-solid fa-floppy-disk"></i> Update Supplier
                        </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] && sidebarElements[0].classList.contains('active')) {
        overlay.style.display = 'block';
    } else {
        overlay.style.display = 'none';
    }
}
</script>
</body>
</html>