<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\OrdersDataTable;
use App\Http\Controllers\Controller;
use App\Repositories\Shop\OrderMails;
use App\Repositories\Shop\Orders;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function index(OrdersDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.Orders.list');
    }

    public function create()
    {
        return view('Admin.Shop.Orders.create');
    }

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

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

    public function edit($id)
    {
        $data = Orders::edit($id);

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

    public function store(Request $request)
    {
        $order = Orders::store($request->all());
        if ($order->wasChanged('status')) {
            switch ($order->status) {
                case 1:
                    OrderMails::sendPreparation($order->id);
                    break;
                case 2:
                    OrderMails::sendShipping($order->id);
                    break;
                default:
            }
        }

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

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

    public function download($id)
    {
        return Orders::download($id);
    }
}
