<?php

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

class MemberList extends Engine {
  private $engine;

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

  public function index () {
    if (getPerms()['usr_ban'] != 1) return view('pages.site.notallowed', ['user' => $this->user]);
    $get = DB::table('users')->orderBy('id', 'desc')->paginate(10);
    setlocale(LC_ALL, 'ja_JP.utf8');

    foreach ($get as $g) {
      if (!is_null($g->id)) {
        $g->showName = '';
        $g->showCol = '';
        $g->perm_id = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $g->id)->first()->perm_id;

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

        $d = DB::table('usr_details')->select('reg_date')->where('user_id', $g->id)->first();
        $g->reg_date = date('Y年m月d日 H:i:s', $d->reg_date);

        if (!empty(userDetail($g->id))) {
          $det = userDetail($g->id);
          $g->showname = $det['showname'];
          $g->showcol = $det['showcol'];
          $g->avatar = $det['avatar'];
        }

        if (!empty($g->display_name)) $g->showname = $g->display_name;
        else $g->showname = $g->username;
    
        if (!empty($g->name_style)) $g->showcol = $g->name_style;
        else {
          foreach (getGroupColours() as $j) {
            if ($j->id == $g->perm_id) {
              if ($g->gender == 1) $g->showcol = $j->colour_m;
              else if ($g->gender == 2) $g->showcol = $j->colour_f;
              else $g->showcol = $j->colour_u;
            }
          }
        }
    
        $g->user = userDetail(null, $g->kero_token);
      }

      if ($g->gender == 1) $g->gender = '男';
      else if ($g->gender == 2) $g->gender = '女';
      else $g->gender = '？';
    }

    return view('pages.site.memberlist', ['res' => $get, 'user' => $this->user]);
  }
}
