<?php

namespace App\Livewire\Buttons;

use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Carbon\Carbon;

class ViewPhotoSlider extends Component
{
    public $uuid;
    public $image,
        $title,
        $description,
        $date_start,
        $date_end,
        $start_time,
        $end_time,
        $created_by,
        $updated_by,
        $created_at,
        $updated_at;

    public function mount($uuid)
    {
        $this->uuid = $uuid;

        $token = Session::get('user')['access_token'] ?? null;

        if (!$token) {
            $this->addError('users', 'No access token found.');
            return;
        }

        $response = Http::withToken($token)->get(config('services.backend_api.url') . "/api/cms/photoSlider/{$uuid}");

        if ($response->successful()) {
            $photoSlider = $response->json('data');

            $this->image = $photoSlider['image'] ?? '';
            $this->title = $photoSlider['title'] ?? '';
            $this->description = $photoSlider['description'] ?? '';
            $this->date_start = isset($photoSlider['date_start']) ? Carbon::parse($photoSlider['date_start'])->format('F j, Y') : '';
            $this->date_end = isset($photoSlider['date_end']) ? Carbon::parse($photoSlider['date_end'])->format('F j, Y') : '';
            $this->start_time = '00:00:00';
            $this->end_time = '23:59:00';
            $this->created_by = $photoSlider['created_by'] ?? '';
            $this->updated_by = $photoSlider['updated_by'] ?? '';
            $this->created_at = isset($photoSlider['created_at']) ? Carbon::parse($photoSlider['created_at'])->format('F j, Y g:i A') : '';
            $this->updated_at = isset($photoSlider['updated_at']) ? Carbon::parse($photoSlider['updated_at'])->format('F j, Y g:i A') : '';
        } else {
            $this->addError('photo sliders', 'Failed to fetch photo slider data.');
        }
    }
    public function render()
    {
        return view('livewire.buttons.view-photo-slider');
    }
}
