<?php 

$status = 'active';

$min_name = 2;
$max_name = 30;
$min_description = 1;
$max_description = 255;
$min_about = 1;
$max_about = 5000;
$image_width = 250;
$image_height = 250;
$max_size = 100;

$dir = '../';

require_once $dir.'includes/main.php';

$errors = array();

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

$name = clean($_POST['name']);
$category = clean($_POST['category']);
$description = clean($_POST['description']);
$about = clean($_POST['about']);
$url = clean($_POST['url']);

// check login
if(!isset($user_id)){
   $errors[] = LOG_IN;
}

// check id
$sql = "SELECT * FROM listings WHERE id = '{$id}' LIMIT 1";
if(fetch_rows($sql) < 1){
   $errors[] = "Listing #$id";
   $errors[] = "we could not find any records with this ID";
}else{

   $x = fetch_records($sql);

   if($user_id != $x['user_id']){
      $errors[] = "You don't have permission to edit this listing";
   }

}

// check name
if(empty($name)){
   $errors[] = "Name is required";
}

if(strlen($name) < $min_name){
   $errors[] = "Name must be $min_name or more characters";
}

if(strlen($name) > $max_name){
   $errors[] = "Name must be $max_name or less characters";
}

// check category
if(empty($category)){
   $errors[] = "You must select a category";
}

$sql = "SELECT * FROM categories WHERE name = '{$category}' LIMIT 1";
if(fetch_rows($sql) < 1){
   $errors[] = "Category: $category";
   $errors[] = "we could not find any records with this Name";
}else{
   $xx = fetch_records($sql);
}

// check description
if(empty($description)){
   $errors[] = "Description is required";
}

if(strlen($description) < $min_description){
   $errors[] = "Description must be $min_description or more characters";
}

if(strlen($description) > $max_description){
   $errors[] = "Description must be $max_description or less characters";
}

// check about
if(empty($about)){
   $errors[] = "About is required";
}

if(strlen($about) < $min_about){
   $errors[] = "About must be $min_about or more characters";
}

if(strlen($about) > $max_about){
   $errors[] = "About must be $max_about or less characters";
}

// check url
if(empty($url)){
   $errors[] = "URL is required";
}

if(!filter_var($url, FILTER_VALIDATE_URL)){
   $errors[] = "URL is invalid. Please enter a URL starting with https://";
}

// check image
$errors[] = check_file($_FILES['image'], $max_size, $image_width, $image_height);

$errors = array_filter($errors);

// check errors
if(!empty($errors)){

   ex_messages($errors);

}else{
    
   if(query("UPDATE listings SET status = '{$status}', name = '{$name}', category_id = '{$xx['id']}', description = '{$description}', about = '{$about}', url = '{$url}' WHERE id = '{$id}' LIMIT 1")){

      if($x['category_id'] != $xx['id']){
         query("UPDATE categories SET listings = listings - 1 WHERE id = '{$x['category_id']}' LIMIT 1");
         query("UPDATE categories SET listings = listings + 1 WHERE id = '{$xx['id']}' LIMIT 1");
      }

      upload_file($_FILES['image'], 'listings', $id, 'listings');

      ex_redirect("listing/$id");

   }else{

      ex_messages([ERROR_DATABASE]);

   }

}

?>