<?php

namespace App\Http\Controllers;

use App\Services\HTMLHelper;
use DataTables;
use App\User;

class DataTableController extends Controller
{
    public function datatable()
    {
        return view('datatable');
    }

    public function getUsers()
    {
        $query = User::with('account')->select('users.*')->where('users.deleted_at', '=', null)->where('users.admin', "<", 4);

        return \DataTables::eloquent($query)
            ->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('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('admin', function (User $user) {
                return '<a href="#" data-toggle="modal" data-target="#modals-admin" data-id="'.$user->id.'" data-email="'.$user->email.'" data-admin="'.$user->admin.'">'.HTMLHelper::getRoleLabel($user->admin).'</a>';
            })
            ->addColumn('confirmed', function (User $user) {
                $date = $user->getConfirmationDateFormat();
                $link = '<a href="#" data-toggle="modal" data-target="#modals-confirmed" data-id="'.$user->id.'" data-email="'.$user->email.'" data-confirmed="'.$user->confirmed.'"  data-confirmation_date="'.$date.'">';
                return $user->confirmed ? $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i> '.$date.'</span></a>' : $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
            })
            ->addColumn('active', function (User $user) {
                $date = $user->getActiveDateFormat();
                $link = '<a href="#" data-toggle="modal" data-target="#modals-active" data-id="'.$user->id.'" data-email="'.$user->email.'" data-active="'.$user->active.'" data-active_date="'.$date.'">';
                return $user->active ? $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i> '.$date.'</span></a>' : $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
            })
            ->addColumn('account', function (User $user) {
                $date = $user->getPaymentAccountDateFormat();
                $link = '<a href="#" data-toggle="modal" data-target="#modals-account" data-id="'.$user->id.'" data-email="'.$user->email.'" data-payment_account="'.$date.'">';
                if($user->payment_account){
                    if($user->isActiveAccount()){
                        return $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i> '.$date.'</span></a>';
                    }
                    return $link.'<span class="badge badge-pill badge-warning"><i class="fa fa-ban"></i> '.$date.'</span></a>';
                }
                return $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
            })
           /* ->addColumn('shop', function (User $user) {
                $date = $user->getPaymentShopDateFormat();
                $link = '<a href="#" data-toggle="modal" data-target="#modals-shop" data-id="'.$user->id.'" data-email="'.$user->email.'" data-payment_shop="'.$date.'">';
                if($user->payment_shop){
                    if($user->isActiveShop()){
                        return $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i> '.$date.'</span></a>';
                    }
                    return $link.'<span class="badge badge-pill badge-warning"><i class="fa fa-ban"></i> '.$date.'</span></a>';
                }
                return $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
            })
            ->addColumn('since', function (User $user) {
                if($user->shop){
                    if($user->shop->active){
                        return $user->shop->getActiveDateFormatSmall();
                    }
                    return $user->shop->getActiveDateFormatSmall();
                }
                return "-";
            })*/
            ->addColumn('my_payment_methods', function (User $user) {
                $payment_methods = json_encode($user->payment_methods);
                $link = '<a href="#" data-toggle="modal" data-target="#modals-payment_methods" data-id="'.$user->id.'" data-email="'.$user->email.'" data-payment_methods="'.htmlspecialchars($payment_methods).'">';
                if(!$user->payment_methods){
                    return $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';
                }
                return $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i> '.$user->getPaymentMethodsShort().'</span></a>';

            })
            ->addColumn('action_delete', function (User $user) {
                return '<span class="no-line-break"><a href="' . route('admin_user_delete', [$user->id]) . '" class="btn icon-btn btn-sm btn-danger" onclick="return confirm(\''.__('Really delete entry?').'\');"><span class="fa fa-trash"></span></a>
                           <a href="' . route('admin_user_login_as', [$user->id]) . '" class="btn icon-btn btn-sm btn-warning" onclick="return confirm(\''.__('Login as User?').'\');"><span class="fa fa-sign-in-alt"></span></a></span>';
            })
            ->addColumn('test_mode', function (User $user) {
                $link = '<a href="#" data-toggle="modal" data-target="#modals-test_mode" data-id="'.$user->id.'" data-email="'.$user->email.'" data-test_mode="'.$user->test_mode.'">';
                return $user->test_mode ? $link.'<span class="badge badge-pill badge-success"><i class="fa fa-check"></i></span></a>' : $link.'<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span></a>';

            })
            ->orderColumn('id', 'id $1')
            ->orderColumn('confirmed', 'confirmed $1')
            ->orderColumn('active', 'active $1')
            ->orderColumn('shop', 'shop $1')
            ->orderColumn('admin', 'active $1')
            ->rawColumns(['id', 'admin', 'confirmed', 'active', 'account', 'shop', 'my_payment_methods', 'test_mode', 'action_delete'])
            ->make(true);
    }
}
