<?php

namespace App\Http\Controllers\Admin;

use App\Models\Card;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class CardController extends Controller
{
    public function index(){
        return view('admin.cards');
    }

    public function show(Request $request){

        $cards = Card::select(['*'])->orderBy('created_at', 'DESC');

        return datatables()->of($cards)
        ->addIndexColumn()
        ->addColumn('username', function(Card $cards){

            return '<span class="tb-product"> <span class="title">' . capWords(getUsername($cards->user_id)) .'</span>';

        })

        ->editColumn('type', function(Card $card){

            return '<span class="tb-sub">' . capWords($card->type). '</span>';
        })

        ->editColumn('status', function (Card $card){
          if ($card->status == PENDING_CARD) {

                return '
                <div class="dropdown">
                    <a href="javascript:"  class="badge badge-pill badge-warning btn-action" data-toggle="dropdown" aria-expanded="false">
                        <span>Pending</span><em class="icon ni ni-chevron-down"></em>
                    </a>
                    <div class="dropdown-menu dropdown-menu-right dropdown-menu-sm mt-1" style="">
                        <ul class="link-list-plain">
                            <li><a href="javascript:" class="btn-toggle" id="'.$card->id.'" data-status="approve">Approve</a></li>
                            <li><a href="javascript:" class="btn-toggle" id="'.$card->id.'" data-status="decline">Decline</a></li>
                        </ul>
                    </div>
                </div>';

            } elseif ($card->status == APPROVED_CARD) {

                return '<span class="badge badge-pill badge-success"> <span>Approved</span> </span>';

            }else{
                return '<span class="badge badge-pill badge-danger"> <span>Declined</span> </span>';
            }

        })

        ->editColumn('created_at', function (Card $card) {
            return '<span class="tb-sub">' . $card->created_at->format('Y-m-d h:i A') . '</span>';
        })

        ->escapeColumns([])
            ->setRowClass('nk-tb-item')
            ->rawColumns(['amount'])
            ->rawColumns(['receiver_account'])
            ->rawColumns(['type'])
            ->rawColumns(['action'])
        ->make(true);
    }

    public function update(Request $request){

        $cardNumber = NULL;
        $cvv = NULL;
        $expiryMonth = NULL;
        $expiryYear = NULL;
        $balance = NULL;

        if($request->status === 'approve'){

            $cardNumber = rand(1111,9999).' '.rand(1111,9999).' '.rand(1111,9999).' '.rand(1111,9999);
            $cvv = rand(111,999);
            $expiryMonth = rand(1,12);
            $expiryYear = Carbon::now()->addYear(5)->format('y');
            $balance = numbers(0);
            $status = APPROVED_CARD;

        }else{

            $status = DECLINED_CARD;
        }

        $card = Card::find($request->id);
        $card->serial_number = $cardNumber;
        $card->cvv = $cvv;
        $card->expiry_month = $expiryMonth;
        $card->expiry_year = $expiryYear;
        $card->balance = $balance;
        $card->status = $status;
        $card->save();

        //TODO SEND $status MAIL
        $mailBody = [
            'username' => capWords(getUsername($card->user_id)),
            'status' => $request->status === 'approve' ? 'approved' : 'declined'
        ];

        return response()->json([
            'status' => 'success',
            'title' => 'DONE',
            'message' => 'You have '.$request->status.'d this card request'
        ]);

    }
}
