<?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'))->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 getSingle($data_id)
    {
        $query = Transportation::where('id', Helper::hash($data_id, 'decode'))->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 postStore(Request $request)
    {
        $post = $request->all();
        // $file = $request->file('gambar');
        //dd($post);
        /**digunakan untuk set rule validator */
        $rules = [
            'gambar' => 'required',
            'user_id' => '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                     = Helper::hash(Request::all()['user_id'], 'decode');
            $query->save();

            $query->hash_id = Helper::hash($query->id, 'encode');
            $query->user_id = Helper::hash($query->user_id, 'encode');


            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 postUpdate(Request $request)
    {        
        $post = $request->all();

        $rules = [
            'id' => 'required',
            'gambar' => 'required',
            'user_id' => 'required',
        ];

        $validator = Validator::make($post, $rules);

        if(!$validator->fails()){
            $transportation = Transportation::where('id', Helper::hash(Request::all()['id'], 'decode'))->firstOrFail();
            
            $transportation->name                        = $request->name;
            $transportation->transportation_identity     = $request->transportation_identity;
            $transportation->transportation_type         = $request->transportation_type;
            $transportation->transportation_photo        = Helper::base64_to_image($request->gambar, 'transportation');
            $transportation->user_id                     = Helper::hash(Request::all()['user_id'], 'decode');
            $transportation->save();

            $transportation->hash_id = Helper::hash($transportation->id, 'encode');
            $transportation->user_id = Helper::hash($transportation->user_id, 'encode');

            $result = $transportation;
            $res_status = true;
            $msg = 'Berhasil membuat user';
            $status_msg = $msg;

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

        }else {

            $res_status = false;
            $msg = 'Gagal Membuat user';
            $status_msg = $validator->errors();

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

    public function delete()
    {
        $validator = Validator::make(Request::all(), [
            'id' => ['required'],
        ]);
        if(!$validator->fails()){
            $query = Transportation::find(Helper::hash(Request::all()['id'], 'decode'))->detroy();

            if($query){
                $result = $query;
                $res_status = true;
                $msg = 'Data berhasil dihapus';
                $status_msg = $msg;

                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }else{
                $result = $query;
                $res_status = false;
                $msg = 'Data gagal dihapus';
                $status_msg = $msg;

                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }
        }else{
            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $validator->errors();

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

        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);
    }


}