<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\ShippingMethod;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Image;

class ShippingMethodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = ShippingMethod::query();
        if($request->has('q')) {
            $query->where('title', 'LIKE', "%{$request->q}%");
        }
        if($request->has('active')) {
            $query->where('active', $request->active);
        }
        $shipping_methods = $query->paginate(10);

        return view('backend.shipping_methods.index', [
            'shipping_methods' => $shipping_methods,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(ShippingMethod $shipping_method)
    {
        $shipping_methods = ShippingMethod::orderBy('title')->pluck('title', 'id')->toArray();

        return view('backend.shipping_methods.form', [
            'shipping_method' => $shipping_method,
            'shipping_methods' => $shipping_methods
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $shipping_method = ShippingMethod::create($request->except('_method', '_token', 'image'));

            return redirect()->route('backend.shipping_methods.edit', $shipping_method->id)->with('status', 'El método de envío ha sido creado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

    public function upload($request, $shipping_method)
    {
        if($file = $request->file('image')) {
            $path = $file->hashName();
            $image = Image::make($file)->resize(576, 576, function($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });

            Storage::put($path, $image->encode());

            $url = Storage::url($path);

            $shipping_method->fill([
                'image' => $url,
            ])->save();
        }
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ShippingMethod  $shipping_method
     * @return \Illuminate\Http\Response
     */
    public function edit(ShippingMethod $shipping_method)
    {
        return view('backend.shipping_methods.form', [
            'shipping_method' => $shipping_method,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\ShippingMethod  $shipping_method
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ShippingMethod $shipping_method)
    {
        try {
            $shipping_method->fill($request->except('_method', '_token'))->save();

            return back()->with('status', 'El método de envío ha sido editado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\ShippingMethod  $shipping_method
     * @return \Illuminate\Http\Response
     */
    public function destroy(ShippingMethod $shipping_method)
    {
        try {
            $shipping_method->delete();

            return redirect()->route('backend.shipping_methods.index')->with('status', 'El método de envío ha sido eliminado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

    public function order(Request $request)
    {
        foreach($request->post as $key => $id) {
            ShippingMethod::where('id', $id)->update([
                'order' => $key
            ]);
        }
    }
}
