<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Modules\Main\Models\Complex;
use Modules\Main\Models\City;

class AdminComplexesController extends Controller
{
    public function index(Request $request)
    {
        $complexes = Complex::orderBy('name');
        switch ( $request->filter )
        {
            case 'trashed':
                $complexes->onlyTrashed();
                break;
            case 'actual':
                break;
            default:
                $complexes->withTrashed();
                break;
        }
        //}
        $complexes = $complexes->get();
        return view('admin::complexes.index', [
            'complexes' => $complexes,
            'cities'  => City::orderBy('name')->get(),
            'filter'    => $request->filter
        ]);
    }
    public function edit(Complex $complex)
    {
        return view('admin::complexes.edit', [
            'complex' => $complex,
            'cities'  => City::orderBy('name')->get(),
            'backUrl' => route(name: 'admin.complexes')
        ]);
    }

    public function update(Request $request, Complex $complex)
    {
        $complex->update($request->only('name', 'city_id'));
        return to_route('admin.complexes');
    }

    public function create(Request $request)
    {
        $validated = $request->validate([
            'name' => "required",
            'city_id' => "required",
        ],
            [
                'name.required' => 'Необходимо указать название жилого комплекса',
                'city_id.required' => 'Необходимо указать город'
            ]
        );
        $city = Complex::create($request->only('name', 'city_id'));
        return to_route('admin.complexes');
    }

    public function delete(Complex $complex)
    {
        $complex->delete();
        return to_route('admin.complexes');
    }

    public function restore(Complex $complex)
    {
        $complex->restore();
        return to_route('admin.complexes');
    }

}
