<?php

namespace App\Http\Controllers;

use App\Models\WordIndex;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class WordIndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        //
        switch ($request->input('view')) {
            case 'key':
                $key = $request->input('key');
                /*
                $result = Cache::remember("/word_index/{$key}",10,function() use($key){
                    return WordIndex::where('word','like',$key."%")
                                    ->whereOr('word_en','like',$key."%")
                                    ->orderBy('word_en')
                                    ->take(10)->get();
                });
                $table = WordIndex::where('word','like',$key."%")
                                   ->whereOr('word_en','like',$key."%")
                                   ->orderBy('len')
                                   ->orderBy('word_en')
                                   ->take(10);
                $result = $table->get();
*/
                $result = DB::select('SELECT * from  word_indices where word like ? or word_en like ? order by len, word_en limit 10', [$key.'%', $key.'%']);

                return $this->ok(['rows' => $result, 'count' => count($result)]);
                break;
            default:
                return $this->error('view error');
                break;
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @return Response
     */
    public function show(WordIndex $wordIndex)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @return Response
     */
    public function update(Request $request, WordIndex $wordIndex)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return Response
     */
    public function destroy(WordIndex $wordIndex)
    {
        //
    }
}
