<?php

namespace app\api\controller;

use app\api\model\City;
use app\common\model\Banner;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

/**
 * 默认控制器
 * Class Index
 * @package app\api\controller
 */
class Index extends Controller
{
    public function index()
    {
        echo '当前访问的index.php,请将index.html设为默认站点入口';
    }

    public function getBannerList()
    {
        $list = Banner::where("status", 10)->with(['image'])->select()->toArray();
        foreach ($list as &$value) {
            unset($value['image']);
            $value['image'] = $value['image_url'];
        }
        return $this->renderSuccess($list);
    }

    /**
     * @notes:获取小程序配置
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function wxAppSetting(): Json
    {
        $data = (new \app\api\model\wxapp\Setting())->info();
        return $this->renderSuccess($data);
    }


    /**
     * @return mixed
     */
    public function cityList($search = "")
    {

        $model = City::withoutGlobalScope()->where('status', 1);
        if ($search) {
            $model = $model->where('name|first_letter', 'like', "%$search%");
        }
        $city_list = $model->select();
        $hotList = [];
        $list = [];
        foreach ($city_list as $seq => &$item) {
            if ($item['is_hot'] == 1) {
                $hotList[] = $item;
            }
            $list[$item['first_letter']][] = $item;
        }
        $result = ['hotList' => $hotList, "cityList" => $list];
        return $this->renderSuccess($result);
    }
}