<?php

namespace App\Http\Controllers\ATControllers;

use App\Models\Currency_expenditure;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\atmodels\Shop;
use App\Models\atmodels\CreditTopup;
use App\Models\fcmodels\ApiTopup;
use App\Models\atmodels\Currency_credit;
use Auth;
use Exception;
use Hash;
use EloquentBuilder;

class CurrencyExpenditureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Currency_expenditure  $currency_expenditure
     * @return \Illuminate\Http\Response
     */
    public function show(Currency_expenditure $currency_expenditure)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Currency_expenditure  $currency_expenditure
     * @return \Illuminate\Http\Response
     */
    public function edit(Currency_expenditure $currency_expenditure)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Currency_expenditure  $currency_expenditure
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Currency_expenditure $currency_expenditure)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Currency_expenditure  $currency_expenditure
     * @return \Illuminate\Http\Response
     */
    public function destroy(Currency_expenditure $currency_expenditure)
    {
        //
    }

    public function topup()
    {
        if(Auth::user()->user_type == 'organization_user' || Auth::user()->user_type == 'organization')
        {
            $shop_id=Auth::user()->shop_id;
            $customer_id=Auth::user()->userShopUser['customer_id'];
        }
        else{
            return abort('401');
        }

        $wallet = Currency_credit::where('shopid',$shop_id)->pluck('amount');

        //fetch all trans
        $account=Shop::where('ShopID',$shop_id)->pluck('mpesa_account_name');
         //$trans = ApiTopup::where('BillRefNumber',$account)->get();

        // return response()->json($customer_id);
        return view('atviews.currencycredit.topup',compact('wallet','account'));


    }
    public function topupTransactions(){
        if(Auth::user()->user_type == 'organization_user' || Auth::user()->user_type == 'organization')
        {
            $shop_id=Auth::user()->shop_id;
            $customer_id=Auth::user()->userShopUser['customer_id'];
        }
        else{
            return abort('401');
        }
        
        $wallet = Currency_credit::where('shopid',$shop_id)->pluck('amount');   

        //$trans = CreditTopup::where('shop_id',$shop_id)->where('description','!=','Airtime Refund')->get();        
         $query =CreditTopup::where('shop_id',$shop_id)->where('description','!=','Airtime Refund');
        $trans=EloquentBuilder::to($query,request()->all())->orderBy('topup_time','desc')->get();
        
       
        return view('atviews.currencycredit.topup_transactions',compact('trans','wallet'));

    }
}
