<?php

namespace App\Http\Controllers;

use App\Http\Requests\EditAuthInput;
use App\Models\Auth;
use Yo;
use Login;

class AuthController extends Controller
{
  public function list()
  {
    Login::admin();
    $group = Auth::select('*')
      ->where('type', 1)->where('del', 2)
      ->orderBy('order', 'desc')->get();
    $list = [];
    foreach ($group as $item) {
      $auth_list = Auth::select('*')->where('pid', $item->id)
        ->where('type', 2)->where('del', 2)
        ->orderBy('order', 'desc')->get();
      $item['children'] = $auth_list;
      $list[] = $item;
    }
    return Yo::echo([
      'list' => $list,
    ]);
  }

  public function delete()
  {
    Login::admin([9]);
    $id = request()->post('id');
    $auth = Auth::where('id', $id)->where('del', 2)->first();
    if (!$auth) Yo::error_echo(100000, ['路由']);
    $auth->del = 1;
    $auth->save();
    if ($auth->pid == 0) {
      Auth::where('pid', $id)->where('del', 2)->update([
        'del' => 1
      ]);
    }
    return Yo::delete_echo($id);
  }

  public function update(EditAuthInput $request)
  {
    Login::admin([9]);
    $id = $request->post('id');
    $name = $request->post('name');
    $title = $request->post('title');
    $icon = $request->post('icon');
    $pid = $request->post('pid');
    $check_type = $request->post('check_type');
    $show = $request->post('show');
    $status = $request->post('status');
    $order = $request->post('order');
    $auth = Auth::where('id', $id)->where('del', 2)->first();
    if (!$auth) Yo::error_echo(100000, ['路由']);
    $type = $auth->type;
    if ($auth->pid != $pid) {
      if ($auth->pid == 0) {
        $s_auth = Auth::where('pid', $id)->where('del', 2)->count();
        if ($s_auth > 0) Yo::error_echo(100023);
        $type = 2;
      } else {
        if ($pid != 0) {
          $p_auth = Auth::where('id', $pid)->where('pid', 0)->where('del', 2)->first();
          if (!$p_auth) Yo::error_echo(100000, ['分组']);
          $type = 2;
        }
      }
    }
    $auth->name = $name;
    $auth->title = $title;
    $auth->icon = $icon ?? '';
    $auth->pid = $pid;
    $auth->type = $type;
    $auth->check_type = $check_type;
    $auth->show = $show;
    $auth->status = $status;
    $auth->order = $order;
    $auth->save();
    return Yo::update_echo($auth->id);
  }

  public function create(EditAuthInput $request)
  {
    Login::admin([9]);
    $name = $request->post('name');
    $title = $request->post('title');
    $icon = $request->post('icon');
    $pid = $request->post('pid');
    $check_type = $request->post('check_type');
    $show = $request->post('show');
    $status = $request->post('status');
    $order = $request->post('order');
    $type = 1;
    if ($pid != 0) {
      $p_auth = Auth::where('id', $pid)->where('pid', 0)->where('del', 2)->first();
      if (!$p_auth) Yo::error_echo(100000, ['路由']);
      $type = 2;
    }
    $auth = new Auth();
    $auth->name = $name;
    $auth->title = $title;
    $auth->icon = $icon ?? '';
    $auth->pid = $pid;
    $auth->type = $type;
    $auth->check_type = $check_type;
    $auth->show = $show;
    $auth->status = $status;
    $auth->order = $order;
    $auth->save();
    return Yo::create_echo($auth->id);
  }

  public function select()
  {
    Login::admin();
    $group = Auth::select('id', 'title')
      ->where('type', 1)->where('del', 2)
      ->orderBy('order', 'desc')->get();
    $list = [];
    foreach ($group as $item) {
      $auth_list = Auth::select('id', 'title')->where('pid', $item->id)
        ->where('type', 2)->where('check_type', 2)->where('del', 2)
        ->orderBy('order', 'desc')->get();
      if (count($auth_list) !== 0) $list[] = [
        "id" => $item->id,
        "title" => $item->title,
        "children" => $auth_list
      ];
    }
    return Yo::echo([
      'list' => $list,
    ]);
  }
}
