<?php

namespace App\Http\Controllers;

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

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

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

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

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

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

    public function update(Request $request, $id)
    {
        DB::table('districts')->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('districts.index');
    }

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