<?php

namespace App\Http\Controllers\NGRS;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\{XM_Socioeconomico, XM_SocioeconomicoConfi, XM_Confi};

use Maatwebsite\Excel\Facades\Excel;
use App\Imports\SocioeconomicoCollectionImport;

use App\Exports\{ExamenSocioExport, ResultadosSocioExport};

use Carbon\Carbon;

use App\{RFCarrera, RFAspirante};

class SocioeconomicoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $carreras = RFCarrera::where('tipo', 'LICENCIATURA')->get();
        return view('examen.socioeconomico.resultados.principal')
                    ->with('carreras', $carreras )
                    ->with('no_pag', false);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

        $socioConf = XM_SocioeconomicoConfi::create([
                      'confi_id' => $data['idConf'],
                      'total_preguntas' => '0',
                      'inicio' => Carbon::parse($data['inicio']),
                      'cierre' => Carbon::parse($data['cierre']),
                    ]);

        $socio = XM_Confi::where('periodo_id', $data['sociop'])->with('xmSocioConfi.socioeconomico')->first();
          
        foreach ($socio->xmSocioConfi->socioeconomico as $p) {
                    
            XM_Socioeconomico::create([
                'socioeconomicoConfi_id' => $socioConf->id, 
                'enunciado' => $p['enunciado'], 
                'resp_1' => $p['respuesta_1'], 
                'resp_2' => $p['respuesta_2'], 
                'resp_3' => $p['respuesta_3'], 
                'resp_4' => $p['respuesta_4'], 
                'resp_5' => $p['respuesta_5'], 
                'resp_6' => $p['respuesta_6'], 
                'texto' => $p['texto']=="ok" ? 1 :0 , 
                'componente' => $p['componente']
            ]);

            $i++;
        }

        $socioConf->total_preguntas = $i;
        $socioConf->save();

        $preguntas = $socio->xmSocioConfi;

        return response()->json([
            'estado'  => 'exito', 
            'mensaje' => "Total de registros analizados: $i", 
            'id'      => $socioConf->id,
            'vista'   => view('examen.ingreso.confi.socioeconomico.respuesta', compact(['preguntas', 'i'])).""
        ]);
    }

    /**
     * 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)
    {
        //
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function import(){
        $h = Excel::import(new SocioeconomicoCollectionImport, request()->file('file'));
        return response()->json(['status'=>'success', 'message'=>'Importación Exitosa!!'.$h]);
    }

    public function export(Request $request){
        $data = $request->all();
        $reactivos = XM_Confi::where('periodo_id', $data['periodo'])->with('xmSocioConfi.socioeconomico')->first();
        return new ExamenSocioExport($reactivos);
    }

    public function result(Request $request){
        $data = $request->all();
        //aqui hacer condiciones de la consulta de acuerdo al filtro seleccionado
        $preguntas = XM_Socioeconomico::where('socioeconomicoConfi_id', '2')->get();
        $aspirantes = RFAspirante::whereHas('socio_resp')
                                ->with('socio_resp', 'carrera')
                                ->where('fk_idPeriodo', '9')
                                ->where('id', '!=', '1')
                                ->get();

        //dd($aspirantes);
        return new ResultadosSocioExport($aspirantes, $preguntas);
    }
    
}
