<?php

namespace App\Http\Controllers;

use App\Models\Role;
use App\Models\SitePermis;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class RoleController extends Controller
{
    public function index(Request $request)
    {
        $page_size=$request->input('page_size',10);
        $num = ($request->input('page_index')-1)*$page_size;
        $data = Role::query();
        $total = $data->count();
        $list = $data->skip($num)->take($page_size)->get();
        return $this->success_list($list,$total);
    }

    public function create(Request $request)
    {
        $role = new Role();
        $role->name       =   $request->input('name');
        $role->desc      =   $request->input('desc');
        $role->permissions    =  $request->input('permissions');
        $res = $role->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    public function show(Request $request)
    {
        $id= $request->input('id');
        $show = Role::find($id);
        return $this->success($show);
    }

    public function update(Request $request)
    {
        $id= $request->input('id');
        $role = Role::find($id);
        $role->name         =   $request->input('name');
        $role->desc      =   $request->input('desc');
        $role->permissions  =  $request->input('permissions');
        $res = $role->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    public function all(Request $request)
    {
        $role = Role::pluck('name','id');
        return $this->success($role);
    }

    public function destroy(Request $request)
    {
        $id= $request->input('id');
        $res = Role::where('id',$id)->delete();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }
}
