<?php

namespace App\Http\Controllers\Video;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Engine;
// use Illuminate\Support\Facades\Log;

class Game extends Engine {
  private $engine;

  public function __construct () {
    $this->engine = new Engine;
  }

  public function index () {
    $res = DB::table('vid_game')->get();
    if (!$res) return view('pages.site.notfound');
    $new = DB::table('vid_video')->orderBy('publish_date', 'desc')->limit(8)->get();
    $pop = DB::table('vid_video')->orderBy('view', 'desc')->orderBy('publish_date', 'desc')->limit(8)->get();

    return view('pages.site.video.game', ['res' => $res, 'new' => $new, 'pop' => $pop, 'err' => $this->engine->err, 'suc' => $this->engine->suc]);
  }

  public function add (Request $r) {
    if ($this->engine->user && $this->engine->user->perm['blg_addpost']) {
      $err = '';
      $res = '';
      $frm = array('name' => '', 'slug' => '');

      if (isset($r->submit)) {
        // フォームの値を保存して
        $frm['platform_id'] = 0;
        $frm['name'] = $r->name;
        $frm['slug'] = $r->slug;

        // 件名、文章又はスラッグがなければ、エラーを出て
        $verify = array('ゲーム名' => $r->name, 'スラッグ' => $r->slug);
        $incomplete = array();
        foreach ($verify as $k => $v) { if (is_null($v) || empty($v) || !isset($v)) $incomplete[] = $k; }
        if (count($incomplete) > 0) $err = implode('、', $incomplete).'をご入力下さい。';

        // スラッグが既に存在したら、エラーを出て
        if (empty($err)) {
          $sl = DB::table('vid_game')->select('slug')->where('slug', $r->slug)->first();
          if ($sl && $sl->slug == $r->slug) $err = 'このスラッグがもう存在しています。';
        }

        // エラーがあったら、フォームに戻って。なければ、データベースに保存したり、ページ又はポストへ移転して
        if (!empty($err)) return view('pages.site.video.game.add', ['res' => $res, 'err' => $err, 'frm' => $frm]);
        else {
          // できたの？
          if ($res = DB::table('vid_game')->insert($frm)) return redirect('/video/'.$r->slug);
          else {
            // やれやれ…
            return view('pages.site.video.game.add', ['res' => $res, 'err' => $err, 'frm' => $frm]);
          }
        }
      }

      return view('pages.site.video.game.add', ['res' => $res, 'err' => $err, 'frm' => $frm]);
    }

    return view('pages.site.notallowed');
  }

  public function delete (Request $r) {
    if ($this->engine->user->perm['blg_delpost']) {
      return DB::table('vid_game')->where('slug', $r->slug)->delete();
    }

    return '許可がありません。';
  }

  public function edit (Request $r) {
    if ($this->engine->user && $this->engine->user->perm['blg_addpost']) {
      $frm = DB::table('vid_game')->where('slug', $r->slug)->first();
      if (!$frm) return view('pages.site.notfound');
      $err = '';
      $res = '';

      if (isset($r->submit)) {
        // フォームの値を保存して
        $frm->name = $r->name;
        $frm->slug = $r->slug;

        // 件名、文章又はスラッグがなければ、エラーを出て
        $verify = array('ゲーム名' => $r->name, 'スラッグ' => $r->slug);
        $incomplete = array();
        foreach ($verify as $k => $v) { if (is_null($v) || empty($v) || !isset($v)) $incomplete[] = $k; }
        if (count($incomplete) > 0) $err = implode('、', $incomplete).'をご入力下さい。';

        // エラーがあったら、フォームに戻って。なければ、データベースに保存したり、ページ又はポストへ移転して
        if (!empty($err)) return view('pages.site.video.game.edit', ['res' => $res, 'err' => $err, 'frm' => $frm]);
        else {
          // できたの？
          if ($res = DB::table('vid_game')->where('slug', $r->slug)->update((array)$frm)) return redirect('/video/'.$r->slug);
          else {
            // やれやれ…
            $err = '編集失敗…';
            return view('pages.site.video.game.edit', ['res' => $res, 'err' => $err, 'frm' => $frm]);
          }
        }
      }

      return view('pages.site.video.game.edit', ['res' => $res, 'err' => $err, 'frm' => $frm]);
    }

    return view('pages.site.notallowed');
  }
}
