<?php

namespace App\Http\Controllers\Home;
use Illuminate\Support\Facades\DB;
// use Illuminate\Support\Facades\Log;

class Index {
  private $valid;

  public function __construct () {
    $this->valid = getPerms();
  }

  public function index () {
    $get = DB::table('blg_content')->where('isPost', 1);
    if ($this->valid['blg_addpost'] == 0 && $this->valid['blg_editpost'] == 0) $get = $get->where('public_status', 0);
    $get = $get->orderBy('publish_date', 'desc')->paginate(10);
    setlocale(LC_ALL, 'ja_JP.utf8');

    return view('pages.site.index', ['res' => $get]);
  }

  public function page ($slug) {
    $sel = array('id', 'title', 'slug', 'isMenu', 'public_status', 'message');
    $res = DB::table('blg_content')->select($sel);
    if ($this->valid['blg_editpage'] == 0) $res = $res->where('public_status', 0);
    $res = $res->where('isPost', 0)->where('slug', $slug)->orderBy('sortorder', 'asc')->first();

    if (!$res) return view('pages.site.notfound');
    return view('pages.site.page', ['res' => $res]);
  }

  public function post ($slug) {
    $get = DB::table('blg_content');
    if ($this->valid['blg_editpost'] == 0) $get = $get->where('public_status', 0);
    $get = $get->where('isPost', 1)->where('slug', $slug)->first();
    if (!$get) return view('pages.site.notfound');

    $ucol = getGroupColours();
    setlocale(LC_ALL, 'ja_JP.utf8');

    $get->showName = '';
    $get->showCol = '';
    $get->comments = array();
    $get->comments['come'] = DB::table('blg_comments')->where('post_id', $get->id)->orderBy('id', 'asc')->get()->toArray();
    $get->comments['total'] = count($get->comments['come']);
    $get->username = DB::table('users')->select('username')->where('id', $get->user_id)->first()->username;
    $get->perm_id = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $get->user_id)->first()->perm_id;

    $p = DB::table('usr_profile')->select('gender', 'avatar', 'name_style', 'display_name')->where('user_id', $get->user_id)->first();
    $get->gender = $p->gender;
    $get->avatar = $p->avatar;
    $get->name_style = $p->name_style;
    $get->display_name = $p->display_name;

    foreach ($get->comments['come'] as $k => $c) {
      $c->replyCount = 0;
      $c->isvideo = 'f';
      $c->src = 'TS';

      if (!is_null($c->user_id) && !empty(userDetail($c->user_id))) {
        $det = userDetail($c->user_id);
        $c->user_id = $det['user_id'];
        $c->showname = $det['showname'];
        $c->showcol = $det['showcol'];
        $c->avatar = $det['avatar'];
      }

      
      unset($c->email);
      $c->created = date('Y年m月d日 H:i:s', $c->created);
    }

    if (!empty($get->display_name)) $get->showname = $get->display_name;
    else $get->showname = $get->username;

    if (!empty($get->name_style)) $get->showcol = $get->name_style;
    else {
      foreach ($ucol as $j) {
        if ($j->id == $get->perm_id) {
          if ($get->gender == 1) $get->showcol = $j->colour_m;
          else if ($get->gender == 2) $get->showcol = $j->colour_f;
          else $get->showcol = $j->colour_u;
        }
      }
    }

    $get->user = userDetail(null, (isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : ''));
    $get->post_date = strftime('%Y/%m/%d(%a) %H:%M:%S %Z', $get->post_date);
    $get->publish_date = strftime('%Y/%m/%d(%a) %H:%M:%S %Z', $get->publish_date);

    return view('pages.site.post', ['res' => $get]);
  }
}
