<?php

namespace App\Http\Controllers\Admin;

use App\Models\Loan;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

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

    public function show(Request $request){

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

        return datatables()->of($loans)
        ->addIndexColumn()
        ->addColumn('username', function(Loan $loans){

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

        })

        ->editColumn('type', function(Loan $loan){

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

        ->editColumn('status', function (Loan $loan){
          if ($loan->status == PENDING_LOAN) {

                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="'.$loan->id.'" data-status="approve">Approve</a></li>
                            <li><a href="javascript:" class="btn-toggle" id="'.$loan->id.'" data-status="decline">Decline</a></li>
                        </ul>
                    </div>
                </div>';

            } elseif ($loan->status == APPROVED_LOAN) {

                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 (Loan $loan) {
            return '<span class="tb-sub">' . $loan->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){


        $status =  $request->status === 'approve'? APPROVED_LOAN : DECLINED_LOAN;

        $loan = Loan::find($request->id);

        $loan->status = $status;
        $loan->save();

        //TODO SEND $status MAIL
        $mailBody = [

            'username' => capWords(getUsername($loan->user_id)),
            'status' => $request->status === 'approve' ? 'approved' : 'declined'
        ];

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

    }
}
