<?php

namespace App\Http\Livewire\Pages\Company;

use Livewire\Component;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Company;
use App\Models\CompanyType;
use Livewire\WithPagination;

class CompanyIndex extends Component
{
    use WithPagination;
    public $perPage = 10;
    public $url;
    public $searchSelected = 'name';
    public $editCompanyId;
    public $deleteCompanyId;
    public $keyword = '';
    public $route = '';
    protected $paginationTheme = 'bootstrap';
    public $selectedOption = 'name';
    public $searchBy;
    public $menu;
    public $action = 'list';
    public $message;
    public $selectedCompanies = [];
    public $showDeleteListModal = false;
    public $showNoPermissionModal = false;
    public $showMessage = false;
    protected $listeners = ['showCompanyList', 'deleteItem', 'deleteSelected'];

    public function mount()
    {
        $this->searchBy = [
            'name' => 'Name',
            'name_en' => 'Name English',
            'tax_branch' => 'Tax:Branch'
        ];
        $this->message = session('message');
    }

    public function render()
    {
        $query = Company::query();

        if ($this->searchSelected && $this->keyword) {
            if ($this->searchSelected === 'tax_branch') {
                $parts = explode(':', $this->keyword);
                if (count($parts) == 2) {
                    [$tax, $branch] = $parts;
                    $query->where('tax', 'LIKE', '%' . trim($tax) . '%')
                        ->where('branch', 'LIKE', '%' . trim($branch) . '%');
                } else {
                    $query->where('tax', 'LIKE', '%' . $this->keyword . '%')
                        ->orWhere('branch', 'LIKE', '%' . $this->keyword . '%');
                }
            } else {
                $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
            }
        }

        $results = $query->paginate($this->perPage);

        $companyTypes = CompanyType::all();


        return view('livewire.pages.company.company-index', [
            'results' => $results,
            'searchBy' => $this->searchBy,
            'route' => $this->route,
            'url' => $this->url,
            'companyTypes' => $companyTypes,
            'selectedCompanies' => $this->selectedCompanies,
            'showDeleteListModal' => $this->showDeleteListModal
        ]);
    }

    public function showCompanyCreateForm()
    {
        if (!\Auth::user()->hasPermissions(['add-company'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $this->action = 'create';
    }

    public function showCompanyEditForm($companyId)
    {
        if (!\Auth::user()->hasPermissions(['edit-company'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $this->action = 'edit';
        $this->editCompanyId = $companyId;
    }
    public function showCompanyList($message = null)
    {
        $this->action = 'list';
        $this->keyword = '';
        $this->resetPage();
        $this->message = $message;
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }

    public function paginationView()
    {
        return 'paginate-custom';
    }
    public function deleteItem($deleteCompanyId)
    {
        if (!\Auth::user()->hasPermissions(['delete-company'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $company = Company::find($deleteCompanyId);

        if ($company) {
            $company->delete();
            $message = "Deleted Successfully";
            $this->message = $message;
            if ($this->message) {
                $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
            }
        }
    }
    public function deleteSelected($selectedCompanies)
    {
        if (!\Auth::user()->hasPermissions(['delete-company'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $companyDeleted = Company::whereIn("id", $selectedCompanies)->pluck('name')->toArray();
        $companyStr = implode(",", $companyDeleted);
        Company::destroy($selectedCompanies);
        $message = "Deleted : (" . $companyStr . " )Successfully";

        $this->message = $message;
        $this->selectedCompanies = [];
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }
}
