<?php

namespace App\Http\Controllers;

use App\Order;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class UserController extends Controller
{
    private $lang;
    public function __construct()
    {
        $this->middleware('auth');
        $lang = (session()->has('filterslang')) ? session('filterslang') : app()->getLocale();
        $this->lang = $lang;
    }

    public function index(){

        $data = [];

        $data['user']  = auth('web')->user();
        $data['title'] = __('frontside.user.us_account');
        $data['mod']   = 'index';

        return view('frontend.user',$data);

    }

    public function whishlist(){

        echo 'Lista zelja';
    }


    // Forma za izmenu korisnickih podataka
    public function data() {

        $data = [];

        $data['user']  = auth('web')->user();
        $data['title'] = __('frontside.user.up_data');
        $data['mod']   = 'data';

        return view('frontend.user',$data);
    }

    // Cuvanje izmena korisnickih podataka
    public function storedata(Request $request) {

        $user = auth()->user();

        $this->validate($request, [
            'name' => 'required|min:2|max:255',
            'lname' => 'required|min:2|max:255',
            'phone' => 'required|min:9|max:255',
            'address' => 'required|min:6|max:255',
            'zip_city' => 'required|min:6|max:255',
            'company' => 'nullable|min:2|max:255',
            'current_password' => ['required', function ($attribute, $value, $fail) use ($user) {
                if (!\Hash::check($value, $user->password)) {
                    return $fail(__('validation.current_password')); //
                }
            }],
        ]);



        $user->name = $request->name;
        $user->lname = $request->lname;
        $user->phone = $request->phone;
        $user->address = $request->address;
        $user->zip_city = $request->zip_city;
        $user->company_info = $request->company;

        $user->save();

        $url = locRoute('user.account');

        return redirect($url)->with('status', __('frontside.common.succ_update'));
    }

    // Forma za izmenu lozinke
    public function pass() {

        $data = [];

        $data['user']  = auth('web')->user();
        $data['title'] = __('frontside.user.up_pass');
        $data['mod']   = 'pass';

        return view('frontend.user',$data);
    }

    // Cuvanje izmene lozinke
    public function storepass(Request $request) {

        $user = auth()->user();

        $this->validate($request, [
            'password' => 'required|min:6|confirmed',
            'current_password' => ['required', function ($attribute, $value, $fail) use ($user) {
                if (!\Hash::check($value, $user->password)) {
                    return $fail(__('validation.current_password')); //
                }
            }],
        ]);

        $user->password = bcrypt($request->password);

        $user->save();

        $url = locRoute('user.account');

        return redirect($url)->with('status', __('frontside.common.succ_update'));


    }

    // Forma za izmenu email-a
    public function email() {

        $data = [];

        $data['user']  = auth('web')->user();
        $data['title'] = __('frontside.user.up_email');
        $data['mod']   = 'email';

        return view('frontend.user',$data);

    }

    // Cuvanje izmene email-a
    public function storeemail(Request $request) {

        $user = auth()->user();

        $this->validate($request, [
            'email' => 'required|email|max:255|confirmed|unique:users,email,'.$user->id,
            'current_password' => ['required', function ($attribute, $value, $fail) use ($user) {
                if (!\Hash::check($value, $user->password)) {
                    return $fail(__('validation.current_password')); //
                }
            }],
        ]);



        $user->email = $request->email;

        $user->save();

        $url = locRoute('user.account');

        return redirect($url)->with('status', __('frontside.common.succ_update'));

    }

    // Prikaz svih adresa korisnika
    public function addresses() {
        echo 'Prikaz svih adresa korisnika';
    }

    // Forma za izmenu adrese
    public function address() {
        echo 'Forma za izmenu adrese';
    }

    // Cuvanje izmene adrese
    public function storeaddress() {
        echo 'Cuvanje izmene adrese';
    }

    // Prikaz svih porudzbina korisnika
    public function orders() {

        $data = [];

        $data['user']  = auth('web')->user();
        $data['title'] = __('frontside.user.orders');
        $data['mod']   = 'orders';

        return view('frontend.user',$data);
    }

    // Prikaz porudzbine korisnika
    public function order($orderid) {

        $user = auth('web')->user();
        $userid = $user->id;

        try {
            $order = Order::with('items')->where('user_id', '=', $userid)->where('ord_id', '=', $orderid)->firstOrFail();
        } catch (ModelNotFoundException $ex) {
            $url = locRoute('user.orders');
            return redirect($url)->with('status', __('frontside.orders.ord_not_found'));
        }

        $data = [];

        $data['user']   = $user;
        $data['order']  = $order;
        $data['title']  = __('frontside.orders.order');
        $data['mod']    = 'order';

        return view('frontend.user',$data);
    }

}
