<?php


namespace App\Http\Controllers;

use Carbon;
use Request;
use App\User;
use App\Services\Util;
use App\Models\UserPayCredit;
use App\Services\Payment;

class PaymentPayCreditController extends Controller
{

    private $filter_user_status;

    public function __construct()
    {
        $this->middleware('auth');
        $this->filter_user_status = 'all';
    }

    public function index()
    {
        
        $this->setActiveFilter();
        $data = [
            'filter_user_status' => $this->filter_user_status,
        ];
        return view('admin.payment.pay_credit.index', $data);
    }

    private function setActiveFilter(){
        if(Request::get('filter_user_status')){
            $this->filter_user_status = Request::get('filter_user_status');
        }
    }

    public function store(){
        $data = Request::all();

        if(isset($data['action']) && $data['action'] === 'add-user-pay-credit'){

            $add_credit_error = false;
            if(!isset($data['member_id']) || !$user = User::find($data['member_id'])){
                $add_credit_error = 'Vertriebspartner nicht gefunden';
            }
            if(!isset($data['credit'])){
                $add_credit_error = 'Bitte Betrag eingeben';
            }
            if(!isset($data['message'])){
                $add_credit_error = 'Bitte Mitteilung eingeben';
            }
            if($add_credit_error){
                $this->setActiveFilter();
                $data = [
                    'filter_user_status' => $this->filter_user_status,
                    'add_credit_error' => $add_credit_error,
                ];
                return view('admin.payment.pay_credit.index', $data);
            }

            $credit = Util::reFormatNumber($data['credit']);
            $credit = number_format($credit, 2, '.', '');      

            Payment::addUserPayCredits($user, $credit, 3, $data['message']);
            \Session()->flash('alert-success', "Einkaufsguthaben aufgeladen");
        }

        return redirect(route('admin_payments_paycredit'));
    }


    public function delete($id){

        $UserPayCredit = UserPayCredit::findOrFail($id);
        if($UserPayCredit->status === 3 && $deleteTime = $UserPayCredit->deleteTime()){
            $user = $UserPayCredit->user;
            $credit = $UserPayCredit->credit;
            $user->payment_credit = $user->payment_credit - $credit;
            $user->save();
            $UserPayCredit->delete();
            \Session()->flash('alert-success', "Guthaben ist gelöscht");
        }else{
            \Session()->flash('alert-error', "Guthaben kann nicht gelöscht werden");
        }
        return redirect(route('admin_payments_paycredit'));
    }

    public function datatable(){

        $this->setActiveFilter();
  
        $query = User::with('account')->select('users.*')->where('users.deleted_at', '=', null)
        ->where('active', true);

        //->orderBy('created_at', 'DESC');

        return \DataTables::eloquent($query)
            ->addColumn('id', function (User $user) {
                return '<a href="' . route('admin_lead_edit', [$user->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
            })
            ->addColumn('first_name', function (User $user) {
                return $user->account ? $user->account->first_name : '';
            })
            ->addColumn('last_name', function (User $user) {
                return $user->account ? $user->account->last_name : '';
            })
            ->addColumn('payment_credit', function (User $user) {
                return $user->payment_credit ? $user->getFormattedPaymentCredit().' €' : '';
            })
            ->addColumn('is_active_account', function (User $user) {
                if($user->payment_account){
                    if($user->isActiveAccount()){
                        return '<span class="badge badge-pill badge-success"><i class="fa fa-check"></i></span></a>';
                    }
                    return '<span class="badge badge-pill badge-warning"><i class="fa fa-ban"></i></span></a>';
                }
                return '<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
            })
            ->addColumn('action', function (User $user) {
                return '<button class="btn btn-warning btn-sm icon-btn md-btn-flat" title="details" data-modal="modal-xl"
                data-toggle="modal" data-target="#modals-load-content" data-id="'.$user->id.'" data-route="'.route('modal_load').'"
                data-action="show-user-pay-credits" data-view="customer"><i class="ion ion-md-eye"></i></button>';
            })
            ->orderColumn('id', 'id $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('payment_credit', 'payment_credit $1')
            ->orderColumn('is_active_account', 'is_active_account $1')
            ->rawColumns(['id', 'shipping_order', 'is_active_account', 'action'])
            ->make(true);
    }
}