<?php

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     */
    public function index(Request $request)
    {
        if (! $request->get('tab')) {
            return redirect()->route('dashboard', ['tab' => 'active_orders']);
        }

        return view('dashboard', [
            'today' => Carbon::today(),
            'tab' => $request->get('tab'),
            'active_orders' => Order::where('status', '!=', 'cancelled')
                ->where('status', '!=', 'completed')
                ->orderByDesc('rush')
                ->orderBy('due_date')
                ->paginate(15)
                ->withQueryString(),
        ]);
    }
}