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/Certificate/Update.php
<?php

namespace App\Livewire\Certificate;

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;

class Update extends Component
{
    use WithFileUploads;
    public $uuid;
    public $certificate;
    public $title;
    public $category_id;
    public $category_name;
    public $year_obtained;
    public $document;
    public $new_document;
    public $years = [];
    public $document_public_url;
    public $subcategories = [];
    public $seller_category_id;

    public function mount($uuid)
    {
        $this->uuid = $uuid;
        $this->queryCertificate();
        $this->title = $this->certificate['title'];
        $this->category_id = $this->certificate['category_id'];
        $this->year_obtained = $this->certificate['year_obtained'];
        $this->document = $this->certificate['document'];
        $this->document_public_url = $this->certificate['document_public_url'];
        $this->getSubCategory();

        $currentYear = now()->year;
        $this->years = range($currentYear - 10, $currentYear);
    }
    public function getSubCategory()
    {
        $shop = Session::get('shop');
        $this->seller_category_id = $shop['category_id'];

        $body = ['seller_category_id' => $this->seller_category_id];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->get(ApiEndpoints::getSellerCategory());
        $this->subcategories = $response->json()['data'][0]['children'];
    }

    public function queryCertificate()
    {
        try {
            $body = [
                "uuid" => $this->uuid,
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->get(ApiEndpoints::queryCert());
            if ($response->successful()) {
                $this->certificate = $response->json()['data'];
            } else {
                Log::error("Failed to fetch seller gig. Status code: {$response->getMessage()}");
                $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
        }
    }

    public function updateShopCertificate()
    {
        $this->validate([
            'title' => 'required|string|max:255',
            'category_id' => 'nullable|integer',
            'year_obtained' => 'required|integer',
            'new_document' => 'nullable|file|mimes:jpeg,png,pdf|max:1024',
        ]);

        try {
            $document = $this->document;
            $document_public_url = $this->document_public_url;

            if ($this->new_document) {
                cloudinary()->destroy($this->document_public_url);

                $uploadedPhoto = $this->new_document->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedPhoto, ['folder' => 'gigPhotos']);
                $document = $uploadResult->getSecurePath();
                $document_public_url = $uploadResult->getPublicId();
            }

            $body = [
                'title' => $this->title,
                'category_id' => $this->category_id,
                'year_obtained' => $this->year_obtained,
                'document' => $document,
                'document_public_url' => $document_public_url,
                "uuid" => $this->uuid,
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->put(ApiEndpoints::updateCert());
            if ($response->successful()) {
                $this->certificate = $response->json()['data'];
                Session::flash('success', 'Shop certificate updated successfully.');
                return redirect('/dashboard-certificates');
            } else {
                Log::error("Failed to fetch seller gig. Status code: {$response->getMessage()}");
                $this->addError('document', 'Failed to fetch seller gig. Please try again later.');
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            Session::flash('error', 'Failed to update certificate. Please try again.');
        }
    }


    public function render()
    {
        return view('livewire.certificate.update');
    }
}