<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\PriceListsDataTable;
use App\Datatables\Admin\Shop\TariffsDataTable;
use App\Repositories\Shop\SaleChannels;
use App\Repositories\Shop\Tariffs;
use App\Repositories\Shop\TariffUnities;
use Illuminate\Http\Request;

class TariffController extends Controller
{
    public function autocomplete(Request $request, $str = false)
    {
        $str = $str ? $str : $request->input('q');

        return response()->json(Tariffs::autocomplete($str));
    }

    public function index(TariffsDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.Tariffs.list');
    }

    public function create()
    {
        $data['sale_channels'] = SaleChannels::getOptions();
        $data['statuses'] = Tariffs::getStatuses();
        $data['tariff_unities'] = TariffUnities::getOptions();

        $model = new PriceListsDataTable();
        $data['datatables']['price_lists'] = $model->html();

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

    public function show($id)
    {
        $data['tariff'] = Tariffs::getFull($id);

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

    public function edit($id)
    {
        $data['tariff'] = Tariffs::get($id);
        $data['sale_channels'] = SaleChannels::getOptions();
        $data['statuses'] = Tariffs::getStatuses();
        $data['tariff_unities'] = TariffUnities::getOptions();

        $model = new PriceListsDataTable();
        $data['datatables']['price_lists'] = $model->html();

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

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

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

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