<?php
$pageTitle = 'One Link';
$buttonAction ='';$buttonName = 'Reset';

if($this->input->get()){
	$buttonName = 'Back To List';
	$backUrl = base_url().'admin/'.$categoryFor;
    $buttonAction = "window.location.href='".$backUrl."'";
}

$act_url = 'admin/'.$categoryFor;

$PER_PAGE = $GLOBALS['PER_PAGE'];
$page = 20;

if($this->input->cookie('perPage',true)){
  $page = $this->input->cookie('perPage',true);
}
// var_dump($page);
// ;exit;

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title><?php echo $pageTitle ?> List - ACP -<?php echo site_name(); ?></title>

<?php $this->load->view('admin/ap_top'); ?>

<script type="text/javascript">

function doActionFn(action,id){
     
	if(action == 'edit' || action == 'view' || action == 'duplicate'){
        window.location= '<?php echo base_url('admin/'.$categoryFor.'/manager/'); ?>'+action+'/'+id;
	}
	else if(action == 'view_analytics'){
	    window.location= '<?php echo base_url('admin/'.$categoryFor.'/statistics'); ?>?link_id='+id;
	}
	else{
		 $('#doAction').val(action);	
 		 if(id != 'all' && $('#checked_'+id).length){
			 $('.checkedAll').prop('checked',false); 	
			 $('#checked_'+id).prop('checked',true); 
		  }
		 var checkBox = false;
		$.each($("input[name='ids[]']:checked"), function() {
			checkBox =  true;
		});
 		 if(checkBox == false){
			$('#showActionErr').html(' <div class="alert alert-danger alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> Please select at least one item </div>');	 
 			return false;
		 }
		 if(action == 'delete'){
			if (confirm("Are you sure you want to delete?")) {
				$('#listFrm').submit();
			}	
		} else{
			$('#listFrm').submit();
		}
 	}
  }

 function one_link_open(event){
    c_url = $(event).data('open_link_url');
    openInNewTabWithNoopener(c_url);
}
</script>

<style>
    .btn_cc{
        padding: 0.35rem 0.7rem !important;
    }
    .table td, .table th {
       padding: 0 18px!important;
    }
</style>
</head>

<body>
    
    <div class="wrapper" >
        <?php $this->load->view('admin/navbar'); ?>
            
		<div class="main-panel main-panel-custom">
		    <div class="content">
				<div class="page-inner">
					<div class="page-header">
						<h4 class="page-title"><?php echo $categoryForPage;?></h4>
						<ul class="breadcrumbs">
							<li class="nav-home">
								<a href="<?php echo base_url('admin/dashboard');?>">
									<i class="flaticon-home"></i>
								</a>
							</li>
							<li class="separator">
								<i class="flaticon-right-arrow"></i>
							</li>
							<li class="nav-item">
								<a href="<?php echo base_url('admin/'.$categoryFor);?>"><?php echo $pageTitle; ?> List</a>
							</li>
							
						</ul>
					</div>
                    <div class="row">
						<div class="col-md-12">
							<div class="card">
								
								<div class="card-header">
								    <div class="card-head-row">
									    <h4 class="card-title"><?php echo $pageTitle; ?> List</h4>
									    
									    <div class="card-tools">
											<ul class="nav nav-pills nav-secondary nav-pills-no-bd " role="tablist">
											    <li class="nav-item">
										            <a class="nav-link active" style="background: #28a745;!important"  href="<?php echo base_url('admin/'.$categoryFor.'/manager') ?>"> <i class="fa fa-plus"></i>&nbsp; Add New Link</a>
									            </li>
									            <!--<li class="nav-item">-->
										           <!-- <a class="nav-link active" style="background: #007bff;!important"  href="app_export.php"> <i class="fas fa-file-export"></i>&nbsp; Export</a>-->
									            <!--</li>-->
									            <!--<li class="nav-item">-->
									                <!--<a class="nav-link active btn-refresh-card" style="background: #007bff;!important">Reload</a>-->
										           <!-- <button id="table-refresh" class="nav-link active btn-refresh-card btn-refresh-card" ><span class="fa fa-sync-alt"></span></button>-->
									            <!--</li>-->
											</ul>
										</div>
									</div>
								</div>
								
								<!--notifiactions-->
    							<div class="container" id="">
                                    <?php 
                             		$errMsg = validation_errors('* ');
                            		if(empty($errMsg)){ $errMsg = $this->session->flashdata('errMsg');  }
                            		$warningMsg = $this->session->flashdata('warningMsg');
                            		$successMsg = $this->session->flashdata('successMsg');
                            		if(isset($errMsg) && !empty($errMsg)){ echo showMessage($errMsg,'error'); }  
                            		if(isset($warningMsg) && !empty($warningMsg)){ echo showMessage($warningMsg,'warning'); }
                            		if(isset($successMsg) && !empty($successMsg)){ echo showMessage($successMsg,'success'); }
                            	?>
                                </div>
                                
                                <div class="container" id="showActionErr"></div>
                                <!--notifiactions-->
								
								<div class="card-body">
								    
								     <div class="row">
                                            <div class="col-md-12 " style="padding: 0rem 1.25rem;">
                                                <!--<div class="text-left">-->
                                                  <!--<h3>Search</h3>-->
                                                <!--</div>-->
                                                <?php 
                                                echo form_open( $act_url ,array('name' => 'searchFrm', 'id' => 'searchFrm','method' => 'get')); ?>
                                                <div class="row">
                                                  <div class="form-group col-md-3"  >
                                                    <label for="searchKey">Search Key</label>
                                                    <?php
                                                    
                                                    $data = array( 'name' => 'searchKey','value' => set_value('searchKey',$this->input->get('searchKey',true)),  'id' => 'searchKey',  'placeholder'   => '', 'class' => 'form-control required', );
                                     						 echo form_input($data);
                                    					?>
                                                  </div>
                                                  <div class="form-group col-md-3"   >
                                                    <label for="searchBy">Search By</label>
                                                    <?php  
                                    				$arrSearchBy = array(''=>'Select', 'link_name'=> 'Link Name ',  'categoryTitle'=> 'Category', 
                                    				'app_keyword'=> 'Keyword',	'share_link'=>'Share Link' );
                                    				$js = 'class="form-control" id="searchBy"  '; 
                                    				echo form_dropdown('searchBy', $arrSearchBy,set_value('searchBy',$this->input->get('searchBy',true)?$this->input->get('searchBy',true):'link_name'), $js); 
                                    		        ?>
                                                  </div>
                                                  
                                                  <div class="form-group col-md-3" >
                                                    <label for="daterange_By">Date Range By</label>
                                                    <div class="input-icon ">
                                                    <?php
                                                        if($this->input->get('daterange_By',true)){
                                                            $set_Date_value=$this->input->get('daterange_By',true);
                                                        }else $set_Date_value='';
                                                        
                                                        $data = array( 'name' => 'daterange_By','autoComplete' => 'off',
                                                        'value' => set_value('daterange_By',$set_Date_value), 
                                                        'id' => 'daterange_By',  'class' => 'form-control', );
                                                        echo form_input($data);
                                                    ?>                                                        
                                                    </div>
                                                  </div>

                                                  <!--<div class="form-group col-md-2"   >-->
                                                    <!--<label for="searchStatus">Status</label>-->
                                                    <?php  
                                        		  //      $arrSearchBy = array('all'=>'All','Disabled'=>'Disabled','Enabled'=>'Enabled');
                                        		  //      $js = 'class="form-control" id="searchStatus"  '; 
                                          		// 		 echo form_dropdown('searchStatus', $arrSearchBy,set_value('searchStatus',$this->input->get('searchStatus',true)), $js);
                                    					?>
                                                  <!--</div>-->
                                                </div>
                                                <div class="row">
                                                  <div class="form-group col-md-12">
                                                    <?php   $data = array( 'name' => 'myAccountBtn','value' => 'true', 'id' => 'myAccountBtn',  'type' => 'submit','content' => 'Search', 'class' => 'btn btn-success btn-sm',   );
                                     				echo form_button($data);
                                    				$data = array( 'name' => 'resetAgrmtBtn','value' => 'true', 'id' => 'resetAgrmtBtn',  'type' => 'reset','content' => $buttonName, 'class' => 'btn btn-warning btn-sm','onClick' => $buttonAction   );
                                    				echo '&nbsp;';
                                     				echo form_button($data) ;
                                     				?>
                                                  </div>
                                                </div>
                                                <?php echo form_close();?>
                                            </div>
                                      </div>
								   
                        		    <div class="btn-wrapper text-center d-flex justify-content-between">
                        		        <div class="d-flex align-items-center">
                        		            <h4 class="card-title"> <?php echo $pageTitle; ?> List</h4>
                        		        </div>
                        		        <div class="d-flex align-items-center">
                        		             <form action="<?php echo $_SERVER['REQUEST_URI']?>" name="perPageForm" method="post">
                        		                 <input type="hidden" name="listUrl" value="<?php echo $act_url ?>">
                                              <?php  
                                				
                                				$js = 'class="form-control" id="perPage" style="width:auto;" onchange="this.form.submit()"  '; 
                                				echo form_dropdown('perPage', $PER_PAGE, set_value('perPage',$page), $js); 
                                			?>
                                            </form>
                        		        </div>
                        		    </div>
                  
                        		        <?php
                        					$listUrl = base_url($act_url.'?'); 
                        				  if($this->input->get()){
                        					 $arrGet = $this->input->get(); unset($arrGet['per_page']);
                        					 $listUrl   = base_url('admin/'.$categoryFor).'?'.  http_build_query($arrGet);
                          					}
                         					$GLOBALS['ARR_ACP_PAGIN_CONF']['base_url'] = $listUrl;
                        					$GLOBALS['ARR_ACP_PAGIN_CONF']['per_page'] = $this->admin_onelink_model->limit;	
                        					$total_rows = $GLOBALS['ARR_ACP_PAGIN_CONF']['total_rows'] = $this->admin_onelink_model->get_OneLink_List('count');	
                         					$this->pagination->initialize($GLOBALS['ARR_ACP_PAGIN_CONF']);
                                           $arrList = $this->admin_onelink_model->get_OneLink_List('list');
                                           
                        					$listUrl1 = "";
                        					$sortBy = 'registerDate';$sortOrder = 'desc';
                        					 if($this->input->get('sortBy',true) && $this->input->get('sortOrder',true)){
                        						 $sortBy = $this->input->get('sortBy',true);$sortOrder = $this->input->get('sortOrder',true);
                        					 }
                        					if(isset($arrGet)){
                        						unset($arrGet['sortBy']); unset($arrGet['sortOrder']);
                        						 $listUrl  = base_url($act_url).'?'. http_build_query($arrGet);
                        						 $listUrl1  = $act_url.'?'. http_build_query($arrGet);
                        					}
  							            ?>
                        		    <div class="btn-wrapper text-center d-flex justify-content-between">
                        		        <div class="d-flex align-items-center">
                        		            <h4 class="card-title"> Total <?php echo $pageTitle;?> : <?php echo $total_rows;?></h4>
                        		        </div>
                        		        <!--<div class="d-flex align-items-center">-->
                        		        <!--     <div class="form-group col-md-4">-->
                                  <!--                <div class="">-->
                                                    <?php  //echo $this->pagination->create_links();  ?>
                                  <!--                </div>-->
                                  <!--          </div>-->
                        		        <!--</div>-->
                        		    </div>
                        		    
                        		    <?php     
                        		        echo form_open($act_url ,array('name' => 'listFrm', 'id' => 'listFrm'));
                            		  	echo form_input(array('name' => 'doAction', 'type'=>'hidden', 'id' =>'doAction'));
                        		    ?>
							        <div class="table-responsive">
										<table id="feedback_list_table" class="display table table-striped table-hover table-bordered " >
										    
											<thead>
 					                            <input type="hidden" name="listUrl" value="<?php echo $listUrl1 ?>">
                                                <tr>
                                                    <th ><?php $data = array( 'name' => 'checkedAll', 'id' => 'checkedAll',  ); echo form_checkbox($data);?>
                                                    </th>
                                                    <th>#</th>
                                                    <!--<th onClick="doSort('id','<?php //echo $listUrl;?>');">ID <i class="fas  if($sortBy != 'id')echo 'fa-sort';else if($sortOrder == 'desc')echo 'fa-sort-up'; else echo 'fa-sort-down'; ?> " id="sort_id" ></i></th>-->
                                                    <th onClick="doSort('link_name','<?php echo $listUrl;?>');">Name <i class="fas <?php if($sortBy != 'id')echo 'fa-sort';else if($sortOrder == 'desc')echo 'fa-sort-up'; else echo 'fa-sort-down'; ?> " id="sort_link_name" ></i></th>
                                                    <th>Category</th>
                                                    <th onClick="doSort('share_link','<?php echo $listUrl;?>');">One Link<i class="fas <?php if($sortBy != 'share_link')echo 'fa-sort';else if($sortOrder == 'desc')echo 'fa-sort-up'; else echo 'fa-sort-down'; ?> " id="sort_share_link" ></i></th>
                                                    <th width="6%">Action</th>
                                                    <th>Total Clicks</th>
                                                    <th>Target Type</th>
                                                    <th>Destination Link</th>
                                                    <!--<th>Status</th>-->
                                                </tr>
											</thead>
										
											<tbody>
									            <?php
                         					    if($arrList){
                        						 
                        						    $sno = (($this->admin_onelink_model->page) * $this->admin_onelink_model->limit)+1;
                    						        foreach($arrList as $objRow){
                        							?>
                                                        <tr>
                                                            <td>
                                                                <?php 
                                                                $data = array( 'name' => 'ids[]','id' => 'checked_'.$objRow->id, 'value' => set_value('ids[]',$objRow->id),  'class' => 'checkedAll', ); 
                                                                echo form_checkbox($data);
                                                                ?>
                                                            </td>
                                           
                                                            <td><?php echo $sno;?></td> 
                                                            <!--<td><?php //echo $objRow->id;?></td>-->
                                                            <td><?php echo (isset($objRow->link_name) && !empty($objRow->link_name) ? $objRow->link_name:"-" );?></td>
                                                            
                                                            <td><?php echo isset($objRow->categoryTitle)&&!empty($objRow->categoryTitle)?$objRow->categoryTitle:'-';?></td>
                                                            
                                                            <td style="white-space: normal;">
                                                                <?php
                                                                $url_Set =  isset($objRow->share_link)&&!empty($objRow->share_link)?$objRow->share_link:'-';
                                                                // echo $url_Set."&nbsp;&nbsp;&nbsp;&nbsp;";
                                                                echo $url_Set;
                                                                ?>
                                    				            
                                                            </td>
                                                           
                                                            <td>
                                                                <?php
                                									if($objRow->is_active == 1){
                                										$enabled = 'disabled';
                                									} else $enabled = ''; 
                                									
                                									if($objRow->is_active == 0){
                                										$disabled = 'disabled';
                                									} else $disabled = '';
                            									?>
                        									    <div class="form-button-action">
                        									        
                                                                    <button type="button" class="btn btn_cc btn-link btn-secondary" 
                                    				                    data-toggle="tooltip" title="Copy" onClick="copyToClipboard_URL(this)" data-copy_url="<?php echo $url_Set; ?>" >
                                    				                  <i class="fa fa-copy"></i>
                                    				                </button>
                                    				                <button type="button" class="btn btn_cc btn-link btn-danger " 
                                    				                    data-toggle="tooltip" title="Open" onClick="one_link_open(this)" data-open_link_url="<?php echo $url_Set; ?>" >
                                    				                  <i class="fa fa-external-link" aria-hidden="true"></i>
                                    				                </button>
                                    				              <button type="button" onClick="doActionFn('edit','<?php echo $objRow->id;?>');" data-toggle="tooltip" title="Edit" 
                                            					class="btn btn_cc btn-link btn-primary" data-original-title="Edit">
                                            						<i class="fas fa-pencil-alt"></i>
                                            					</button>
                                            					<button type="button" onClick="doActionFn('view','<?php echo $objRow->id;?>');" data-toggle="tooltip" title="View" 
                                            					class="btn btn_cc btn-link btn-info " data-original-title="View">
                                            						<i class="fa fa-info"></i>
                                            					</button>
                                            					<button type="button" onClick="doActionFn('duplicate','<?php echo $objRow->id;?>');" data-toggle="tooltip" title="Duplicate" 
                                            					class="btn btn_cc btn-link btn-success " data-original-title="Duplicate">
                                            						<i class="fas fa-plus-circle"></i>
                                            					</button>
                                            					<!--<button type="button" onClick="doActionFn('delete','<?php //echo $objRow->id;?>');" data-toggle="tooltip" title="Delete" -->
                                            					<!--class="btn btn-link btn-danger delbtn" data-original-title="Delete">-->
                                            						<!--<i class="fas fa-trash"></i>-->
                                            					<!--</button>-->
                                            				    </div>
    									                    </td>
    									                     <td>
                                                                <?php 
                                                                // echo isset($objRow->hits)&&!empty($objRow->hits)?$objRow->hits:'0';
                                                                if(isset($objRow->hits)&&!empty($objRow->hits)){
                                                                    echo $objRow->hits;
                                                                    ?>
                                                                    <button type="button" onclick="doActionFn('view_analytics','<?php echo $objRow->id;?>');" data-toggle="tooltip" title="" class="btn btn-link btn-warning " style="padding: 0.65rem 1rem;" data-original-title="View Analytics">
                                                						<i class="fas fa-chart-bar fa-lg"></i>
                                                					</button>
                                                                    <?php
                                                                }
                                                                else{
                                                                    echo "0";
                                                                }
                                                                ?>
                                                            </td>
                                                            <td><?php 
                                                                echo isset($objRow->target_type)&&!empty($objRow->target_type)? '<button type="button" class="btn btn-primary btn-sm btn-round">'.$objRow->target_type.'</button>':'-';
                                                                echo isset($objRow->sub_link_flag)&&!empty($objRow->sub_link_flag)? '&nbsp;&nbsp;<button type="button" class="btn btn-secondary btn-sm btn-round">Sub-links</button>':'';
                                                                
                                                            ?>
                                                            </td>
                                                            
                                                            <td><?php echo isset($objRow->destination_default_link)&&!empty($objRow->destination_default_link)?$objRow->destination_default_link:'-';?></td>
                                                            
                								            <!--<td>-->
                                                                <?php 
                                                                // far fa-check-circle
                                        //                         if($objRow->is_active == 0)
                                        //                         {
                                        //                             echo '<button type="button" data-toggle="tooltip" data-original-title="Enable" title="Enable" 
                                        //                                     class="btn btn-xs btn-icon btn-round btn-success">
                                								// 			<i class="fa fa-check"></i>
                                								// 		    </button>';
                                        //                         }
                                        //                         else
                                        //                         {
                                        //                             echo '<button type="button" data-toggle="tooltip" data-original-title="Disable" title="Disable" 
                                        //                                     class="btn btn-xs btn-icon btn-round btn-warning">
                                								// 			<i class="fas fa-ban"></i>
                                								// 		</button>';
                                        //                         }
                                                                ?>
                                                            <!--</td>-->
                                                    </tr>
                                                    <?php	
                        							$sno++;
                        						    }	 
					                            }else{
						                        ?>
                                                <tr>
                                                    <td colspan="11" align="center">No records</td>
                                                </tr>
                                                <?php	 
                            					}
			                                    ?>
											</tbody>
										</table>
									</div>
									<?php echo form_close();?>
								</div>
								
								<div class="card-footer ">
								     <div class="btn-wrapper text-center d-flex justify-content-between">
                        		        <div class="d-flex align-items-center">
                        		            <h4 class="card-title"></h4>
                        		        </div>
                        		        <div class="d-flex align-items-center">
                        		             <div class="form-group col-md-4">
                                                  <div class="">
                                                    <?php  echo $this->pagination->create_links();  ?>
                                                  </div>
                                            </div>
                        		        </div>
                        		    </div>
								    <div class="btn-wrapper text-center d-flex justify-content-between">
								        <div class="d-flex align-items-center">
											<label for="inlineinput" class="col-form-label "> With Selected &nbsp;&nbsp;&nbsp;&nbsp; </label>
											<div class="p-0">
											    <button type="button" class="btn btn-success btn-sm btn-enable"   title="Enable" onClick="doActionFn('enable','all');"> <i class="far fa-check-circle"></i> Enable </button>
											    <button type="button" class="btn btn-warning btn-sm btn-disable"  title="Disable" onClick="doActionFn('disable','all');"> <i class="fas fa-ban"></i> Disable</button>
                                                <button type="button" class="btn btn-danger btn-sm  btn-delete " title="Delete" onClick="doActionFn('delete','all');"> <i class="fas fa-trash"></i> Delete</button>
											</div>
										</div>
										<div class="d-flex align-items-center">
											<form action="<?php echo $_SERVER['REQUEST_URI']?>" name="perPageForm" method="post">
											    <input type="hidden" name="listUrl" value="<?php echo $act_url ?>">
                                                <?php
                                    				$js = 'class="form-control" id="perPage" style="width:auto;" onchange="this.form.submit()"  '; 
                                    				echo form_dropdown('perPage', $PER_PAGE,set_value('perPage',$page), $js); 
                                    			?>
                                            </form>
										</div>
                                    </div>
                                </div>
							</div>
						</div>
					</div>
				</div>
			</div>
		    <!--<footer class="footer">-->
		        <!--<p>Page rendered in {elapsed_time} seconds.</p>-->
			    <?php //footer();?>
			<!--</footer>-->
		</div>
	</div>

</body>
<script>
    function copyToClipboard_URL(event)
    {
        c_url = $(event).data('copy_url');
        
        const input = document.createElement('input')
        
        input.value = c_url;
        document.body.appendChild(input)
        input.select()
        document.execCommand('Copy')
        document.body.removeChild(input)

        // $(".copy_txtc_u").css("font-weight",'900');
        // $(".copy_txtc_u").text("Copied!");
        $(event).attr('title', 'Copied');
        $(event).attr('data-original-title','Copied');
        
        // $(event).tooltip('show');
        // setTimeout(() => { $(".copy_txtc_u").text("Copy URL"); $(".copy_txtc_u").css("font-weight",'400'); }, 1500);
        setTimeout(() => { $(event).attr('title', 'Copy'); $(event).attr('data-original-title','Copy'); }, 4000);
        
    }
</script>

</html>