<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\MerchandisesDataTable;
use App\Http\Requests\Admin\Shop\StoreMerchandisePost;
use App\Repositories\Shop\Merchandises;
use Illuminate\Http\Request;

class MerchandiseController extends Controller
{
    public function index(MerchandisesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.Merchandises.list');
    }

    public function create()
    {
        $data = Merchandises::init();

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

    public function store(StoreMerchandisePost $request)
    {
        $data = $request->all();
        Merchandises::storeFull($data);

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

    public function edit($id)
    {
        $data = Merchandises::init();
        $data['merchandise'] = Merchandises::getFull($id);

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

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

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

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

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

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

    public function getOptions()
    {
        return response()->json(['0' => ''] + Merchandises::getOptions());
    }
}
