<?php

declare(strict_types=1);

namespace App\Repository;

final class TblRolPanelRepository
{
    private \PDO $database;

    public function __construct(\PDO $database)
    {
        $this->database = $database;
    }

    public function getDb(): \PDO
    {
        return $this->database;
    }

    public function checkAndGet(int $TblRolPanelId): object
    {
        $query = 'SELECT * FROM `TblRolPanel` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblRolPanelId);
        $statement->execute();
        $TblRolPanel = $statement->fetchObject();
        if (! $TblRolPanel) {
            throw new \Exception('TblRolPanel not found.', 404);
        }

        return $TblRolPanel;
    }

    public function getAll(): array
    {
        $query = 'SELECT * FROM `TblRolPanel` ORDER BY `id`';
        $statement = $this->getDb()->prepare($query);
        $statement->execute();

        return (array) $statement->fetchAll();
    }

    public function create(object $TblRolPanel): object
    {
        $query = 'INSERT INTO `TblRolPanel` (`Rol`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (:Rol, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $TblRolPanel->id);
//         $statement->bindParam('fecha', $TblRolPanel->fecha);
        $statement->bindParam('Rol', $TblRolPanel->Rol);
        $statement->bindParam('field1', $TblRolPanel->field1);
        $statement->bindParam('field2', $TblRolPanel->field2);
        $statement->bindParam('field3', $TblRolPanel->field3);
        $statement->bindParam('field4', $TblRolPanel->field4);
        $statement->bindParam('field5', $TblRolPanel->field5);

        $statement->execute();

        return $this->checkAndGet((int) $this->getDb()->lastInsertId());
    }

    public function update(object $TblRolPanel, object $data): object
    {
        if (isset($data->fecha)) {
            $TblRolPanel->fecha = $data->fecha;
        }
        if (isset($data->Rol)) {
            $TblRolPanel->Rol = $data->Rol;
        }
        if (isset($data->field1)) {
            $TblRolPanel->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblRolPanel->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblRolPanel->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblRolPanel->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblRolPanel->field5 = $data->field5;
        }

        $query = 'UPDATE `TblRolPanel` SET `fecha` = :fecha, `Rol` = :Rol, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblRolPanel->id);
        $statement->bindParam('fecha', $TblRolPanel->fecha);
        $statement->bindParam('Rol', $TblRolPanel->Rol);
        $statement->bindParam('field1', $TblRolPanel->field1);
        $statement->bindParam('field2', $TblRolPanel->field2);
        $statement->bindParam('field3', $TblRolPanel->field3);
        $statement->bindParam('field4', $TblRolPanel->field4);
        $statement->bindParam('field5', $TblRolPanel->field5);

        $statement->execute();

        return $this->checkAndGet((int) $TblRolPanel->id);
    }

    public function delete(int $TblRolPanelId): void
    {
        $query = 'DELETE FROM `TblRolPanel` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblRolPanelId);
        $statement->execute();
    }
}
