<?php

$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();

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

$status = $set['default_status'] == 'approved' ? 'active' : $set['default_status'];

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

// 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 points
if($points < 1){
   $errors[] = 'Points must be 1 or more';
}

if($u['points'] < $points){
   $errors[] = 'You do not have enough points';
}

// 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("INSERT INTO listings (status, user_id, category_id, points, name, image, description, about, url, last_click, date_created) VALUES ('{$status}', '{$user_id}', '{$xx['id']}', '{$points}', '{$name}', 'default.png', '{$description}', '{$about}', '{$url}', '{$t}', '{$t}')")){

      $a = fetch_records("SELECT * FROM listings WHERE user_id = '{$user_id}' ORDER BY id DESC LIMIT 1");

      query("UPDATE users SET points = points - $points WHERE id = '{$user_id}' LIMIT 1");

      query("UPDATE users SET listings = listings + 1, last_post = '{$t}' WHERE id = '{$user_id}' LIMIT 1");
      query("UPDATE categories SET listings = listings + 1, last_post = '{$t}' WHERE id = '{$xx['id']}' LIMIT 1");
      query("UPDATE _stats SET listings = listings + 1 ORDER BY id DESC LIMIT 1");

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

      ex_redirect("listing/{$a['id']}");

   }else{

      ex_messages([ERROR_DATABASE]);

   }

}

?>