<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<!-- Main wrapper - style you can find in pages.scss -->
<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>
        
<!-- Page wrapper  -->
<div class="page-wrapper">
<!-- Container fluid  -->
<div class="container-fluid">
<!-- Bread crumb and right sidebar toggle -->
<!--?php include ("../Header/breadcumb.php"); ?-->
<br><br>
<!-- End Bread crumb and right sidebar toggle -->
    
<?php if ($level == "1")
{
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <form method="post" action="" enctype="multipart/form-data">
                    <div class="form-body">
                        <h3 class="card-title">Group Sale Selection</h3>
                        <hr>
                        <div class="row p-t-20">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="control-label">Member ID</label>
                                    <input type="text" class="form-control" name="member_ref_code"  required>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="control-label">From</label>
                                    <input type="date" class="form-control" name="from_date"  required>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="control-label">To</label>
                                    <input type="date" class="form-control" name="to_date"  required>
                                </div>
                            </div>                   
                        </div>
                    </div>
                    <div class="form-actions">
                        <button name="Date_Selection" class="btn btn-success"><i class="fa fa-check"></i> Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php
}
?>


<?php
if ($level == "1" && isset($_POST['Date_Selection']))
{
    $from_date = $_POST['from_date'];
    $to_date = $_POST['to_date'];
    $member_ref_code = $_POST['member_ref_code'];

    $from_date_display = date('d-m-Y', strtotime($_POST['from_date']));
    $to_date_display = date('d-m-Y', strtotime($_POST['to_date']));

    date_default_timezone_set("Asia/Kuala_Lumpur");
    $from_date_display = $from_date_display ?? date('d-m-Y');
    $to_date_display   = $to_date_display   ?? date('d-m-Y');

    if (empty($from_date)) $from_date = date('Y-m-d');
    if (empty($to_date))   $to_date   = date('Y-m-d');
    
    $sql = "SELECT * FROM user
    WHERE ref_code='$member_ref_code'
    And status='1'";
    $result = $mysqli->query($sql);
	if ($result->num_rows > 0)
	{
		$row = $result ->fetch_assoc();
		$id = $row['user_id'];
	}
    else
    {
        echo "<script type='text/javascript'>alert('Member Not Exist');</script>";
		echo "<script type='text/javascript'> document.location='group_sale.php'; </script>";
		exit();
	}
}
else
if ($level == "2")
{
    $sql = "SELECT * FROM user
    WHERE ref_code='$ref_code'
    And status='1'";
    $result = $mysqli->query($sql);
	if ($result->num_rows > 0)
	{
		$row = $result ->fetch_assoc();
		 $id = $row['user_id'];
	}
}

if ($id != "")
{
    $group_sale_looping = array("1", "2", "3", "4", "5", "6");

    foreach ($group_sale_looping as $group_sale_looping_list) 
    {
        if ($group_sale_looping_list == '1')
        {
            $array = Check_Group_Sale($id, $group_sale_looping_list, $from_date, $to_date, $level);

            $My_downlinelvl1 = $array[0];
            $total_invest_lvl1 = $array[1];
	    }
        else
        if ($group_sale_looping_list == '2')
        {
            if ($My_downlinelvl1 != "")
            {
                $array = Check_Group_Sale($My_downlinelvl1, $group_sale_looping_list, $from_date, $to_date, $level);

                $My_downlinelvl2 = $array[0];
                $total_invest_lvl2 = $array[1];
            }
            
	    }
        else
        if ($group_sale_looping_list == '3')
        {
            if ($My_downlinelvl2 != "")
            {
                $array = Check_Group_Sale($My_downlinelvl2, $group_sale_looping_list, $from_date, $to_date, $level);

                $My_downlinelvl3 = $array[0];
                $total_invest_lvl3 = $array[1];
            }
            
	    }
        else
        if ($group_sale_looping_list == '4')
        {
            if ($My_downlinelvl3 != "")
            {
                $array = Check_Group_Sale($My_downlinelvl3, $group_sale_looping_list, $from_date, $to_date, $level);

                $My_downlinelvl4 = $array[0];
                $total_invest_lvl4 = $array[1];
            }  
	    }
        else
        if ($group_sale_looping_list == '5')
        {
            if ($My_downlinelvl4 != "")
            {
                $array = Check_Group_Sale($My_downlinelvl4, $group_sale_looping_list, $from_date, $to_date, $level);

                $My_downlinelvl5 = $array[0];
                $total_invest_lvl5 = $array[1];
			}
            
	    }
        else
        if ($group_sale_looping_list == '6')
        {
            if ($My_downlinelvl5 != "")
            {
                $array = Check_Group_Sale($My_downlinelvl5, $group_sale_looping_list, $from_date, $to_date, $level);

                $My_downlinelvl6 = $array[0];
                $total_invest_lvl6 = $array[1];
            }
            
	    }
    }

    $total_invest = $total_invest_lvl1 + $total_invest_lvl2 + $total_invest_lvl3 + $total_invest_lvl4 + $total_invest_lvl5 + $total_invest_lvl6;
    $total_invest_display = number_format($total_invest,2);
}
    
			   
?>     
<!-- Start Page Content -->
<!-- ============================================================== -->
<div class="row">
<div class="col-12">
    <div class="card">
        <div class="card-body">
            <?php if ($level == "1")
            {
            ?>
                <h4 class="card-title">Total Group Sale of <?php echo $member_ref_code; ?> from <?php echo $from_date_display; ?> to <?php echo $to_date_display; ?></h4>
            <?php
			}
            else
            {
            ?>
                <h4 class="card-title">Total Group Sale</h4>
            <?php
			}
            ?>
            
            <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>
            <div class="table-responsive m-t-40">
                                
                <table id="example23"
                    class="display nowrap table table-hover table-striped table-bordered"
                    cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Level</th>
                            <th>Level Group Sale</th>                           
                        </tr>
                    </thead>                                      
                    <tbody>
                        <tr>
                            <td>Level 1</td>
                            <td>$<?php echo number_format($total_invest_lvl1,2);?></td>
                        </tr>

                        <tr>
                            <td>Level 2</td>
                            <td>$<?php echo number_format($total_invest_lvl2,2);?></td>
                        </tr>

                        <tr>
                            <td>Level 3</td>
                            <td>$<?php echo number_format($total_invest_lvl3,2);?></td>
                        </tr>

                        <tr>
                            <td>Level 4</td>
                            <td>$<?php echo number_format($total_invest_lvl4,2);?></td>
                        </tr>

                        <tr>
                            <td>Level 5</td>
                            <td>$<?php echo number_format($total_invest_lvl5,2);?></td>
                        </tr>

                        <tr>
                            <td>Level 6</td>
                            <td>$<?php echo number_format($total_invest_lvl6,2);?></td>
                        </tr>

                        <tr>
                            <td><b>TOTAL</b></td>
                            <td><b>$<?php echo $total_invest_display;?></b></td>
                        </tr>
                    </tbody>
                    </table>
                </form>
                                            
            </div>
        </div>
    </div>                      
</div>
</div>
<!-- ============================================================== -->
<!-- End PAge Content -->

</div>
<!-- End Container fluid  -->
</div>
<!-- End Page wrapper  -->

<!-- Footer Start-->
<?php include ("../Footer/footer.php"); ?>
<!-- Footer End-->

</div>
<!-- End Wrapper -->
<!-- All Jquery -->
    

<!-- All Jquery Start-->
<?php include ("../Header/all_jquery.php"); ?>
<!-- All Jquery End-->

</body>

</html>