<?php require_once 'includes/main.php'; logged_in('user_id'); ?>
<?php require_once 'elements/head.php'; ?>
<?php 

$id = (int)$_GET['id'];

$sql = "SELECT * FROM listings WHERE id = '{$id}' LIMIT 1";
if(fetch_rows($sql) < 1){

   $_SESSION['messages'][] = "Listing could not be found";
   header("Location: ".URL);
   exit;

}else{

   $x = fetch_records($sql);

}

?>

<!-- META: TITLE -->
<title>Edit: <?php echo $x['name']; ?> - <?php echo $set['website_name']; ?></title>

</head>

<body>

<?php include_once 'elements/header.php'; ?>

<?php include_once 'elements/sidebar/dashboard.php'; ?>

<!-- CONTENT -->
<section class="Content R">

   <h2 class="GLOB-Title"><?php echo $x['name'] ?>: <span class="FR">Form</span></h2>

   <!-- FORM -->
   <form class="Form" onsubmit="exForm(event, this, 'ex/update.listing.php')" method="POST" enctype="multipart/form-data">

      <!-- ID -->
      <input type="hidden" name="id" class="H" value="<?php echo $id; ?>">

      <!-- FILE: IMAGE -->
      <div class="field">
         <div class="icon"><img src="images/icons/file.png" width="100%" height="100%"></div>
         <label for="input-image" class="textfield" onclick="fileName(this);">Select JPG Image: <span class="FR">250px by 250px</span></label>
         <input type="file" name="image" class="H" id="input-image" accept="image/jpeg">
      </div>

      <!-- TEXTFIELD: TEXT -->
      <div class="field">
         <div class="icon"><img src="images/icons/text.png" width="100%" height="100%"></div>
         <input type="text" name="name" class="textfield" maxlength="30" placeholder="Listing Name" value="<?php echo $x['name']; ?>">
      </div>

      <!-- SELECT MENU -->
      <div class="field">
         <div class="select">
            <input type="hidden" name="category" id="input-category" value="<?php echo fetch_id('categories', $x['category_id'])['name']; ?>">
            <div class="selectfield" onclick="selectMenu(this);"><?php echo fetch_id('categories', $x['category_id'])['name']; ?></div>
            <div class="options">
               <?php fetch_elements("SELECT name FROM categories ORDER BY name ASC", 'elements/options/main.php'); ?>
            </div>
         </div>
      </div>

      <!-- TEXTFIELD: TEXT -->
      <div class="field">
         <div class="icon"><img src="images/icons/text.png" width="100%" height="100%"></div>
         <input type="text" name="description" class="textfield" maxlength="255" placeholder="Short Description" value="<?php echo $x['description']; ?>">
      </div>

      <!-- TEXTAREA -->
      <div class="field">
         <textarea type="text" name="about" class="textarea" placeholder="Long Description"><?php echo br2nl($x['about']); ?></textarea>
      </div>

      <!-- TEXTFIELD: URL -->
      <div class="field">
         <div class="icon"><img src="images/icons/link.png" width="100%" height="100%"></div>
         <input type="url" name="url" class="textfield" placeholder="URL" value="<?php echo $x['url']; ?>" required>
      </div>

      <div class="ERRORS"></div>

      <!-- SUBMIT -->
      <div class="submit">
         <input type="submit" value="Submit Listing">
      </div>

   </form>
   <!-- FORM -->

</section>
<!-- CONTENT -->

<?php include_once 'elements/footer.php'; ?>

</body>
</html>
