<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TruckController extends Controller
{
    public function truckIn(Request $request)
    {
        DB::table('trucks')->insert([
            'truck_no' => $request->truck_no,
            'driver_name' => $request->driver_name,
            'phone' => $request->phone,
            'product' => $request->product,
            'weight_in' => $request->weight,
            'status' => 'IN'
        ]);

        return response()->json(['ok' => true]);
    }

    public function truckOut(Request $request)
    {
        $truck = DB::table('trucks')
            ->where('truck_no', $request->truck_no)
            ->orderBy('id', 'desc')
            ->first();

        $net = $request->weight - $truck->weight_in;

        DB::table('trucks')
            ->where('id', $truck->id)
            ->update([
                'weight_out' => $request->weight,
                'net_weight' => $net,
                'status' => 'OUT'
            ]);

        return response()->json(['net_weight' => $net]);
    }
}