<?php

namespace App\Http\Controllers\V1;

use App\Models\Help;
use App\Models\Region;
use Illuminate\Http\Request;
use App\Models\Advertise;
use App\Models\Setting;
use Hamcrest\Type\IsString;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class SettingsController extends Controller
{
    //获取前端配置
    public function get(Request $request)
    {
        $tags = $request->input('tags');
        $key = $request->input('key');
        $is_string = $request->input('is_string');

        if($request->filled('tags')){
          $ret = Setting::get($tags);
        }
        if($request->filled('tags','key')){
          $ret = Setting::get($tags,$key);
        }
        if($request->filled('tags','key','is_string')){
          $ret = Setting::get($tags,$key,$is_string);
        }
        return $this->output($ret);
    }

    //设置用户配置信息
    public function set(Request $request)
    {
        $tags = $request->input('tags', '');
        $key = $request->input('key', '');
        $value = $request->input('value', '');

        $validator = Validator::make($request->all(), [
            'tags' => 'required',
            'key' => 'required',
            'value' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $ret = Setting::set($tags,$key,$value);
        return $this->output($ret);
    }


    /**
     * @return void
     * 公告|政策
     */
    public function noticePolicy(Request $request){
        $data = Setting::query()->whereIn('key',['notice','privacy_policy'])->where('is_delete',0)->get();
        return $this->success($data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 关于我们
     */
    public function aboutUs(){
        $data = Setting::query()->where('key','=','about_us')->value('value');
        return $this->success($data);
    }
    /**
     * @return \Illuminate\Http\JsonResponse
     * 客服
     */
    public function customerService(){
        $data = Setting::query()->where('key','=','customer_service')->value('value');
        return $this->success($data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 小程序
     */
    public function applet(){
        $data = Setting::query()->where('key','=','applet')->value('value');
        return $this->success($data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 版本号
     */
    public function version(){
        $data = Setting::query()->where('key','=','version')->value('value');
        return $this->success($data);
    }

}
