<?php

namespace App\Http\Controllers;

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

class CashController extends Controller
{
    public function index()
    {
        $cashbook = CashBook::whereYear('date', '=', Carbon::now()->year)
            ->whereMonth('date', '=', Carbon::now()->month)
            ->take(30)
            ->get();
        return view('admin.cash.index', [
            'cashBooks' => $cashbook
        ]);
    }
    public function cashBookMonth(Request $request)
    {
        $selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));

        $customerBooks = CashBook::whereYear('updated_at', '=', Carbon::parse($selectedMonth)->year)
            ->whereMonth('updated_at', '=', Carbon::parse($selectedMonth)->month)
            ->get();
        return view('admin.cash.index', [
            'cashBooks' => $customerBooks
        ]);
    }

    public function cashBookReportBasedOnDate(Request $request)
    {
        $today = Carbon::now()->format('Y-m-d');
        $startDate = $request->startDate ?? $today;
        $endDate = $request->endDate ?? $startDate;
        $customerBooks = CashBook::whereBetween('date', [$startDate, $endDate])->get();
        return view('admin.cash.index', [
            'cashBooks' => $customerBooks,
            'startDate' => $startDate,
            'endDate'   => $endDate
        ]);
    }

}
