<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Policy;
use App\Output;
use App\Reguler;
use App\Formal;
use App\Internal;
use App\Minute;
use App\Kategori;
use App\Pencarian;

class pencarianController extends Controller
{
    // public function index()
    // {
    //     if ($policies = Policy::all()->get()){
    //     return view('backend.pencarian.index')->with('policies', $policies);
    //     } elseif 
    //          ($ogp = Output::all()){
    //             return view('backend.pencarian.index')->with('ogp', $ogp);
            
    //     } elseif
    //          ($reguler = Reguler::all()){
    //             return view('backend.pencarian.index')->with('regulers', $reguler);
            
    //     } elseif 
    //          ($formal = Formal::all()){
    //             return view('backend.pencarian.index')->with('formals', $formal);
            
    //     } elseif 
    //          ($internal = Internal::all()){
    //             return view('backend.pencarian.index')->with('internals', $internal);
            
    // } elseif 
    //          ($minute = Minute::all()){
    //             return view('backend.pencarian.index')->with('minutes', $minute);
    //         }
        
    // }


    // public function index(Request $request)
    // {

        
    //    $data =array( $policies => Policy::all()->get()->compact('policies',$policies),
    //             $ogp = Output::pluck('nama','tahun','keterangan','file')->compact('ogp',$ogp),
    //             $reguler = Reguler::all('nama')->compact('reguler',$reguler),
    //             $formal = Formal::pluck('nama','tahun','keterangan','file')->compact('formal', $formal),
    //             $internal = Internal::all('id')->compact('internal', $internal),
    //             $minute = Minute::all('id')->compact('minute',$minute)
    //    );
    //    dd($data->$policies);exit();
    // return view('backend.pencarian.index', compact('data', $data));
    // }

    public function index()
    {
        $policies = Policy::all();
        $formal = Formal::all();
        $ogp = Output::all();
        $reguler = Reguler::all();
        $internal = Internal::all();
        $minute = Minute::all();
        $kategoris = Kategori::all();
        $katpol = Kategori::whereIn('id',[1])->get();
         $katogp = Kategori::whereIn('id', [2])->get();
         $katrci = Kategori::whereIn('id', [3])->get();
         $katfor = Kategori::whereIn('id',[4])->get();
         $katmin = Kategori::whereIn('id',[5])->get();
         $katint = Kategori::whereIn('id',[6])->get();


        return view('backend.pencarian.index', ['policies' => $policies, 'formal' => $formal, 'ogp' => $ogp, 'reguler' => $reguler, 'internal' => $internal, 'minute' => $minute, 'kategoris' => $kategoris, 'katpol' => $katpol, 'katogp' => $katogp, 'katrci' => $katrci, 'katfor' => $katfor, 'katmin' => $katmin, 'katint' => $katint ]);

    }

    // public function kiri()
    // {
    //     $kategoris = Kategori::pluck('kode','nama');
    //     return view('backend.pencarian.index', ['kategoris', $kategoris]);
    // }
    
    public function dataOnly(Request $request)
    // public function dataOnly($id = '')
    {
        dd($request->all());exit;
        // dd($id);exit;
        if ($id=='id-policy') {
            $data = Policy::all();
        } else if ($id=='id-ogp') {
            $data = Output::all();
        } else if ($id=='id-reguler') {
            $data = Reguler::all();
        } else if ($id=='id-formal') {
            $data = Formal::all();
        } else if ($id=='id-internal') {
            $data = Internal::all();
        } else if ($id=='id-minute') {
            $data = Minute::all();
        }

        return response()->json($data);
        
    }

    public function caripol()
    {
        // $caripol = Policy::all();
        
        // return view('backend.pencarian.index', compact('policies'));
    }

    public function data(Request $request)
    {

           // if ($request->ajax()) {
                $formal = Formal::orderBy('id','desc');
                return Datatables::of($formal)
                ->addIndexColumn()
                ->addColumn('lampiran', function($formal){
                    return '<a href="'. $formal->url_berkas .'">Download</a>';
                });
    }

    // Tambahan Download Formal 
    public function dataformal(Request $request)
    {
       // if ($request->ajax()) {
            $dataformal = Formal::orderBy('id','desc');
            return Datatables::of($formal)
            ->addIndexColumn()
            ->addColumn('lampiran', function($formal){
                return '<a href="'. $dataformal->url_berkas .'">Download</a>';
            })
           
              ->rawColumns('action', 'lampiran')->make(true);
        // } else {
        //     exit("Not an AJAX request -_-");
        // }
    }

    // public function formal($id,$gbr)
    // {
    //   //  dd($id);
    //   $formal = Formal::orderBy('id','desc');
    //   //return Datatables::of($formal)
    //   return Datatables::of($formal)
    //   ->addIndexColumn()
    //   ->addColumn('lampiran', function($formal){
    //       return '<a href="'. $formal->url_berkas .'">Download</a>';
    //   })

    // public function download($id, $gbr)

    // $dataformal = Formal::find($id);
    // return response()->download($dataformal->file_berkas);
        
    // }
}

