<?php

namespace App\Http\Controllers;

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

class AdminController extends Controller
{
    public function web(){
        return view('website');
    }
    public function index(Request $request)
    {
        $role_id=$request->input('role_id',null);
        $page_size=$request->input('page_size',10);
        $num = ($request->input('page_index')-1)*$page_size;
        $data = Admin::query();
        if($role_id){
            $data->where('role_id',$role_id);
        }
        $total= $data->count();
        $list= $data->skip($num)->take($page_size)->get();
        foreach($list as $key=>$val){
            $list[$key]->role = $val->role->name;
            $role_id_str = ','.$val->role_id.',';
            $site_arr = Site::where('limit_role','like','%'.$role_id_str.'%')->pluck('name')->toArray();
            $list[$key]->show_site = !empty($site_arr) ? implode('，',$site_arr) : null;
        }
        return $this->success_list($list,'',$total);
    }

    public function create(Request $request)
    {
        $rules=[
            'name' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
            'password' => 'required|min:6|max:18',
        ];
        $messages = [
            'name.required' => '名称为必填项.',
            'phone.required' => '手机号为必填项.',
            'phone.regex' => '手机号格式有误.',
            'password.required' => '密码为必填项.',
            'password.min' => '密码不能少于6个字符.',
            'password.max' => '密码不能超过18个字符.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('450001',$validator->errors()->first());
        }
        $admin = new Admin();
        $admin->name        =   $request->input('name');
        $admin->phone       =   $request->input('phone');
        $admin->avatar      =   $request->input('avatar');
        $admin->password    =  Hash::make($request->input('password'));
        $admin->role_id     =   $request->input('role_id');
        $admin->status      =   1;
        $res = $admin->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    public function setStatus(Request $request){
        $id= $request->input('id');
        $admin = Admin::find($id);
        if($admin->role_id == 1){
            return $this->error('450001','超级管理员不能被禁用');
        }
        $admin->status = $admin->status==1?0:1;
        $res = $admin->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

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

    public function update(Request $request, AdminController $admin)
    {
        $rules=[
            'name' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
            'password' => 'required|min:6|max:18',
        ];
        $messages = [
            'name.required' => '名称为必填项.',
            'phone.required' => '手机号为必填项.',
            'phone.regex' => '手机号格式有误.',
            'password.required' => '密码为必填项.',
            'password.min' => '密码不能少于6个字符.',
            'password.max' => '密码不能超过18个字符.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('450001',$validator->errors()->first());
        }
        $id= $request->input('id');
        $admin = Admin::find($id);
        if(Hash::check($request->input('password'),$admin->password)){
            return $this->error('450001','新旧密码相同');
        }
        $admin->name = $request->input('name');
        $admin->phone = $request->input('phone');
        $admin->avatar = $request->input('avatar');
        $admin->password = Hash::make($request->input('password'));
        $admin->role_id     =   $request->input('role_id');
        $admin->status= 1;
        $res=$admin->save();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    public function destroy(Request $request)
    {
        $id= $request->input('id');
        $res= Admin::where('id',$id)->delete();
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }
    public function uploadImg(Request $request)
    {
        $path_url = 'image/avatar';
        $result = $request->file('img')->store($path_url);
        if($result) {
            $path=Storage::url($result);
            return $this->success($path);
        }
        return $this->error();
    }

    public function choiceAdminRole(Request $request){
        $res = Admin::where('id',$request->input('admin_id'))
            ->update([
                'role_id' => $request->input('role_id'),
                'desc' => $request->input('desc')
            ]);
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

}
