<?php

namespace App\Http\Controllers\Admin;

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

class CommonsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param string $model
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
     */
    public function index(Request $request, string $model)
    {

        return view('admin.commons.list1', array_merge(
            $this->search($request, $this->getModel($model)), ['model' => $model]));
    }

    public function create(string $model)
    {
        return view('admin.commons.add', [
            'fields' => $this->getModel($model)::editFields,
            'model' => $model
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param string $model
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request, string $model)
    {
        $this->getModel($model)::query()->create($request->all());

        return back()->withInput();
    }

    /**
     * Display the specified resource.
     *
     * @param string $model
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
     */
    public function show(string $model,$id)
    {
        return view('admin.commons.edit', [
            'fields' =>  $this->getModel($model)::editFields,
            'data' =>  $this->getModel($model)::query()->find($id),
            'model' => $model
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param string $model
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, string $model, $id)
    {
        $this->getModel($model)::query()->find($id)->update($request->except('id'));

        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param string $model
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(string $model, $id)
    {
        $this->getModel($model)::query()->find($id)->delete();

        return back()->withInput();
    }
}
