<?php
include 'ominterior_db.php';

$success = $error = "";

// Fetch existing contact details (assume only one row)
$result = $conn->query("SELECT * FROM contact_details LIMIT 1");
$contact = $result->fetch_assoc();

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $address = trim($_POST['address']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);

    if (!empty($address) && !empty($email) && !empty($phone)) {
        if ($contact) {
            // Update existing
            $stmt = $conn->prepare("UPDATE contact_details SET address = ?, email = ?, phone = ? WHERE id = ?");
            $stmt->bind_param("sssi", $address, $email, $phone, $contact['id']);
        } else {
            // Insert if empty
            $stmt = $conn->prepare("INSERT INTO contact_details (address, email, phone) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $address, $email, $phone);
        }

        if ($stmt->execute()) {
            $success = "Contact details updated successfully!";
            header("Location: contactdetalis.php?msg=updated");
            exit;
        } else {
            $error = "Failed to update contact details.";
        }
        $stmt->close();
    } else {
        $error = "All fields are required.";
    }
}
?>

<!doctype html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="icon" href="assets/images/black-logo.png" type="image/x-icon"/>

<title>OM INTERIOR - Dashborad</title>

<!-- Bootstrap Core and vandor -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css" />

<!-- Plugins css -->
<link rel="stylesheet" href="assets/plugins/charts-c3/c3.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Core css -->
<link rel="stylesheet" href="assets/css/main2.css"/>
<link rel="stylesheet" href="assets/css/theme1.css"/>
</head>

<body class="font-montserrat">
<!-- Page Loader -->
<div class="page-loader-wrapper">
    <div class="loader">
    </div>
</div>

<div id="main_content">

<?php include 'haderTop_logo.php'?>
    
    <?php include 'rightside_menu.php'?>
    
    <?php include 'leftside_menu.php' ?>
    
        <div class="page">
            <div id="page_top" class="section-body top_dark">
                <div class="container-fluid">
                    <div class="page-header">
                        <div class="left">
                            <a href="javascript:void(0)" class="icon menu_toggle mr-3"><i class="fa  fa-align-left"></i></a>
                            <h1 class="page-title">Edit Contact Detalis</h1>                        
                        </div>
                        <div class="right">
                            <?php include 'dropdown_flex_menu.php' ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="container mt-5">
                <div class="card shadow">
                    <div class="card-header">
                        <h4 class="mb-0">Edit Contact Details</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($success): ?>
                            <div class="alert alert-success"><?= $success ?></div>
                        <?php endif; ?>
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?= $error ?></div>
                        <?php endif; ?>

                        <form method="POST">
                            <div class="form-group">
                                <label><strong>Address</strong></label>
                                <textarea name="address" rows="3" class="form-control" required><?= htmlspecialchars($contact['address'] ?? '') ?></textarea>
                            </div>

                            <div class="form-group">
                                <label><strong>Email</strong></label>
                                <input type="email" name="email" class="form-control" required value="<?= htmlspecialchars($contact['email'] ?? '') ?>">
                            </div>

                            <div class="form-group">
                                <label><strong>Phone</strong></label>
                                <input type="text" name="phone" class="form-control" required value="<?= htmlspecialchars($contact['phone'] ?? '') ?>">
                            </div>

                            <button type="submit" class="btn btn-primary">Save Changes</button>
                            <a href="contact-details.php" class="btn btn-secondary">Cancel</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
                
        <div class="section-body">
            <footer class="footer">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12 col-sm-12 text-md-right">
                            <ul class="list-inline mb-0">
                                <li class="list-inline-item"><a href="doc/index.html">OM INTERIOR</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </footer>
        </div>
    </div>    
</div>

<script src="assets/bundles/lib.vendor.bundle.js"></script>

<script src="assets/bundles/apexcharts.bundle.js"></script>
<script src="assets/bundles/counterup.bundle.js"></script>
<script src="assets/bundles/knobjs.bundle.js"></script>
<script src="assets/bundles/c3.bundle.js"></script>

<script src="assets/js/core.js"></script>
<script src="assets/js/page/project-index.js"></script>
</body>
</html>

<?php

include 'ominterior_db.php'; // Ensure connection is established

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $client = $_POST['cname'];
    $category = $_POST['Category'];
    $ptype = $_POST['ptype'];
    $pname = $_POST['pname'];
    $pdes = $_POST['pdes'];
    $values = [];
   

    $uploadDir = "uploads/$pname/";
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }
    
    $values = [];
    
    foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
        $originalName = basename($_FILES['images']['name'][$key]);
        $fileType = pathinfo($originalName, PATHINFO_EXTENSION);
    
        // Allow certain file formats
        $allowTypes = array('jpg', 'png', 'jpeg', 'gif', 'webp');
        if (in_array(strtolower($fileType), $allowTypes)) {
            // Generate unique name using timestamp + random ID
            $uniqueName = uniqid('img_') . "_" . time() . "." . $fileType;
            $targetFilePath = $uploadDir . $uniqueName;
    
            if (move_uploaded_file($tmp_name, $targetFilePath)) {
                $values[] = "('$uniqueName')";
            }
        }
    }
    
    if (!empty($values)) {
        echo "Images uploaded successfully!";
    }
    
    if (!empty($category) && !empty($ptype) && !empty($pname) && !empty($pdes) &&!empty($values)) {
        $stmt = $conn->prepare("INSERT INTO project_list (client_name, project_category, project_type, project_title, project_description, project_images) VALUES (?, ?, ?, ?, ?, ?)");
        
        if (!$stmt) {
            die("Statement preparation failed: " . $conn->error);
        }

        $stmt->bind_param("ssssss",$client, $category, $ptype, $pname, $pdes, implode(",",$values));

        if ($stmt->execute()) {
            echo "Project added successfully!";
        } else {
            echo "Error executing statement: " . $stmt->error;
        }
        $stmt->close();
    } 
}
?>

?>