<?php

namespace App\Http\Controllers\Admin;

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

class AdminBonusHistoryController extends Controller
{
    public function BonusHistory()
    {
        $users = User::where(['status' => 1])->where('role', '!=', 'admin')->get();
        $bonuses = Bonus::with(['bonusGivenTo'])->latest()->paginate(10);
        return view('adminDashboard.bonus-history', compact('users', 'bonuses'));
    }

    public function BonusHistorySearch(Request $request)
    {
        $request->validate([
            'user_mobile' => 'required'
        ]);
        $bonuses = Bonus::with(['bonusGivenTo'])->whereRelation('bonusGivenTo', 'mobile', 'like', '%' . $request->user_mobile . '%')->latest()->limit(500)->get();
        foreach ($bonuses as $key => $bonus) {
            $bonuses[$key]->created = date_format($bonus->created_at, "Y-m-d H:i:s");
        }
        return response()->json(['status' => 'success', 'bonuses' => $bonuses]);
    }
}
