<?php

namespace Dao;

use Conn\MySqlConnect;

/**
 * Description of Notificacoes
 *
 * @author Délio
 */
class Notificacoes {

    public $id;
    public $tabela;
    public $fk_id_tabela;
    public $fk_id_de;
    public $fk_id_para;
    public $acao;
    public $descricao;
    public $situacao;
    public $status;
    public $data;
    public $usuario_painel;

    function __construct($res = null) {
        $this->id = (int) 0;
        $this->tabela = (string) '';
        $this->fk_id_tabela = (int) 0;
        $this->fk_id_de = (int) 0;
        $this->fk_id_para = (int) 0;
        $this->acao = (string) '';
        $this->descricao = (string) '';
        $this->situacao = (string) 'N';
        $this->status = (string) 'A';
        $this->data = (string) date('Y-m-d H:i:s');
        $this->usuario_painel = (string) 'N';
        $this->fill($res);
    }

    private function fill($res) {
        if ($res != null && $res instanceof \stdClass) {
            $this->id = (int) (isset($res->id) ? $res->id : 0);
            $this->tabela = (string) (isset($res->tabela) ? $res->tabela : '');
            $this->fk_id_tabela = (int) (isset($res->fk_id_tabela) ? $res->fk_id_tabela : 0);
            $this->fk_id_de = (int) (isset($res->fk_id_de) ? $res->fk_id_de : 0);
            $this->fk_id_para = (int) (isset($res->fk_id_para) ? $res->fk_id_para : 0);
            $this->acao = (string) (isset($res->acao) ? $res->acao : '');
            $this->descricao = (string) (isset($res->descricao) ? $res->descricao : '');
            $this->situacao = (string) (isset($res->situacao) ? $res->situacao : 'N');
            $this->status = (string) (isset($res->status) ? $res->status : 'A');
            $this->data = (string) (isset($res->data) ? $res->data : date('Y-m-d H:i:s'));
            $this->usuario_painel = (string) (isset($res->usuario_painel) ? $res->usuario_painel : 'N');
        }
    }

    private function inserir(MySqlConnect $conecta) {
        $sql = $conecta->prepare("INSERT INTO notificacoes (tabela, fk_id_tabela, fk_id_de, fk_id_para, acao, descricao, situacao, status, data, usuario_painel) "
                . "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die($conecta->error);
        $sql->bind_param("siiissssss", $this->tabela, $this->fk_id_tabela, $this->fk_id_de, $this->fk_id_para, $this->acao, $this->descricao, $this->situacao, 
                $this->status, $this->data, $this->usuario_painel);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Notificação 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 notificacoes SET situacao = ? WHERE id = ?") or die($conecta->error);
        $sql->bind_param("si", $this->situacao, $this->id);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Notificação 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 notificacoes 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' => 'Notificação Excluida com Sucesso!', 'id' => $this->id]);
        return json_encode(['data' => 'Error', 'message' => $sql->error, 'id' => $this->id]);
    }

    function getLista(MySqlConnect $conecta, $where = '') {
        $lst_notificacoes = [];
        $sql = $conecta->query("SELECT id, tabela, fk_id_tabela, fk_id_de, fk_id_para, acao, descricao, situacao, data, usuario_painel FROM notificacoes "
                . "WHERE status = 'A' $where") or die($conecta->error);
        while ($res = $sql->fetch_object())
            $lst_notificacoes[] = new Notificacoes($res);
        return $lst_notificacoes;
    }

    function getNotificacaoPorId(MySqlConnect $conecta, $id) {
        $sql = $conecta->query("SELECT id, tabela, fk_id_tabela, fk_id_de, fk_id_para, acao, descricao, situacao, data, usuario_painel FROM notificacoes "
                . "WHERE id = '$id' AND status = 'A'") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->fill($res);
        }
    }
    
    function sendMail($mail, $params = []) {
        $mail->IsMail();
        $mail->isHTML(true);
        $mail->setLanguage('br');
        $mail->setFrom($params['from']['email'], $params['from']['nome']);
        $mail->Subject = $params['assunto'];
        $mail->CharSet = 'UTF-8';
        $mail->ContentType = 'text/html';
        $mail->Priority = 3;
        $mail->addAddress($params['to']['email'], $params['to']['nome']);
        $mail->Body = '<!DOCTYPE HTML>';
        $mail->Body .= '<html lang="pt-BR">';
        $mail->Body .= '<head>';
        $mail->Body .= '<title>' . $params['assunto'] . '</title>';
        $mail->Body .= '<meta charset="UTF-8">';
        $mail->Body .= '<meta name="robots" content="index, follow" />';
        $mail->Body .= '<meta http-equiv="X-UA-Compatible" content="IE=edge">';
        $mail->Body .= '<meta name="viewport" content="width=device-width, initial-scale=1">';
        $mail->Body .= '<meta name="distribution" content="global" />';
        $mail->Body .= '<meta name="rating" content="general" />';
        $mail->Body .= '<link href="https://fonts.googleapis.com/css?family=Merriweather:300&display=swap" rel="stylesheet">';
        $mail->Body .= '<style type="text/css">';
        $mail->Body .= 'html, body {background: #d2d3d5; margin: 0; padding: 0; font-family: Merriweather, serif; font-weight: 300;}';
        $mail->Body .= 'div#page {width: 100%; min-height: 100vh; max-width: 600px; margin: 0 auto;}';
        $mail->Body .= 'table {background: #fff; border-top: 5px solid #000; font-size: 14px; border-bottom: 5px solid #000; padding-bottom: 15px;}';
        $mail->Body .= 'img#notification_image { width: 100%; max-width: 60px; }';
        $mail->Body .= '</style>';
        $mail->Body .= '</head>';
        $mail->Body .= '<body>';
        $mail->Body .= '<div id="page">';
        $mail->Body .= '<table width="100%" border="0">';
        $mail->Body .= '<tbody>';
        $mail->Body .= '<tr>';
        $mail->Body .= '<td width="30%" align="center">';
        $mail->Body .= '<img src="' . $params['image'] . '" alt="notification" id="notification_image" />';
        $mail->Body .= '</td>';
        $mail->Body .= '<td width="70%">';
        $mail->Body .= '<table width="100%">';
        $mail->Body .= '<tbody>';
        $mail->Body .= '<tr>';
        $mail->Body .= '<td width="50%" align="left">';
        $mail->Body .= '<strong>' . $params['from']['nome'] . '</strong>';
        $mail->Body .= '</td>';
        $mail->Body .= '<td width="50%" align="right">';
        $mail->Body .= '<strong>' . $params['data'] . '</strong>';
        $mail->Body .= '</td>';
        $mail->Body .= '</tr>';
        $mail->Body .= '<tr>';
        $mail->Body .= '<td colspan="2">';
        $mail->Body .= '<p>' . $params['descricao'] . '</p>';
        $mail->Body .= '</td>';
        $mail->Body .= '</tr>';
        $mail->Body .= '</tbody>';
        $mail->Body .= '</table>';
        $mail->Body .= '</td>';
        $mail->Body .= '</tr>';
        $mail->Body .= '</tbody>';
        $mail->Body .= '</table>';
        $mail->Body .= '</div>';
        $mail->Body .= '</body>';
        $mail->Body .= '</html>';
        $mail->send();
    }
}