<?php
include("connection.php");
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

if (!isset($_GET['id'])) {
    header("Location: viewmeta_tag.php");
    exit;
}

$id = intval($_GET['id']);
$error_message = '';
$success_message = '';

// Fetch existing data
$result = mysqli_query($con, "SELECT * FROM meta_tag WHERE id = $id");
if (mysqli_num_rows($result) === 0) {
    echo "Meta tag not found!";
    exit;
}

$meta = mysqli_fetch_assoc($result);

// Update logic
if (isset($_POST['update'])) {
    $url = trim($_POST['url']);
    $meta_title = trim($_POST['meta_title']);
    $meta_key = trim($_POST['meta_key']);
    $description = trim($_POST['description']);

    // Duplicate URL check (excluding current ID)
    $check = mysqli_query($con, "SELECT * FROM meta_tag WHERE url = '$url' AND id != $id");
    if (mysqli_num_rows($check) > 0) {
        $error_message = "Another meta tag with this URL already exists.";
    } else {
        $update = mysqli_query($con, "UPDATE meta_tag SET 
            url = '$url',
            meta_title = '$meta_title',
            meta_key = '$meta_key',
            description = '$description'
            WHERE id = $id");

        if ($update) {
            header("Location: viewmeta_tag.php");
            exit;
        } else {
            $error_message = "Update failed: " . mysqli_error($con);
        }
    }
}
?>



<?php
include("header.php")
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Meta tag Update</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta content="Premium Multipurpose Admin & Dashboard Template" name="description" />
    <meta content="Themesbrand" name="author" />
    <!-- App favicon -->
    <!-- <link rel="shortcut icon" href="assets/images/favicon.ico" /> -->

    <!-- Plugins css -->
    <link href="assets/libs/dropzone/dropzone.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
    <link rel="shortcut icon" href="./assets/logo/fav.png" />

    <!-- Bootstrap Css -->
    <link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
    <!-- Icons Css -->
    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
    <!-- App Css-->
    <link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
    <!-- App js -->
    <!-- <script src="assets/js/plugin.js"></script> -->
    <style>
        .form .input {
            width: 100%;
            background: white;
            border: none;
            padding: 15px 20px;
            border-radius: 20px;
            margin-bottom: 9px;
            margin-top: 15px;
            box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
            border-inline: 2px solid transparent;
        }

        input[name='Image'] {
            display: none;

        }

        div#upload {
            cursor: pointer;

        }

        ::-webkit-file-upload-button {
            display: none;
        }

        /* 
        .form .input::-moz-placeholder {
            color: rgb(170, 170, 170);
        }

        .form .input::placeholder {
            color: rgb(170, 170, 170);
        }

        .form .input:focus {
            outline: none;
            border-inline: 2px solid #12b1d1;
        } */

        .filepath {
            display: none;
        }

        img[alt="img"] {
            width: clamp(50px, 15%, 100px);
            aspect-ratio: 1/0.70;
        }
    </style>
</head>

<body data-sidebar="dark">
    <!-- <body data-layout="horizontal" data-topbar="dark"> -->

    <!-- Begin page -->
    <div id="layout-wrapper">

        <div class="w-80 ">
            <div class="page-content" style="width: 80%;margin-left: 260px;">
                <div class="container">
                    <!-- start page title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0 font-size-18">Meta TagUpdate</h4>


                            </div>
                        </div>
                    </div>
                    <!-- end page title -->

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">

                                    <div>
                                        <form action="" method="POST">
                                            <div class="row mb-3">
                                                <div class="col-lg-4">
                                                    <label class="form-label">Page URL*</label>
                                                    <input type="text" class="form-control" name="url" value="<?php echo htmlspecialchars($meta['url']); ?>" required />
                                                </div>
                                                <div class="col-lg-4">
                                                    <label class="form-label">Meta Title*</label>
                                                    <input type="text" class="form-control" name="meta_title" value="<?php echo htmlspecialchars($meta['meta_title']); ?>" required />
                                                </div>
                                                <div class="col-lg-4">
                                                    <label class="form-label">Meta Keywords*</label>
                                                    <input type="text" class="form-control" name="meta_key" value="<?php echo htmlspecialchars($meta['meta_key']); ?>" required />
                                                </div>
                                            </div>

                                            <div class="mb-3">
                                                <label class="form-label">Meta Description*</label>
                                                <textarea name="description" class="form-control" id="Experience" required><?php echo htmlspecialchars($meta['description']); ?></textarea>
                                            </div>

                                            <div class="text-center">
                                                <button type="submit" name="update" class="btn btn-danger">Update</button>
                                                <a href="viewmeta_tag.php" class="btn btn-secondary">Cancel</a>
                                            </div>
                                        </form>

                                    </div>

                                </div>
                            </div>
                        </div>
                        <!-- end col -->
                    </div>
                    <!-- end row -->
                </div>
                <!-- container-fluid -->
            </div>
            <!-- End Page-content -->


        </div>
        <!-- end main content-->
    </div>
    <!-- END layout-wrapper -->




    <!-- JAVASCRIPT -->
    <script src="assets/libs/jquery/jquery.min.js"></script>
    <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/metismenu/metisMenu.min.js"></script>
    <script src="assets/libs/simplebar/simplebar.min.js"></script>
    <script src="assets/libs/node-waves/waves.min.js"></script>

    <!-- Plugins js -->
    <script src="assets/libs/dropzone/dropzone-min.js"></script>

    <!-- Form file upload init js -->
    <script src="assets/js/pages/form-file-upload.init.js"></script>

    <script src="assets/js/app.js"></script>
    <script src="./ck editor/ckeditor.js"></script>
    <script>
        CKEDITOR.replace('Experience');
    </script>
</body>

</html>