<?php
namespace App\Models;

class Post extends \Core\Model
{
    public function __construct()
    {
        // Create table for posts
        $this->createTable(
            [
                'id int(3) NOT NULL AUTO_INCREMENT',
                'category int(3) NOT NULL',
                'created_at timestamp NOT NULL DEFAULT "00-00-00 00:00:00"',
                'valid_at timestamp NOT NULL DEFAULT "00-00-00 00:00:00"',
                'expired_at timestamp NOT NULL DEFAULT "00-00-00 00:00:00"',
                'creator int(3) NOT NULL',
                'edited_at timestamp DEFAULT 0',
                'editor int(3)',
                'content varchar(255) NOT NULL',
                'status tinyint(1) NOT NULL DEFAULT 1',
                'PRIMARY KEY (id)'
            ]
        );

        // Create table for categories
        $this->createTable(
            [
                'id int(3) NOT NULL AUTO_INCREMENT',
                'category varchar(20) NOT NULL',
                'background char(7) NOT NULL DEFAULT "#ffffff"',
                'foreground char(7) NOT NULL DEFAULT "#000000"',
                'status tinyint(1) NOT NULL DEFAULT 1',
                'PRIMARY KEY (id)'
            ],
            'kategori'
        );
    }

    protected function createTable($fields, $table = 'pengumuman') {
        try {
            if (empty($fields)) {
                return false;
            }
            $sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";

            $db = static::connectDB();
            $query = $db->prepare($sql);

            $query->execute();
            return true;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage(), 444);
        }
    }

    protected function dropTable($table = 'pengumuman') {
        try {
            $sql = "DROP TABLE IF EXISTS {$table}";

            $db = static::connectDB();
            $query = $db->prepare($sql);
            $query->execute();
            return true;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage(), 444);
        }
    }

    public function showAll($conditions = [], $table = 'pengumuman')
    {
        try {
            $db = static::connectDB();

            $sql = "SELECT * FROM {$table}";

            if ($conditions) {
                $sql .= " WHERE";
                foreach ($conditions as $condition) {

                    $keys[] = $condition[0];
                    $operators[] = $condition[1];
                    $values[] = $condition[2];
                }

                $x = 0;
                foreach ($keys as $key) {
                    $sql .= " $key $operators[$x] ?";
                    $x++;
                    if ($x < count($keys)) {
                        $sql .= " AND";
                    }
                }
            }


            $query = $db->prepare($sql);

            if (count($conditions)) {
                $x = 1;
                foreach ($values as $value) {
                    $query->bindValue($x, $value);
                    $x++;
                }
            }

            $query->execute();
            if ($query->rowCount() == 1) {
                $result = $query->fetch(\PDO::FETCH_ASSOC);
            } elseif ($query->rowCount() > 1) {
                $result = $query->fetchAll(\PDO::FETCH_ASSOC);
            } else {
                return false;
            }
            return $result;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage, 444);
        }
    }

    public function showJoin($conditions = [])
    {
        try {
            $db = static::connectDB();

            $sql = "SELECT pengumuman.id, kategori.background, kategori.foreground, pengumuman.valid_at, pengumuman.expired_at, pengumuman.creator, pengumuman.editor, pengumuman.content, pengumuman.status FROM pengumuman INNER JOIN kategori ON pengumuman.category=kategori.id";

            if ($conditions) {
                $sql .= " WHERE";
                foreach ($conditions as $condition) {

                    $keys[] = $condition[0];
                    $operators[] = $condition[1];
                    $values[] = $condition[2];
                }

                $x = 0;
                foreach ($keys as $key) {
                    $sql .= " $key $operators[$x] ?";
                    $x++;
                    if ($x < count($keys)) {
                        $sql .= " AND";
                    }
                }
            }

            $query = $db->prepare($sql);
            if (count($conditions)) {
                $x = 1;
                foreach ($values as $value) {
                    $query->bindValue($x, $value);
                    $x++;
                }
            }

            $query->execute();
            if ($query->rowCount() == 1) {
                $result = $query->fetch(\PDO::FETCH_ASSOC);
            } elseif ($query->rowCount() > 1) {
                $result = $query->fetchAll(\PDO::FETCH_ASSOC);
            } else {
                return false;
            }
            return $result;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage(), 444);
        }

    }

    public function entry($args, $table = 'pengumuman')
    {
        if (count($args)) {
            $keys = '`'.implode('`, `', array_keys($args)).'`';
            $values = '';

            // This is if want to insert multiple rows
            foreach ($args as $key => $val) {
                if (preg_match('/,/', $val)) {
                    $val = explode(',', $val);
                    $args[$key] = $val;
                }
            }

            $x = 1;
            foreach ($args as $field) {
                if ($x === 1) {
                    $values .= '(';
                }
                $values .= '?';
                if ($x < count($args)) {
                    $values .= ', ';
                } else {
                    $values .= ')';
                }
                $x++;
            }

            try {
                $sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}";

                $db = static::connectDB();

                $query = $db->prepare($sql);

                $x = 1;
                foreach ($args as $value) {
                    if (is_array($value)) {
                        foreach ($value as $vals) {
                            $query->bindValue($x, $vals);
                            $x++;
                        }
                    } else {
                        $query->bindValue($x, $value);
                        $x++;
                    }
                }

                $query->execute();
                return true;
            } catch (PDOException $e) {
                throw new \Exception($e->getMessage(), 444);
            }
        }
        return false;
    }

    public function update($args, $id, $table = 'pengumuman')
    {
        if (count($args)) {
            $keys = array_keys($args);

            $fields = [];
            foreach ($keys as $key) {
                $fields[] = $key.' = ?';
            }

            if (count($fields) > 1) {
                $fields = implode(', ', $fields);
            } else {
                $fields = implode('', $fields);
            }

            try {
                $db = static::connectDB();

                $result = $this->showAll([
                    ['id', '=', $id]
                ]);

                $sql = "UPDATE {$table} SET {$fields} WHERE id = ?";

                $query = $db->prepare($sql);
                $x = 1;
                foreach ($args as $value) {
                    $query->bindValue($x, $value);
                    $x++;
                }
                $query->bindValue($x, $id);

                $query->execute();
                return true;
            } catch (PDOException $e) {
                throw new \Exception($e->getMessage(), 444);
            }
        }
        return false;
    }

    public function delete($id, $status, $table = 'pengumuman')
    {
        try {
            $db = static::connectDB();

            $sql = "UPDATE {$table} SET status = ? WHERE id = ?";

            $query = $db->prepare($sql);
            $query->bindValue(1, $status);
            $query->bindValue(2, $id);

            $query->execute();
            return true;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage(), 444);
        }
    }
}
