<?php

namespace Dao;

use Conn\MySqlConnect;

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

    public $id;
    public $titulo;
    public $zero_800;
    public $whatsapp;
    public $facebook;
    public $instagram;
    public $access_token;
    public $email;
    public $endereco;

    function __construct($res = null) {
        $this->id = (int) 0;
        $this->titulo = (string) '';
        $this->zero_800 = (string) '';
        $this->whatsapp = (string) '';
        $this->facebook = (string) '';
        $this->instagram = (string) '';
        $this->access_token = (string) '';
        $this->email = (string) '';
        $this->endereco = (string) '';
        $this->fill($res);
    }

    private function fill($res) {
        if ($res != null && $res instanceof \stdClass) {
            $this->id = (int) (isset($res->id) ? $res->id : 0);
            $this->titulo = (string) (isset($res->titulo) ? $res->titulo : '');
            $this->zero_800 = (string) (isset($res->zero_800) ? $res->zero_800 : '');
            $this->whatsapp = (string) (isset($res->whatsapp) ? $res->whatsapp : '');
            $this->facebook = (string) (isset($res->facebook) ? $res->facebook : '');
            $this->instagram = (string) (isset($res->instagram) ? $res->instagram : '');
            $this->access_token = (string) (isset($res->access_token) ? $res->access_token : '');
            $this->email = (string) (isset($res->email) ? $res->email : '');
            $this->endereco = (string) (isset($res->endereco) ? $res->endereco : '');
        }
    }

    private function inserir(MySqlConnect $conecta) {
        $sql = $conecta->prepare("INSERT INTO configuracoes (titulo, zero_800, whatsapp, facebook, instagram, access_token, email, endereco) VALUES (?, ?, "
                . "?, ?, ?, ?, ?, ?)") or die($conecta->error);
        $sql->bind_param("ssssssss", $this->titulo, $this->zero_800, $this->whatsapp, $this->facebook, $this->instagram, $this->access_token, $this->email, 
                $this->endereco);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Configurações ajustadas 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 configuracoes SET titulo = ?, zero_800 = ?, whatsapp = ?, facebook = ?, instagram = ?, access_token = ?, email = ?, "
                . "endereco = ? WHERE id = ?") or die($conecta->error);
        $sql->bind_param("ssssssssi", $this->titulo, $this->zero_800, $this->whatsapp, $this->facebook, $this->instagram, $this->access_token, $this->email, 
                $this->endereco, $this->id);
        if ($sql->execute())
            return json_encode(['data' => 'Success', 'message' => 'Configurações ajustadas 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 getConfiguracoes(MySqlConnect $conecta) {
        $sql = $conecta->query("SELECT * FROM configuracoes ORDER BY id DESC LIMIT 1") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->fill($res);
        }
    }
}