File: /var/www/html/app/Livewire/Pages/ShowService.php
<?php
namespace App\Livewire\Pages;
use Carbon\Carbon;
use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class ShowService extends Component
{
public $gig;
public $faqs;
public $currentUrl;
public function mount($slug)
{
$body = [
"gig_slug" => $slug,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->get(ApiEndpoints::queryGig());
if ($response->successful()) {
$this->gig = $response->json()['data'];
$this->faqs = $response->json()['data']['faq'] ?? [];
} else {
Session::flash('error', $response->json()['message']);
}
$this->getSimilarGigs();
}
public function getSimilarGigs()
{
if (!isset($this->gig['category_id'])) {
Log::error('Missing category_id in $this->gig.', ['gig' => $this->gig]);
$this->similarGigs = [];
return;
}
$response = Http::post(ApiEndpoints::getSimilarGig(), [
'category_id' => $this->gig['category_id']
]);
info($response->json());
$this->similarGigs = $response->json()['data'] ?? [];
}
public $similarGigs;
public $gig_id;
public $budget_amount;
public $end_time;
public $start_time;
public $gig_price;
public $message;
public function updatedEndTime()
{
if ($this->start_time && $this->end_time) {
// Convert times to Carbon instances
$start = Carbon::parse($this->start_time);
$end = Carbon::parse($this->end_time);
// Check if end time is the same as or earlier than start time
if ($end <= $start) {
$this->addError('end_time', 'The delivery date and time must be after the start date and time.');
$this->end_time = null; // Reset the invalid end_time
} else {
$this->resetErrorBag('end_time');
}
}
}
public function makeBooking()
{
$this->validate([
'budget_amount' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'message' => 'nullable|max:255|string',
]);
$body = [
'gig_id' => $this->gig['id'],
'agreed_amount' => $this->budget_amount,
'gig_price' => $this->gig['price'],
'start_time' => $this->start_time,
'end_time' => $this->end_time,
'message' => $this->message,
'app_type' => 'web',
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::bookGig());
if ($response->successful()) {
Session::flash('success', $response->json()['message']);
$this->redirect('/account/chat', navigate: true);
} else {
$this->redirect('/service/' . $this->gig['slug'], navigate: true);
Session::flash('error', value: $response->json()['message']);
}
}
public function render()
{
return view(view: 'livewire.pages.show-service');
}
}