<?php

namespace App\Http\Controllers;

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

class PurposeController extends Controller
{
    public function index()
    {
        $purposes = DB::table('purposes')->get();
        $purpose = null;
        return view('purposes.index', compact('purposes', 'purpose'));
    }

    public function create() { return view('purposes.create'); }

    public function store(Request $request)
    {
        DB::table('purposes')->insert(['uz' => $request->uz ?? $request->name, 'ru' => $request->ru ?? $request->name, 'cr' => $request->cr ?? $request->name, 'created_at' => now(), 'updated_at' => now()]);
        return redirect()->route('purposes.index');
    }

    public function show($id)
    {
        $purpose = DB::table('purposes')->find($id);
        return view('purposes.show', compact('purpose'));
    }

    public function edit($id)
    {
        $purpose = DB::table('purposes')->find($id);
        return view('purposes.edit', compact('purpose'));
    }

    public function update(Request $request, $id)
    {
        DB::table('purposes')->where('id', $id)->update(['uz' => $request->uz ?? $request->name, 'ru' => $request->ru ?? $request->name, 'cr' => $request->cr ?? $request->name, 'updated_at' => now()]);
        return redirect()->route('purposes.index');
    }

    public function destroy($id)
    {
        DB::table('purposes')->delete($id);
        return redirect()->route('purposes.index');
    }
}
