<?php
namespace Dao;

use Conn\MySqlConnect;

/**
 * Description of GaleriasImages
 *
 * @author Délio
 */

class GaleriasImages {
    public $id;
    public $fk_id_galeria;
    public $image;
    public $thumb;
    public $legenda;
    public $ordem;
    public $status;

    function __construct($res = null) {
        $this->id = (int) 0;
        $this->fk_id_galeria = (int) 0;
        $this->image = (string) '';
        $this->thumb = (string) '';
        $this->legenda = (string) '';
        $this->ordem = (int) 0;
        $this->status = (string) 'A';
        $this->fill($res);
    }

    private function fill($res) {
        if ($res != null && $res instanceof \stdClass) {
            $this->id = (int) (isset($res->id) ? $res->id : 0);
            $this->fk_id_galeria = (int) (isset($res->fk_id_galeria) ? $res->fk_id_galeria : 0);
            $this->image = (string) (isset($res->image) ? $res->image : '');
            $this->thumb = (string) (isset($res->thumb) ? $res->thumb : '');
            $this->legenda = (string) (isset($res->legenda) ? $res->legenda : '');
            $this->ordem = (int) (isset($res->ordem) ? $res->ordem : 0);
            $this->status = (string) (isset($res->status) ? $res->status : 'A');
        }
    }

    private function inserir(MySqlConnect $conecta) {
        $sql = $conecta->prepare("INSERT INTO galerias_images (fk_id_galeria, image, thumb, legenda, ordem, status) VALUES (?, ?, ?, ?, ?, ?)") or die($conecta->error);
        $sql->bind_param("isssis", $this->fk_id_galeria, $this->image, $this->thumb, $this->legenda, $this->ordem, $this->status);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Imagem inserida com sucesso!', 'id' => $sql->insert_id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => 0]);
    }
    
    private function editar(MySqlConnect $conecta) {
        $sql = $conecta->prepare("UPDATE galerias_images SET legenda = ? WHERE id = ?") or die($conecta->error);
        $sql->bind_param("si", $this->legenda, $this->id);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Imagem atualizada com sucesso!', 'id' => $this->id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => $this->id]);
    }
    
    function salvar(MySqlConnect $conecta) {
        if ($this->id == 0)
            return $this->inserir($conecta);
        return $this->editar($conecta);
    }

    function excluir(MySqlConnect $conecta) {
        $sql = $conecta->prepare("UPDATE galerias_images SET status = ? WHERE id = ?") or die($conecta->error);
        $sql->bind_param("si", $this->status, $this->id);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Imagem excluida com sucesso!', 'id' => $this->id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => $this->id]);
    }
    
    function getLista(MySqlConnect $conecta, $id_galeria = 0, $where = '') {
        $lst_images = [];
        $sql = $conecta->query("SELECT * FROM galerias_images WHERE fk_id_galeria = '$id_galeria' AND status = 'A' $where") or die($conecta->error);
        while ($res = $sql->fetch_object())
            $lst_images[] = new GaleriasImages($res);
        return $lst_images;
    }
    
    function getImagemPorId(MySqlConnect $conecta, $id) {
        $sql = $conecta->query("SELECT * FROM galerias_images WHERE id = '$id' AND status = 'A'") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->fill($res);
        }
    }

    function getImagensPorIdGaleria(MySqlConnect $conecta, $id) {
        $sql = $conecta->query("SELECT * FROM galerias_images WHERE fk_id_galeria = '$id' AND status = 'A'") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->fill($res);
        }
    }
}