<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\DeliveriesDataTable;
use App\Repositories\Shop\Deliveries;
use App\Repositories\Shop\SaleChannels;
use Illuminate\Http\Request;

class DeliveryController extends Controller
{
    public function index(DeliveriesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.Deliveries.list');
    }

    public function create()
    {
        $data = [
            'sale_channels' => SaleChannels::getOptions(),
        ];

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

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

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

    public function show($id)
    {
        $data = [
            'delivery' => Deliveries::get($id),
        ];

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

    public function edit($id)
    {
        $data = [
            'delivery' => Deliveries::get($id)->toArray(),
            'sale_channels' => SaleChannels::getOptions(),
        ];

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

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

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

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