<?php
$msg = '';

include("include/settings.php");
include("checksession.php");

$usermaster = $mysqli->prepare("select count(*) from cdi_usermaster where user_id=?");
$usermaster->bind_param("i", $_GET['userid']);
$usermaster->execute();
$usermaster->bind_result($membercount);
$usermaster->fetch();
$usermaster->close();

if ($membercount == 0) {
    header("location:home.php");
}

$memberdetailmaster = $mysqli->prepare("select * from cdi_usermaster where user_id=?");
$memberdetailmaster->bind_param("i", $_GET['userid']);
$memberdetailmaster->execute();
$memberdetailmaster = $memberdetailmaster->get_result();
$userrow = mysqli_fetch_object($memberdetailmaster);

$userid = $userrow->user_id;

$usermaster = $mysqli->prepare("select * from rishta_target where user_id=$userid  ORDER BY `month` ASC, `year` DESC ");
$usermaster->execute();
$userresult = $usermaster->get_result();
//$userrows=mysqli_fetch_object($userresult);




function getCountMonthlyLeads($tablename, $type, $e_id, $month = 0, $year = 0)
{
    global $mysqli;
    $count = 0;

    if ($e_id != 0) {

        $select_query = "";
        if ($type == "rishta_lead") {

            $select_query = "select count(*) from " . $tablename . " where lead_type = '" . $type . "' and MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;

        } else if ($type == "not_rishta_lead") {

            $type = "rishta_lead";
            $select_query = "select count(*) from " . $tablename . " where lead_type != '" . $type . "' and MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;

        } else {

            $select_query = "select count(*) from " . $tablename . " where MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;
        }

        $countmaster = $mysqli->prepare($select_query);
        $countmaster->execute();
        $countmaster->bind_result($count);
        $countmaster->fetch();
        $countmaster->close();
    }
    return $count;
}


function getCountMonthlyLeadsd($tablename, $disposition, $e_id, $month = 0, $year = 0)
{
    global $mysqli;
    $counts = 0;

    if ($e_id != 0) {

        $select_querys = "";
        if ($disposition == "Fully Paid") {

            $select_querys = "select count(*) from " . $tablename . " where disposition = '" . $disposition . "' and MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;

        } else if ($disposition == "not_fully_paid") {

            $disposition = "fully paid";
            $select_querys = "select count(*) from " . $tablename . " where disposition != '" . $disposition . "' and MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;

        } else {

            $select_querys = "select count(*) from " . $tablename . " where MONTH(lead_created)='" . $month . "' and YEAR(lead_created)='" . $year . "' and lead_allocated_to=" . $e_id;
        }

        $countmaster = $mysqli->prepare($select_querys);
        $countmaster->execute();
        $countmaster->bind_result($counts);
        $countmaster->fetch();
        $countmaster->close();
    }
    return $counts;
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>View Sales Target Report - Rishta Depot</title>
  
		
<?php include('./include/meta.php'); ?>

</head>

	
<?php include('./include/header.php'); ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
	  <div class="container-full">
		<!-- Content Header (Page header) -->
		<div class="content-header">
			<div class="d-flex align-items-center">
				<div class="me-auto">
					<h4 class="page-title">Reports</h4>
					<div class="d-inline-block align-items-center">
						<nav>
							<ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="#"><i class="mdi mdi-home-outline"></i></a></li>
								<li class="breadcrumb-item" aria-current="page">View Sales Target Report</li>
								<li class="breadcrumb-item active" aria-current="page">Reports</li>
							</ol>
						</nav>
					</div>
				</div>
				
			</div>
		</div>

		<!-- Main content -->
		<section class="content">
		  <div class="row">
			<div class="col-12">
     <?php
                    if (trim($msg) != null) {
                        ?>
                        <div class='row'>
                            <div class='col-md-12'>
                                <div class="alert alert-success errormessage" id="errormessage">
                                    <strong><?php echo $msg ?></strong>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
			 <div class="box">
				<div class="box-header with-border">
				  <h3 class="box-title">View Report</h3>
				</div>
				<form class="form">
					<div class="box-body">
						<div class="row">
						  <div class="col-md-4">
							<div class="form-group">
								<label class="form-label">Email Id</label>
							  <input type="text" id="txtEmailId" name="txtEmailId" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
                            <input type="hidden" id="txtHfEmailId" name="txtHfEmailId" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
                        </div>
						  </div>
						</div>
					</div>
				</form>
				<!-- /.box-header -->
				<div class="box-body" style="margin-top: -30px;">
				         <?php
                            $salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
                            $salesmaster->execute();
                            $salesresult = $salesmaster->get_result();

                            $salesrow = mysqli_fetch_object($salesresult);
                            /*$total_leads = getCountMonthlyLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);*/
                            ?>

					<div class="table-responsive">
					         <?php
                            if (mysqli_num_rows($userresult) > 0) {
                                ?>
					  <table id="example1" class="table table-bordered table-striped">
						<thead>
						     <tr>
                                        <th>Sr.No</th>
                                        <th>Target</th>
                                        <th>Month</th>
                                        <th>Year</th>
                                        <th>Total Leads</th>
                                        <th>Lead Closed</th>
                                        <th>Pending</th>
                                        <th>Percentage</th>
                                        <!--th>Delete</th-->
                                    </tr>
						</thead>
						 <tbody>
                                    <?php
                                    $i = 0;
                                    while ($userrow = mysqli_fetch_object($userresult)) {
                                        $i++;
                                     
                                        $total_leads = getCountMonthlyLeads('rishta_leadmaster', 'total', $userrow->user_id, $userrow->month, $userrow->year);
                                        $total_achievements = getCountMonthlyLeadsd('rishta_leadmaster', 'Fully Paid', $userrow->user_id, $userrow->month, $userrow->year);
                                        $total_pending = getCountMonthlyLeadsd('rishta_leadmaster', 'not_fully_paid', $userrow->user_id, $userrow->month, $userrow->year);
                                        $percentage = 0;
                                        if(!empty($total_leads)) {

                                            if(!empty($total_achievements)) {

                                                $percentage = (($total_achievements * 100)/$total_leads);
                                            }
                                        }
                                        $percentage = number_format($percentage, 2, '.', '');
                                        ?>
                                        <tr class="text-center">
                                            <td><?php echo $i; ?></td>
                                            <td><?php echo $userrow->target ?></td>
                                            <td>
                                                
                                               <?php  $monthNum= $userrow->month;
                                             //  echo  $monthNum ;
                                                                    
                    $dateObj   = DateTime::createFromFormat('!m', $monthNum);
                    $monthName = $dateObj->format('F'); // March
                                             echo  $monthName ;
                                               ?>
                                            
                                            </td>
                                            <td><?php echo $userrow->year ?></td>
                                            <td><?php echo $total_leads ?></td>
                                            <td><?php echo $total_achievements ?></td>
                                            <td><?php echo $total_pending ?></td>
                                            <td><?php echo $percentage ?>%</td>
                                             <!--td><a href="delete-target.php?leadid=<?php //echo $userrow->id ?>">Delete</a></td-->
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                    </tbody>
				
					  </table>
					     <?php
                            }
                            ?>
					</div>
				</div>
				<!-- /.box-body -->
			  </div>
			  <!-- /.box -->


			  <!-- /.box -->          
			</div>
			<!-- /.col -->
		  </div>
		  <!-- /.row -->
		</section>
		<!-- /.content -->
	  
	  </div>
  </div>
  <!-- /.content-wrapper -->
  <script>

    function saveuser() {
        var txtName = $('#txtName').val();
        var txtPassword = $('#txtPassword').val();
        var txtConfirmPassword = $('#txtConfirmPassword').val();
        var txtEmailId = $('#txtEmailId').val();
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if ($.trim(txtName).length == 0 || $.trim(txtEmailId).length == 0) {
            alert("Please fill all the required fields.");
            return false;
        }
        else if (emailReg.test(txtEmailId) == false) {
            alert("Enter the valid email");
            return false;
        }
        else if (txtPassword != txtConfirmPassword) {
            alert("Enter The Correct Passwords");
            return false;
        }
        else if (txtPassword.length != 0 && ((txtConfirmPassword.length < 8 || txtConfirmPassword.length > 20) || (!/\d/.test(txtConfirmPassword)) || (!/[a-z]/.test(txtConfirmPassword)) || (!/[A-Z]/.test(txtConfirmPassword)) || (!/[!@#$%\^&*(){}[\]<>?/|\-]/.test(txtConfirmPassword)))) {
            msg = "Password must be 8-20 characters<br/>";
            msg += "Password must contain 1 digit<br/>";
            msg += "Password must contain 1 lower case character<br/>";
            msg += "Password must contain 1 upper case character<br/>";
            msg += "Password must contain 1 special character<br/>";
            $('.errormessage').html(msg);
            return false;
        }
        else {
            return confirm('Are You Sure?');
        }
    }

    function back() {
        var c = confirm('Are You Sure');
        if (c == true) {
            window.location = "assign-leads.php";
        }
    }

    function isDecimal(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
            (charCode < 48 || charCode > 57))
            return false;
        return true;
    }

    function isNumber(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode < 48 || charCode > 57))
            return false;
        return true;
    }

    $(document).ready(function () {
        $('body').on("keypress", '#txtExperience', function () {
            return isDecimal(event, this)
        });
        $('body').on("keypress", '#txtCredits', function () {
            return isNumber(event, this)
        });
        $('#txtName').keypress(function (e) {
            var regex = new RegExp("^[a-zA-Z ]+$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }

            e.preventDefault();
            return false;
        });
        $('input').bind("cut copy paste", function (e) {
            e.preventDefault();
        });
    });
</script>
	
<?php include('./include/footer.php'); ?>