<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\History;
use App\Supports\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * @group Lottery Prize
 */
class HistoryController extends Controller
{
    public function __construct(
    )
    {
    }

    public function list($locale = 'tw')
    {
        $locale = $locale == "tw" ? "zh_TW" : $locale;
        $data = History::where("visible", 1)->orderByDesc("selected_year", "desc")->orderByDesc("selected_month")->get();
        $yearList = History::select("selected_year", \DB::raw("concat(selected_year, '年') as lable"))->distinct()->orderBy("selected_year", "desc")->pluck('lable', 'selected_year');
        $result = [];
        $result["yearList"] = $yearList;
        foreach($data as $item){
            $result["list"][$item->selected_year][] = [
                "operateMonth " => $item->selected_year . "." . str_pad($item->selected_month, 2, "0", STR_PAD_LEFT),
                "title" => $item->getTranslation("title", $locale),
                "imgUrl" => $item->img_url_link
            ];
        }
        return Response::ok($result);
    }
}
