<?php

namespace App\Http\Livewire\Pages\News;


use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Shippingnetnews;
use App\Models\Shippingnetnewstype;
use App\Models\Shippingnetservergroup;
use Illuminate\Support\Facades\Cache;
use Livewire\WithPagination;

class NewsIndex extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';
    public $perPage = 10;
    public $url;
    public $searchSelected = 'topic';
    public $searchCateSelected;
    public $searchTypeSelected;
    public $editNewsId;
    public $deleteNewsId;
    public $keyword = '';
    public $selectedOption = 'topic';
    public $searchBy;
    public $action = 'list';
    public $message;
    public $selectedNews = [];
    public $searchCategory = [];
    public  $searchType = [];
    public $showDeleteListModal = false;
    public $showNoPermissionModal = false;
    public $showMessage = false;
    public $totalItems;
    protected $listeners = ['showNewsList', 'deleteItem', 'deleteSelected', 'setShowMessageFalse'];

    public function mount()
    {
        $this->searchBy = [
            'topic' => 'Topic',
            'issueDate' => 'Issue Date',
            'expireDate' => 'Expire Date',
            'authorDate' => 'Author Date',
        ];

        $this->searchType = Cache::remember('shippingnetnewstype', 60, function () {
            return Shippingnetnewstype::all()->pluck('newstype', 'id');
        });

        $this->searchCategory = Cache::remember('shippingnetservergroup', 60, function () {
            return Shippingnetservergroup::all()->pluck('groupname', 'groupID');
        });
    }
    public function search()
    {
        $this->resetPage();
    }
    public function updatedPerPage($value)
    {
        $this->adjustPageForNewPerPage();
    }

    public function adjustPageForNewPerPage()
    {
        $lastPage = ceil($this->totalItems / $this->perPage);
        if ($this->page > $lastPage) {
            $this->setPage($lastPage);
        }
    }
    public function showNewsAddForm()
    {
        $this->action = 'create';
    }

    public function showNewsEditForm($newsId)
    {
        $this->action = 'edit';
        $this->editNewsId = $newsId;
    }

    public function hideMessage()
    {
        $this->showMessage = false;
    }
    public function showNewsList($message = null)
    {
        $this->action = 'list';
        $this->resetPage();
        $this->message = $message;
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }

    public function paginationView()
    {
        return 'paginate-custom';
    }
    public function deleteItem($deleteNewsId)
    {
    
        // if (!Auth::user()->hasPermissions(['delete-news'])) {
        //     $this->showNoPermissionModal = true;
        //     return;
        // }
        $news = Shippingnetnews::find($deleteNewsId);
      
        if ($news) {
            $news->delete();
            $message = "Deleted Successfully";
            $this->message = $message;
            if ($this->message) {
                $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
            }
        }
    }


    public function render()
    {


        $query = Shippingnetnews::with('group');
        if (!empty($this->searchCateSelected)) {
            $query->where('groupID', $this->searchCateSelected);
        }

        if (!empty($this->searchTypeSelected)) {
            $query->where('newstype', $this->searchTypeSelected);
        }

        $results = $this->searchSelected && $this->keyword
            ? $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%')->orderBy('shippingnetnews_ID', 'desc')->paginate($this->perPage)
            : $query->orderBy('shippingnetnews_ID', 'desc')->paginate($this->perPage);
        $this->totalItems = $results->total();
        return view('livewire.pages.news.news-index', [
            'results' => $results,
            'selectedNews' => $this->selectedNews,
            'showDeleteListModal' => $this->showDeleteListModal
        ]);
    }
}
