<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Repositories\Shop\Articles;
use App\Repositories\Shop\ArticleNatures;
use App\Repositories\Shop\Categories;
use App\Repositories\Shop\Tags;

use App\Datatables\Shop\ArticlesDataTable;

class ArticleController extends Controller
{
    public function autocomplete(Request $request, $str = false)
    {
        $str = $str ? $str : $request->input('q');
        return response()->json(Articles::autocomplete($str));
    }

    public function index(ArticlesDataTable $dataTable)
    {
        $data['article_natures'] = ArticleNatures::getOptions();
        $data['categories'] = Categories::getOptions();
        $data['tags'] = Tags::getOptionsFullName();
        return $dataTable->render('Admin.Shop.Articles.list', $data);
    }

    public function create()
    {
        $data = Articles::getMeta();
        return view('Admin.Shop.Articles.create', $data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        Articles::storeFull($data);
        return redirect()->route('Admin.Shop.Articles.index');
    }

    public function show($id)
    {
        $data['article'] = Articles::get($id);
        return view('Admin.Shop.Articles.view', $data);
    }

    public function edit($id)
    {
        $data = Articles::getFull($id);
        return view('Admin.Shop.Articles.edit', $data);
    }

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

    public function getProductDescription($product_id, $model)
    {
        $data['article']['inherited'] = Articles::getInheritedByProduct($product_id, base64_decode($model));
        return view('Admin.Shop.Articles.partials.product.description', $data);
    }

    public function getProductTags($product_id, $model)
    {
        $data = Articles::getInheritedByProduct($product_id, base64_decode($model));
        return view('Admin.Shop.Articles.partials.product.tags', $data);
    }

    public function getProductImages($product_id, $model)
    {
        $data['article']['product_id'] = $product_id;
        $data['no_popup'] = false;
        return view('Admin.Shop.Articles.partials.product.images', $data);
    }

    public function getImages(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data['images'] = Articles::getImages($id);
        return view('components.uploader.mini-gallery-items', $data);
    }

    public function deleteImage(Request $request)
    {
        $id = $request->input('id');
        $index = $request->input('index');
        return Articles::deleteImage($id, $index);
    }

    public function toggleVisible(Request $request)
    {
        $data = Articles::toggleVisible($request->input('id'), ($request->input('visible') == 'true') ? 1 : 0);
        return response()->json(['error' => 0]);
    }

    public function toggleHomepage(Request $request)
    {
        $data = Articles::toggleHomepage($request->input('id'), ($request->input('homepage') == 'true') ? 1 : 0);
        return response()->json(['error' => 0]);
    }

}
