<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Pernyataan_Instrument;
use App\Models\Sub_Pernyataan_Instrument;
use App\Models\Sub_Instrument;
use App\Models\Fakultas;
use App\Models\Instrument;
use App\Models\Tahun;
use App\Models\Hasil_Instrument;
use Illuminate\Http\Request;
use PDF;

class LembarInstrumentMonitoringController extends Controller
{
    private $indexRoute = null;

    public function __construct(){
        $this->indexRoute = route('pernyataan_instrument.index');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        
        $data['filter_instrument'] = $request->input('instrument');
        
        $data['filter_tahun'] = $request->input('tahun'); 

        $data['instrumentdetail']              = Instrument::query()->where('id', $data['filter_instrument'])->first();
       
        $data['pernyataan_instrument']         = Pernyataan_Instrument::query()->orderBy('created_at','asc')->get();
        $data['sub_instrument']                = Sub_Instrument::query()->with('jenis_input')
                                                                        ->where('id_instrument',$data['filter_instrument'] )
                                                                        ->orderBy('created_at','asc')->get();
        $data['sub_pernyataan_instrument']     = Sub_Pernyataan_Instrument::query()->orderBy('sub_peryataan')->get();
        $data['fakultas']                      = Fakultas::query()->orderBy('id')->get();
        
        $data['instrument']                    = Instrument::query()->orderBy('instrument')->get();
        $data['tahun']                         = Tahun::query()->orderBy('tahun')->get();

        $data['hasil_instrumen'] = Hasil_Instrument::query()->get();
        $data['arr_hasil'] = [];

        foreach($data['hasil_instrumen'] as $item){
            $data['arr_hasil'][$item->id_pernyataan_instrument][$item->id_sub_pernyataan_instrument][$item->id_fakultas][$item->tahun] = $item;
        }
        
        $data['arr_hasil_total'] = [];
        foreach($data['hasil_instrumen'] as $item)
        {
            $data['arr_hasil_total'][$item->id_sub_instrument][$item->id_fakultas][$item->tahun] = $item;
        }

        //dd($data['arr_hasil_total']->toArray());
    
        return view('content.backend.masters.lembar_instrument_monitoring.index', $data);
    }

    public function filter(Request $request)
    {
      
       

        return redirect()->intended($this->indexRoute);
    }

    /**
     * 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)
    {
        //dd($request->all());
        foreach ($request->input('jumlah_pernyataan_instrument') as $index => $value) 
        {
            if(!empty($request->jumlah_pernyataan_instrument[$index]))
            {
                    $post   = Hasil_Instrument::updateOrCreate(
                        [
                        'id_pernyataan_instrument'      => $request->id_pernyataan_instrument[$index],
                        'tahun'                         => $request->input('tahun'),
                        'id_fakultas'                   => $request->id_fakultas[$index],
                        'id_sub_instrument'             => $request->id_sub_instrument,
                    ],
                    [
                        'jumlah'                        => $request->jumlah_pernyataan_instrument[$index]
                    ]);
            }
                    
               
        }
        
        if(!empty($request->input('jumlah_sub_peryataan')))
        {
                foreach ($request->input('jumlah_sub_peryataan') as $idparent => $value) 
                {
                    foreach ($value as $idsubparent => $value1) 
                    {
                        foreach ($value1 as $idfakultas => $value2) 
                        {
                            
                            if(!empty($value2))
                            {
                                            $post   = Hasil_Instrument::updateOrCreate(
                                                [
                                                'id_pernyataan_instrument'      => $idparent,
                                                'id_sub_pernyataan_instrument'  => $idsubparent,
                                                'tahun'                         => $request->input('tahun'),
                                                'id_fakultas'                   => $idfakultas,
                                                
                                                'id_sub_instrument'             => $request->id_sub_instrument,
                                                ],
                                                [
                                                    'jumlah'                        => $value2,
                                                ]);
                            }
                        }
                        
                    }   
                     
                                
                }

        }

        $fakultas     = Fakultas::query()->orderBy('id')->get();
        foreach($fakultas as $data)
        {
            $total = Hasil_Instrument::where('id_sub_instrument', $request->id_sub_instrument)
            ->where('id_fakultas',$data->id) 
            ->where('tahun',$request->input('tahun'))                                     
            ->sum('jumlah');
            
            $update = Hasil_Instrument::where('id_sub_instrument', $request->id_sub_instrument)
                ->where('id_fakultas',$data->id) 
                ->where('tahun',$request->input('tahun'))    
                ->update(['total' => $total]);       
        }
            
        return redirect()->back()->with('success', 'Data Berhasil Disimpan');   
       
    }

   
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function cetak_pdf(Request $request)
    {
        $data['no_dokumen']       =  $request->input('no_dokumen');
        $data['revisi']           =  $request->input('revisi');
        $data['tanggal_terbit']   =  $request->input('tanggal_terbit');
        $data['filter_instrument']   =  $request->input('filter_instrument');
        $data['filter_tahun']        =  $request->input('filter_tahun');
        $data['instrumentdetail']              = Instrument::query()->where('id', $request->input('filter_instrument'))->first();
       
        $data['pernyataan_instrument']         = Pernyataan_Instrument::query()->orderBy('pernyataan_instrument')->get();
        $data['sub_instrument']                = Sub_Instrument::query()
                                                                ->where('id_instrument',$request->input('filter_instrument'))
                                                                ->orderBy('id_instrument')->get();
        $data['sub_pernyataan_instrument']     = Sub_Pernyataan_Instrument::query()->orderBy('sub_peryataan')->get();
        $data['fakultas']                      = Fakultas::query()->orderBy('id')->get();

        $data['hasil_instrumen'] = Hasil_Instrument::query()->get();
        $data['arr_hasil'] = [];

        foreach($data['hasil_instrumen'] as $item){
            $data['arr_hasil'][$item->id_pernyataan_instrument][$item->id_sub_pernyataan_instrument][$item->id_fakultas][$item->tahun] = $item;
        }
        
        $data['arr_hasil_total'] = [];
        foreach($data['hasil_instrumen'] as $item)
        {
            $data['arr_hasil_total'][$item->id_sub_instrument][$item->id_fakultas][$item->tahun] = $item;
        }
       
    	$pdf = PDF::loadview('content.backend.masters.lembar_instrument_monitoring.laporan', $data)->setPaper('a4', 'landscape');
    	return $pdf->stream('laporan-pegawai-pdf');
    }
}
