<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ConverterController extends Controller
{
    public function showForm()
    {
        return view('convert');
    }

    public function convert(Request $request)
    {
        $kg = $request->input('kg');
        $lbs = $request->input('lbs');
        $result = null;

        // Convert one direction based on which field is filled
        if ($kg && !$lbs) {
            $lbs = $kg * 2.20462;
            $result = "{$kg} KG = " . number_format($lbs, 2) . " lbs";
        } elseif ($lbs && !$kg) {
            $kg = $lbs / 2.20462;
            $result = "{$lbs} lbs = " . number_format($kg, 2) . " KG";
        } else {
            $result = "Please fill only one field!";
        }

        return view('convert', [
            'kg' => $kg,
            'lbs' => $lbs,
            'result' => $result,
        ]);
    }
}
