<?php

namespace App\Http\Controllers;

use App\Carrera;
use App\Encabezadopie;
use Illuminate\Http\Request;
use App\Planestudios;
use App\Exports\PlanestudiosExport;
use Barryvdh\DomPDF\Facade\Pdf;
use Maatwebsite\Excel\Facades\Excel;

class PlanEstudiosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = $request->all();
        $planE = "";
        if (count($data) > 0) {
            $planE = Planestudios::where('status_planest', '1');
            if ($data['claveOfi'] != null) {
                $planE = $planE->where('claveOfi_planest', 'like', '%' . $data['claveOfi'] . '%');
            }
            if ($data['nombre'] != null) {
                $planE = $planE->where('nombre_planest', 'like', '%' . $data['nombre'] . '%');
            }
            if ($data['reticula'] != null) {
                $planE = $planE->where('reticula_planest', 'like', '%' . $data['reticula'] . '%');
            }
            if ($data['nivEsc'] != null) {
                $planE = $planE->where('nivesc_planest', 'like', '%' . $data['nivEsc'] . '%');
            }
            $planE = $planE->get();
        } else {
            $planE = Planestudios::where('status_planest', '1')->get();
        }

        return view('plan_estudios.listar', compact('planE', 'data'));
    }

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

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

        Planestudios::create(['claveOfi_planest' => $data['claveO'], 'iduni_planest' => $data['iduni'], 'nombre_planest' => $data['nom'], 'reticula_planest' => $data['reticula'], 'nivesc_planest' => $data['nivEsc'], 'modalidad_planest' => $data['modalidad'], 'status_planest' => '1', 'id' => $data['car']]);

        return response()->json(['estado' => 'Exito', 'mensaje' => 'Plan de Esctudios guardado con exito!']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $car = Carrera::where('estado', '1')->get();
        $planE = Planestudios::where('cve_planest', $id)->first();
        return view('plan_estudios.consultar', compact('planE', 'car'));
    }

    /**
     * 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();
        $planE = Planestudios::where('cve_planest', $id)->first();
        return view('plan_estudios.modificar', compact('planE', 'car'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $plan = Planestudios::findOrFail($id);
        $plan->claveOfi_planest = $request->claveO;
        $plan->iduni_planest = $request->iduni;
        $plan->nombre_planest = $request->nom;
        $plan->reticula_planest = $request->reticula;
        $plan->nivesc_planest = $request->nivEsc;
        $plan->modalidad_planest = $request->modalidad;
        $plan->id = $request->car;
        $plan->save();
        return response()->json(['estado' => 'Exito', 'mensaje' => 'Plan de Estudios actualizada con exito!']);
    }

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

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

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


        return Excel::download(new PlanestudiosExport($claveOfi, $nombre, $reticula, $nivEsc), 'planEstudios.xlsx');
    }

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

        $enca = Encabezadopie::max('cve_encpie');
        $enca = Encabezadopie::where('cve_encpie', $enca)->first();
        if ($claveOfi == "" && $nombre == "" && $reticula == "" && $nivEsc == "") {
            $pdf = Pdf::loadView('planE.tablapdf', ['planEst' => Planestudios::where('status_planest', '1')->get(), 'enca' => $enca]);
        } else {
            $PlanEst = Planestudios::where('status_planest', '1');
            if ($claveOfi != "") {
                $PlanEst = $PlanEst->where('claveOfi_planest', 'like', '%' . $claveOfi . '%');
            }
            if ($nombre != "") {
                $PlanEst = $PlanEst->where('nombre_planest', 'like', '%' . $nombre . '%');
            }
            if ($reticula != "") {
                $PlanEst = $PlanEst->where('reticula_planest', 'like', '%' . $reticula . '%');
            }
            if ($nivEsc != "") {
                $PlanEst = $PlanEst->where('nivesc_planest', 'like', '%' . $nivEsc . '%');
            }
            $PlanEst = $PlanEst->get();
            $pdf = Pdf::loadView('planE.tablapdf', ['planEst' => $PlanEst, 'enca' => $enca]);
        }

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