<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Carrera;
use App\Planestudios;
use App\Materia;
use App\MateriaCarrera;
use App\Correquisito;
use App\Encabezadopie;
use App\Exports\MateriaExport;
use App\Prerrequisito;
use App\MateriaCorrequisito;
use App\MateriaPrerrequisito;
use Barryvdh\DomPDF\Facade\Pdf;
use Maatwebsite\Excel\Facades\Excel;

class MateriaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = $request->all();
        $mat = "";
        if (count($data) > 0) {
            $mat = Materia::where('status_mat', '1');
            if ($data['nombre'] != null) {
                $mat = $mat->where('nombre_mat', 'like', '%' . $data['nombre'] . '%');
            }
            if ($data['clave'] != null) {
                $mat = $mat->where('clave_mat', 'like', '%' . $data['clave'] . '%');
            }
            if ($data['planest'] != null) {
                $mat = $mat->where('cve_planest', 'like', '%' . $data['planest'] . '%');
            }
            $mat = $mat->with('MateriaCarrera.carrera');
            $mat = $mat->get();
        } else {
            $mat = Materia::with('MateriaCarrera.carrera')->where('status_mat', '1')->get();
        }
        $planest = Planestudios::where('status_planest', '1')->get();
        return view('materia.listar', compact('mat', 'data', 'planest'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $mat = Materia::where('status_mat', '1')->get();
        $car = Carrera::where('estado', '1')->get();
        $plan = Planestudios::where('status_planest', '1')->get();
        return view('materia.nuevo', compact('car', 'plan', 'mat'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data = $request->all();

        $m = Materia::create(['nombre_mat' => $data['nombre'], 'clave_mat' => $data['clave'], 'status_mat' => '1', 'cve_planest' => $data['planE']]);

        $car = json_decode($data['carrera']);
        foreach ($car as $c) {

            MateriaCarrera::create([
                'cve_mat' => $m->cve_mat,
                'id' => $c->id,
                'status_matcar' => '1',
            ]);
        }
        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia guardada con exito!']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $mat = Materia::with('MateriaCarrera.carrera')->where('cve_mat', $id)->first();
        //$corre = Materia::with('MateriaCorrequisito.correquisito.materia')->where('cve_mat', $id)->get();
        //$prerre = Materia::with('MateriaPrerrequisito.prerrequisito.materia')->where('cve_mat', $id)->get();
        //$corre = Materia::with('MateriaCorrequisito.correquisito')->get();
        return view('materia.consultar', compact('mat'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $car = Carrera::where('estado', '1')->get();
        $plan = Planestudios::where('status_planest', '1')->get();
        $mat = Materia::with('MateriaCarrera.carrera')->where('cve_mat', $id)->first();
        $mate = Materia::where('status_mat', '1')->get();
        return view('materia.modificar', compact('mat', 'car', 'plan', 'mate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $mat = Materia::findOrFail($id);
        $mat->nombre_mat = $request->nombre;
        $mat->clave_mat = $request->clave;
        $mat->cve_planest = $request->planE;
        $mat->save();
        if (isset($request['carrera'])) {
            $car = json_decode($request['carrera']);
            foreach ($car as $c) {
                if (MateriaCarrera::where('id', $c->id)->where('cve_mat', $mat->cve_mat)->where('status_matcar', 1)->count() == 0) {

                    MateriaCarrera::create([
                        'cve_mat' => $mat->cve_mat,
                        'id' => $c->id,
                        'status_matcar' => '1'
                    ]);
                }
            }
        }

        if (isset($request['ante'])) {
            $ant = json_decode($request['ante']);
            foreach ($ant as $a) {
                $m = MateriaCarrera::where('id', $a->id_ant)->where('cve_mat', $id)->first();
                $id2 = $m->cve_matcar;
                $matc = MateriaCarrera::findOrFail($id2);
                $matc->status_matcar = '0';
                $matc->save();
            }
        }

        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia actualizada con exito!']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $mat = Materia::findOrFail($id);
        $mat->status_mat = '0';
        $mat->save();

        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia eliminado con exito!']);
    }

    public function exportExcel(Request $request)
    {
        $data = $request->all();
        $nombre = $data['nombre'];
        $clave = $data['clave'];
        $planest = $data['planest'];
        if ($nombre == null) {
            $nombre = "";
        }
        if ($clave == null) {
            $clave = "";
        }
        if ($planest == null) {
            $planest = "";
        }

        return Excel::download(new MateriaExport($nombre, $clave, $planest), 'materias.xlsx');
    }

    public function exportPdf(Request $request)
    {
        $data = $request->all();
        $nombre = $data['nombre'];
        $clave = $data['clave'];
        $planest = $data['planest'];
        $pdf = "";

        $enca = Encabezadopie::max('cve_encpie');
        $enca = Encabezadopie::where('cve_encpie', $enca)->first();
        if ($nombre == "" && $clave == "" && $planest == "") {
            $pdf = Pdf::loadView('materia.tablapdf', ['mat' => Materia::where('status_mat', '1')->get(), 'enca' => $enca]);
        } else {
            $mat = Materia::where('status_mat', '1');
            if ($nombre != "") {
                $mat = $mat->where('nombre_mat', 'like', '%' . $nombre . '%');
            }
            if ($clave != "") {
                $mat = $mat->where('clave_mat', 'like', '%' . $clave . '%');
            }
            if ($planest != "") {
                $mat = $mat->where('cve_planest', 'like', '%' . $planest . '%');
            }
            $mat = $mat->get();
            $pdf = Pdf::loadView('materia.tablapdf', ['mat' => $mat, 'enca' => $enca]);
        }

        return $pdf->download('materia.pdf');
    }
}
