<?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']));
$category=$_POST["category"];
$projectid=$_POST["id"];
$p1_input=$_POST["p1_input"];
$p2_input=$_POST["p2_input"];
$p3_input=$_POST["p3_input"];
$p4_input=$_POST["p4_input"];
$audience=$_POST["audience"];
$language=$_POST["language"];
$tone=$_POST["tone"];

file_put_contents("TEMPcontent.txt","POST: ".$p4_content);

if ($projectid == 0) {
    $sql = "INSERT INTO projects (user_id, project_name, date_created, last_edited, category, tone, language, audience, p1_input, p1_content, p2_input, p2_content, p3_input, p3_content, p4_input, p4_content) VALUES (:user_id, :project_name, NOW(), NOW(), :category, :tone, :language, :audience, :p1_input, :p1_content, :p2_input, :p2_content, :p3_input, :p3_content, :p4_input, :p4_content)";
    $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_input', $p2_input);
    $stmt->bindParam(':p2_content', $p2_content);
	$stmt->bindParam(':p3_input', $p3_input);
    $stmt->bindParam(':p3_content', $p3_content);
	$stmt->bindParam(':p4_input', $p4_input);
    $stmt->bindParam(':p4_content', $p4_content);
    $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_input=:p2_input, p2_content=:p2_content, p3_input=:p3_input, p3_content=:p3_content, p4_input=:p4_input, p4_content=:p4_content 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_input', $p2_input);
        $stmt->bindParam(':p2_content', $p2_content);
		$stmt->bindParam(':p3_input', $p3_input);
        $stmt->bindParam(':p3_content', $p3_content);
		$stmt->bindParam(':p4_input', $p4_input);
        $stmt->bindParam(':p4_content', $p4_content);
        $stmt->bindParam(':projectid', $projectid);
        $stmt->bindParam(':user_id', $userid);
        
        $stmt->execute();
        
        echo $projectid;
		exit();
        
    } else {
        echo "ERROR";
    }
}

?>
