<?php
include("auth.php");
session_start();
?>

<?php
function getSponser($cid)
{
	require("../conn.php");
	$sql = "select sponser as sponser from parent_child where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	return ($rs['sponser']);
}
function getDna($cid)
{
	require("../conn.php");
	if ($cid == "EMPTY") {
		return ("NULL");
	} else {
		$sponser = getSponser($cid);
		$sql = "select dna as dna from parent_child where cid='$cid'";
		$smt = mysqli_query($conn, $sql);
		$rs = mysqli_fetch_assoc($smt);
		return ($rs['dna']);
	}
}
function getLeftNode($cid)
{
	require("../conn.php");

	$sponser = getSponser($cid);
	$sql = "select lcid from parent_child where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	return ($rs['lcid']);
}
function getRightNode($cid)
{
	require("../conn.php");
	$sponser = getSponser($cid);
	$sql = "select rcid from parent_child where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	return ($rs['rcid']);
}
function getTotal($cid)
{
	require("../conn.php");
	$dna = getDna($cid);

	$sql = "select count(*)as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	return ($rs['tot']);
}
function getTotalLeft($cid)
{
	require("../conn.php");

	$dna = getDna(getLeftNode($cid));

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalRight($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and candidate.cid<>'$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalActiveLeft($cid)
{
	require("../conn.php");
	$dna = getDna(getLeftNode($cid));

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveRight($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalActiveLeftBv($cid)
{
	require("../conn.php");
	$dna = getDna(getLeftNode($cid));

	$sql = "select sum(bv) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveRightBv($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));

	$sql = "select sum(bv) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalActiveLeftPur($cid)
{
	require("../conn.php");
	$dna = getDna(getLeftNode($cid));

	$sql = "select sum(pkg_amt) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveRightPur($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));

	$sql = "select sum(pkg_amt) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and parent_child.dna like '%" . $dna . "%' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveLeftRep($cid)
{
	require("../conn.php");
	$dna = getDna(getLeftNode($cid));
	$bv = 0;

	$sql = "select sum(amount) as tot from parent_child INNER JOIN tbl_repurchase on parent_child.cid=tbl_repurchase.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	if ($rs['tot'] != "")
		$rep = $rs['tot'];
	return ($rep);
}
function getTotalActiveRightRep($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));
	$bv = 0;
	$sql = "select sum(amount) as tot from parent_child INNER JOIN tbl_repurchase on parent_child.cid=tbl_repurchase.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	if ($rs['tot'] != "")
		$rep = $rs['tot'];
	return ($rep);
}

function getTotalActiveLeftRepBv($cid)
{
	require("../conn.php");
	$dna = getDna(getLeftNode($cid));
	$bv = 0;

	$sql = "select sum(bv) as tot from parent_child INNER JOIN tbl_repurchase on parent_child.cid=tbl_repurchase.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	if ($rs['tot'] != "")
		$bv = $rs['tot'];
	return ($bv);
}
function getTotalActiveRightRepBv($cid)
{
	require("../conn.php");
	$dna = getDna(getRightNode($cid));
	$bv = 0;
	$sql = "select sum(bv) as tot from parent_child INNER JOIN tbl_repurchase on parent_child.cid=tbl_repurchase.cid and parent_child.dna like '%" . $dna . "%'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	if ($rs['tot'] != "")
		$bv = $rs['tot'];
	return ($bv);
}
function getTotalRepurchase($cid)
{
	require("../conn.php");

	$sql = "select sum(amount) as tot from tbl_repurchase where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalActiveSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalLeftSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid' and position='Left'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalRightSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid' and position='Right'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalRepurchaseAmt($cid)
{
	require("../conn.php");

	$sql = "select sum(amount) as tot from tbl_repurchase where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

function getTotalRepurchaseBv($cid)
{
	require("../conn.php");

	$sql = "select sum(bv) as tot from tbl_repurchase where cid='$cid'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveLeftSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid' and position='Left' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}
function getTotalActiveRightSponsered($cid)
{
	require("../conn.php");

	$sql = "select count(*) as tot from parent_child INNER JOIN candidate on parent_child.cid=candidate.cid and sponser='$cid' and position='Right' and status='1'";
	$smt = mysqli_query($conn, $sql);
	$rs = mysqli_fetch_assoc($smt);
	$res = 0;
	if ($rs['tot'])
		$res = $rs['tot'];
	return ($res);
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Wallet</title>
  <meta content="" name="description">
  <meta content="" name="keywords">
    <link href="assets/img/favicon.png" rel="icon">
    <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  <?php include("templates/inc.php"); ?>
</head>

<body>

  <!-- ======= Header ======= -->
  <?php
  include("templates/header.php");
  ?>

  <!-- End Header -->

  <!-- ======= Sidebar ======= -->

  <?php
  include("templates/sidebar.php");

  ?>


  <!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1>Wallet</h1>
     
    </div><!-- End Page Title -->


       <?php
			$v_code = $_SESSION['v_code'];
			include "./conn.php";
			$sql = "select * from tbl_vendor where v_code='$v_code'";
			$smt = mysqli_query($conn, $sql);

			$rs = mysqli_fetch_assoc($smt);


			?>


    <section class="section dashboard">
      <div class="row">
        <!-- Left side columns -->
        <div class="col-lg-12">
          <div class="row">
            <!-- Sales Card -->
            <div class="col-xxl-2 col-md-6">
              <div class="card info-card revenue-card">

                <div class="card-body">
                  <a href="profile.php">
                    <h5 class="card-title">Wallet <span>| Balance</span></h5>
                  </a>
                  <div class="d-flex align-items-center"><a href="profile.php">
                      <div class="card-icon rounded-circle d-flex align-items-center justify-content-center">
                        <i class="bi bi-person-bounding-box"></i>
                      </div>
                    </a>
                    <div class="ps-3"><a href="">
                      </a>
              <h6> ₹&nbsp;<?php echo $rs['wallet'];  ?>  </h6>


                    </div>
                  </div>
                </div>

              </div>
            </div><!-- End Revenue Card -->
            
            
           
               <div class="col-md-6">
          <div class="card info-card revenue-card">
            <div class="card-body">
              <a href="withdrawal.php">
                <h5 class="card-title">Withdraw <span>| Amount</span></h5>
                <div class="d-flex align-items-center">
              <div class="card-icon rounded-circle d-flex align-items-center justify-content-center">
                <i class="bi bi-wallet2"></i>
                  </div>
                  <div class="ps-3">
                     <h6><img src="assets/withdraw.png" alt="withdwawel button" style="height: 3rem;"></h6>
                     
                    
              </a>

  
            </div>
          </div>
        </div>

      </div>
    </div>
    
                

          </div>
        </div>

      </div>


    </section>
    <br><br><br>

  </main><!-- End #main -->
  <!-- ======= Footer ======= -->
  <?php
  include("templates/footer.php");
  ?>

  <!-- End Footer -->


  <?php include("templates/vendors.php"); ?>


</html>