<?php
require_once ("../config.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/arrays.inc.php");
require_once ("../classes/users.class.php");
require_once ("../includes/Pager/Pager.php");

$message	= "";
$pages		= "";
$data		= array();
$array		= array();
$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$flag		= isset($_REQUEST['act'])	?	trim($_REQUEST['act'])	:	"0";

$title		= isset($_REQUEST['title'])	?	trim($_REQUEST['title'])	:	"";
$order		= isset($_REQUEST['order'])	?	trim($_REQUEST['order'])	:	"";
//selected for profile filtering
$ddlProfile=($_POST['ddlProfile']!='')?$_POST['ddlProfile']:$_GET['ddlProfile'];
//for paging
$_SESSION['ddlProfile']=$ddlProfile;
//--------------Pagination---------------
 $page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"";
if(!is_numeric($page))
    $page = 1;
//--------------Pagination---------------

$image='<img src="../images/bgar.gif"></img>';
if($order=='DESC') {

    $image='<img src="../images/descar.gif"></img>';
}

else if($order=='ASC') {

    $image='<img src="../images/ascrar.gif" ></img>';

}



if (!isset($_REQUEST['page'])) {
    $_SESSION['api_search']	= "";
}

if (!$objDB) {
    $message	= "Unable to Locate Database Server.";
} else {
   
    $objU		= new users($objDB);
    if ($flag == "1") {
        $message	= "<font color='green'>Profile updated successfully</font><br>";
    }
    if (isset($_POST['DelNow'])) {
        $message	= $objU->delete_users($_POST);
    }
    if(isset($_POST['btnExportExcel'])) {
        $array		= $objU->export_to_excel($_POST, true);
        
    }//end if
    else if (isset($_POST['SearchNow'])) {
        $cntUsers       = $objU->search_users_count($_POST,true);
        $pageCount      = ceil($cntUsers / PAGESIZE);
        $pageMaxCount   = PAGESIZE;
        $data           = $objU->search_users($_POST,true,$sort=$title.' '.$order,'',$page,$pageMaxCount);
    }//end if
    else if(isset($ddlProfile) && $ddlProfile!='') {
        //$array		= $objU->search_users2($_POST, true);
        $cntUsers       = $objU->search_users2_count($_POST,true);
        $pageCount      = ceil($cntUsers / PAGESIZE);
        $pageMaxCount   = PAGESIZE;
        $data           = $objU->search_users2($_POST,true,'',$page,$pageMaxCount);
    }//end if
    else {
        //$array		= $objU->search_users($_SESSION,false,$sort=$title.' '.$order);
         //------------------Pagination------------------
        $cntUsers       = $objU->search_users_count($_SESSION,false,$sort=$title.' '.$order);
        $pageCount      = ceil($cntUsers / PAGESIZE);
        $pageMaxCount   = PAGESIZE;
        $data           = $objU->search_users($_SESSION,false,$sort=$title.' '.$order,'',$page,$pageMaxCount);
    }//end else


//    $params 	= array(
//            'mode'      => 'Sliding',
//            'perPage'   => PAGESIZE,
//            'urlVar'    => 'page',
//            'extraVars' => array(),
//            'itemData'  => $array
//    );
//    $objP 		= & Pager::factory($params);
//    $data  		= $objP->getPageData();

    if (empty($data))
        $message	.= "<br>Sorry, no data found<br>";
}



include_once('title.inc.php');


?>
<script type="text/javascript" src="../scripts/calendar.js"></script>
<script type="text/javascript" src="../scripts/calendar-setup.js"></script>
<script type="text/javascript" src="../scripts/calendar-en.js"></script>

<link href="../scripts/calendar.css" rel="stylesheet" type="text/css">




<link href="../js/searchstyle.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
    function confirm_check()
    {
        var chkVar;
        for (var i = 0; i < document.form1.elements.length; i++)
        {
            var e = document.form1.elements[i];
            if (e.name == "ids[]")
            {
                if(e.checked==true)
                {
                    chkVar=true;
                }//end if
            }//end if
        }//end for loop

        if(chkVar==true)
        {
            var where_to= confirm("Do you really want to delete these users for permenantly??");
            if (where_to== true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }//end if
    }
</script>
<script type="text/javascript"><!--

    var formblock;
    var forminputs;

    function prepare() {
        formblock= document.getElementById('form_id');
        forminputs = formblock.getElementsByTagName('input');
    }
    /*
    function select_all(name, value) {
        for (i = 0; i < forminputs.length; i++) {
            // regex here to check name attribute
            var regex = new RegExp(name, "i");
            if (regex.test(forminputs[i].getAttribute('name'))) {
                if (value == '1') {
                    forminputs[i].checked = true;
                } else {
                    forminputs[i].checked = false;
                }
            }
        }
    }
    */

    /*
    function checkUncheckAll(event){
        var checked = ($(event).attr("checked") == 'checked') ? true : false;
        $("#tblSortId tr td input:checkbox").attr("checked", checked);
    }
    
    if (window.addEventListener) {
        window.addEventListener("load", prepare, false);
    } else if (window.attachEvent) {
        window.attachEvent("onload", prepare)
    } else if (document.getElementById) {
        window.onload = prepare;
    }
    */

   $(function(){
        // add multiple select / deselect functionality
        $("#checkAll").click(function () {
              $('.case').attr('checked', this.checked);
        });

        // if all checkbox are selected, check the selectall checkbox
        // and viceversa
        $(".case").click(function(){

            if($(".case").length == $(".case:checked").length) {
                $("#checkAll").attr("checked", "checked");
            } else {
                $("#checkAll").removeAttr("checked");
            }

        });
    });

    //--></script>




<script type="text/javascript" src="../js/jquery.tablesorter.min.js"></script>

<script type="text/javascript" src="../js/jquery.autocomplete.js"></script>

 <script type="text/javascript">
$(document).ready(function() {
    /*$("#tblSortId")
    .tablesorter({widthFixed: false, widgets: ['zebra']})
    */
    //.tablesorterPager({container: $("#pager")});
});
</script>
<?php include_once('tophtml.php');?>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="72%" valign="top">
                        <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                            <tr>
                                <td><table width="100%"  border="0" cellspacing="0" cellpadding="5">
                                        <tr>
                                            <td height="10">&nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td width="41%" class="headings1">Users</td>
                                                
                                        </tr>
                                    </table>
                                    <div class="bordernew padding2">
                                        <table width="100%" cellspacing="0" cellpadding="0" border="0" class="search_wrapper_tbl_admin">
                                            <tr>
                                                <td class="maintext" width="21%"><form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="frmProfileSearch">
                                                        Profile Status&nbsp;&nbsp;
                                                        <select name="ddlProfile" class="textbox" onChange="document.frmProfileSearch.submit();">
                                                            <option value="all" <?php
                                                                if ($ddlProfile == 'all') {
                                                                    echo 'selected';
                                                                }
                                                                ?>>All
                                                            </option>
                                                            <option value="active" <?php
                                                            if ($ddlProfile == 'active') {
                                                                echo 'selected';
                                                            }
                                                            ?>>Active
                                                            </option>
                                                            <option value="not" <?php
                                                            if ($ddlProfile == 'not') {
                                                                echo 'selected';
                                                            }
                                                            ?>>In Active
                                                            </option>
                                                            <option value="susp" <?php
                                                            if ($ddlProfile == 'susp') {
                                                                echo 'selected';
                                                            }
                                                            ?>>Suspended
                                                            </option>
                                                            <option value="expire" <?php
                                                            if ($ddlProfile == 'expire') {
                                                                echo 'selected';
                                                            }
                                                            ?>>Expired
                                                            </option>
                                                        </select>
                                                    </form>                                              
                                                </td>
                                                <td align="left" class="maintext"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="maintext">
                                                            <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="frmSearch">
                                                        
                                                            <tr>
                                                                <td scope="col" width="25%"> Keyword<input name="user" id="user" type="text" maxlength="100" value="<?php echo isset($objU->arrSearch['user']) ? stripslashes($objU->arrSearch['user']) : "" ?>" class="searchwidth1"> </td>
                                                                <td scope="col" width="18%">Type<select name="ddlSearch" class="textbox">
                                                                        <option value="username" <?php
                                                                                if ($_POST['ddlSearch'] == 'username') {
                                                                                    echo 'selected';
                                                                                }
                                                                                ?>>Username</option>
                                                                        <option value="name" <?php
                                                                                if ($_POST['ddlSearch'] == 'name') {
                                                                                    echo 'selected';
                                                                                }
                                                                                ?>>Name</option> 
                                                                        <option value="email" <?php
                                                                        if ($_POST['ddlSearch'] == 'email') {
                                                                            echo 'selected';
                                                                        }
                                                                        ?>>Email</option> 
                                                                        <option value="age" <?php
                                                                        if ($_POST['ddlSearch'] == 'age') {
                                                                            echo 'selected';
                                                                        }
                                                                        ?>>Age</option> 
                                                                    </select></td>
                                                                <td scope="col" width="24%">Gender<select name="gend" class="searchwidth1">
                                                            <?php foreach ($arrGenderSearch as $keys => $values) { ?>
                                                                            <option value="<?php echo $keys ?>" <?php echo isset($objU->arrSearch['gend']) && ($objU->arrSearch['gend'] == $keys) ? "selected" : "" ?>>
                                                                                <?php echo $values ?>
                                                                            </option>
                                                                                <?php } ?>
                                                                    </select>	 </td>
                                                                <td scope="col">
                                                                    <!--input name="imageField" type="image" class="loginbutton" width="63" height="20" border="0" value="Search" style="float:left "-->
                                                                    <input  type="submit" class="loginbutton" width="63" height="20" border="0" value="Search" style="float:left ">&nbsp;
                                                                    <input  type="submit" name="btnExportExcel" class="loginbutton" value="Export to excel">
                                                                    <input type="hidden" name="SearchNow" value="SearchNow"> </td>
                                                                <td scope="col" align="right"><a href="manage_user.php" class="add-btn">Add New User</a></td>
                                                           
                                                            </tr>
                                                            <script type="text/javascript">
                                                                $(document).ready(function() {
                                                                    $("#user").autocomplete("takeusernames.php", { minChars:1, matchSubset:1, matchContains:1, selectOnly:1 });
                                                                });
                                                            </script>
                                                            
                                                        </form>
                                                    </table></td>
                                            </tr>
                                        </table>
                                        <form name="form1" id="form_id" method="post" action="" onSubmit="return confirm_check();">
                                            <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                                                <tr><?php if (isset($message) && $message != '') {
                                                    ?>
                                                        <td colspan="8" class=" warning" class="maintext" align="center"><?php echo $message; ?></td>
                                                    <?php } ?></tr>
                                                <tr>
                                                    <td>
                                                        <table width="100%"  class="listing_tbl_admin" border="0" cellspacing="1" cellpadding="0">
                                            
                                                                    <?php
                                                                    //if (is_resource($objDB)) {
                                                                    //if (is_object($objDB)) {
                                                                    if ($objDB) {
                                                                        if (!empty($data)) {
                                                                            ?>
                                                                    <thead>
                                                                        <tr align="left">
                                                                            <th align="center" class="not-sortable">
                                                                                <!--<a href="#" onClick="select_all('ids', '1');">Check All</a> / <a href="#" onClick="select_all('ids', '0');">Uncheck All</a>-->
                                                                                &nbsp;<input type="checkbox" id="checkAll"  name="checkAll" title="Check All / Uncheck All"/>
                                                                    
                                                                            </th>
                                                                            <th align="center" class="maintext2"><strong class="boldtext latest">#</strong></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Username<a href="users.php?title=vLogin&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Name<a href="users.php?title=vFirstName&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Gender<a href="users.php?title=vGender&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></strong></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Created Date<a href="users.php?title=dStartDate&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></strong></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Email<a href="users.php?title=vEmail&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></strong></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Status<a href="users.php?title=vStatus&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC' ?>"><?php echo $image ?></a></strong></th>
                                                                            <th class="maintext2"><strong class="boldtext latest">Action</strong></th>
                                                                
                                                                        </tr>
                                                                    </thead>
                                                        
                                                        
                                                                    <tbody>	<?php
                                                                $i = 0;
                                                                //foreach ($data as $key => $value) {
                                                                foreach ($data as $value) {

                                                                    $i++;?>
                                                                            <tr align="left" >
                                                                                <td width="5%"  align="center" class="maintext2"><input name="ids[]" type="checkbox" class="case" value="<?php echo $value['nUser_Id'] ?>"></td>
                                                                                <td width="5%"   align="center" class="maintext2"><?php echo ($page - 1) * $pageMaxCount + $i; ?></td>
                                                                                <td width="15%" class="maintext2"><a href="viewprofile.php?id=<?php echo $value['nUser_Id'] ?>&page=<?php echo $page ?>"><?php echo htmlspecialchars(stripslashes($value['vLogin'])) ?></a></td>
                                                                                <td width="11%" class="maintext2"><?php echo htmlspecialchars(stripslashes($value['vFirstName'])) ?> <?php echo htmlspecialchars(stripslashes($value['vMiddleName'])) ?> <?php echo htmlspecialchars(stripslashes($value['vLastName'])) ?></td>
                                                                                <td width="10%" class="maintext2"><?php echo $value['vGender'] ?></td>
                                                                                <td width="14%" class="maintext2"><?php echo date("m/d/Y", strtotime($value['dStartDate'])) ?></td>
                                                                                <td width="4%" class="maintext2"><?php echo htmlspecialchars(stripslashes($value['vEmail'])) ?></td>
                                                                                <td width="10%" class="maintext2"><?php if ($value['vStatus'] == "ANNN") echo "Active"; else if ($value['vStatus'] == "SNNN") echo "Suspended"; else if ($value['vStatus'] == "ENNN") echo "Expired";else echo "Inactive"; ?></td>
                                                                                <td width="10%" align="center" class="maintext2"><a href="manage_user.php?id=<?php echo $value['nUser_Id'] ?>&page=<?php echo $page ?>" class="signupbuttonlink">Edit</a></td>
                                                                    
                                                                            </tr>
                                                                        <?php }//end foreach
                                                                        ?>
                                                                        <tr>
                                                                            <td colspan="9">
                                                                                <div class="pagination">
                                                                                    <ul>
                                                                            <?php if ($page != 1) { ?>
                                                                                            <li><a href="users.php?page=1" class="page_first" >First</a></li>
                                                                                            <li><a class="page_prev" href="users.php?page=<?php if ($page != 1) echo $page - 1; else echo $page; ?>" >Previous</a></li>
                                                                                            <?php }
                                                                                        ?>
                                                                                        <li class="pageinfo"><div id="page-info">Page <strong><?php echo $page; ?></strong> / <?php echo $pageCount; ?></div></li>
                                                                                        <?php if ($pageCount != $page) { ?>
                                                                                            <li><a class="page_next" href="users.php?page=<?php if ($pageCount != $page) echo $page + 1; else echo $pageCount; ?>">Next</a></li>
                                                                                            <li><a class="page_last" href="users.php?page=<?php echo $pageCount; ?>" >Last</a></li>
                                                                                            <?php }
                                                                                        ?>
                                                                                    </ul>
                                                                                    <div class="clear"></div>
                                                                                </div> 
                                                                            </td>
                                                                        <tr>
                                                                        <?php
                                                                        }//end if
                                                                    }//end first if
                                                                    ?>
    
                                                            </tbody>
                                                
                                                    <?php if (!empty($data)) { ?>


                                                    <?php } ?>
                                                        </table>
                                                        <table width="100%" cellspacing="1" cellpadding="1" border="0" style="margin-top:10px;">
                                                                <tr align="left">
                              
                                                                <td align="left" >
                                                                    <!--
                                                                    <input type="button" name="DelNow" value="Delete" class="loginbutton" style="cursor:pointer" title="Delete Selected" onClick="deleteProducts(this.form);">
                                                                    -->
                                                                    <input type="submit" name="DelNow" value="Delete" class="loginbutton" style="cursor:pointer" title="Delete Selected" >
                                                            
                                                                </td>
                                                                <td width="18%"><?php
                                                                if (!empty($data)) {
                                                                    ?><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                                                            <tr>
                                                                                <td width="82%">&nbsp;</td>
                                                                                <td width="18%"><table width="100%"  border="0" cellspacing="2" cellpadding="0" class="pagination_style">
                                                                                        <tr align="center" class="textbox">
                                                                                            <td><?php echo $objP->links; ?></td>
                                                                                        </tr>
                                                                                    </table></td>
                                                                            </tr>
                                                                        </table><?php }//end if ?></td>
                                                            </tr>
                    
                                                        </table>
                              
                              
                                                        <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                                            <tr>
                                                                <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                                                        <tr>
                                                                            <td>&nbsp;</td>
                                                                        </tr>
                                                                    </table></td>
                                                            </tr>
                                                        </table></td>
                                                </tr>
                                            </table></form>
                                    </div>
                                </td>
                            </tr>
                        </table>
                     </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<?php include_once('bottomhtml.php');?>

<script type="text/javascript">
    function deleteProducts(formReference)
    {

        alert(formReference);
        if($(".case:checked").length == '0'){
            alert('Please select the user(s) to delete');
            return false;
        }
        else
        {
            var status =  confirm('Are you sure you want to delete the selected user(s) !!');
            if(status == true)
            {
                alert("testing");
                formReference.submit();
            }
        }

        /*
        if(document.frmMyProducts2.txtProductId.value == "")
        {
        alert('Please select the products to delete');
        }
        else
        {

        var status =  confirm('Are you sure you want to delete the selected products !!');

        if(status == true)
        {
        document.frmMyProducts2.submit();
        }
        }
        */

    }
</script>