<?php

namespace App\Http\Controllers;

use App\Http\Requests\EditAdminAuthInput;
use App\Models\Admin;
use App\Models\AdminAuth;
use App\Models\Auth;
use Login;
use Yo;

class AdminAuthController extends Controller
{
  public function select()
  {
    Login::admin();
    $admin_auth_list = AdminAuth::select('id', 'name', 'del')->where('del', 2)->orderBy('updated_at', 'desc')->get();
    $list = [];
    foreach ($admin_auth_list as $item) {
      $push = true;
      if ($item->del == 1) {
        $admin_count = Admin::where('admin_auth_id', $item->id)->where('del', 2)->count();
        if ($admin_count == 0) $push = false;
      }
      if ($push) {
        $list[] = [
          'value' => $item->id,
          'label' => $item->name,
          'disabled' => $item->del == 1,
        ];
      }
    }
    return Yo::echo([
      'list' => $list
    ]);
  }

  public function list()
  {
    Login::admin();
    $admin_auth_list = AdminAuth::where('del', 2)->orderBy('updated_at', 'desc')->get();
    $list = [];
    foreach ($admin_auth_list as $item) {
      $auth_ids_turn = [];
      foreach (json_decode($item->auth_ids, true) as $i) {
        $auth_ids_turn[] = intval($i);
      }
      $list[] = [
        'id' => $item->id,
        'name' => $item->name,
        'auth_ids' => $item->auth_ids,
        'auth_ids_turn' => $auth_ids_turn,
        'remark' => $item->remark,
      ];
    }
    return Yo::echo([
      'list' => $list
    ]);
  }

  public function delete()
  {
    Login::admin([5]);
    $ids = request()->post('ids');
    AdminAuth::whereIn('id', $ids)->update([
      'del' => 1
    ]);
    return Yo::delete_echo($ids);
  }

  public function update(EditAdminAuthInput $request)
  {
    Login::admin([5]);
    $id = request()->post('id');
    $name = $request->post('name');
    $auth_ids = $request->post('auth_ids');
    $remark = $request->post('remark');
    $auth_ids_arr = [];
    foreach ($auth_ids as $auth_id) $auth_ids_arr[] = (string)$auth_id;
    $auth_ids_str = json_encode($auth_ids_arr, JSON_UNESCAPED_UNICODE);
    if (mb_strlen($auth_ids_str) > 1000) Yo::error_echo(100014);
    $admin_auth = AdminAuth::find($id);
    if (!$admin_auth || $admin_auth->del !== 2) Yo::error_echo(100000, ['权限']);
    $admin_auth->name = $name;
    $admin_auth->auth_ids = $auth_ids_str;
    $admin_auth->remark = $remark ?? '';
    $admin_auth->save();
    return Yo::update_echo($admin_auth->id);
  }

  public function create(EditAdminAuthInput $request)
  {
    Login::admin([5]);
    $name = $request->post('name');
    $auth_ids = $request->post('auth_ids');
    $remark = $request->post('remark');
    $auth_ids_arr = [];
    foreach ($auth_ids as $auth_id) $auth_ids_arr[] = (string)$auth_id;
    $auth_ids_str = json_encode($auth_ids_arr, JSON_UNESCAPED_UNICODE);
    if (mb_strlen($auth_ids_str) > 1000) Yo::error_echo(100014);
    $admin_auth = new AdminAuth();
    $admin_auth->name = $name;
    $admin_auth->auth_ids = $auth_ids_str ?? '[]';
    $admin_auth->remark = $remark ?? '';
    $admin_auth->save();
    return Yo::create_echo($admin_auth->id);
  }
}
