<?php


namespace App\Http\Controllers\User;
use Carbon;
use Request;
use App\User;
use App\Services\Credit;
use App\Models\UserCredit;
use App\Http\Controllers\Controller;
use App\Models\UserPayCredit;

class PaymentController extends Controller
{

    private $startYear;
    private $endYear;
    private $rangeYears;
    private $activeYear;

    public function __construct()
    {
        $this->middleware('auth');
        $this->startYear = 2021;
        $this->endYear = date('Y');
        $this->rangeYears = range($this->startYear, $this->endYear);
        $this->activeYear = $this->endYear;
    }

    public function paycredit()
    {
        $data = [
            'user' => \Auth::user()
        ];
        return view('user.payment.paycredit', $data);
    }

    public function paycredit_datatable(){

        $user = \Auth::user();
        $query = UserPayCredit::select('user_pay_credits.*')->where('user_id', $user->id);

        return \DataTables::eloquent($query)
          
            ->addColumn('message', function (UserPayCredit $user_pay_credit) {
                if($user_pay_credit->status === 3){
                    return nl2br($user_pay_credit->message);
                }elseif($user_pay_credit->status === 5 || $user_pay_credit->status === 6){
                    return trans('payment.'.$user_pay_credit->message).
                    ' &nbsp; <a class="btn btn-outline-secondary btn-xs" href="'.route('user_sales_detail', [$user_pay_credit->shopping_order_id]).'"><i class="ion ion-md-eye"></i></a>';
                }else{
                    return trans('payment.'.$user_pay_credit->message).
                    ' &nbsp; <a class="btn btn-outline-secondary btn-xs" href="'.route('user_myorder_detail', [$user_pay_credit->shopping_order_id]).'"><i class="ion ion-md-eye"></i></a>';
                }
            })
            ->addColumn('credit', function (UserPayCredit $user_pay_credit) {
                return formatNumber($user_pay_credit->credit)." €";
            })
            ->addColumn('old_credit_total', function (UserPayCredit $user_pay_credit) {
                return formatNumber($user_pay_credit->old_credit_total)." €";
            })
            ->addColumn('new_credit_total', function (UserPayCredit $user_pay_credit) {
                return formatNumber($user_pay_credit->new_credit_total)." €";
            })
            ->addColumn('created_at', function (UserPayCredit $user_pay_credit) {
                return formatDate($user_pay_credit->created_at);
            })
           
            ->orderColumn('message', 'message $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('credit', 'credit $1')
            ->orderColumn('created_at', 'created_at $1')
            ->rawColumns(['message'])
            ->make(true);
    }

    public function credit()
    {
        $user = \Auth::user();
        $data = [
            'user' => $user,
        ];
        return view('user.payment.credit', $data);
    }

    public function credit_datatable(){

        $user = \Auth::user();
        $query = UserCredit::with('user', 'user.account')->select('user_credits.*')->where('auth_user_id', $user->id);

        return \DataTables::eloquent($query)
            ->addColumn('total', function (UserCredit $UserCredit) {
                return $UserCredit->getFormattedTotal()." €";
            })
            ->addColumn('user_margins', function (UserCredit $UserCredit) {
                $ret = "";
                if($UserCredit->user_margins){
                    foreach($UserCredit->user_margins as $user_margin){
                        $ret .= $user_margin->firstname."/".$user_margin->lastname."/".$user_margin->reference."/".$user_margin->created_at."<br>"; 
                    }
                }
                if($UserCredit->user_credits){
                    foreach($UserCredit->user_credits as $user_credit){
                        $ret .= nl2br($user_credit->message)." / ".$user_credit->created_at."<br>"; 

                    }
                }
                return $ret;
            })
          /*  ->addColumn('txaction', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getShoppingOrderBadge($ShoppingOrder);
            })*/
            ->addColumn('credit', function (UserCredit $UserCredit) {
                $ret = "";
                if(Credit::isCredit($UserCredit)){
                    $ret .= '<a href="'.route('storage_file', [$UserCredit->id, 'credit', 'download']).'" class="btn btn-primary btn-xs"><i class="fa fa-download"></i></a> ';
                    $ret .= '<a href="'.route('storage_file', [$UserCredit->id, 'credit', 'stream']).'" target="_blank" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i></a>';
                }else{
                    $ret = "-";
                }
                return $ret;
            })

            ->addColumn('status', function (UserCredit $UserCredit) {
                return '<span class="badge  badge-pill badge-'.$UserCredit->getStatusColor().'">'.$UserCredit->getStatusType().' <span class="ion ion-md-cash"></span></span>';
            })

           
            ->orderColumn('id', 'id $1')
            ->orderColumn('status', 'status $1')
            ->orderColumn('total', 'total $1')
            ->rawColumns(['shipping_order', 'total', 'credit', 'status', 'user_margins'])
            ->make(true);
    }


    private function setActiveYears(){
        if(Request::get('filter_year')){
            $this->activeYear = Request::get('filter_year');
        }
    }
    
    public function revenue()
    {
        $this->setActiveYears();

        $user = \Auth::user();
        $data = [
            'user' => $user,
            'years' => $this->rangeYears,
            'active_year' => $this->activeYear,
            'months' => range(1, 12),
        ];
        return view('user.payment.revenue', $data);
    }
}