<?php

namespace App\Http\Livewire\Pages\FormatFileMaster;

use App\Models\ConfFormatFile;
use Livewire\Component;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Role;
use App\Models\Permission;
use Livewire\WithPagination;

class FileIndex extends Component
{
    use WithPagination;
    public $perPage = 10;
    public $searchSelected = 'name';
    public $editId;
    public $deleteId;
    public $keyword = '';
    public $route = '';
    public $selectedOption = 'name';
    public $searchBy;
    public $menu;
    public $action = 'list';
    public $message;
    public $showDeleteListModal = false;
    public $showMessage = false;
    protected $listeners = [ 'showRoleList', 'deleteItem' , 'deleteSelected'];

    public function mount()
    {
        $this->searchBy = [
            'name' => 'Name',
            'fiel' => 'File'
        ];
        $this->message = session('message');
    }

    public function render()
    {
 
        $query = ConfFormatFile::select('formatservice_ID', 'name', 'file', 'date', 'timestamp', 'ac');

        if ($this->searchSelected && $this->keyword) {
            $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
        }
        $query->orderBy('formatservice_ID', 'DESC');
        $results = $query->paginate($this->perPage);

        return view('livewire.pages.format-file-master.file-index', compact('results'));
    }
    public function search()
    {
        $this->resetPage();
    }
    public function showListForm()
    {
        $this->action = 'list';
    }

    public function showAddForm()
    {
        $this->action = 'add';
    }

    public function showEditForm($id)
    {
        $this->editId= $id;
        $this->action = 'edit';
    }

    public function showDeleteModal($patchFileId)
    {
        $this->emit('showDeleteModal' ,$patchFileId);
    }

    public function deleteItem($id) {
    
        $file = ConfFormatFile::where("formatservice_ID", $id)->delete();
        $message = "Deleted Patch ID : " . json_encode($file) . " Successfully";
        $this->message = $message;
    }
    
}
