<?php

namespace App\Http\Controllers;

use App\Charts\PieChart;
use App\DataTables\AllDonationsDataTable;
use App\Models\AllDonation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class IndexController extends Controller
{
    public function index(AllDonationsDataTable $dataTable){

        $count_total = AllDonation::query()->where('client_id', '148')->count();
        $success_amt = AllDonation::query()->where('client_id', '148')->where('status', 'success')->sum('amount');
        $count_success = AllDonation::query()->where('client_id', '148')->where('status', 'success')->count();
        $count_pending = AllDonation::query()->where('client_id', '148')->where('status', 'pending')->count();
        $count_failed = AllDonation::query()->where([['client_id', '148'],['status', 'failed']])->orWhere([['client_id', '148'],['status', 'TIMEOUT']])->orWhere([['client_id','148'],['status','WRONG_PIN']])->count();
        return $dataTable->render('admin.index', 
        [
            'count_total' => $count_total,
            'count_success' => $count_success,
            'count_pending' => $count_pending,
            'count_failed' => $count_failed,
            'success_amt' => $success_amt
        ]);
    }
    public function indexReport(AllDonationsDataTable $dataTable){

        return $dataTable->render('admin.report');
    }


    public function show($id){
        $show = AllDonation::find($id);
        return view('admin.show', compact('show'));
    }

    public function pieChart(PieChart $chart)
    {
        return view('admin.analytics', ['chart' => $chart->build()]);
    }
}

