<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once("config.php");
require_once("user/protect.php");
require_once("user/user-lib.php");

$db = new PDO(
      "mysql:host=".USER_DB_HOST.";dbname=".USER_DB_NAME.";charset=".USER_DB_CHARSET,
      USER_DB_USER, USER_DB_PASSWORD, [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);

$projectname=$_POST["projectname"];
$p1_content = urldecode(base64_decode($_POST['p1_content']));
$p2_content = urldecode(base64_decode($_POST['p2_content']));
$p3_content = urldecode(base64_decode($_POST['p3_content']));
$p4_content = urldecode(base64_decode($_POST['p4_content']));
$p5_content = urldecode(base64_decode($_POST['p5_content']));
$category=$_POST["category"];
$projectid=$_POST["id"];
$p1_input=$_POST["p1_input"];
$audience=$_POST["audience"];
$language=$_POST["language"];
$tone=$_POST["tone"];
$title=$_POST["title"];
$imageprompt=$_POST["imageprompt"];
$imagefile=$_POST["imagefile"];
$wordsperpost=$_POST["wordsperpost"];
$promptmode=$_POST["promptmode"];
$promptstrict=$_POST["promptstrict"];

if ($projectid == 0) {
    $sql = "INSERT INTO projects (user_id, project_name, date_created, last_edited, category, tone, language, audience, p1_input, p1_content, p2_content, p3_content, p4_content, p5_content, title, imageprompt, imagefile, wordsperpost, promptmode, promptstrict) VALUES (:user_id, :project_name, NOW(), NOW(), :category, :tone, :language, :audience, :p1_input, :p1_content, :p2_content, :p3_content, :p4_content, :p5_content, :title, :imageprompt, :imagefile, :wordsperpost, :promptmode, :promptstrict)";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':user_id', $userid);
    $stmt->bindParam(':project_name', $projectname);
    $stmt->bindParam(':category', $category);
    $stmt->bindParam(':tone', $tone);
    $stmt->bindParam(':language', $language);
    $stmt->bindParam(':audience', $audience);
    $stmt->bindParam(':p1_input', $p1_input);
    $stmt->bindParam(':p1_content', $p1_content);
    $stmt->bindParam(':p2_content', $p2_content);
    $stmt->bindParam(':p3_content', $p3_content);
    $stmt->bindParam(':p4_content', $p4_content);
    $stmt->bindParam(':p5_content', $p5_content);
    $stmt->bindParam(':title', $title);
    $stmt->bindParam(':imageprompt', $imageprompt);
    $stmt->bindParam(':imagefile', $imagefile);
    $stmt->bindParam(':wordsperpost', $wordsperpost);
    $stmt->bindParam(':promptmode', $promptmode);
    $stmt->bindParam(':promptstrict', $promptstrict);
    $stmt->execute();
    $lastId = $db->lastInsertId();
    echo $lastId;
	exit();
} else {
    $sql = "SELECT * FROM projects WHERE id = :projectid AND user_id = :user_id";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':projectid', $projectid);
    $stmt->bindParam(':user_id', $userid);
	file_put_contents("TEMPid.txt","P: ".$projectid." - U: ".$userid);
    $stmt->execute();

    if($stmt->rowCount() > 0) {
        $sql = "UPDATE projects SET project_name=:project_name, last_edited = NOW(), category=:category, tone=:tone, language=:language, audience=:audience, p1_input=:p1_input, p1_content=:p1_content, p2_content=:p2_content, p3_content=:p3_content, p4_content=:p4_content, p5_content=:p5_content, title=:title, imageprompt=:imageprompt, imagefile=:imagefile, wordsperpost=:wordsperpost, promptmode=:promptmode, promptstrict=:promptstrict WHERE id = :projectid AND user_id=:user_id";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':project_name', $projectname);
        $stmt->bindParam(':category', $category);
        $stmt->bindParam(':tone', $tone);
        $stmt->bindParam(':language', $language);
        $stmt->bindParam(':audience', $audience);
        $stmt->bindParam(':p1_input', $p1_input);
        $stmt->bindParam(':p1_content', $p1_content);
        $stmt->bindParam(':p2_content', $p2_content);
        $stmt->bindParam(':p3_content', $p3_content);
        $stmt->bindParam(':p4_content', $p4_content);
		$stmt->bindParam(':p5_content', $p5_content);
		$stmt->bindParam(':title', $title);
		$stmt->bindParam(':imageprompt', $imageprompt);
		$stmt->bindParam(':imagefile', $imagefile);
		$stmt->bindParam(':wordsperpost', $wordsperpost);
		$stmt->bindParam(':promptmode', $promptmode);
		$stmt->bindParam(':promptstrict', $promptstrict);
        $stmt->bindParam(':projectid', $projectid);
        $stmt->bindParam(':user_id', $userid);
        
        $stmt->execute();
        
        echo $projectid;
		exit();
        
    } else {
        echo "ERROR";
    }
}

?>
