<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Betting;
use App\Models\User;
use Illuminate\Http\Request;

class AdminTradeHistoryController extends Controller
{
    public function TradeHistory()
    {
        $users = User::where(['status' => 1])->where('role', '!=', 'admin')->get();
        $bettings = Betting::with(['user'])->latest()->paginate(10);
        return view('adminDashboard.trade-history', compact('users', 'bettings'));
    }

    public function TradeHistorySearch(Request $request)
    {
        $request->validate([
            'user_mobile' => 'required',
            'type' => 'required'
        ]);
        $betting = Betting::where('tab', '=', $request->type)->with(['user'])->whereRelation('user', 'mobile', 'like', '%' . $request->user_mobile . '%')->latest()->limit(500)->get();
        foreach ($betting as $key => $bet) {
            $betting[$key]->created = date_format($bet->created_at, "Y-m-d H:i:s");
        }
        return response()->json(['status' => 'success', 'betting' => $betting]);
    }
}
