<?php

namespace App\Http\Controllers;




use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShippingPrice;
use Request;
use Validator;


class ShippingController extends Controller
{

    public function __construct()
    {
        $this->middleware('superadmin');

    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data = [
            'values' =>  Shipping::all(),
        ];
        return view('admin.shipping.index', $data);
    }

    public function edit($shipping_id)
    {
        if($shipping_id === "new"){
            $shipping = new Shipping();
            $shipping->active = 1;

        }else{
            $shipping = Shipping::findOrFail($shipping_id);

        }
        $data = [
            'value' => $shipping,
        ];
        return view('admin.shipping.edit', $data);


    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function store(Request $request)
    {
        $shipping = false;
        $data = Request::all();

        if($data['action'] === 'shipping'){
            if ($data['id'] === "new") {
                $shipping = new Shipping();
                $rules = array('name' => 'required');
            } else {
                $shipping = Shipping::findOrFail($data['id']);
                $rules = array('name' => 'required');
            }
            $ret = ['value' => $shipping];
            $validator = Validator::make(Request::all(), $rules);
            if ($validator->fails()) {
                return view('admin.shipping.edit', $ret)->withErrors($validator);
            }
            $data = Request::all();
            $shipping->name = $data['name'];
            $shipping->free = $data['free'];
            $shipping->active = isset($data['active']) ? true : false;
            $shipping->save();
        }

        if($data['action'] === 'price'){
            $shipping = Shipping::findOrFail($data['shipping_id']);
            $rules = array('price' => 'required');
            $ret = ['value' => $shipping];
            $validator = Validator::make(Request::all(), $rules);
            if ($validator->fails()) {
                return view('admin.shipping.edit', $ret)->withErrors($validator);
            }
            if ($data['id'] === "new") {
                $price = ShippingPrice::create($data);
            } else {
                $price = ShippingPrice::findOrFail($data['id']);
                if($price->shipping_id != $shipping->id){
                    abort(404);
                }
                $price->fill($data);
                $price->save();
            }

        }
        if($data['action'] === 'country'){
            $shipping = Shipping::findOrFail($data['shipping_id']);
            foreach($data['country_ids'] as $country_id){
                if(ShippingCountry::where('country_id', $country_id)->count() == 0){
                    ShippingCountry::create([
                        'shipping_id' => $shipping->id,
                        'country_id' => $country_id
                    ]);
                }
            }
        }

        if($shipping){
            \Session()->flash('alert-save', true);
            return redirect(route('admin_shipping_edit', [$shipping->id]));
        }
        return redirect(route('admin_shippings'));

    }


    public function deleteShipping($id)
    {
        $model = Shipping::findOrFail($id);
        $model->delete();
        \Session()->flash('alert-success', "Versandkosten gelöscht");
        return redirect('/admin/shippings');
    }

    public function deletePrice($id)
    {
        $model = ShippingPrice::findOrFail($id);
        $shipping = $model->shipping;
        $model->delete();
        \Session()->flash('alert-success', "Preis gelöscht");
        return redirect(route('admin_shipping_edit', [$shipping->id]));
    }

    public function deleteCountry($id)
    {
        $model = ShippingCountry::findOrFail($id);
        if($model->shopping_orders->count()){
            abort(403, 'Einträge vorhanden');
        }
        $shipping = $model->shipping;
        $model->delete();
        \Session()->flash('alert-success', "Preis gelöscht");
        return redirect(route('admin_shipping_edit', [$shipping->id]));
    }

}