<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Grants;
use App\Models\donations;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Yajra\DataTables\Facades\DataTables;

class donationsController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
         $projects = DB::table('projects')->where('organization_id', Auth::user()->referencesUser)->count();
        
         $grantstotalCost = DB::table('grants')
           ->where('organization_id', Auth::user()->referencesUser)
            ->sum('grant_amount');

        $donationstotal = DB::table('donations')
           ->where('organization_id', Auth::user()->referencesUser)
            ->sum('quantity');
        
         $lastGrants = DB::table('grants')
        ->join('sectors', 'sectors.id', '=', 'grants.sector_id')
        ->where('organization_id', Auth::user()->referencesUser)
        ->orderBy('grants.created_at', 'desc')
                        ->take(5)
                        ->get(['grants.*','sectors.sectorName']);

          return view('donations.index',compact('projects','grantstotalCost','donationstotal'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
        $sectors = DB::table('sectors')->get();
        return view('donations.create', compact('sectors'));
    }


     public function getData()
    {
        //$GrantsData   = Grants::all();
         $user_role = DB::table('role_users')->where('user_id', Auth::user()->id)->first();
 if($user_role->role_id == 1) //============admin=========================
    {
         $donationsData = DB::table('donations')
        ->join('sectors', 'sectors.id', '=', 'donations.sector_id')
        ->join('organizations', 'organizations.id', '=', 'donations.organization_id')
        ->get(['donations.*','sectors.sectorName'
             ,'organizations.organizationName']);
    }else
    if($user_role->role_id == 2) //=====================================
    {
        $donationsData = DB::table('donations')
        ->join('sectors', 'sectors.id', '=', 'donations.sector_id')
        ->join('organizations', 'organizations.id', '=', 'donations.organization_id')
        ->where('donations.organization_id', Auth::user()->referencesUser)
        ->get(['donations.*','sectors.sectorName'
             ,'organizations.organizationName']);
       
    }
        


        return DataTables::of($donationsData)
        ->addColumn('action2', function ($donationsData) {
            $donationsData_status = $donationsData->status;
            if($donationsData_status == "In_progress")
            {
                return '
                <a href="../show-Project/'.$donationsData_status.'" 
                style="background-color: #fdfd00;color: #9b9b00;"
                class="btn btn-sm btn-primary">جاري التنفبذ</a>
            ';
        }else
        if($donationsData_status == "Planned"){
             return '
                <a href="../show-Project/'.$donationsData_status.'" 
                style="background-color: #da3f4cff;color: #fcf8f8ff;"
                class="btn btn-sm btn-primary">مؤجل</a>
            ';
            }
            else
        if($donationsData_status == "Completed"){
             return '
                <a href="../show-Project/'.$donationsData_status.'"
                class="btn btn-sm btn-success">انتهاء</a>
            ';
            }
        })
        ->rawColumns(['action2']) // allow HTML rendering
    ->make(true);
    }



    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
{
    $validated = $request->validate([
        'organization_id'   => 'required|exists:users,referencesUser',
        'user_id'           => 'required|integer',
        'donation_title'        => 'required|string|max:255',
        'sector_id'         => 'required|exists:sectors,id',
        'donation_type'     => 'required|string',
        'donation_date'     => 'required|date',
        'quantity'          => 'nullable|integer',
        'donation_amount'   => 'required|numeric',
        'currency'          => 'required|in:USD,EUR,SDG,SAR',
    ]);

    $grant = donations::create($validated);

    return redirect()->back()->with('success', 'تم حفظ الهبة بنجاح ✅');
}

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
