<?php

namespace App\Livewire\Buttons;

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

class ViewCardType extends Component
{
    public $uuid;
    public $code,
    $name,
    $description,
    $image,
    $virtual_card_font_color,
    $bg_image,
    $id_number,
    $id_number_description,
    $terms_and_conditions,
    $faqs;

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

        $token = Session::get('user.access_token');

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

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

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

            $this->code = $cardType['code'] ?? '';
            $this->name = $cardType['name'] ?? '';
            $this->description = $cardType['description'] ?? '';
            $this->image = $cardType['image'] ?? '';
            $this->virtual_card_font_color = $cardType['virtual_card_font_color'] ?? '';
            $this->bg_image = $cardType['bg_image'] ?? '';
            $this->id_number = $cardType['id_number'] ?? '';
            $this->id_number_description = $cardType['id_number_description'] ?? '';
            $this->terms_and_conditions = $cardType['terms_and_conditions'] ?? '';
            $this->faqs = $cardType['faqs'] ?? '';
        } else {
            $this->addError('card types', 'Failed to fetch card type data.');
        }
    }
    public function render()
    {
        return view('livewire.buttons.view-card-type');
    }
}
