<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use DB;
use Session;
use Auth;

class ReportController extends Controller
{
    public function report()
    {
        $user_id = Auth::guard('member')->user()->id;

        $startDate = date('Y-m-d  H:i:s');
        $endDate = date("Y-m-d H:i:s", strtotime("$startDate +1 day"));

        $data['repo_count'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', 'Balance')->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->count();

        $data['totalin'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', 'Balance')->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->sum('bal_in');

        $data['totalout'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', 'Balance')->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->sum('bal_out');
        
        $data['reports'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', 'Balance')->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->orderBy('created_at', 'desc')->get();
        $data['rname'] = 'Earning Wallet';
        $data['rtype'] = 'Today';

        return view('member.report', $data);
    }

    public function reposrch(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            $user_id = Auth::guard('member')->user()->id;
            $today = date('Y-m-d  H:i:s');

            if ($data['rtype'] == 1) {
                $wallet = 'Balance';
                $data2['rname'] = 'Earning Wallet';
            }elseif ($data['rtype'] == 2) {
                $wallet = 'Buying';
                $data2['rname'] = 'Buying Wallet';
            }elseif ($data['rtype'] == 3) {
                $wallet = 'Balance';
                $baltype = 'Profit';
                $data2['rname'] = 'Buying Profit';
            }elseif ($data['rtype'] == 4) {
                $wallet = 'Balance';
                $baltype = 'Affiliate';
                $data2['rname'] = 'Affiliate Bonus';
            }elseif ($data['rtype'] == 5) {
                $wallet = 'Balance';
                $baltype = 'Club';
                $data2['rname'] = 'Club Bonus';
            }elseif ($data['rtype'] == 6) {
                $wallet = 'Balance';
                $baltype = 'Founder';
                $data2['rname'] = 'Founder Bonus';
            }elseif ($data['rtype'] == 7) {
                $wallet = 'Buying';
                $baltype = 'Add Fund';
                $data2['rname'] = 'Add Fund';
            }elseif ($data['rtype'] == 8) {
                $wallet = 'Balance';
                $baltype = 'Payout';
                $data2['rname'] = 'Withdraw';
            }

            if ($data['dtype'] == 1) {
                $startDate = date('Y-m-d  H:i:s');
                $endDate = date("Y-m-d H:i:s", strtotime("$startDate +1 day"));
                $data2['rtype'] = 'Today';
            }elseif ($data['dtype'] == 2) {
                $startDate = date("Y-m-d H:i:s", strtotime("$today -1 day"));
                $endDate = date('Y-m-d  H:i:s');
                $data2['rtype'] = 'Yesterday';
            }elseif ($data['dtype'] == 3) {
                $startDate = date("Y-m-d H:i:s", strtotime("$today -2 day"));
                $endDate = date("Y-m-d H:i:s", strtotime("$today +1 day"));
                $data2['rtype'] = 'Last 3 days';
            }elseif ($data['dtype'] == 4) {
                $startDate = date("Y-m-d H:i:s", strtotime("$today -6 day"));
                $endDate = date("Y-m-d H:i:s", strtotime("$today +1 day"));
                $data2['rtype'] = 'Last 7 days';
            }elseif ($data['dtype'] == 5) {
                $startDate = date("Y-m-d H:i:s", strtotime("$today -29 day"));
                $endDate = date("Y-m-d H:i:s", strtotime("$today +1 day"));
                $data2['rtype'] = 'Last 30 days';
            }elseif ($data['dtype'] == 6) {
                $startDate = date("Y-m-d H:i:s", strtotime("$today -364 day"));
                $endDate = date("Y-m-d H:i:s", strtotime("$today +1 day"));
                $data2['rtype'] = 'Last 365 days';
            }

            if ($data['dtype'] == 7) {
                $data2['rtype'] = 'Lifetime';
                if ($data['rtype'] <= 2) {
                    $data2['repo_count'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->count();
                    $data2['totalin'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('bal_out', null)->sum('bal_in');
                    $data2['totalout'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('bal_in', null)->sum('bal_out');
                    $data2['reports'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->orderBy('created_at', 'desc')->get();
                }else{
                    $data2['repo_count'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->count();
                    $data2['totalin'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('bal_out', null)->sum('bal_in');
                    $data2['totalout'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('bal_in', null)->sum('bal_out');
                    $data2['reports'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->orderBy('created_at', 'desc')->get();
                }
            }else{
                if ($data['rtype'] <= 2) {
                    $data2['repo_count'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->count();
                    $data2['totalin'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->where('bal_out', null)->sum('bal_in');
                    $data2['totalout'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->where('bal_in', null)->sum('bal_out');
                    $data2['reports'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->orderBy('created_at', 'desc')->get();
                }else{
                    $data2['repo_count'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->count();
                    $data2['totalin'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->where('bal_out', null)->sum('bal_in');
                    $data2['totalout'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->where('bal_in', null)->sum('bal_out');
                    $data2['reports'] = DB::table('reports')->where('user_id', $user_id)->where('type', 'member')->where('wallet', $wallet)->where('action', $baltype)->where('created_at','>=',$startDate)->where('created_at','<=',$endDate)->orderBy('created_at', 'desc')->get();
                }
            }

            if (!empty($data2)) {
                return view('member.report_view', $data2);
            }else{
                return redirect('report');
            }

            

        }
    }

    
}
