<?php

namespace App\Http\Controllers\Admin\Color;

use App\Models\Period;
use App\Models\GamePlayTime;
use App\Models\NumberResult;
use App\Models\NumberBetting;

use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ColorController extends Controller
{
    # Define Base view
    protected $view = 'admin.color.';

    #Bind Period model
    protected $period;

    #Bind GamePlayTime model
    protected $gamePlayTime;

    #Bind NumberResult model
    protected $numberResult;

    #Bind NumberBetting model
    protected $numberBetting;

    /**
     * Defining the default constructor for controller
     *
     */

    public function __construct(
        Period                      $period,
        GamePlayTime                $gamePlayTime,
        NumberResult                $numberResult,
        NumberBetting               $numberBetting
    ) {

        $this->period                       = $period;
        $this->gamePlayTime                 = $gamePlayTime;
        $this->numberResult                 = $numberResult;
        $this->numberBetting                = $numberBetting;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            $relation = ['period', 'user', 'rates'];
            $gamePlayTime = $this->gamePlayTime;
            if (isset($request->gamePlayTime_id)) {
                $searchPeriod = $request->period_id;
                $gamePlayTime = $gamePlayTime->where('id', $searchPeriod);
            }
            $details['gamePlayTime'] = $gamePlayTime->first();
            $period = $this->period;
            if (isset($request->period_id)) {
                $searchPeriod = $request->period_id;
                $period = $period->where('id', $searchPeriod);
            } else {
                $period = $period->where('game_id', 1)->where('play_time_id', $details['gamePlayTime']->id)->orderBy('id', 'DESC');
            }
            $details['period'] = $period->first();

            $lists = $this->numberBetting->with($relation);

            if (isset($request->search_name)) {
                $username = $request->search_name;
                $lists = $lists->whereHas('user', function ($q) use ($username) {
                    $q->where('name', 'LIKE', '%' . $username . '%');
                });
            }
            $details['lists'] = $lists->where('period_id', $details['period']->id)->paginate(10);
            $details['count'] = $this->numberBetting->where('period_id', $details['period']->id)->count();
            $details['total'] = $this->numberBetting->where('period_id', $details['period']->id)->sum('amount');
            $details['periods'] = [];
            $details['result'] = $this->numberResult->where('period_id', $details['period']->id)->first();
            #render view
            return view($this->view . 'index', $details);
        } catch (\Exception $ex) {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {}

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
