<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\ShoppingOrder;
use App\Services\Payment;
use App\User;

class SalesController extends Controller
{
    protected $userShopRepo;

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

   
    public function index()
    {
        $data = [
        ];
        return view('user.sales.index', $data);
    }

    public function detail($id)
    {
        $user = User::find(\Auth::user()->id);
        $shopping_order = ShoppingOrder::findOrFail($id);
        if($shopping_order->member_id !== $user->id){
            abort(404);
        }
        if($shopping_order->payment_for !== 6 && $shopping_order->payment_for !== 7 && $shopping_order->payment_for !== 8){
            return redirect(route('user_myorder_detail', [$shopping_order->id]));
            abort(403, 'Beraterbestellung');
        }
        $data = [
             'shopping_order' => $shopping_order,
             'isAdmin' => false,
        ];
        
        return view('user.sales.detail', $data);
    }

    public function datatable(){

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

        return \DataTables::eloquent($query)
            ->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
                return '<a href="' . route('user_sales_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 '<span class="no-line-break">'.$ShoppingOrder->getFormattedTotalShipping()." €</span>";
            })
            ->addColumn('orders', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->shopping_user ? $ShoppingOrder->shopping_user->orders : '';
            })
            ->addColumn('user_shop_id', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->user_shop ? '<a href="'.$ShoppingOrder->user_shop->getSubdomain(false).'" target="_blank">'.$ShoppingOrder->user_shop->getSubdomain(false).'</span>' : '';
            })
            ->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getPaymentForTypeBadge($ShoppingOrder);
            })
            ->addColumn('shipped', function (ShoppingOrder $ShoppingOrder) {
                return '<span class="badge badge-pill badge-'.$ShoppingOrder->getShippedColor().'">'.$ShoppingOrder->getShippedType().'</span>
                ';
            })
            ->addColumn('invoice', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->isInvoice() ? '<span class="no-line-break"><a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'download']).'" class="btn btn-primary btn-xs"><i class="fa fa-download"></i></a> 
                    <a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'stream']).'" target="_blank" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i></a></span>' : '-';
            })
            ->orderColumn('payment_for', 'payment_for $1')
            ->orderColumn('id', 'id $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('user_shop_id', 'user_shop_id $1')
            ->orderColumn('total_shipping', 'total_shipping $1')

            ->rawColumns(['id', 'txaction', 'user_shop_id', 'total_shipping', 'invoice', 'shipped', 'payment_for'])
            ->make(true);
    }
    

}