<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\JenisBarang;
use App\Models\Barang;
use App\Models\Divisi;
use App\Models\Pengguna;
use App\Models\SatuanBarang;
use App\Models\Transaksi;

class RestController extends Controller
{
    /** 
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\View\View
     */
    public function get_jenis_barang(Request $request)
    {
            $id = $request->id;

            $data = JenisBarang::where('id',$id)->first();

            echo json_encode($data);
    }
    public function show_jenis_barang()
    {
            

            $data = JenisBarang::all();

            foreach($data as $i){

                $arr[] = array(
                        'stok' => \Helperku::get_stok_jenis_barang($i->id,'awal'),
                        'nama_jenis' => $i->nama_jenis
                );
            }

            echo json_encode($arr);
    }
    public function get_divisi(Request $request)
    {
            $id = $request->id;

            $data = Divisi::where('id',$id)->first();

            echo json_encode($data);
    }

    public function get_pengguna(Request $request)
    {
            $id = $request->id;

            $data = Pengguna::where('id',$id)->first();

            echo json_encode($data);
    }
    public function get_satuan(Request $request)
    {
            $id = $request->id;

            $data = SatuanBarang::where('id',$id)->first();

            echo json_encode($data);
    }

    public function get_barang(Request $request)
    {
            $id = $request->id;

            $data = DB::table('tbl_barang')
                            ->join('tbl_satuan','tbl_satuan.id','=','tbl_barang.satuan_id')
                            ->join('tbl_jenis_barang','tbl_jenis_barang.id','=','tbl_barang.jenis_barang_id')
                            ->where('tbl_barang.id',$id)
                            ->get();
                            
        $stok = array(
                'stok'=>\Helperku::get_stok_barang($data[0]->id,'now')
        );
        
        echo json_encode($data);
    }

    public function get_detail_pengguna()
    {

        $id = $request->id;
        // $data = Pengguna::
    }
    public function get_detail_stok(Request $request)
    {

        $id = $request->id;
        
        $stok_rusak = \Helperku::get_stok_barang($id,'rusak');
        $stok_pinjam = \Helperku::get_stok_barang($id,'pinjam');
        $stok_now = \Helperku::get_stok_barang($id,'now');
        $stok_awal = \Helperku::get_stok_barang($id,'awal');
        $data_barang = DB::table("tbl_barang")->where('id',$id)->get();
        // $data = Pengguna::

        $data = array(
                'stok_awal'=>$stok_awal,
                'stok_rusak'=>$stok_rusak,
                'stok_now'=>$stok_now,
                'stok_pinjam'=>$stok_pinjam,
                'nama_barang'=>$data_barang[0]->nama_barang
        );
        echo json_encode($data);
    }


    //HTML 

    public function get_html_barang_rusak()
    {

        $data = Transaksi::join('tbl_barang','tbl_barang.id','tbl_barang_keluar.barang_id')
                        ->join('tbl_satuan','tbl_satuan.id','=','tbl_barang.satuan_id')
                        ->join('tbl_jenis_barang','tbl_jenis_barang.id','=','tbl_barang.jenis_barang_id')
                        ->whereNull('tbl_barang_keluar.pengguna_id')
                        ->get();

        
		ob_start();
		?>

                <thead class="thead-light">
                  <tr>
                    <th scope="col" class="sort" data-sort="budget">Barang</th>
                    <th scope="col" class="sort" data-sort="status">Lokasi Keluar</th>
                    <th scope="col" class="sort" data-sort="completion">Jumlah Keluar</th>
                    <th scope="col"> Tanggal Rusak</th>
                    <th scope="col"> Action</th>
                  </tr>
                </thead>
                <tbody class="list isi_data">


                <?php foreach($data as $i){ ?>
                        <tr>
                            <td><?=  $i->nama_barang ?>  <br>
                                <span class="badge badge-primary">S/N : <?= $i->serial_number ?></span> <span class="badge badge-success"><?= $i->nama_jenis ?></span>
                            <td> <a href="https://maps.google.com/?q=<?= $i->lokasi_keluar ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                                 <?= $i->lokasi_keluar ?>  <span class="badge badge-light"><i class="fas fa-map-marker text-success"></i></span>
                            </a></td>
                            <td><?= $i->jumlah_keluar ?> / <?= $i->nama_satuan ?></td>
                            <td><span class="fas fa-calendar-check"></span> <?= $i->tanggal_keluar ?></td>
                            <td class="text-right">
                   
                   <a data-id="<?= $i->id ?>" href="#" data-toggle="tooltip" data-placement="top" title="Edit"   class=" modal_edit text-right btn btn-md btn-outline-success"><i class="fas fa-edit  text-right"></i> </a>
                   <a data-toggle="tooltip" data-placement="top" title="Hapus" onclick="return confirm('barang ini akan di delete ... ?')" href="<?= route('barang.hapus',$i->id) ?>" class=" text-right btn btn-md btn-outline-danger"><i class="fas fa-trash"></i> </a>
                   <a data-id="<?= $i->id ?>" href="#" data-toggle="tooltip" data-placement="top" title="Barang Keluar"   class=" modal_keluar text-right btn btn-md btn-outline-primary"><i class="fas fa-arrow-right  text-right"></i> </a> 
             </td>
                            
                        </tr>
                <?php } ?>
                </tbody>

                 
		
		<?php
		$output = ob_get_clean();
		ob_flush();
		
		echo $output; 


    }



}

?>