<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Customer;
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);
        
        return view('backend.dashboard.index', compact('suppliers', 'customers', 'products','purchases','invoice','sales_due','expenses','transactions','slr'));
        
        //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)
    {
        //
    }
}
