<?php

namespace App\Exports;

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

class TemarioExport implements FromView
{
    protected $clavem;
    protected $materia;

    public function __construct(String $clavem, String $materia)
    {
        $this->clavem = $clavem;
        $this->materia = $materia;
    }
    public function view(): View
    {
        if ($this->clavem == "" && $this->materia == "") {
            return view("temario.tabla", ['tem' => Temario::where('status_tem', '1')->get()]);
        } else {
            $tem = Temario::where('status_tem', '1');
            if ($this->clavem != "") {
                $clavem = $this->clavem;
                $tem = $tem->whereHas('materia', function ($query) use ($clavem) {
                    $query = $query->where('clave_mat', 'like', '%' . $clavem . '%');
                });
            }
            if ($this->materia != "") {
                $materia = $this->materia;
                $tem = $tem->where('cve_mat', $materia);
            }
            $tem = $tem->get();
            return view('temario.tabla', ['tem' => $tem]);
        }
    }
}
