<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\ArticleNaturesDataTable;
use App\Repositories\Shop\ArticleNatures;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ArticleNatureController extends Controller
{
    public function index(ArticleNaturesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.ArticleNatures.list');
    }

    public function create()
    {
        return view('Admin.Shop.ArticleNatures.create');
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $data['slug'] = Str::slug($data['name'], '-');
        $articleNature = ArticleNatures::store($request->all());
        ArticleNatures::storeIcon($articleNature, $request->file('icon'), 'icon');
        ArticleNatures::storeIcon($articleNature, $request->file('icon_selection'), 'icon_selection');

        return redirect()->route('Admin.Shop.ArticleNatures.index');
    }

    public function show($id)
    {
        $data = [
            'article_nature' => ArticleNatures::get($id),
        ];

        return view('Admin.Shop.ArticleNatures.view', $data);
    }

    public function edit($id)
    {
        $data = [
            'article_nature' => ArticleNatures::get($id),
            'product_types' => ArticleNatures::getProductTypes(),
        ];
        $data['article_nature']['icon'] = ArticleNatures::getIcon($id, 'normal', 'icon');
        $data['article_nature']['icon_selection'] = ArticleNatures::getIcon($id, 'normal', 'icon_selection');

        return view('Admin.Shop.ArticleNatures.edit', $data);
    }

    public function destroy($id)
    {
        return ArticleNatures::destroy($id);
    }

    public static function getOptions(Request $request)
    {
        $data = ArticleNatures::getOptionsByProductTypeModel($request->input('product_type'));

        return response()->json(['0' => ''] + $data);
    }
}
