<?php namespace App\Http\Controllers;

		use Session;
		use Request;
		use DB;
		use CRUDBooster;

		class ApiCariDokumenController extends \crocodicstudio\crudbooster\controllers\ApiController {

		    function __construct() {    
				$this->table       = "peraturan";        
				$this->permalink   = "cari_dokumen";    
				$this->method_type = "get";    
		    }
		

		    public function hook_before(&$postdata) {
		        //This method will be execute before run the main process
                if($postdata['subjek']){
                    $subjek = DB::table('subjek')->where('subjek','like', '%'.$postdata['subjek'].'%')->pluck('id');
                    $persub = DB::table('peraturan_subjek')->whereIn('id_subjek',$subjek)->pluck('id_peraturan');
                    
                    if(sizeof($persub)>0){
                        dd($persub[0]);
                        $postdata['peraturan.id'] = $persub[0]; 
                    }
                   
                }
                if($postdata['judul']){
                    $postdata['singkatan_jenis'] = $postdata['judul'];
                    $postdata['jenis_peradilan'] = $postdata['judul'];
                    $postdata['nomor_panggil'] = $postdata['judul'];
                    $postdata['nomor'] = $postdata['judul'];
                    $postdata['pemrakarsa'] = $postdata['judul'];
                    $postdata['penandatangan'] = $postdata['judul'];
                    $postdata['penerbit'] = $postdata['judul'];
                    $postdata['sumber'] = $postdata['judul'];
                    $postdata['tempat'] = $postdata['judul'];
                    $postdata['teu'] = $postdata['judul'];
                    $postdata['urusan'] = $postdata['judul'];
                    $postdata['isbn'] = $postdata['judul'];
                }
		    }

		    public function hook_query(&$query) {
		        //This method is to customize the sql query

		    }

		    public function hook_after($postdata,&$result) {
		        //This method will be execute after run the main process
                if($result['api_status']==1){
                    foreach($result['data'] as $data){
                        $subjek = DB::table('peraturan_subjek')->join('subjek','subjek.id','peraturan_subjek.id_subjek')->where('peraturan_subjek.id_peraturan',$data->id)->pluck('subjek.subjek');
                        $data->subjek = $subjek;
                        $bidang_hukum = DB::table('peraturan_bidang_hukum')->join('bidang_hukum','bidang_hukum.id','peraturan_bidang_hukum.id_bidang_hukum')->where('peraturan_bidang_hukum.id_peraturan',$data->id)->pluck('bidang_hukum.bidang_hukum');
                        $data->bidang_hukum = $bidang_hukum;
                        $jenis = DB::table('tipe_dokumen')->where('id',$data->jenis_id)->first();
                        $data->jenis = $jenis->nama;
                        $data->parent = $jenis->parent_id;
                        $data->total = sizeof($result['data']);
                    }
                    
                }
		    }

		}