<?php

namespace Dao;

use Conn\MySqlConnect;

/**
 * Description of Usuarios
 *
 * @author Delio
 */
class Usuarios {

    public $id;
    public $nome;
    public $email;
    public $login;
    public $senha;
    public $tipo;
    public $permissoes;
    public $image;
    public $status;

    function __construct($res = null) {
        $this->id = (int) 0;
        $this->nome = (string) '';
        $this->email = (string) '';
        $this->login = (string) '';
        $this->senha = (string) '';
        $this->tipo = (string) 'A';
        $this->permissoes = (string) '';
        $this->image = (string) '';
        $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->nome = (string) (isset($res->nome) ? $res->nome : '');
            $this->email = (string) (isset($res->email) ? $res->email : '');
            $this->login = (string) (isset($res->login) ? $res->login : '');
            $this->senha = (string) (isset($res->senha) ? $res->senha : '');
            $this->tipo = (string) (isset($res->tipo) ? $res->tipo : '');
            $this->permissoes = (string) (isset($res->permissoes) ? $res->permissoes : '');
            $this->image = (string) (isset($res->image) ? $res->image : '');
            $this->status = (string) (isset($res->status) ? $res->status : 'A');
        }
    }

    private function inserir(MySqlConnect $conecta): string {
        $sql = $conecta->prepare("INSERT INTO usuarios (nome, email, login, senha, tipo, permissoes, image, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or die($conecta->error);
        $sql->bind_param("ssssssss", $this->nome, $this->email, $this->login, $this->senha, $this->tipo, $this->permissoes, $this->image, $this->status);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Usuário inserido com sucesso', 'id' => $sql->insert_id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => 0]);
    }

    private function editar(MySqlConnect $conecta): string {
        $sql = $conecta->prepare("UPDATE usuarios SET nome = ?, email = ?, login = ?, senha = ?, tipo = ?, permissoes = ?, image = ? WHERE id = ?") or die($conecta->error);
        $sql->bind_param("sssssssi", $this->nome, $this->email, $this->login, $this->senha, $this->tipo, $this->permissoes, $this->image, $this->id);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Usuário atualizado com sucesso', 'id' => $this->id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => $this->id]);
    }

    function salvar(MySqlConnect $conecta): string {
        if ($this->id == 0)
            return $this->inserir($conecta);
        return $this->editar($conecta);
    }

    function excluir(MySqlConnect $conecta): string {
        $sql = $conecta->prepare("UPDATE usuarios 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' => 'Usuário excluido com sucesso', 'id' => $this->id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => $this->id]);
    }

    function getLista(MySqlConnect $conecta, $where = '') {
        $lst_usuarios = [];
        $sql = $conecta->query("SELECT id, nome, email, login, tipo FROM usuarios WHERE status = 'A' $where") or die($conecta->error);
        while ($res = $sql->fetch_object())
            $lst_usuarios[] = new Usuarios($res);
        return $lst_usuarios;
    }

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

    function getUsuarioPorLogin(MySqlConnect $conecta, $user, $pass): void {
        $sql = $conecta->query("SELECT id FROM usuarios WHERE login = '$user' AND senha = '$pass' AND status = 'A' LIMIT 1") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->getUsuarioPorId($conecta, $res->id);
        }
    }
}