<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\DataTables\WithdrawRequestDataTable;
use App\Exports\WithdrawRequestExport;
use App\Models\User;
use App\Models\UserBankAccount;
use App\Models\Wallet;
use App\Models\WithdrawRequest;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class WithdrawRequestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(WithdrawRequestDataTable $dataTable)
    {
        $auth = Auth::user();
        $pageTitle = __('message.list_form_title',['form' => __('message.withdrawrequest')] );
        $auth_user = authSession();
        $params = [
            'status' => request('status') ?? null,
        ];
        $assets = ['datatable'];

        $reset_file_button = '<a href="' . route('withdrawrequest.index') . '" class="mr-1 mt-0 btn btn-sm btn-info text-dark mt-3 pt-2 pb-2"><i class="ri-repeat-line" style="font-size:12px"></i> ' . __('message.reset') . '</a>';
        $filter_file_button = '<a href="' . route('filter.order.data', $params) . '" class="float-right mr-1 mt-1 btn btn-sm btn-success text-dark loadRemoteModel"><i class="fas fa-filter"></i> ' . __('message.filter') . '</a>';

        $withdraw_type = request()->input('withdraw_type', 'all');
        $export = '';
        if($withdraw_type === 'pending'){
            $export = '<a href="' . route('download-withdrawexcel') . '" class="mr-1 mt-0 btn btn-sm btn-success text-dark mt-3 pt-2 pb-2"><i class="fas fa-file-csv"></i> ' . __('message.export') . '</a>';
        }
        $button = '';
        if (($auth->user_type == 'storeowner' || $auth->user_type == 'client') && $auth_user->can('withdrawrequest-add')) {
            $button = '<a href="' . route('withdrawrequest.create') . '" class="float-right btn btn-sm btn-primary loadRemoteModel"><i class="fa fa-plus-circle"></i> ' . __('message.add_form_title', ['form' => __('message.withdrawrequest')]) . '</a>';
        }

        return $dataTable->render('global.withdrawrequest-filter', compact('pageTitle', 'auth_user', 'filter_file_button', 'params', 'reset_file_button', 'assets', 'button','export'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $auth_user = authSession();
        if($auth_user->user_type == 'client'){

            $pageTitle = __('message.withdraw_money');
            return view('clientside.withdrawpop', compact('pageTitle'));
        }else if($auth_user->user_type == 'storeowner'){
            $pageTitle = __('message.withdraw_money');
            return view('clientside.withdrawpop', compact('pageTitle'));

        }
            return redirect('withdrawrequest');

    }

    public function clientwithdraw()
    {
        if(Auth::user()->user_type == 'client')
        {
         $pageTitle = __('message.withdraw');

            $client = Auth::user()->id;
            $withdrawrequest = WithdrawRequest::where('user_id',$client)->OrderBy('created_at','desc')->get();
            $wallte = Wallet::where('user_id',$client)->first();

            return view('clientside.withdraw',compact('pageTitle','wallte','withdrawrequest'));
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = auth()->user()->id ?? $request->user_id;

        $withdrawrequest_exist = WithdrawRequest::where('user_id', $data['user_id'])->where('status', 'requested')->exists();
        if($withdrawrequest_exist) {
            $message = __('message.already_withdrawrequest');
            return response()->json(['status' => true,  'message' => $message,'event' => 'refresh']);
        }
        $withdrawrequest = WithdrawRequest::create($data);

        $message = __('message.save_form',['form' => __('message.withdrawrequest')]);

        return response()->json(['status' => true,  'message' => $message,'event' => 'refresh']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $pageTitle = __('message.add_form_title', ['form' => __('message.order')]);
        $user = User::findOrFail($id);
        $data = UserBankAccount::where('user_id',$id)->first();
        return view('withdrawrequest.show', compact('data','pageTitle','user'));
    }

    public function withdrawExcel(Request $request)
    {

        $export = new WithdrawRequestExport($request);
        $filename = "pending-withdraw-request.xlsx" ;

        return Excel::download($export, $filename);
    }
}
