<?php
$msg = '';
include("include/settings.php");
include("checksession.php");

$where = '';
$condition = '';

$skip = 0;
if (isset($_POST['page_skip'])) {
    $skip = $_POST['page_skip'] * 100 - 100;
} else {
    $_POST['page_skip'] = 0;
}

$pass = 0;

if (isset($_POST['ddlUserId']) && trim($_POST['ddlUserId']) != 0) {
    $pass = 1;
} else if (isset($_POST['ddlLeadType']) && trim($_POST['ddlLeadType']) != 0) {
    $pass = 1;
} else if (isset($_POST['txtFromDate']) && trim($_POST['txtFromDate']) != null) {
    $pass = 1;
} else if (isset($_POST['txtToDate']) && trim($_POST['txtToDate']) != null) {
    $pass = 1;
} else if (isset($_POST['txtSearch']) && trim($_POST['txtSearch']) != null) {
    $pass = 1;
} else if (isset($_POST['countryId']) && trim($_POST['countryId']) != null) {
    $pass = 1;
} else if (isset($_POST['stateId']) && trim($_POST['stateId']) != null) {
    $pass = 1;
} else if (isset($_POST['cityId']) && trim($_POST['cityId']) != null) {
    $pass = 1;
} else if (isset($_POST['searchGender']) && trim($_POST['searchGender']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMemberStatus']) && trim($_POST['searchMemberStatus']) != null) {
    $pass = 1;
} else if (isset($_POST['searchCaste']) && trim($_POST['searchCaste']) != null) {
    $pass = 1;
} else if (isset($_POST['searchResidencyStatus']) && trim($_POST['searchResidencyStatus']) != null) {
    $pass = 1;
} else if (isset($_POST['searchCurrency']) && trim($_POST['searchCurrency']) != null) {
    $pass = 1;
} else if (isset($_POST['searchDisposition']) && trim($_POST['searchDisposition']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinAge']) && trim($_POST['searchMinAge']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxAge']) && trim($_POST['searchMaxAge']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinFootHeight']) && trim($_POST['searchMinFootHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinInchHeight']) && trim($_POST['searchMinInchHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxFootHeight']) && trim($_POST['searchMaxFootHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxInchHeight']) && trim($_POST['searchMaxInchHeight']) != null) {
    $pass = 1;
} else {
    $pass = 0;
}

if ($pass == 1) {

    $where = 'where';

    if (trim($_POST['ddlLeadType']) != 0) {
        switch ($_POST['ddlLeadType']) {
            case 1:
                $leadtype = 'New Call';
                break;
            case 2:
                $leadtype = 'No Match Shared yet';
                break;
            case 3:
                $leadtype = 'Match Shared';
                break;
            case 4:
                $leadtype = 'Follow up';
                break;
            case 5:
                $leadtype = 'Close to conversion';
                break;
            case 6:
                $leadtype = 'No Answer';
                break;
            case 7:
                $leadtype = 'Lead Rejected';
                break;
            case 8:
                $leadtype = 'Refund';
                break;
            case 9:
                $leadtype = 'Fresh Lead';
                break;
            case 10:
                $leadtype = 'Renewal Lead';
                break;
            case 11:
                $leadtype = 'Ptential CRM Lead';
                break;
            default:
                $leadtype = 'Old Leads';
        }
        $condition .= " disposition like '%{$leadtype}%'";
    }

    /*if (trim($_POST['txtSearch']) != null) {
        $search_data = $_POST['txtSearch'];
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " (lead_mobile like '%{$search_data}%' or lead_email like '%{$search_data}%' or lead_name like '%{$search_data}%')";
    }*/

    if (trim($_POST['txtSearch']) != null) {
        $search_data = trim($_POST['txtSearch']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " (profile_contact_no like '%{$search_data}%' or contact_no like '%{$search_data}%' or  profile_email like '%{$search_data}%' or profile_name like '%{$search_data}%' or contact_name like '%{$search_data}%')";
    }

    if (trim($_POST['txtFromDate']) != null) {
        $fromdate = $_POST['txtFromDate'];
        $condition .= trim($condition) != null ? ' and ' : ' ';

        if (trim($_POST['txtFromDate']) != null) {
            $condition .= " lead_created >= '{$fromdate}' ";
        }
    }

    if (trim($_POST['txtToDate']) != null) {
        $todate = $_POST['txtToDate'];
        $condition .= trim($condition) != null ? ' and ' : ' ';

        if (trim($_POST['txtToDate']) != null) {
            $condition .= " lead_created <= '{$todate}' ";
        }
    }

    if (trim($_POST['ddlUserId']) != 0) {
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " lead_allocated_to = " . $_POST['ddlUserId'];
    }

    if (isset($_POST['countryId']) && trim($_POST['countryId']) != null) {

        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " country = '" . $_POST['countryId'] . "' ";
    }

    if (isset($_POST['stateId']) && trim($_POST['stateId']) != null) {

        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " state = '" . $_POST['stateId'] . "' ";
    }

    if (isset($_POST['cityId']) && trim($_POST['cityId']) != null) {

        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " city = '" . $_POST['cityId'] . "' ";
    }

    if (isset($_POST['searchGender']) && trim($_POST['searchGender']) != null) {

        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " gender = '" . $_POST['searchGender'] . "' ";
    }

    if (isset($_POST['searchMemberStatus']) && trim($_POST['searchMemberStatus']) != null) {
        $search_data = trim($_POST['searchMemberStatus']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " member_status = '" . $search_data . "' ";
    }

    if (isset($_POST['searchCaste']) && trim($_POST['searchCaste']) != null) {
        $search_data = trim($_POST['searchCaste']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " caste = '" . $search_data . "' ";
    }

    if (isset($_POST['searchResidencyStatus']) && trim($_POST['searchResidencyStatus']) != null) {
        $search_data = trim($_POST['searchResidencyStatus']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " residency_status = '" . $search_data . "' ";
    }

    if (isset($_POST['searchCurrency']) && trim($_POST['searchCurrency']) != null) {
        $search_data = trim($_POST['searchCurrency']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " currency = '" . $search_data . "' ";
    }

    if (isset($_POST['searchDisposition']) && trim($_POST['searchDisposition']) != null) {
        $search_data = trim($_POST['searchDisposition']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " disposition = '" . $search_data . "' ";
    }

    if (isset($_POST['searchMinAge']) && trim($_POST['searchMinAge']) != null) {
        $search_data = trim($_POST['searchMinAge']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " age >= '" . $search_data . "' ";
    }

    if (isset($_POST['searchMaxAge']) && trim($_POST['searchMaxAge']) != null) {
        $search_data = trim($_POST['searchMaxAge']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " age <= '" . $search_data . "' ";
    }

    if (isset($_POST['searchMinFootHeight']) && trim($_POST['searchMinFootHeight']) != null) {
        $search_data = trim($_POST['searchMinFootHeight']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " height >= '" . $search_data . "' ";
    }

    if (isset($_POST['searchMinInchHeight']) && trim($_POST['searchMinInchHeight']) != null) {
        $search_data = trim($_POST['searchMinInchHeight']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " height_inches >= '" . $search_data . "' ";
    }

    if (isset($_POST['searchMaxFootHeight']) && trim($_POST['searchMaxFootHeight']) != null) {
        $search_data = trim($_POST['searchMaxFootHeight']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " height_inches <= '" . $search_data . "' ";
    }

    if (isset($_POST['searchMaxInchHeight']) && trim($_POST['searchMaxInchHeight']) != null) {
        $search_data = trim($_POST['searchMaxInchHeight']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " height_inches <= '" . $search_data . "' ";
    }
}

if (isset($_POST['ddlLeadMasterType']) && trim($_POST['ddlLeadMasterType']) != null) {
    $where = ' where ';
    $condition .= trim($condition) != null ? ' and ' : ' ';
    $condition .= " lead_type = '" . $_POST['ddlLeadMasterType'] . "' ";
}

$query = "select count(*) from rishta_leadmaster " . $where . " " . $condition . " order by lead_created desc";
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$usermaster->bind_result($total_records);
$usermaster->fetch();
$usermaster->close();


$query = "select *,(select count(*) from cdi_followups where lead_id = rishta_leadmaster.lead_id) as followcount  from rishta_leadmaster " . $where . " " . $condition . " order by lead_created desc limit 100 offset " . $skip;
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$userresult = $usermaster->get_result();

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

<div class="row mtop20">
    <div class="col-md-6">
        <h4 class="subheading">No. of Leads: <?php echo mysqli_num_rows($userresult) ?></h4>
    </div>
    <!--div class="col-md-6 text-right">
        <?php
       // if (isset($_POST['ddlLeadMasterType']) && trim($_POST['ddlLeadMasterType']) != null) {
         //   if (trim($_POST['ddlLeadMasterType']) == 'training') {
                ?>
                <a href="manage-rishta-leads.php" id="" name="" class="btn btn-primary ">Reset Search</a>
          
                <a href="add-new-leads.php" id="btnAddNewUser2" name="btnAddNewUser" class="btn btn-primary">Add Lead</a>
                <?php
          //  } else {
                ?>
                <a href="manage-rishta-leads.php" id="" name="" class="btnCustom ">Reset Search</a>
                <a href="add-new-leads.php" id="btnAddNewUser1" name="btnAddNewUser" class="btnCustom ">Add Lead</a> &nbsp;&nbsp;
                <?php
         //   }
      //  } else {
            ?>
            <a href="add-new-leads.php" id="btnAddNewUser1" name="btnAddNewUser" class="btnCustom ">Add Lead</a> &nbsp;&nbsp;
            <a href="add-new-leads.php" id="btnAddNewUser2" name="btnAddNewUser" class="btnCustom">Add Lead</a>
            <?php
       // }
        ?>
    </div-->
</div>
<div class="row mtop10">
    <div class="col-md-12">
        <?php
        if (mysqli_num_rows($userresult) > 0) {
            ?>
            <table class="table table-bordered table-striped">
                <thead>
                <tr>
               <th>Sr.No</th>
                                        <th><input type='checkbox' id='checkAll'> Check</th>
                                        <th>Disposition</th>
                                        <th>Allocated To</th>
                                        <th>Profile Name</th>
                                        <th>Profile E-mail Id</th>
                </tr>
                </thead>
               				         <tbody>
                                    <?php
                                    $i = 0;
                                    while ($userrow = mysqli_fetch_object($userresult)) {
                                        switch ($userrow->disposition) {
                                            case 'fresh':
                                                $the_alert_class = 'alert-info';
                                                break;
                                            case 'processing':
                                                $the_alert_class = 'alert-warning';
                                                break;
                                            case 'deal':
                                                $the_alert_class = 'alert-success';
                                                break;
                                            case 'lost':
                                                $the_alert_class = 'alert-danger';
                                                break;
                                            default:
                                                $the_alert_class = '';
                                        }
                                        $i++;
                                        ?>
                                        <tr class="text-center alert <?php echo $the_alert_class ?>">
                                            <td><?php echo $i; ?></td>
                                            <td><input type="checkbox" class="chk_lead_allocated" id="chk_lead_allocated_<?php echo $userrow->lead_id ?>" name="lead_allocated[]" value="<?php echo $userrow->lead_id ?>"></td>
                                            <td class="text-left"><?php echo $userrow->disposition ?></td>
                                            <td>
                                                <select name="" class="form-select" onchange="changeLeadAllocation(this.value,<?php echo $userrow->lead_id ?>)">
                                                    <?php
                                                    mysqli_data_seek($salesresult, 0);
                                                    while ($a_userrow = mysqli_fetch_object($salesresult)) {
                                                        ?>
                                                        <option value="<?php echo $a_userrow->user_id ?>" <?php echo $userrow->lead_allocated_to == $a_userrow->user_id ? 'selected' : ''; ?>><?php echo $a_userrow->user_full_name ?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td class="text-left"><a style="color:blue;" href="edit-leads.php?leadid=<?php echo $userrow->lead_id ?>"><?php echo $userrow->profile_name ?></a></td>
                                            <td><?php echo $userrow->email_id ?></td>
                                            <!--td><?php //echo $userrow->contact_no ?></td>
                                            <td><?php //echo $userrow->source ?></td>
                                            <td><?php //echo date("m/d/Y", strtotime($userrow->lead_created_on)) ?></td>
                                            <?php
                                          //  if ($userrow->lead_type == 'rishta_lead') {
                                                ?>
                                                <td><a href="followlead.php?leadid=<?php //echo $userrow->lead_id ?>&is_p=0"><?php //echo $userrow->followcount ?> Follow Ups</a></td>
                                                <td><a href="edit-rishta-lead.php?leadid=<?php //echo $userrow->lead_id ?>">Edit/View</a></td>
                                                <?php
                                          //  } else {
                                                ?>
                                                <td><a href="followlead.php?leadid=<?php //echo $userrow->lead_id ?>&is_p=1"><?php //echo $userrow->followcount ?> Follow Ups</a></td>
                                                <td><a href="edit-rishta-lead.php?leadid=<?php //echo $userrow->lead_id ?>">Edit/View</a></td-->
                                                <?php
                                         //   }
                                            ?>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                    </tbody>
            </table>
            <div align="center">
                <nav>
                    <ul class="pagination">
                        <?php
                        $total = ceil($total_records / 100);
                        for ($i = 1; $i <= $total; $i++) {
                            ?>
                            <li><a style="background-color:yellow; display: flex;" onclick='getLeadsPagination(<?php echo $i ?>)' href="javascript:void(0)" class="pagelink <?php if ($i == $_POST['page_skip']) {
                                    echo "active";
                                } ?>"><?php echo $i ?></a></li>
                            <?php
                        }
                        ?>
                    </ul>
                </nav>
            </div>

            <?php
        } else {
            ?>
            <div class="alert alert-danger">
                <strong>Warning!</strong> You have no leads.
            </div>
            <?php
        }
        ?>
    </div>
</div>