<?php
$msg = '';

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

$conn=new MySQLi("localhost","rishtadepot_new","Sailor@#321","rishtadepot_live");


$salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
$salesmaster->execute();
$salesresult = $salesmaster->get_result();


// Fetch records
$sql = "SELECT * FROM source";
$result = mysqli_query($conn, $sql);


// Prepare the SQL query
$query = "
    SELECT s.name AS source_name,
           COUNT(*) AS total_count,
           COUNT(CASE WHEN l.disposition = 'Fully Paid' THEN 1 END) AS fully_paid_count
    FROM source s
    LEFT JOIN rishta_leadmaster l ON s.name = l.source AND l.lead_type = 'rishta_lead'
    GROUP BY s.name
";

// Execute the query
$result = mysqli_query($conn, $query);



?>
<!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>Sales Status 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">Sales Status 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">

			 <div class="box">
				<div class="box-header with-border">
				  <h3 class="box-title">Lead Source Report</h3>
				</div>
			<form action="#" method="post" enctype="multipart/form-data">
					<div class="box-body">
					         <?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="row">
						  <div class="col-md-4">
							<div class="form-group">
							  <input placeholder='Start Date' type="date" id="txtFromDate" name="txtFromDate" class="form-control" autocomplete='off' onchange="getLeads()"/>
                    </div>
						  </div>
						  <div class="col-md-4">
							<div class="form-group">
							 <input placeholder='End Date' type="date" id="txtToDate" name="txtToDate" class="form-control" autocomplete='off' onchange="getLeads()"/>
                    </div>
						  </div>
						  	     <div class="col-md-4">
                        <select id="ddlUserId" name="ddlUserId" class="form-select" onchange="getLeads()">
                            <option value="0">All Executives</option>
                            <?php
                            while ($e_userrow = mysqli_fetch_object($salesresult)) {
                                ?>
                                <option value="<?php echo $e_userrow->user_id ?>"><?php echo $e_userrow->user_full_name ?></option>
                                <?php
                            }
                            ?>
                        </select>
                    </div>
						</div>
					</div>
				</form>
				<!-- /.box-header -->
				<div class="box-body"  id="ajaxcoursedata">
					<div class="table-responsive">
					  <table  class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Sr. No</th>
								<th>Lead Source</th>
								<th>Total</th>
								<th>Conversion Count</th>
							</tr>
						</thead>
								<tbody>
											        
													
			<?php										              
// Check if there are any results
if ($result) {
    // Fetch and display the results
    $i = 0;
    while ($row = mysqli_fetch_assoc($result)) {
        $i++;
        ?>
        <tr class="text-center">
            <td><?php echo $i; ?></td>
            <td><?php echo $row['source_name']; ?></td>
            <td><?php echo $row['total_count']; ?></td>
            <td><?php echo $row['fully_paid_count']; ?></td>
        </tr>
        <?php
    }
} else {
    // Handle query execution failure
    echo "Error executing the query: " . mysqli_error($conn);
}
?>
														
												</tbody>
				
					  </table>
					</div>
				</div>
				<!-- /.box-body -->
			  </div>
			  <!-- /.box -->


			  <!-- /.box -->          
			</div>
			<!-- /.col -->
		  </div>
		  <!-- /.row -->
		</section>
		<!-- /.content -->
	  
	  </div>
  </div>
  <!-- /.content-wrapper -->
  <script>
    function getLeads() {

        var txtFromDate = $('#txtFromDate').val();
        var txtToDate = $('#txtToDate').val();
        var txtSearch = $('#txtSearch').val();

        var ddlUserId = $('#ddlUserId').val();

    


        $('#loading').css('display', '');
        $.ajax({
            url: 'sale_status_ajax.php',
            type: 'post',
            data: {
                ddlUserId: ddlUserId,

            
                txtFromDate: txtFromDate,
                txtToDate: txtToDate,
                txtSearch: txtSearch,
      
            },
            datatype: 'html',
            success: function (rsp) {
                $('#ajaxcoursedata').html(rsp);
                $('#loading').css('display', 'none');
            }
        });
    }
    
    
 function getLeadsPagination(page_skip) {
        var txtFromDate = $('#txtFromDate').val();
        var txtToDate = $('#txtToDate').val();
        var txtSearch = $('#txtSearch').val();
       
        var ddlUserId = $('#ddlUserId').val();

        $('#loading').css('display', '');
        $.ajax({
            url: 'sale_status_ajax.php',
            type: 'post',
            data: {"ddlUserId": ddlUserId, "txtFromDate": txtFromDate, "txtToDate": txtToDate, "txtSearch": txtSearch, "page_skip": page_skip},
            datatype: 'html',
            success: function (rsp) {
                $('#ajaxcoursedata').html(rsp);
                $('#loading').css('display', 'none');
            }
        });
    }


    function changeLeadAllocation(executive_id, lead_id) {
        $('#loading').css('display', '');
        $.ajax({
            url: 'allocate_lead.php',
            type: 'post',
            data: {"executive_id": executive_id, "lead_id": lead_id},
            datatype: 'html',
            success: function (rsp) {
                $('#loading').css('display', 'none');
            }
        });
    }

  

    $(document).ready(function () {
        $("#txtFromDate,#txtToDate").datepicker({
            dateFormat: 'yy-mm-dd',
			//dateFormat: 'mm/dd/yy'
            // minDate: 0, // 0 days offset = today
        });
    });
</script>
	    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	    <style>
	        .padd{
	            padding-top:40px;
	            padding-left:20px;
	        }
	    </style>
<?php include('./include/footer.php'); ?>
