<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Transportation;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Validator;
use Helper;


class TransportationController extends Controller
{
    public function __construct()
    {
        DB::enableQueryLog();
    }

    public function getAll()
    {
        $query = Transportation::get();

        if($query){
            $result = $query;

            $res_status = true;
            $msg = 'Mendapatkan Data';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);

        }else {

            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }

    public function getByUser($user_id)
    {
        $query = Transportation::where('user_id', Helper::hash($user_id, 'decode'))->findOrFail();

        if($query){
            $result = $query;

            $res_status = true;
            $msg = 'Mendapatkan Data';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);

        }else {

            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }

    public function postStore(Request $request)
    {
        $post = $request->all();
        // $file = $request->file('gambar');
        //dd($post);
        /**digunakan untuk set rule validator */
        $rules = [
            'gambar' => 'required',
            // 'gambar' => 'required|file|max:1000',
        ];
        /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
        // $alert = [
        //     'file'  => 'File maksimal 1MB'
        // ];
        // $validator = Validator::make($post, $rules, $alert);
        $validator = Validator::make($post, $rules);

        if ($validator->passes()) {
            /**menggunakan transaction */
            DB::beginTransaction();

            // $nama_file = time()."_".$file->getClientOriginalName();

            // $tujuan_upload = 'gambar';

            // //$file->move($tujuan_upload,$nama_file);
            // Storage::putFileAs('public/images/transportation/', $file, $nama_file );

            $query                              = new Transportation();
            $query->name                        = $request->name;
            $query->transportation_identity     = $request->transportation_identity;
            $query->transportation_type         = $request->transportation_type;
            $query->transportation_photo        = Helper::base64_to_image($request->gambar, 'transportation');
            $query->user_id                     = 123456;
            $query->save();
            if($query){
                $result = $query;
    
                $res_status = true;
                $msg = 'Mendapatkan Data';
                $status_msg = $msg;
    
                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
    
            }else {
    
                $res_status = false;
                $msg = 'Data tidak ditemukan';
                $status_msg = $msg;
    
                return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
            }
        }
    }

    public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
    {
        $response['response'] = array(
            'status'     => $status,
            'message'    => $msg,
            'status_msg' => $status_msg,
        );
        $response['param'] = !empty($param) ? $param : '';

        $response['results'] = $result;

        return response()->json($response, 200);
    }


}