<?php
require_once "../base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: /storage/login.php");
if(!isAdmin($_SESSION["userId"])){
    http_response_code(403);
    print("Forbidden, you don't have access to this resource.");
    exit;
}
$id = htmlspecialchars($_GET['id']);
require "../db.php";
if($_SERVER["REQUEST_METHOD"]==="POST"){
    $name = htmlspecialchars($_POST['name']);
    $surname = htmlspecialchars($_POST['surname']);
    if(!isset($_POST["newAddress"])){
        $stmt=$pdo->prepare("UPDATE people SET name=?,surname=?,address=? WHERE id=?");
        $stmt->execute([$name,$surname,htmlspecialchars($_POST['address']),$id]);
    } else {
        print_r($_POST);
        $country = htmlspecialchars($_POST["country"]);
        $city = htmlspecialchars($_POST["city"]);
        $street = htmlspecialchars($_POST["street"]);
        $block = htmlspecialchars($_POST["block"]);
        $flat = htmlspecialchars($_POST["flat"]);
        $postCode = htmlspecialchars($_POST["postCode"]);
        $pdo->beginTransaction();
        $stmt=$pdo->prepare("SELECT id FROM address WHERE country=? AND city=? AND street=? AND block=? AND flat=? AND postCode=?");
        $stmt->execute([$country,$city,$street,$block,$flat,$postCode]);
        if($stmt->rowCount()==0){
            $stmt=$pdo->prepare("INSERT INTO address (country, city, street, block, flat, postCode) VALUES (?,?,?,?,?,?)");
            $stmt->execute([$country,$city,$street,$block,$flat,$postCode]);
            $stmt=$pdo->prepare("INSERT INTO people (name,surname,address) VALUES (?,?,LAST_INSERT_ID())");
            $stmt->execute([$name,$surname]);
        } else {
            $id = $stmt->fetch()["id"];
            $stmt=$pdo->prepare("INSERT INTO people (name,surname,address) VALUES (?,?,?)");
            $stmt->execute([$name,$surname,$id]);
        }
        $pdo->commit();
    }
    header("Location: list.php");
} else {
$stmt=$pdo->prepare("SELECT * FROM people WHERE id=?");
$stmt->execute([$id]);
if($data=$stmt->fetch()):
head("Add person");
nav();
?>
<div>
<h1>Edit person</h1>
<form method="POST">
<table class="table table-striped table-sm">
    <thead>
        <tr>
            <td>name</td>
            <td>surname</td>
            <td>address</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" name="name" value="<?= $data["name"] ?>"></td>
            <td><input type="text" name="surname" value="<?= $data["surname"] ?>"></td>
            <td>
                <label><input type="checkbox" name="newAddress" id="newAddress" onInput="switchAddressInputType()">New address</label>
                <select name="address" id="addressSelect">
                    <?php 
                        $stmt2=$pdo->prepare("SELECT id,street,block,flat,postCode,country FROM address");
                        $stmt2->execute([]);
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>" <?= $row2['id']==$data["address"]?"selected":"" ?>><?= $row2['street'].' '.$row2["block"].(isset($row2["flat"])?'/'.$row2["flat"]:"").' '.$row2["postCode"].' '.$row2["country"]?></option>
                        <?php endwhile; ?>
                </select>
                <div id="addressInput" style="display: none">
                    <?php 
                    $stmt=$pdo->prepare("SELECT * FROM address WHERE id=?");
                    $stmt->execute([$data["address"]]);
                    $address = $stmt->fetch();
                    ?>
                    <input type="text" name="country" placeholder="country" value="<?= $address["country"] ?>">
                    <input type="text" name="city" placeholder="city" value="<?= $address["city"] ?>">
                    <input type="text" name="street" placeholder="street" value="<?= $address["street"] ?>">
                    <input type="text" name="block" placeholder="block" value="<?= $address["block"] ?>">
                    <input type="text" name="flat" placeholder="flat" value="<?= $address["flat"] ?>">
                    <input type="text" name="postCode" placeholder="00-000" value="<?= $address["postCode"] ?>">
                </div>
            </td>
        </tr>
    </tbody>
</table>
<input type="submit" value="Add" class="btn btn-success">
</form>
</div>
<script>
    function switchAddressInputType() {
        if(!document.getElementById("newAddress").checked){
            document.getElementById("addressSelect").style.display = "inline";
            document.getElementById("addressInput").style.display = "none";
        } else {
            document.getElementById("addressSelect").style.display = "none";
            document.getElementById("addressInput").style.display = "block";
        }
    }
</script>
<?php
footer();
                        endif;
                    }
?>
