<?php

namespace App\Http\Controllers\Backend;

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

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

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

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

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

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

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

    public function upload($request, $payment_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);

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

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

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

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

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

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