<?php

namespace App\Exports;

use App\Especialidad;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class EspecialidadExport implements FromView
{
    protected $nombre;
    protected $clave;
    protected $planest;

    public function __construct(String $nombre, String $clave, String $planest)
    {
        $this->nombre = $nombre;
        $this->clave = $clave;
        $this->planest = $planest;
    }

    public function view(): View
    {
        if ($this->nombre == "" && $this->clave == "" && $this->planest == "") {
            return view("especialidad.tabla", ['esp' => Especialidad::with('planestudio')->where('status_esp', '1')->get()]);
        } else {
            $esp = Especialidad::where('status_esp', '1');
            if ($this->nombre != null) {
                $nombre = $this->nombre;
                $esp = $esp->where('nombre_esp', 'like', '%' . $nombre . '%');
            }
            if ($this->clave != null) {
                $clave = $this->clave;
                $esp = $esp->where('clave_esp', 'like', '%' . $clave . '%');
            }
            if ($this->planest != null) {
                $planest = $this->planest;
                $esp = $esp->where('cve_planest', $planest);
                $esp = $esp->with("planestudio");
            }
            $esp = $esp->get();
            return view("especialidad.tabla", ['esp' => $esp]);
        }
    }
}
