<?php

namespace App\Http\Controllers;

use App\Models;
use Session;
use Illuminate\Http\Request;
use DB;

class UssdClientsPaymentsController extends Controller
{
    public function index(Request $request)
    {
        $columns = [
            'payment_type' => 'Payment Type',
            'name' => 'Client Name',
            'client_email' => 'Client Email',
            'account_mgr' => 'Account Manager',
        ];

        $clients = Models\UssdClientPayment::with('client_info');
        $account_managers = Models\SystemUser::pluck('name', 'id');

        if (!empty($request->filter) && !empty($request->keyword)) {
            $clients = $this->resolveClientQuery($clients, $request);
        }

        $clients = $clients->orderBy('ussd_client_payments.created_at', 'DESC')->paginate(20);

        $data = [
            'page_title' => 'Ussd Clients Payments',
            'client_arr' => $clients,
            'account_managers' => $account_managers,
            'columns' => $columns
        ];
        return view('ussdclients.index', $data);
    }


    public function edit($id)
    {
        $client = Models\UssdClientPayment::find($id);

        $data = [
            'client' => $client,
        ];
        return view('ussdclients.edit', $data);
    }


    public function show($id)
    {
        $client = Models\UssdClientPayment::find($id);
        $data = [
            'client' => $client,
        ];
        return view('ussdclients.edit', $data);
    }


    public function update(Request $request, $id)
    {

        $request->validate([
            'amount_paid' => 'required|numeric',
            'payment_type' => 'required',
            'remarks' => 'sometimes',
        ]);
        $ussd_client = Models\UssdClientPayment::find($id);


        $ussd_client->payment_type = $request->payment_type;
        $ussd_client->amount_paid = $request->amount_paid;
        $ussd_client->remarks = $request->remarks;
        $ussd_client->last_modified_by_id = session('current_user.id');

        $result = $ussd_client->save();

        Session::flash('success_message', 'Client successfully Updated');
        return redirect(url('ussdclients'));
    }


    public function resolveClientQuery($clients, $request)
    {
        switch ($request->filter) {
            case 'name':
                return $clients
                    ->join('clients', 'clients.id', '=', 'ussd_client_payments.client_id')
                    ->where('clients.name', 'like', "%$request->keyword%");
                break;
            case 'client_email':
                return $clients
                    ->join('clients', 'clients.id', '=', 'ussd_client_payments.client_id')
                    ->where('clients.email', 'like', "%$request->keyword%");
                break;
            case 'account_mgr':
                return $clients
                    ->join('clients', 'clients.id', '=', 'ussd_client_payments.client_id')
                    ->join('auth_users', 'auth_users.id', '=', 'clients.auth_user_id')
                    ->where('auth_users.name', 'like', "%$request->keyword%");
                break;
            default:
                # code...
                break;
        }
    }
}
