<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Departamento;
use App\Semestre;
use App\DetalleMateria;
use App\Correquisito;
use App\DetalleMateriaCorrequisito;
use App\Prerrequisito;
use App\DetalleMateriaPrerrequisito;
use App\Materia;

class DetalleMateriaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $data = $request->all();
        //->where('cve_planest', $data['planE'])
        $deto = Departamento::where('academico', 1)->get();
        $mat = Materia::where('status_mat', 1)->get();
        return view('semestre.nuevo', compact('mat', 'deto','data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->all();
        $n=$data['x']+1;
        $sem=Semestre::where('cve_rec',$data['ret'])->where('no_sem',$n)->first();
        $dm=DetalleMateria::create(['esp_detmat'=>$data['materiaEsp'],'x_detmat'=>$data['x'],'y_detmat'=>$data['y'],'id'=>$data['departamento'],'cve_sem'=>$sem->cve_sem,'cve_mat'=>$data['materia']]);
        $cad = json_decode($data['cadena']);
        $corr = json_decode($data['correq']);
        $pre = json_decode($data['prerr']);
        if ($cad != []){
            foreach ($cad as $c) {
                Cadena::create(['cve_detmat'=>$dm->cve_detmat, 'cve_mat'=>$c->id]);
            }
        }
        if ($corr != []) {
            foreach ($corr as $cq) {
                $crr=Correquisito::create(['cve_mat'=>$cq->id]);
                DetalleMateriaCorrequisito::create(['cve_detmat'=>$dm->cve_detmat,'cve_corr'=>$crr->cve_corr]);
            }
        }
        if ($pre != []) {
            foreach ($pre as $p) {
                $prr=Prerrequisito::create(['cve_mat'=>$p->id]);
                DetalleMateriaPrerrequisito::create(['cve_detmat'=>$dm->cve_detmat,'cve_pre'=>$prr->cve_pre]);
            }
        }
        $tam=Semestre::where('cve_rec',$data['ret'])->count();
        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia agregada a la reticula con exito!','tam'=>$tam]);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $dm=DetalleMateria::with('departamento')->with('materia')->with('cadena.materia')->with('DetalleMatateriaP.Prerrequisito.materia')->with('DetalleMateriaC.Correqui.materia')->where('cve_detmat',$id)->first();
        $sem=Semestre::where('cve_sem',$dm->cve_sem)->first();
        $deto = Departamento::where('academico', 1)->get();
        $mat = Materia::where('status_mat', 1)->get();
        return view('semestre.modificar', compact('dm','mat','deto','sem'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->all();
        $dm=DetalleMateria::findOrFail($id);
        $rec=semestre::where('cve_sem',$dm->cve_sem)->first();
        $dm->cve_mat=$data['materia'];
        $dm->id=$data['departamento'];
        $dm->esp_detmat=$data['materiaEsp'];
        $dm->save();
        $cad = json_decode($data['cadena']);
        $corr = json_decode($data['correq']);
        $pre = json_decode($data['prerr']);
        if ($cad != []){
            Cadena::where('cve_detmat',$id)->delete();
            foreach ($cad as $c) {
                Cadena::create(['cve_detmat'=>$id, 'cve_mat'=>$c->id]);
            }
        }
        if ($corr != []) {
            foreach ($corr as $cq) {
                $crr=DetalleMateriaPrerrequisito::where('cve_detmat',$id)->delete();
                $crr=Correquisito::create(['cve_mat'=>$cq->id]);
                DetalleMateriaCorrequisito::create(['cve_detmat'=>$id,'cve_corr'=>$crr->cve_corr]);
            }
        }
        if ($pre != []) {
            $prr=DetalleMateriaCorrequisito::where('cve_detmat',$id)->delete();
            foreach ($pre as $p) {
                $prr=Prerrequisito::create(['cve_mat'=>$p->id]);
                DetalleMateriaPrerrequisito::create(['cve_detmat'=>$id,'cve_pre'=>$prr->cve_pre]);
            }
        }

        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia actualizada en la Reticula con exito!','tam'=>8,'cve'=>$rec->cve_rec]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $cve="";
        $eprr=DetalleMateriaCorrequisito::where('cve_detmat',$id)->get();
        foreach ($eprr as $el) {
            Correquisito::where('cve_corr',$el->cve_corr)->delete();
        }
        DetalleMateriaCorrequisito::where('cve_detmat',$id)->delete();
        $epre=DetalleMateriaPrerrequisito::where('cve_detmat',$id)->get();
        foreach ($epre as $ep) {
            Prerrequisito::where('cve_pre',$ep->cve_pre)->delete();
        }
        DetalleMateriaPrerrequisito::where('cve_detmat',$id)->delete();
        $dm=DetalleMateria::where('cve_detmat',$id)->first();
        $s=Semestre::where('cve_sem',$dm->cve_sem)->first();
        $cve=$s->cve_rec;
        DetalleMateria::where('cve_detmat',$id)->delete();

        return response()->json(['estado' => 'Exito', 'mensaje' => 'Materia eliminada de la reticula con exito!','cve'=>$cve]);
    }
}
