<?php

namespace App\Http\Controllers\User;

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

class TransactionController extends Controller
{
    public function index()
    {
        $transactions = Transaction::where('user_id', auth()->user()->id)->orderBy('created_at', 'DESC')->get();
        return view('user.statement', compact('transactions'));
    }

    public function show()
    {
        $transactions = Transaction::where('user_id', auth()->user()->id)->orderBy('created_at', 'DESC')->get();
        return view('user.print-statement', compact('transactions'));
    }

    public function loadStatement(Request $request)
    {

        if ($request->ajax()) {
            $tx = Transaction::select(['*'])->where('user_id', auth()->id())->orderBy('created_at', 'DESC');

            return datatables()->of($tx)
                ->addIndexColumn()
                ->editColumn('amount', function (Transaction $tx) {
                    return '<span class="tb-lead">' . getUserCurrency() . number_format($tx->amount) . '</span>';
                })
                ->editColumn('receiver_account', function (Transaction $tx) {
                    return '<span class="tb-product">
                        <span class="title">' . $tx->receiver_account . '<small>' . ucwords($tx->receiver_name) . '</small></span>
                    </span>
                ';})

                ->editColumn('type', function (Transaction $tx) {
                    return '<span class="tb-sub">' . ucwords($tx->type) . '(' . $tx->category . ')</span>';
                })
                ->editColumn('bank_name', function (Transaction $tx) {
                    return '<span class="tb-sub">' . ucwords(strtolower($tx->bank_name)) . '</span>';
                })
                ->editColumn('remark', function (Transaction $tx) {
                    $cont = '';
                    if (strlen($tx->remark) > 15) {
                        $cont = '...';
                    }
                    return '<span class="tb-sub">' . substr($tx->remark, 0, 15) . $cont . '</span>';
                })
                ->editColumn('status', function (Transaction $tx) {
                    if ($tx->status == PENDING_TX) {
                        return '
                    <a href="javascript:" data-reason="' . $tx->reason . '" class="tx-info">
                        <span class="tb-sub">
                            <span class="badge badge-pill badge-info">
                                <em class="icon ni ni-help-fill"></em>Under Review
                            </span>
                        </span>
                    </a>';
                    } elseif ($tx->status == APPROVED_TX) {
                        return '
                    <a href="javascript:" data-reason="' . $tx->reason . '" class="tx-info">
                        <span class="tb-sub">
                            <span class="badge badge-pill badge-success">
                                Successful
                            </span>
                        </span>
                    </a>';
                    } elseif ($tx->status == HELD_BY_IMF_TX) {
                        return '
                    <a href="javascript:" data-reason="' . $tx->reason . '" class="tx-info">
                        <span class="tb-sub">
                            <span class="badge badge-pill badge-warning">
                                <em class="icon ni ni-help-fill"></em>Hold
                            </span>
                        </span>
                    </a>';
                    }elseif ($tx->status == IN_PROGRESS_TX) {
                        return '
                    <a href="javascript:" data-reason="' . $tx->reason . '" class="tx-info">
                        <span class="tb-sub">
                            <span class="badge badge-pill badge-secondary">
                                <em class="icon ni ni-help-fill"></em>In Progress
                            </span>
                        </span>
                    </a>';
                    } else {
                        return '
                    <a href="javascript:" data-reason="' . $tx->reason . '" class="tx-info">
                        <span class="tb-sub">
                            <span class="badge badge-pill badge-danger">
                                <em class="icon ni ni-help-fill"></em>Failed
                            </span>
                        </span>
                    </a>';
                    }
                })

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

                ->escapeColumns([])
                ->setRowClass('nk-tb-item')
                ->rawColumns(['amount'])
                ->rawColumns(['receiving_account'])
                ->rawColumns(['type'])
                ->rawColumns(['bank_name'])
                ->rawColumns(['remark'])
                ->rawColumns(['status'])
                ->rawColumns(['created_at'])
                ->make(true);
        }else{
            return back();
        }
    }
}
