<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Site;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class SiteController extends Controller
{

    public function index(Request $request)
    {
        $admin_id=Auth::user()->id;
        $role_id=Admin::where('id',$admin_id)->value('role_id');
        $version= $request->input('version',1);
        $page_size= $request->input('page_size',10);
        $num = ($request->input('page_index')-1)*$page_size;
        $data= Site::query()->where('version',$version);
        if(!$role_id){
            return $this->error('450001','账号未分配角色');
        }
        if($role_id != 1){
            $where_role = ','.$role_id.',';
            $data->where('limit_role','like','%'.$where_role.'%');
        }
        $total= $data->count();
        $list= $data->skip($num)->take($page_size)->get();
        return $this->success_list($list,$total);
    }

    public function create(Request $request)
    {
        $rules=[
            'name' => 'required',
            'url' => 'required',
            'bg_img' => 'required',
        ];
        $messages = [
            'name.required' => '名称为必填项.',
            'url.required' => '链接为必填项.',
            'bg_img.required' => '图标为必填项.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('450001',$validator->errors()->first());
        }
        $site = new Site();
        $site->name = $request->input('name');
        $site->url = $request->input('url');
        $site->status = 1;
        $site->bg_img = $request->input('bg_img');
        $site->version = $request->input('version');
        $site->limit_role = self::role($request->input('limit_role'));
        $res=$site->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    protected static function role($limit_role = null){
        if(isset($limit_role)){
            $role = implode(',',$limit_role);
            return ','.$role.',';
        }
       return null;
    }

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

    public function update(Request $request, Site $site)
    {
        $rules=[
            'name' => 'required',
            'url' => 'required',
            'bg_img' => 'required',
        ];
        $messages = [
            'name.required' => '名称为必填项.',
            'url.required' => '链接为必填项.',
            'bg_img.required' => '图标为必填项.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('450001',$validator->errors()->first());
        }
        $site = Site::find($request->input('id'));
        $site->name = $request->input('name');
        $site->url = $request->input('url');
        $site->status = 1;
        $site->bg_img = $request->input('bg_img');
        $site->version = $request->input('version');
        $site->limit_role = self::role($request->input('limit_role'));
        $res=$site->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

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

    public function uploadImg(Request $request)
    {
        $path_url = 'image/site';
        $result = $request->file('img')->store($path_url);
        if($result) {
            $path=Storage::url($result);
            return $this->success($path);
        }
        return $this->error();
    }

    public function all(){
        $site = Site::query()->pluck('name','id');
        return $this->success($site);
    }
}
