<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class StatusController extends Controller
{
    public function dailyStatus(Request $request)
    {
        $current_date             = date('Y-m-d');
        $crone_hit_exist          = DB::table('crone_records')->where('date', $current_date)->where('type', 'daily_status')->first();            
        
        if(empty($crone_hit_exist)){
            $admin = DB::table('admins')->where('id', 1)->first();
            $wallet = DB::table('wallet_manages')->where('id', 1)->first();
            
            // Pack Profit Paid
            if ($admin->days <= 6) {
                if ($wallet->payon == 1) {
                    $plan = DB::table('plan')->where('id', 1)->first();
                    if ($admin->days == 1) {
                        $pay = $plan->day_one;
                    }elseif ($admin->days == 2) {
                        $pay = $plan->day_two;
                    }elseif ($admin->days == 3) {
                        $pay = $plan->day_thr;
                    }elseif ($admin->days == 4) {
                        $pay = $plan->day_for;
                    }elseif ($admin->days == 5) {
                        $pay = $plan->day_fiv;
                    }elseif ($admin->days == 6) {
                        $pay = $plan->day_six;
                    }

                    $packs = DB::table('packs')->where('pleft', '>', 0)->where('status', 0)->get();
                    if (!empty($packs)) {
                        foreach ($packs as $pk) {
                            if ($pk->pleft >= $pay) {
                                $pdata1['uppro'] = $pk->uppro+$pay;
                                $pdata1['pleft'] = $pk->pleft-$pay;
                            }else{
                                $pdata1['uppro'] = $pk->uppro+$pk->pleft;
                                $pdata1['pleft'] = 0;
                            }
                            DB::table('packs')->where('id', $pk->id)->update($pdata1);
                        } 
                    }
                }
                    
            }

            // Pack Status Paid 
            $packs2 = DB::table('packs')->where('pleft', 0)->where('status', 0)->get();
            if (!empty($packs2)) {
                foreach ($packs2 as $pk2) {
                    $pdata2['status'] = 1;
                    DB::table('packs')->where('id', $pk2->id)->update($pdata2);
                } 
            }

            // Club Status Search
            $cmems = DB::table('members')->where('club', 1)->where('refexc', '>=', 5)->get();
            if (!empty($cmems)) {
                foreach ($cmems as $cm) {
                    $cmdata['club'] = 2;
                    DB::table('members')->where('id', $cm->id)->update($cmdata);
                }
            }

            // Rank Search
            $rank1 = DB::table('members')->where('rank_status', null)->where('refexc', '>=', 10)->get();
            if (!empty($rank1)) {
                foreach ($rank1 as $rnk1) {
                    $rnkd1['rank_status'] = 1;
                    DB::table('members')->where('id', $rnk1->id)->update($rnkd1);

                    $rnkdata1['rank'] = 1;
                    $rnkdata1['user_id'] = $rnk1->id;
                    $rnkdata1['username'] = $rnk1->username;
                    $rnkdata1['name'] = $rnk1->name;
                    $rnkdata1['mobile'] = $rnk1->mobile;
                    $rnkdata1['status'] = 0;
                    DB::table('ranks')->insert($rnkdata1);

                    $r1sp = DB::table('members')->where('username', $rnk1->sponsor)->get();
                    if (!empty($r1sp)) {
                        foreach ($r1sp as $rs1) {
                            $rsd1['agm'] = $rs1->agm+1;
                            DB::table('members')->where('id', $rs1->id)->update($rsd1);
                        }
                    }
                }
            }

            $rank2 = DB::table('members')->where('rank_status', 1)->where('agm', '>=', 8)->get();
            if (!empty($rank2)) {
                foreach ($rank2 as $rnk2) {
                    $rnkd2['rank_status'] = 2;
                    DB::table('members')->where('id', $rnk2->id)->update($rnkd2);

                    $rnkdata2['rank'] = 2;
                    $rnkdata2['user_id'] = $rnk2->id;
                    $rnkdata2['username'] = $rnk2->username;
                    $rnkdata2['name'] = $rnk2->name;
                    $rnkdata2['mobile'] = $rnk2->mobile;
                    $rnkdata2['status'] = 0;
                    DB::table('ranks')->insert($rnkdata2);

                    $r2sp = DB::table('members')->where('username', $rnk2->sponsor)->get();
                    if (!empty($r2sp)) {
                        foreach ($r2sp as $rs2) {
                            $rsd2['dgm'] = $rs2->dgm+1;
                            DB::table('members')->where('id', $rs2->id)->update($rsd2);
                        }
                    }
                }
            }

            $rank3 = DB::table('members')->where('rank_status', 2)->where('dgm', '>=', 7)->get();
            if (!empty($rank3)) {
                foreach ($rank3 as $rnk3) {
                    $rnkd3['rank_status'] = 3;
                    DB::table('members')->where('id', $rnk3->id)->update($rnkd3);

                    $rnkdata3['rank'] = 3;
                    $rnkdata3['user_id'] = $rnk3->id;
                    $rnkdata3['username'] = $rnk3->username;
                    $rnkdata3['name'] = $rnk3->name;
                    $rnkdata3['mobile'] = $rnk3->mobile;
                    $rnkdata3['status'] = 0;
                    DB::table('ranks')->insert($rnkdata3);

                    $r3sp = DB::table('members')->where('username', $rnk3->sponsor)->get();
                    if (!empty($r3sp)) {
                        foreach ($r3sp as $rs3) {
                            $rsd3['gm'] = $rs3->gm+1;
                            DB::table('members')->where('id', $rs3->id)->update($rsd3);
                        }
                    }
                }
            }

            $rank4 = DB::table('members')->where('rank_status', 3)->where('gm', '>=', 6)->get();
            if (!empty($rank4)) {
                foreach ($rank4 as $rnk4) {
                    $rnkd4['rank_status'] = 4;
                    DB::table('members')->where('id', $rnk4->id)->update($rnkd4);

                    $rnkdata4['rank'] = 4;
                    $rnkdata4['user_id'] = $rnk4->id;
                    $rnkdata4['username'] = $rnk4->username;
                    $rnkdata4['name'] = $rnk4->name;
                    $rnkdata4['mobile'] = $rnk4->mobile;
                    $rnkdata4['status'] = 0;
                    DB::table('ranks')->insert($rnkdata4);

                    $r4sp = DB::table('members')->where('username', $rnk4->sponsor)->get();
                    if (!empty($r4sp)) {
                        foreach ($r4sp as $rs4) {
                            $rsd4['ed'] = $rs4->ed+1;
                            DB::table('members')->where('id', $rs4->id)->update($rsd4);
                        }
                    }
                }
            }

            $rank5 = DB::table('members')->where('rank_status', 4)->where('ed', '>=', 5)->get();
            if (!empty($rank5)) {
                foreach ($rank5 as $rnk5) {
                    $rnkd5['rank_status'] = 5;
                    DB::table('members')->where('id', $rnk5->id)->update($rnkd5);

                    $rnkdata5['rank'] = 5;
                    $rnkdata5['user_id'] = $rnk5->id;
                    $rnkdata5['username'] = $rnk5->username;
                    $rnkdata5['name'] = $rnk5->name;
                    $rnkdata5['mobile'] = $rnk5->mobile;
                    $rnkdata5['status'] = 0;
                    DB::table('ranks')->insert($rnkdata5);
                }
            }

            $cdate = date('Y-m-01');
            if ($current_date == $cdate) {
                // Club Bonus Paid
                $c1_count = DB::table('members')->where('club', 1)->count();
                if ($c1_count > 0) {
                    if ($wallet->exclub > 0) {
                        $expunit = $wallet->exclub/$c1_count;
                        
                        $c1_mem = DB::table('members')->where('club', 1)->get();
                        foreach ($c1_mem as $clm1) {
                            $clmdata1['balance'] = $clm1->balance+$expunit;
                            $clmdata1['tearn'] = $clm1->tearn+$expunit;
                            DB::table('members')->where('id', $clm1->id)->update($clmdata1);

                            $crepo1['type']            = 'member';
                            $crepo1['bal_type']        = 'Earning';
                            $crepo1['wallet']          = 'Balance';
                            $crepo1['action']          = 'Club';                        
                            $crepo1['details']         = 'Executive Club Bonus';
                            $crepo1['bal_in']          = $expunit;
                            $crepo1['bal']             = $clm1->balance+$expunit;   
                            $crepo1['user_id']         = $clm1->id;
                            DB::table('reports')->insert($crepo1);

                        }

                    }
                }

                $c2_count = DB::table('members')->where('club', 2)->count();
                if ($c2_count > 0) {
                    if ($wallet->mclub > 0) {
                        $mnpunit = $wallet->mclub/$c2_count;
                        
                        $c2_mem = DB::table('members')->where('club', 2)->get();
                        foreach ($c2_mem as $clm2) {
                            $clmdata2['balance'] = $clm2->balance+$mnpunit;
                            $clmdata2['tearn'] = $clm2->tearn+$mnpunit;
                            DB::table('members')->where('id', $clm2->id)->update($clmdata2);

                            $crepo2['type']            = 'member';
                            $crepo2['bal_type']        = 'Earning';
                            $crepo2['wallet']          = 'Balance';
                            $crepo2['action']          = 'Club';                        
                            $crepo2['details']         = 'Manager Club Bonus';
                            $crepo2['bal_in']          = $mnpunit;
                            $crepo2['bal']             = $clm2->balance+$mnpunit;   
                            $crepo2['user_id']         = $clm2->id;
                            DB::table('reports')->insert($crepo2);

                        }

                    }
                }

                // Founder Bonus Paid
                $c3_count = DB::table('members')->where('founder', 1)->count();
                if ($c3_count > 0) {
                    if ($wallet->founder > 0) {
                        $fndpunit = $wallet->founder/$c3_count;
                        
                        $c3_mem = DB::table('members')->where('founder', 1)->get();
                        foreach ($c3_mem as $clm3) {
                            $clmdata3['balance'] = $clm3->balance+$fndpunit;
                            $clmdata3['tearn'] = $clm3->tearn+$fndpunit;
                            DB::table('members')->where('id', $clm3->id)->update($clmdata3);

                            $crepo3['type']            = 'member';
                            $crepo3['bal_type']        = 'Earning';
                            $crepo3['wallet']          = 'Balance';
                            $crepo3['action']          = 'Founder';                        
                            $crepo3['details']         = 'Founder Bonus';
                            $crepo3['bal_in']          = $fndpunit;
                            $crepo3['bal']             = $clm3->balance+$fndpunit;   
                            $crepo3['user_id']         = $clm3->id;
                            DB::table('reports')->insert($crepo3);

                        }

                    }
                }

                // Fund Zero
                $fzro['exclub'] = 0;
                $fzro['mclub'] = 0;
                $fzro['founder'] = 0;
                DB::table('wallet_manages')->where('id', $wallet->id)->update($fzro);
            }                
              
            // Day Update
            if ($admin->days < 7) {
                $ddata['days'] = $admin->days+1;
            }else{
                $ddata['days'] = 1;
            }
            DB::table('admins')->where('id', $admin->id)->update($ddata);

            $dateX['date']         = date('Y-m-d');
            $dateX['updated_at']   = date("Y-m-d H:i:s");                 
            DB::table('crone_records')->where('type', 'daily_status')->update($dateX);
        }
    }

   
}
