<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Qiniu\Auth;

class SettingController extends Controller
{
    public function getUploadQiNiuToken(Request $request){
        $config = [
            'driver' => 'qiniu',
            'domain' => 'http://image.chuliu.cc', //七牛域名
            'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn',  //AccessKey
            'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp',  //SecretKey
            'bucket' => 'daweiboshi-app',  //Bucket名字
        ];
        $auth = new Auth($config['access_key'], $config['secret_key']);
        $token = $auth->uploadToken($config['bucket']);
        return $this->success($token);
    }
    //获取全部视频信息
    public function getVideoAll(){
        $setting=Setting::all();
        if($setting){
            return $this->success($setting);
        }else{
            return $this->error();
        }
    }
    //获取单个视频信息
    public function getVideo2Id(Request $request){
        $id=$request->input('id');
        $setting=Setting::where('id',$id)->first();
        if($setting){
            return $this->success($setting);
        }else{
            return $this->error();
        }
    }
    //添加操作视频信息
    public function addVideo(Request $request)
    {
        $rules=[
            'name'=>'required|max:32',
            'url'=>'required'
        ];
        $messages=[
            'name.required'=>'标题不能为空.',
            'name.max'=>'标题长度最长32个字符.',
            'url.required'=>'视频链接不能为空.'
        ];
        $validator = Validator::make($request->input(), $rules, $messages);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        $name = $request->input('name');
        $value = $request->input('url');
        $res = Setting::create([
            'name' => $name,
            'value' => $value
        ]);
        if ($res) {
            return $this->success([]);
        } else {
            return $this->error();
        }
    }
    //修改操作视频信息
    public function editVideo(Request $request)
    {
        $rules=[
            'name'=>'required|max:32',
            'url'=>'required'
        ];
        $messages=[
            'name.required'=>'标题不能为空.',
            'name.max'=>'标题长度最长32个字符.',
            'url.required'=>'视频链接不能为空.'
        ];
        $validator = Validator::make($request->input(), $rules, $messages);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        $id=$request->input('id');
        $name = $request->input('name');
        $value = $request->input('url');
        $res = Setting::where('id',$id)->update([
            'name' => $name,
            'value' => $value
        ]);
        if ($res) {
            return $this->success([]);
        } else {
            return $this->error();
        }
    }
    //删除操作视频信息
    public function deleteVideo(Request $request)
    {
        $id=$request->input('id');
        $res = Setting::where('id',$id)->delete();
        if ($res) {
            return $this->success([]);
        } else {
            return $this->error();
        }
    }
}
