<?php


namespace App\Http\Controllers\User;
use Auth;
use Yard;
use Request;
use App\User;
use Validator;
use App\Services\Util;
use App\Models\Product;
use App\Models\UserShop;
use App\Services\Payment;
use App\Models\UserHistory;
use App\Models\ShoppingUser;
use App\Models\ShoppingOrder;
use App\Models\ProductCategory;
use App\Models\ShippingCountry;
use App\Models\ShoppingInstance;
use App\Http\Controllers\Controller;
use App\Models\ProductBuy;

class MyOrderController extends Controller
{

    public function __construct()
    {
        $this->middleware('active.account');
    }

    public function index()
    {

        $data = [
        ];
        return view('user.order.index', $data);
    }

    public function detail($id)
    {
        $user = User::find(\Auth::user()->id);
        $shopping_order = ShoppingOrder::findOrFail($id);
        if($shopping_order->auth_user_id !== $user->id){
            abort(404);
        }
        $shopping_order->getLastShoppingPayment();

        $data = [
             'shopping_order' => $shopping_order,
             'isAdmin' => false,
        ];
        return view('user.order.detail', $data);
    }

    public function datatable(){

        $user = User::find(\Auth::user()->id);
        $query = ShoppingOrder::with('shopping_user', 'shopping_payments')->select('shopping_orders.*')->where('auth_user_id', '=', $user->id)->where('txaction', '!=', NULL);

        return \DataTables::eloquent($query)
            ->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
                return '<a href="' . route('user_myorder_detail', [$ShoppingOrder->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
            })
            ->addColumn('created_at', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->created_at->format("d.m.Y");
            })
            ->addColumn('txaction', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getShoppingOrderBadge($ShoppingOrder);
            })
            ->addColumn('total_shipping', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->getFormattedTotalShipping()." €";
            })
            ->addColumn('payment', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->getLastShoppingPayment('getPaymentType');
            })
            ->addColumn('shipped', function (ShoppingOrder $ShoppingOrder) {
                return '<span class="badge badge-pill badge-'.$ShoppingOrder->getShippedColor().'">'.$ShoppingOrder->getShippedType().'</span>';
            })
            ->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getPaymentForTypeBadge($ShoppingOrder);
            })
            ->addColumn('reference', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->getLastShoppingPayment('reference');
            })
            ->orderColumn('id', 'id $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('payment_for', 'payment_for $1')
            ->orderColumn('shipped', 'shipped $1')
            ->orderColumn('total_shipping', 'total_shipping $1')
            ->rawColumns(['id', 'txaction', 'payment_for', 'shipped'])
            ->make(true);
    }

}