<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreResponsibleRequest;
use App\Models\Dictionary\Area;
use App\Models\Responsible;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ResponsibleController extends Controller
{

    protected array $data = [
        'active'    => 'responsibles',
        'title'     => 'Ответственные',
        'id'        => 'responsibles',
        'header'    => [
            'id'            => 'ID',
            'area-name'     => 'Район',
            'name'          => 'ФИО',
            'phone'         => 'Телефон',
            'created_at'    => 'Дата создания'
        ],
        'searchFields' => [
            'name',
            'phone',
        ],
        'ranges' => [],
        'filters' => [],
    ];
    public function __construct()
    {
        $this->data['areas'] = ['' => '-'] + Area::query()->get()->pluck('name', 'id')->toArray();
    }


    public function index(Request $request)
    {
        $model = new Responsible;
        $this->createDateFilters($model, 'created_at');

        $q = $model::query();
        $this->acceptFilters($q, $request);
        $this->acceptSearch($q, $request);
        $this->setSortAndOrderBy($model, $request);

        $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
        $this->data['responsibles'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();

        return view('responsibles.index', $this->data);
    }

    /**
     * @param StoreResponsibleRequest $request
     * @return RedirectResponse
     */
    public function store(StoreResponsibleRequest $request)
    {
        $responsible = Responsible::query()->create($request->validated());
        Area::query()->where('areas.responsible_id', $responsible->id)->update(['responsible_id' => null]);
        Area::query()->where('id', $request->validated('area_id'))->update(['responsible_id' => $responsible->id]);
        return redirect()->route('responsible.index');
    }

    /**
     * @param Responsible $responsible
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function show(Responsible $responsible)
    {
        $this->data['responsible'] = $responsible;
        return view('responsibles.edit', $this->data);
    }

    /**
     * @param StoreResponsibleRequest $request
     * @param Responsible $responsible
     * @return RedirectResponse
     */
    public function update(StoreResponsibleRequest $request, Responsible $responsible)
    {
        $responsible->update($request->validated());
        Area::query()->where('areas.responsible_id', $responsible->id)->update(['responsible_id' => null]);
        Area::query()->where('id', $request->validated('area_id'))->update(['responsible_id' => $responsible->id]);
        return redirect()->route('responsible.index');
    }

    /**
     * @param Responsible $responsible
     * @return RedirectResponse
     */
    public function destroy(Responsible $responsible)
    {
        $responsible->delete();
        return redirect()->route('responsible.index');
    }
}
