<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MnoPaymentsController extends Controller
{
    public function index(){
        $data = [
            'page_title' => 'MNO Payment Report',
            'current_user' => session('current_user')
        ];
        return view('reports.mno-payments', $data);
    }
    public function getMnoPaymentsJson(Request $request)
    {
        #$this->log_query();
        $payment_arr = \DB::table('mno_payments')
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'mno_payments.user_id')
            ->join('network_operators', 'network_operators.id', '=', 'mno_payments.mno_id')
            ->select('mno_payments.id', 'network_operators.name AS mnoName', 'mno_payments.invoice_number', 'mno_payments.invoice_amount', 'mno_payments.invoice_date', 'mno_payments.services', 'mno_payments.invoice_status', 'mno_payments.remarks',  'staffcreate.name As createdBy', 'mno_payments.created_at')
            ->orderBy('mno_payments.created_at', 'DESC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $payment_arr = \DB::table('mno_payments')
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'mno_payments.user_id')
            ->join('network_operators', 'network_operators.id', '=', 'mno_payments.mno_id')
            ->select('mno_payments.id', 'network_operators.name AS mnoName', 'mno_payments.invoice_number', 'mno_payments.invoice_amount', 'mno_payments.invoice_date', 'mno_payments.services', 'mno_payments.invoice_status', 'mno_payments.remarks',  'staffcreate.name As createdBy', 'mno_payments.created_at')
            ->whereRaw("mno_payments.invoice_amount LIKE '%$keyword%' OR mno_payments.invoice_status LIKE '%$keyword%' OR mno_payments.invoice_number LIKE '%$keyword%' OR mno_payments.invoice_amount LIKE '%$keyword%' OR staffcreate.name LIKE '%$keyword%' OR mno_payments.created_at LIKE '%$keyword%'")
            ->orderBy('mno_payments.created_at', 'DESC')
            ->paginate(15);
        }     
        return response()->json($payment_arr);
    }
}
