<?php
// error_reporting(E_ALL);
// ini_set("display_errors", 1);



header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

session_start();

require __DIR__.'/classes/Database.php';
require __DIR__.'/classes/lib.php';

$db_connection = new Database();
$conn = $db_connection->dbConnection();

$data = json_decode(file_get_contents("php://input"));

//IF REQUEST METHOD IS NOT EQUAL TO POST
if($_SERVER["REQUEST_METHOD"] != "POST")
{
    $returnData = new CMsg(0,404,'Page Not Found! REQUEST_METHOD');
    echo $returnData->jsonarray();
    return;
}

$returnData = new CMsg(0);

if(
    !isset($data->qr_id) 
    || !isset($data->qr_id) 
)
{
    $fields = ['fields' => ['qr_id']];
    $returnData = new CMsg(0, 422, 'Please Fill in all Required Fields!', $fields);
}
else
{
    try
    {
        $qr_id = $data->qr_id;
        $stmt = $conn->prepare("DELETE FROM `dogs` WHERE `qr_id`=?");
        $stmt->execute([$qr_id]); 

        if($stmt->rowCount())
        {
            if(deleteDir('../uploads/' . $qr_id))
            {
                $returnData = new CMsg(1, 200, 'Profile deleted!');
            }
            else
            {
                $returnData = new CMsg(0, 500, 'Can\'t delete Directory: '. 
                                    '../uploads/' . $qr_id);
            }
        }
        else
        {
            $returnData = new CMsg(0, 500, 'Can\'t delete Profil in Database');
        }
    }
    catch(PDOException $e)
    {
        $returnData = new CMsg(0,500,"deleteProfil ".$e->getMessage());
    }
}
echo $returnData->jsonarray();
return;
