<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\DB;

class BonusController extends Controller
{
    public function index(Request $request)
    {
        $size = $request->get('size', 20);
        $rawBonuses = DB::table('bonuses')->orderByDesc('id')->paginate($size)->withQueryString();
        $bonuses = $rawBonuses->through(function ($b) {
            $b->appraiser = $b->appraiser_id ? DB::table('users')->select('id', 'name', 'phone')->find($b->appraiser_id) : null;
            $table = ($b->order_type ?? '') === 'auto_' ? 'auto_orders' : 'estate_orders';
            $b->order = $b->order_id ? DB::table($table)->select('id', 'number')->find($b->order_id) : null;
            return $b;
        });
        $bonus = null;
        return view('bonus.index', compact('bonuses', 'bonus', 'size'));
    }

    public function create()
    {
        return view('bonus.create');
    }

    public function store(Request $request)
    {
        return redirect()->route('bonus.index');
    }

    public function show($id)
    {
        $bonus = DB::table('bonuses')->find($id);
        return view('bonus.show', compact('bonus'));
    }

    public function edit($id)
    {
        $bonus = DB::table('bonuses')->find($id);
        return view('bonus.edit', compact('bonus'));
    }

    public function update(Request $request, $id)
    {
        return redirect()->route('bonus.index');
    }

    public function destroy($id)
    {
        return redirect()->route('bonus.index');
    }

    public function storeCredit(Request $request)
    {
        return redirect()->back();
    }
}
