<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\ContractPlan;
use App\Models\Plan;

class ContractPlansController extends Component
{
    public $componentName,$selected_CP_id,$plan_id,$contract_sel_id,$name_plan;


    public function mount($contract_sel_id)
    {
        $this->contract_sel_id=$contract_sel_id;
        $this->componentName = 'Planes asignados al contrato';
    }

    public function render()
    {

        $contractplans = ContractPlan::where('contract_id',$this->contract_sel_id)
        ->join('plans', 'contract_plans.plan_id', '=', 'plans.id')
        ->select('contract_plans.*', 'plans.name as name_plan', 'plans.color as color_plan')
        ->get();
        $plans = Plan::all();
        return view('livewire.contracts.detail.plan.component',compact('contractplans','plans'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

    public function resetUI()
    {
        $this->selected_CP_id = 0;
        $this->plan_id ="";
        $this->resetValidation();
    }


    public function openAddCP()
    {
        $this->emit('show-modal-CP','Abrir Modal');
    }

    public function EditCP(ContractPlan $contractplans)
    {
        $this->selected_CP_id = $contractplans->id;
        $this->plan_id = $contractplans->plan_id;
        $this->emit('show-modal-CP', 'Abrir Modal');
    }

    public function UpdateCP()
    {
        $rules = [
            'plan_id' => 'required|unique:contract_plans,plan_id,'.$this->plan_id.',id,contract_id,'.$this->contract_sel_id,
        ];
        $messages = [
            'plan_id.required' => 'El campo es obligatorio',
            'plan_id.unique' => 'El Plan ya esta registrado',
        ];
        $this->validate($rules, $messages);
        $contractplans = ContractPlan::find($this->selected_CP_id);
        $contractplans->update([
            'contract_id' => $this->contract_sel_id,
            'plan_id' => $this->plan_id,
        ]);
        $this->resetUI();
        $this->emit('item-updated-CP', 'Plan del contrato Actualizado con exito');

    }

    public function StoreCP()
    {
        $rules = [
            'plan_id' => 'required|unique:contract_plans,plan_id,'.$this->plan_id.',id,contract_id,'.$this->contract_sel_id,
        ];
        $messages = [
            'plan_id.required' => 'El campo es obligatorio',
            'plan_id.unique' => 'El Plan ya esta registrado',
        ];
        $this->validate($rules, $messages);
        $contractplans = ContractPlan::create([
            'contract_id' => $this->contract_sel_id,
            'plan_id' => $this->plan_id,
        ]);
        $this->resetUI();
        $this->emit('item-updated-CP', 'Plan del contrato Guardado con exito');

    }


    protected $listeners = ['deleteRowCP' => 'DestroyCP'];

    public function DestroyCP(ContractPlan $contractplans)
    {
        $contractplans::where('contract_id',$this->contract_sel_id);
        $contractplans->delete();
        $this->emit('item-deleted-CP', 'Se eliminó el plan de contrato con éxito');
    }
}
