<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\OffersDataTable;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Shop\StoreOfferPost;
use App\Repositories\Shop\ArticleNatures;
use App\Repositories\Shop\Articles;
use App\Repositories\Shop\Offers;
use App\Repositories\Shop\Packages;
use App\Repositories\Shop\Tariffs;
use App\Repositories\Shop\Variations;
use Illuminate\Http\Request;

class OfferController extends Controller
{
    public function index(OffersDataTable $dataTable)
    {
        $data = [
            'article_natures' => ArticleNatures::getOptions(),
            'packages' => Packages::getOptions(),
        ];

        return $dataTable->render('Admin.Shop.Offers.list', $data ?? []);
    }

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

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

    public function edit($id)
    {
        $data = Offers::init();
        $data['offer'] = Offers::getArray($id);

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

    public function store(StoreOfferPost $request)
    {
        $data = $request->all();
        $ret = Offers::store($data);

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

    public function show($id)
    {
        $data = [
            'offer' => Offers::getArray($id),
        ];

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

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

    public function previewArticle($id)
    {
        $data = [
            'article' => Articles::getArticle($id),
        ];

        return view('Admin.Shop.Offers.partials.article', $data);
    }

    public function previewVariation($id)
    {
        $data = [
            'variation' => Variations::getArray($id),
        ];

        return view('Admin.Shop.Offers.partials.variation', $data);
    }

    public function previewTariff($id)
    {
        $data = [
            'tariff' => Tariffs::getPrices($id),
        ];

        return view('Admin.Shop.Offers.partials.tariff', $data);
    }

    public function toggleActive(Request $request)
    {
        $data = Offers::toggleActive($request->input('id'), $request->input('status_id') === 'true' ? 1 : 0);

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