<?php

namespace App\Http\Livewire;

use App\Models\Contract;
use App\Models\ContractPlan;
use Livewire\Component;
use Livewire\WithPagination;

class ContractsController extends Component
{
    use WithPagination;

    public $search, $selected_id, $pageTitle, $componentName, $count_reg;
    public $name, $description, $type, $sub_type, $status;
    public $breadcrumb_show = true;

    private $pagination = 9;

    protected $paginationTheme = 'bootstrap';

    public function mount()
    {
        UserAuthPermission('Contract_index');
        $this->pageTitle = 'Listado';
        $this->componentName = 'Contratos';
    }

    public function openAdd()
    {
        $this->selected_id = 0;
        $this->resetUI();
        $this->emit('show-modal','Abrir Modal');
    }

    public function resetUI()
    {
        $this->name = '';
        $this->description = '';
        $this->type = '';
        $this->sub_type = '';
        $this->status = '';
    }

    public function render()
    {
        $contracts = Contract::select('contracts.*')
                            ->orderBy('contracts.name', 'asc')
                            ->paginate($this->pagination);

        $plans = ContractPlan::select('contract_plans.id as id','contract_plans.contract_id','plans.name as plan_name', 'plans.color as plan_color')
                            ->join('contracts', 'contracts.id', '=', 'contract_plans.contract_id')
                            ->join('plans', 'plans.id', '=', 'contract_plans.plan_id')
                            ->orderBy('contracts.created_at', 'asc')
                            ->get();

        return view('livewire.contracts.component',compact('contracts', 'plans'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

    public function Store(){
        $rule =[
            'name' => 'required|max:99|unique:contracts',
            'description' => 'required|max:255',
            'type' => 'required',
            'sub_type' => 'required',
            'status' => 'required',
        ];
        $messages =[
            'name.required' => 'El campo nombre es obligatorio',
            'name.max' => 'El campo nombre debe tener máximo 99 caracteres',
            'name.unique' => 'El nombre del contrato ya existe',
            'description.required' => 'El campo descripción es obligatorio',
            'description.max' => 'El campo descripción debe tener máximo 255 caracteres',
            'type.required' => 'El campo tipo de contratoes obligatorio',
            'sub_type.required' => 'El campo sub tipo de contrato es obligatorio',
            'status.required' => 'El campo estado es obligatorio',
        ];
        $this->validate($rule,$messages);
        $contract = Contract::create([
            'name' => $this->name,
            'description' => $this->description,
            'type' => $this->type,
            'sub_type' => $this->sub_type,
            'status' => $this->status,
        ]);

        return redirect()->route('contratos.edit', [$contract->id]);

    }

    protected $listeners = ['deleteRow' => 'Destroy'];

    public function ToggleStatusContract($id)
    {
        $contract = Contract::find($id);
        $contract->status = $contract->status == 'Activo' ? 'Inactivo' : 'Activo';
        $contract->save();
    }
}
