<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreClientRequest;
use App\Http\Requests\UpdateClientRequest;
use App\Models\Client;
use Illuminate\Support\Facades\Log;

class ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $client = Client::all();
        return view('client.client', ['clients' => $client]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('client.clientForm');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreClientRequest $request)
    {
        // Retrieve the validated input data...
        $validated = $request->validated();

        // include this column as not passed from ui
        $validated['CREATED_BY'] = auth()->user()->name;
        $validated['MODIFIED_BY'] = auth()->user()->name;

        // Store the client data
        Client::create($validated);

        // return a response to the user and redirect
        return redirect()->route('client.index')->with("success", "Client created successfully!");
    }

    /**
     * Display the specified resource.
     */
    public function show(Client $client)
    {
        return view('client.clientDetails', ['client' => $client]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Client $client)
    {
        return view('client.clientEdit', ['client' => $client]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateClientRequest $request, Client $client)
    {
        // get validated data
        $validated = $request->validated();

        // update the data in db
        Client::query()->where('RID', $client->rid)->update([
            'NAME_SH' => $validated['NAME_SH'],
            'NAME_FL' => $validated['NAME_FL'],
            'ADDRESS' => $validated['ADDRESS'],
            'BANK_ACC' => $validated['BANK_ACC'],
            'DISCOUNT' => $validated['DISCOUNT'],
            'BOSS_SURN' => $validated['BOSS_SURN'],
            'BOSS_NAME' => $validated['BOSS_NAME'],
            'BOSS_OTHNAME' => $validated['BOSS_OTHNAME'],
            'BOSS_POST' => $validated['BOSS_POST'],
            'BOSS_PHONE' => $validated['BOSS_PHONE'],
            'BOSS_CELL' => $validated['BOSS_CELL'],
            'BOSS_EMAIL' => $validated['BOSS_EMAIL'],
            'BOSS_FAX' => $validated['BOSS_FAX'],
            'ACC_SURN' => $validated['ACC_SURN'],
            'ACC_NAME' => $validated['ACC_NAME'],
            'ACC_OTHNAME' => $validated['ACC_OTHNAME'],
            'ACC_POST' => $validated['ACC_POST'],
            'ACC_PHONE' => $validated['ACC_PHONE'],
            'ACC_CELL' => $validated['ACC_CELL'],
            'ACC_EMAIL' => $validated['ACC_EMAIL'],
            'ACC_FAX' => $validated['ACC_FAX'],
            'STATUS_CL' => $validated['STATUS_CL']
        ]);

        // return a response to the user and redirect
        return redirect()->route('client.index')->with("success", "Client information updated successfully!");
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Client $client)
    {
        //
    }
}
