<?php


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


class ShopSalesController extends Controller
{

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

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

    public function orderDetail($id)
    {
        $user = User::find(\Auth::user()->id);
        $shopping_order = ShoppingOrder::findOrFail($id);
        if($shopping_order->member_id !== $user->id){
            abort(404);
        }
        $data = [
             'shopping_order' => $shopping_order,
             'isAdmin' => false,
        ];
        return view('user.shop.sales.order_detail', $data);
    }

    public function ordersDatatable(){

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

        return \DataTables::eloquent($query)
            ->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
                return '<a href="' . route('user_shop_order_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('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>' : '';
            })

            ->orderColumn('id', 'id $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('user_shop_id', 'user_shop_id $1')
            ->rawColumns(['id', 'txaction', 'user_shop_id'])
            ->make(true);
    }



}