<?php

namespace App\Http\Controllers\V1;

use App\Models\Config;
use App\Models\Help;
use App\Models\PaymentConfig;
use App\Models\Region;
use App\Models\TaskList;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class IndexController extends Controller
{
        public function getServiceConfig()
    {
        $service_phone = Config::query()->where('key', 'service_phone')->first();
        $service_wx = Config::query()->where('key', 'service_wx')->first();
        $service_qrcode = Config::query()->where('key', 'service_qrcode')->first();
        $about = Config::query()->where('key', 'about')->first();
        $push_promotion = Config::query()->where('key', 'push_promotion')->first();
        return $this->success(['service_qrcode' => $service_qrcode,'service_phone' => $service_phone,'service_wx' => $service_wx,'about' => $about,'push' => $push_promotion]);
    }
    // 隐私政策
    public function privacyPolice()
    {
        $info = Config::query()->where('key', 'privacy')->first();

        return $this->success($info);
    }
    public function getConfigByKey(){
        $key = \request()->input('key','');
        $result = Config::query()->where('key', $key)->first();
        return $this->success($result);
    }
    // 用户协议
    public function userAgreement()
    {
        $info = Config::query()->where('key', 'agreement')->first();

        return $this->success($info);
    }

    // 用户协议
    public function payConfig()
    {
        $info = PaymentConfig::query()->where('state', 1)->get();

        return $this->success($info);
    }

    /**
     * 获取精选.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getHandpick()
    {
        // $size = \request()->input('size',15);
        // var_dump($size);
        $result = TaskList::query()->where('is_handpick', 1)->orderByDesc('sort')->paginate();

        return $this->success($result);
    }

    // 帮助类型
    public function helpList()
    {
        $list = Help::query()->select('id', 'title')->get();

        if ($list->isEmpty()) {
            return [];
        }
        $list = $list->toArray();

        return $this->success($list);
    }

    // 帮助类答案
    public function answer(Request $request)
    {
        $info           = Help::query()->where('id', $request->id)->first();
        $info->look_num = $info->look_num + 1;
        $info->save();

        return $this->success($info);
    }

    // 全国地区
    public function allRegion()
    {
        $regionList = Redis::get('all_region');
        if (!empty($regionList)) {
            return $this->success(json_decode($regionList, true));
        }

        $list = Region::query()
            ->where('type', 1)
            ->select('code', 'full_name')
            ->get()
            ->toArray();

        foreach ($list as $key => $val) {
            $cityList = Region::query()->where('parent_code', $val['code'])->select('code', 'full_name')->get()->toArray();
            foreach ($cityList as $k => $v) {
                $areaList       = Region::query()->where('parent_code', $v['code'])->select('code', 'full_name')->get()->toArray();
                $v['area_list'] = $areaList;
                $cityList[$k]   = $v;
            }
            $val['city_list'] = $cityList;
            $list[$key]       = $val;
        }

        Redis::setex('all_region', 24 * 3600, json_encode($list));

        return $this->success($list);
    }
}
