<?php
namespace App\Controllers;
SESSION_START();
use \Core\View;
use \Core\Menu;
use \Core\Form;
use \Core\Inputs;
use App\Models\Homem;
class User extends \Core\Controller
{
    private static function styles()
    {
        $a = '
                <head>
                    <meta charset="UTF-8" />
                    <meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover" />
                    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
                    <title>GRASPORT - NURTURING TALENT</title>
                    <link rel="stylesheet" href="/grasport/bootstrap-4.3.1-dist/css/bootstrap.min.css" />
                    <link rel="stylesheet" href="/grasport/css/font-awesome.min.css" />
                    <link rel="stylesheet" href="/grasport/css/animate.css" />
                    <link rel="stylesheet" href="/grasport/css/lightbox.css" />
                    <link rel="stylesheet" href="/grasport/css/owl.carousel.css" />
                    <link rel="stylesheet" href="/grasport/css/owl.theme.default.css" />
                    <link rel="stylesheet" href="/grasport/css/arrow.css" />
                    <link rel="stylesheet" href="/grasport/css/fixed.css" />
                    <link rel="stylesheet" href="/grasport/css/waypoints.css" />
                    <link rel="stylesheet" href="/grasport/css/style.css" />
                </head>
            ';
            return  $a;
    }
    private static function scripts()
    {
        $a = '
            <script src="/grasport/js/jquery-3.3.1.min.js"></script>
            <script src="/grasport/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
            <script src="https://use.fontawesome.com/releases/v5.10.2/js/all.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
            <script src="/grasport/js/custom.js"></script>
            <script src="/grasport/js/jquery.waypoints.min.js"></script>
            <script src="/grasport/js/waypoints.js"></script>
            <script src="/grasport/js/lightbox.js"></script>
            <script src="/grasport/js/owl.carousel.js"></script>
            <script src="/grasport/js/jquery.counterup.js"></script>
            <script src="/grasport/js/validator.js"></script>
            <script src="/grasport/js/contact.js"></script>
            <script>$(".navbar, .dropdown-menu").addClass("solid");</script>
        ';
        return $a;
    }
    private static function script()
    {
        $a = '
            <div id="contact" class="offset">
              <footer>
                <div class="row">
                  <hr class="socket" />
                    Grasport International &copy; 2020 all rights reserved.
                </div>
              </footer>
            </div>
            <a href="#home" class="top-scroll">
              <i class="fas fa-angle-up"></i>
            </a>
            <script src="/grasport/js/jquery-3.3.1.min.js"></script>
            <script src="/grasport/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
            <script src="https://use.fontawesome.com/releases/v5.10.2/js/all.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
            <script src="/grasport/js/custom.js"></script>
            <script src="/grasport/js/jquery.waypoints.min.js"></script>
            <script src="/grasport/js/waypoints.js"></script>
            <script src="/grasport/js/lightbox.js"></script>
            <script src="/grasport/js/owl.carousel.js"></script>
            <script src="/grasport/js/jquery.counterup.js"></script>
            <script src="/grasport/js/validator.js"></script>
            <script src="/grasport/js/contact.js"></script>
            <script>$(".navbar, .dropdown-menu").addClass("solid");</script>
        ';
        return $a;
    }
    private static function loginmenu()
    {
        $a = '
        <nav class="navbar navbar-expand-lg fixed-top">
          <div class="container-fluid">
            <a class="navbar-brand" href="/user/home"><img src="/grasport/img/Grasport.png" alt=""/></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive">
              <span class="custom-toggler-icon"><i class="fas fa-bars"></i></span>
            </button>
  
            <div class="collapse navbar-collapse" id="navbarResponsive">
              <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                  <a class="nav-link" href="/user/home">Home</a>
                </li>
                <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" id="dropdown1" aria-haspopup="true" aria-expanded="false">Blog</a>
                  <ul class="first dropdown-menu" aria-labelledby="dropdown1">
                    <li><a class="dropdown-item" href="/user/postcat">Blog Post Category</a></li>
                    <li><a class="dropdown-item" href="/user/blog">Post a Blog</a></li>
                  </ul>
                </li>  
                <li class="nav-item">
                  <a class="nav-link" href="/user/newuser">Create User</a>
                </li>  
                <li class="nav-item">
                  <a class="nav-link" href="/user/career">Career</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="/user/Contactus">Contact Us</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="/home/logout">Logout</a>
                </li>
              </ul>
            </div>
          </div>
        </nav>
        ';
        return $a;
    }
    public static function home()
    {
      if($_SESSION['user_id'] != '')
      {
        View::render("Grasport/User/home.php", ['style'=>static::styles(), 'menu'=>static::loginmenu(), 'script'=>static::script()]);
      }
    }
    public static function blog()
    {
      if(isset($_GET['deleteid']))
      {
        Homem::update("UPDATE `blog_posts` SET `status`='0', `editdate`='' WHERE `post_id`='$_GET[deleteid]' AND `fk_user_id`='$_SESSION[user_id]'");
      }
      if($_SESSION['user_id'] != '')
      {
        $data = Homem::getdata("SELECT * FROM `blog_posttype`");
        $cat = Inputs::select('cat', Inputs::optionsdb($data, 'id', 'name'), Inputs::options([''], ['Select Post Category']), '', '');
        $posts = Form::dbtableed(Homem::getdata("SELECT * FROM `blog_posts` WHERE `fk_user_id`='$_SESSION[user_id]' AND `status`='1' ORDER BY `post_id` DESC"), ['List of Posts Created', 'Created Date'], ['message', 'regdate'], '/user/blog', '', 'post_id');
        View::render("Grasport/User/blog.php", ['style'=>static::styles(), 'menu'=>static::loginmenu(), 'script'=>static::script(), 'cat'=>$cat, 'posts'=>$posts]);
      }
    }
    public static function saveblog()
    {
      if($_SESSION['user_id'] != '')
      {
        if(isset($_POST['body']))
        {
          $image = '';
          $file = '';
          if($_FILES['image']['name'] != '')
          {
            if(!is_file('grasport/blogposts/'.$_FILES['image']['name']))
            {
              @move_uploaded_file($_FILES['image']['tmp_name'], 'grasport/blogposts/'.$_FILES['image']['name']);
              $image = $_FILES['image']['name'];
            }
            else
            {
              echo '<script>window.location="/user/blog?status=0&message='.$_POST['body'].'&url='.$_POST['url'].'";</script>';
            }
          }
          if($_FILES['file']['name'] != '')
          {
            if(!is_file('grasport/bloguploads/'.$_FILES['file']['name']))
            {
              @move_uploaded_file($_FILES['file']['tmp_name'], 'grasport/bloguploads/'.$_FILES['file']['name']);
              $file = $_FILES['file']['name'];
            }
            else
            {
              echo '<script>window.location="/user/blog?status=0&message='.$_POST['body'].'&url='.$_POST['url'].'";</script>';
            }
          }
          // Homem::insert("INSERT INTO `blog_posts` (`post_id`, `fk_user_id`, `fk_posttype_id`, `image`, `atitle`, `message`, `url`, `file`, `regdate`, `editdate`, `status`) VALUES 
          // (NULL, '$_SESSION[user_id]', '$_POST[cat]', '$image', $_POST[title], '$_POST[body]', '$_POST[url]', '$file', '$_POST[date]', '', '1')");
          Homem::insert("INSERT INTO `blog_posts` (`post_id`, `fk_user_id`, `fk_posttype_id`, `image`, `title`, `message`, `url`, `file`, `regdate`, `editdate`, `status`) VALUES 
          (NULL, '$_SESSION[user_id]', '$_POST[cat]', '$image', '$_POST[title]', '$_POST[body]', '$_POST[url]', '$file', '$_POST[date]', '', '1')");
          echo '<script>window.location="/user/blog?status=1"</script>';
        }
      }
    }
    public static function postcat()
    {
      if($_SESSION['user_id'] != '')
      {
        $data = Homem::getdata("SELECT * FROM `blog_posttype`");
        $table = Form::dbtable($data, ['List of Availbale Blog Post Categorys'], ['name']);
        $a = '<!DOCTYPE html>
              <html lang="en">';      
        $a .= static::styles();
        $a .= '<body data-spy="scroll" data-target="#navbarResponsive">
                <div id="home">';
        $a .= static::loginmenu();
        $a .= '
            <div class="container" style="margin-top:100px;">
            <div class="container-fluid">
              <div class="row">
                <div class="col-xl-2">
                </div>
                <div class="col-xl-8">
                <form method="POST" action="/user/savepostcat" enctype="multipart/form-data">
                <div class="card">
                  <div class="card-body">
                    <div class="row">
                      <div class="col-xl-12">
                      <div class="form-group">
                        Create New Blog Category
                      </div>
                      <div class="form-group">
                        <input type="text" id="name" name="name" class="form-control" placeholder="New category" required />
                      </div>
                      <div class="form-group">
                        <div class="float-right">
                          <input type="submit" id="submit" name="submit" class="btn btn-primary" required />
                          <br>
                        </div>
                      </div>
                      </div>
                    </div>
                  </div>
                </div>
                </form>
                <div class="card">
                  <div class="card-body">
                    <div class="row">
                      <div class="col-xl-12">
                      '.$table.'
                      </div>
                    </div>
                  </div>
                </div>
                </div>
                <div class="col-xl-2">
                </div>
              </div>
            </div>
          </div>
        </div>   
        ';
        $a .= static::script();
        $a .= '</body>
              </html>';
        echo $a;
      }
    }
    public static function savepostcat()
    {
      if($_SESSION['user_id'] != '')
      {
        if(isset($_POST['name']))
        {
          Homem::insert("INSERT INTO `blog_posttype`(`name`) VALUES ('$_POST[name]')");
          echo '<script>window.location="/user/postcat"</script>';
        }
        else
        {
          echo '<script>window.location="/user/postcat"</script>';
        }
      }
    }
    public static function newuser()
    {
      if(isset($_GET['deleteid']))
      {
        Homem::update("UPDATE `user_details` SET `status`='0' WHERE `user_id`='$_GET[deleteid]'");
      }
      if($_SESSION['user_id'] != '')
      {
        $data = Homem::getdata("SELECT * FROM `user_roles`");
        $user = Inputs::select('user', Inputs::optionsdb($data, 'id', 'name'), Inputs::options([''], ['Select User Role']), '', '');
        $posts = Form::dbtableed(Homem::getdata("SELECT * FROM `user_details` WHERE `created_by`='$_SESSION[user_id]' AND `status`='1'"), ['Name', 'Email', 'Mobile'], ['name', 'email', 'mobile'], '/user/newuser', '', 'user_id');
        View::render("Grasport/User/newuser.php", ['style'=>static::styles(), 'menu'=>static::loginmenu(), 'script'=>static::script(), 'user'=>$user, 'posts'=>$posts]);
      }
    }
    public static function savenewuser()
    {
      if($_POST['name'] != '')
      {
        Homem::insert("INSERT INTO `user_details` (`salutation`, `name`, `email`, `mobile`, `designation`, `password`, `fk_userrole_id`, `created_by`, `regdate`, `status`) VALUES 
        ('$_POST[title]', '$_POST[name]', '$_POST[email]', '$_POST[mobile]', '$_POST[user]', '$_POST[password]', '$_POST[user]', '$_SESSION[user_id]', '$_POST[date]', '1')");
        echo '<script>window.location="/user/newuser"</script>';
      }
    }
    public static function career()
    {
      if(isset($_GET['deleteid']))
      {
        Homem::update("UPDATE `career` SET `status`='0' WHERE `id`='$_GET[deleteid]'");
      }
      if($_SESSION['user_id'] != '')
      {
        $posts = Form::dbtableed(Homem::getdata("SELECT * FROM `career` WHERE `status`='1'"), ['Name', 'Email', 'Mobile'], ['name', 'email', 'mobile'], '/user/career', '', 'id');
        View::render("Grasport/User/career.php", ['style'=>static::styles(), 'menu'=>static::loginmenu(), 'script'=>static::script(), 'posts'=>$posts]);
      }
    }
    public static function contactus()
    {
      if(isset($_GET['deleteid']))
      {
        Homem::update("UPDATE `contactus` SET `status`='0' WHERE `id`='$_GET[deleteid]'");
      }
      if($_SESSION['user_id'] != '')
      {
        $posts = Form::dbtableed(Homem::getdata("SELECT * FROM `contactus` WHERE `status`='1'"), ['Name', 'Email', 'Mobile'], ['name', 'email', 'mobile'], '/user/contactus', '', 'id');
        View::render("Grasport/User/contactus.php", ['style'=>static::styles(), 'menu'=>static::loginmenu(), 'script'=>static::script(), 'posts'=>$posts]);
      }
    }
    public static function test()
    {
      if($_SESSION['user_id'] != '')
      {
        $a = '<!DOCTYPE html>
              <html lang="en">';      
        $a .= static::styles();
        $a .= '<body data-spy="scroll" data-target="#navbarResponsive">
                <div id="home">';
        $a .= static::loginmenu();
        $a .= '
            <div class="container" style="margin-top:100px;">
            <div class="container-fluid">
              <div class="row">
                <div class="col-xl-2">
                </div>
                <div class="col-xl-8">
                <form method="POST" action="/home/auth" enctype="multipart/form-data">
                <div class="card">
                  <div class="card-body">
                    <div class="row">
                      <div class="col-xl-12">
                      <div class="form-group">
                        Create New Blog Category
                      </div>
                      <div class="form-group">
                        <input type="text" id="name" name="name" class="form-control" placeholder="New category" required />
                      </div>
                      <div class="form-group">
                      <div class="float-right">
                      <input type="submit" id="submit" name="submit" class="btn btn-primary" required />
                      
                      </div>
                      </div>
                      </div>
                    </div>
                  </div>
                </div>
                </form>
                </div>
                <div class="col-xl-2">
                </div>
              </div>
            </div>
          </div>
        </div>   
        ';
        $a .= static::scripts();
        $a .= '</body>
              </html>';
        echo $a;
      }
    }
}