<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\Customer;
use App\Models\CustomerRecord;
use App\Models\Supplier;
use App\Models\Product;
use App\Models\Productcategory;
use App\Models\Purchase;
use App\Models\Sale;
use App\Models\Expense;
use App\Models\Transaction;


class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index(){
        $suppliers = Supplier::count('id');
        $customers = Customer::count('id');
        $products = Product::count('id');
        $purchases = Purchase::count('id');
        $invoice = Sale::count('id');
        $sales_due = Sale::sum('due_amount');
        $expenses = Expense::sum('expense_amount');
        $transactions = 0;
        // last 30 day sales report
         $sl = DB::select("SELECT YEAR(sale_date) AS Y, MONTH(sale_date) AS m, DAY(sale_date) AS d, sale_date, SUM(grand_total_price) AS sale_total FROM sales WHERE sale_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() GROUP BY sale_date");
        //return $sl;
        $sale = array();
        foreach($sl as $i=>$ss){
            $sale[] = [$ss->d,round($ss->sale_total) ];
        }
        $slr =  json_encode($sale, true);
        
        // গতকাল পর্যন্ত মোট বাকি কত ছিল
        $yesterday = Carbon::yesterday(); // গতকালের তারিখ
        $yesterday_due = CustomerRecord::where('transaction_date', '<=', $yesterday)->sum('total_due'); // মোট বাকি গণনা
        // আজকে কত টাকা বাকি গেছে
        $today = Carbon::today(); // আজকের তারিখ
        $today_due = CustomerRecord::where('transaction_date', $today)->sum('due_amount'); // আজকের বাকি গণনা
        
        // এখন মোট কত টাকা বাকি আছে
        $total_due_now = CustomerRecord::sum('total_due'); // বর্তমান মোট বাকি
        $dues = [
            'yesterday_due' => $yesterday_due,
            'today_due' => $today_due,
            'total_due_now' => $total_due_now,
        ];
        
        return view('backend.dashboard.index', compact('suppliers', 'customers', 'products','purchases','invoice','sales_due','expenses','transactions','slr','dues'));
        
        //return view('backend.dashboard.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        return 'hello';
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
