<?php if(isset($_SESSION['successMsg'])){?>
	<div class="alert alert-success" role="alert"><?php echo $_SESSION['successMsg']; unset($_SESSION['successMsg']);?></div>
<?php }?>

<?php if(isset($_SESSION['errorMsg'])){?>
    <div class="alert alert-danger" role="alert"><?php echo $_SESSION['errorMsg']; unset($_SESSION['errorMsg']);?></div>
<?php }?>
<style>
.btn-sm, .btn-group-sm > .btn {
    padding: 0px 5px;
    font-size: 0.875rem;
    line-height: 1.5;
    border-radius: 0.1875rem;
}
.table td, .table th {
    padding:1px 1px;
	vertical-align: middle;
    font-size: 11px;
    line-height: 1;
    white-space: nowrap;
}
.btn i {
    margin-right: 0px;
}
.content-wrapper {
    background: #f2f8f9;
    padding: 0px;
    width: 100%;
    -webkit-flex-grow: 1;
    flex-grow: 1;
}
.page-body-wrapper:not(.auth-page) {
    padding-top: 50px;
}
div.dataTables_wrapper div.dataTables_filter {
    text-align: right;
    margin-top: -45px;
}
</style>
<div class="col-lg-12 grid-margin stretch-card">	                
    <div class="card">    
        <div class="card-body">
        	
            <div class="text-center ActionBox" style="position:fixed;top:110px;width:calc(100% - 21%);z-index: 999;display:none;">            	
                <button type="button" class="btn btn-success btn-fw verifyQuestion"><i class="mdi mdi-check"></i> Verify</button>
                <button type="button" class="btn btn-danger btn-fw deleteQuestion"><i class="mdi mdi-alert-outline"></i>Delete</button>            
            </div>
                    		    		    	        
            <h4 class="card-title">View Retailer</h4>                   
            <div class="table-responsive">
            	<form method="post" enctype="multipart/form-data">                    
                    <div class="row">                                                                                        
                        <div class="col-md-2">
                            <div class="form-group">
                                <label for="exampleInputCity1">State <span style="color:#F00;">*</span></label>
                                <select name="state" class="form-control" id="state" onchange="getDistrict(this.value);" required> 
                                    <option value="">Select State</option>
                                    <?php
                                    $sqlState = mysqli_query($cn, "select `state` from `tbl_retailer` group by `state` order by `state`");
                                    while($sqlStateData = mysqli_fetch_array($sqlState)){?>
                                    <option value="<?php echo $sqlStateData['state'];?>" <?php if(isset($_GET['edit_id'])){if($sqlStateData['state']==$sqlData['state']){ echo "selected";}}?>><?php echo $sqlStateData['state'];?></option>
                                    <?php }?>
                                </select>                           
                            </div>
                        </div>
                    
                        <div class="col-md-2">
                            <div class="form-group">
                                <label for="exampleInputCity1">District <span style="color:#F00;">*</span></label>
                                <select name="district" class="form-control" id="district" onchange="getBeat(this.value);"> 
                                    <option value="">Select District</option>                                
                                </select>                           
                            </div>
                        </div>
                                                               
                        <div class="col-md-2">
                            <div class="form-group">
                                <label for="exampleInputCity1">Beat <span style="color:#F00;">*</span></label>
                                <select name="beat" class="form-control" id="beat"> 
                                    <option value="">Select Beat</option>                                
                                </select>
                            </div>
                        </div>
                        
                        <div class="col-md-1">
                            <div class="form-group">        
                                <label for="exampleInputName1">&nbsp;</label>                
                                <button type="submit" class="btn btn-success mr-2 form-control" name="search">Search</button>                                    	                    	
                            </div>                
                        </div>                                                                                                                           
                    </div>            	            				                    
                </form>
                <table class="table datatable-sorting" id="myTable">
                    <thead>
                        <tr>                            
                            <th>S.No.</th>
                            <th>Shop Name</th>
                            <th>Mobile</th>
                            <th>State</th>
                            <th>District</th>
                            <th>Sales District</th>
                            <th>Beat</th>
                            <th>SH Code</th>                     
                            <th style="text-align:center;">Action</th>                                           
                        </tr>
                    </thead>
                    <tbody>
                    <?php
					if(isset($_POST['search']))
					{
						$searchQuery = "";
						if(!empty($_POST['state']))
						{
							$searchQuery .= " and `state`='".$_POST['state']."'";
						}
						if(!empty($_POST['district']))
						{
							$searchQuery .= " and `district`='".$_POST['district']."'";
						}
						if(!empty($_POST['beat']))
						{
							$searchQuery .= " and `beat`='".$_POST['beat']."'";
						}
						$list_qry       = "select * from `tbl_retailer` where `status`=1 $searchQuery";
					}
					else
					{
                    	$list_qry       = "select * from `tbl_retailer` where `status`=1";
					}
                    if (isset($_GET['page_no']) && $_GET['page_no']!="") {
                        $page_no = $_GET['page_no'];
                    } else {
                        $page_no = 1;
                    }					
                    $total_records_per_page = 100;
                    $offset = ($page_no-1) * $total_records_per_page;
                    $previous_page = $page_no - 1;
                    $next_page = $page_no + 1;
                    $adjacents = "2"; 
                    
                    $result_count = mysqli_query($cn,$list_qry);
                    $total_records = mysqli_num_rows($result_count);
                    $total_no_of_pages = ceil($total_records / $total_records_per_page);
                    $second_last = $total_no_of_pages - 1; // total page minus 1
                    $list_qry  .= " LIMIT $offset, $total_records_per_page ";		
                    $sqlQuestion = mysqli_query($cn,$list_qry);			
                    
					$i=1;
					while($sqlQuestionData = mysqli_fetch_array($sqlQuestion))
                    {								
                        ?>
                        <tr>                            
                            <td><?php echo $i++;;?></td>
                            <td><?php echo $sqlQuestionData['shop_name'];?></td>
                            <td><?php echo $sqlQuestionData['mobile'];?></td>
                            <td><?php echo $sqlQuestionData['state'];?></td>
                            <td><?php echo $sqlQuestionData['district'];?></td>
                            <td><?php echo $sqlQuestionData['sale_district'];?></td>
                            <td><?php echo $sqlQuestionData['beat'];?></td>
                            <td><?php echo $sqlQuestionData['ds_code'];?></td>                       
                            <td style="text-align:center;"><a href="home.php?pages=addRetailer&edit_id=<?php echo $sqlQuestionData['id'];?>" class="btn btn-outline-success btn-sm"><i class="mdi mdi-pencil"></i></a></td> 
                        </tr>
                    <?php }?>    
                    </tbody>
                </table>

                <?php if($total_no_of_pages>0){?>
                <div class="botoom-number">
                    <ul class="pagination realestate-pagination">                            
                        <li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
                        <a <?php if($page_no > 1){ echo "href='home.php?pages=viewRetailer&page_no=$previous_page'"; } ?>>Previous</a>
                        </li>
                           
                        <?php 
                        if($total_no_of_pages <= 10){  	 
                            for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
                                if ($counter == $page_no) {
                               echo "<li><a class='active'>$counter</a></li>";	
                                    }else{
                               echo "<li><a href='home.php?pages=viewRetailer&page_no=$counter'>$counter</a></li>";
                                    }
                            }
                        }
                        elseif($total_no_of_pages > 10){
                            
                        if($page_no <= 4) {			
                         for ($counter = 1; $counter < 8; $counter++){		 
                                if ($counter == $page_no) {
                               echo "<li><a class='active'>$counter</a></li>";	
                                    }else{
                               echo "<li><a href='home.php?pages=viewRetailer&page_no=$counter'>$counter</a></li>";
                                    }
                            }
                            echo "<li><a>...</a></li>";
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=$second_last'>$second_last</a></li>";
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
                            }
                    
                         elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {		 
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=1'>1</a></li>";
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=2'>2</a></li>";
                            echo "<li><a>...</a></li>";
                            for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {			
                               if ($counter == $page_no) {
                               echo "<li><a class='active'>$counter</a></li>";	
                                    }else{
                               echo "<li><a href='home.php?pages=viewRetailer&page_no=$counter'>$counter</a></li>";
                                    }                  
                           }
                           echo "<li><a>...</a></li>";
                           echo "<li><a href='home.php?pages=viewRetailer&page_no=$second_last'>$second_last</a></li>";
                           echo "<li><a href='home.php?pages=viewRetailer&page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";      
                                }
                            
                            else {
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=1'>1</a></li>";
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=2'>2</a></li>";
                            echo "<li><a>...</a></li>";
                    
                            for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
                              if ($counter == $page_no) {
                               echo "<li><a class='active'>$counter</a></li>";	
                                    }else{
                               echo "<li><a href='home.php?pages=viewRetailer&page_no=$counter'>$counter</a></li>";
                                    }                   
                                    }
                                }
                        }
                    ?>
                        
                        <li <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
                        <a <?php if($page_no < $total_no_of_pages) { echo "href='home.php?pages=viewRetailer&page_no=$next_page'"; } ?>>Next</a>
                        </li>
                        <?php if($page_no < $total_no_of_pages){
                            echo "<li><a href='home.php?pages=viewRetailer&page_no=$total_no_of_pages'>Last &rsaquo;&rsaquo;</a></li>";
                            } ?>
                    </ul>
                </div>
                <br />
            	<?php }?>
            </div>
        </div>
    </div>
</div>    
