<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\CategoriesDataTable;
use App\Repositories\Shop\Categories;
use App\Repositories\Shop\TagGroups;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function index(CategoriesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.Categories.list');
    }

    public function create()
    {
        $data = [
            'category_id' => 0,
            'categories' => Categories::getOptions(),
            'tags_list' => TagGroups::getTreeTags(),
        ];

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

    public function store(Request $request)
    {
        $ret = Categories::storeFull($request->all());

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

    public function show($id)
    {
        $data = Categories::get($id);

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

    public function edit($id)
    {
        $data['category'] = Categories::getFull($id);
        $data['categories'] = Categories::getOptions();
        $data['tags_list'] = TagGroups::getTreeTags();

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

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

    public function getImages(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data['images'] = Categories::getImages($id);

        return view('components.uploader.mini-gallery-items', $data);
    }

    public function deleteImage(Request $request)
    {
        $id = $request->input('id');
        $index = $request->input('index');

        return Categories::deleteImage($id, $index);
    }

    public function moveTree(Request $request)
    {
        $node_id = $request->input('node_id');
        $target_id = $request->input('target_id');
        $type = $request->input('type');

        return Categories::moveTree($node_id, $target_id, $type);
    }

    public function toggleVisible(Request $request)
    {
        $data = Categories::toggleVisible($request->input('id'), $request->input('visible') === 'true' ? 1 : 0);

        return response()->json(['error' => 0]);
    }

    public function toggleHomepage(Request $request)
    {
        $data = Categories::toggleHomepage($request->input('id'), $request->input('homepage') === 'true' ? 1 : 0);

        return response()->json(['error' => 0]);
    }
}
