<?php
include "include/db.php";
include "include/adminauth.php";
require 'vendor/autoload.php';
$time = date("Y-m-d H:i:s");
function clean_input_data($cellVal){	
    include "include/db.php";
	$cellVal = trim($cellVal);
	$cellVal = mysqli_real_escape_string($conn, $cellVal);
	$cellVal = strtolower($cellVal);
    $cellVal = str_replace(' ', '', $cellVal);
    $cellVal = str_replace('*', '', $cellVal);
	return $cellVal;
}

function clean_cell_input_data($cellVal1){	
    include "include/db.php";
	$cellVal1 = trim($cellVal1);
	$cellVal1 = mysqli_real_escape_string($conn, $cellVal1);
	return $cellVal1;
}
    $successmessage = '';
    $message = '';
if(isset($_REQUEST['mess'])){
    $message = $_REQUEST['mess'];
}
if($conn){  
    
   
    if(isset($_REQUEST['insert']) && isset($_REQUEST['client_name']) && isset($_REQUEST['client_email'])){
        if(strlen($_REQUEST['client_name']) >0 && strlen($_REQUEST['client_email']) > 2){
            $new_client_id = $_REQUEST['client_name'];
            $new_client_email = $_REQUEST['client_email'];
            $query2 = "SELECT * FROM client_pay_settle_email WHERE client_id = $new_client_id AND email = '$new_client_email' AND status IS NULL";
            $run2 = mysqli_query($conn, $query2);
            if(mysqli_num_rows($run2) == 0){
                $query1 = "INSERT INTO client_pay_settle_email
                SET client_id = $new_client_id, email = '$new_client_email', time ='$time'";        
                $run1 = mysqli_query($conn, $query1);
                if(mysqli_affected_rows($conn)== 1){
                    $successmessage = "Cleint Email Added for payment processing.";
                }else{
                    $message = "Failed.";
                } 
            }else{
                $message = "That email id and client already exist.";
            }                                                
                        
        } else{
            $message = "Client name or Email invalid.";
        } 
    }
   
    if(isset($_REQUEST['spe'])){

        $id2 = $_REQUEST['spe'];

        $query2 = "SELECT * FROM client_pay_settle_email WHERE id = '$id2'";
        $run2 = mysqli_query($conn, $query2);

        if($run2){
            while($row2 = mysqli_fetch_array($run2)){
                $client_id_int = $row2['client_id'];
                $client_email_int = $row2['email'];
                $query_maps_int = "SELECT * FROM client WHERE client_id = $client_id_int";
                $run_maps_int = mysqli_query($conn, $query_maps_int);
                $row_maps_int = mysqli_fetch_array($run_maps_int);
                $client_name_int = $row_maps_int['client_name'];
            }
        }

    }

    if(isset($_REQUEST['edit']) && isset($_REQUEST['client_name']) && isset($_REQUEST['client_email'])){
        if(strlen($_REQUEST['edit']) >0 && strlen($_REQUEST['client_name']) >0 && strlen($_REQUEST['client_email']) > 2){
            $old_id = $_REQUEST['edit'];
            $query2 = "SELECT * FROM client_pay_settle_email WHERE id = $old_id AND status IS NULL";
            $run2 = mysqli_query($conn, $query2);
            $old_client_id_int = '';
            $old_client_email_int = '';  
            if($run2){
                while($row2 = mysqli_fetch_array($run2)){
                    $old_client_id_int = $row2['client_id'];
                    $old_client_email_int = $row2['email'];                    
                }
            }
            
            $new_client_id = $_REQUEST['client_name'];
            $new_client_email = $_REQUEST['client_email'];

            if($new_client_id != $old_client_id_int || $new_client_email != $old_client_email_int){
                $query2 = "SELECT * FROM client_pay_settle_email WHERE client_id = $new_client_id AND email = '$new_client_email' AND status IS NULL";
                $run2 = mysqli_query($conn, $query2);
                if(mysqli_num_rows($run2) == 0){
                    $query1 = "UPDATE client_pay_settle_email
                    SET client_id = $new_client_id, email = '$new_client_email', time ='$time' WHERE id = $old_id AND status IS NULL";        
                    $run1 = mysqli_query($conn, $query1);
                    if(mysqli_affected_rows($conn)== 1){
                        $successmessage = "Cleint Email Updated for payment processing.";
                    }else{
                        $message = "Failed.";
                    } 
                }else{
                    $message = "That email id and client already exist.";
                }  
            }else{
                $message = "Same Details Filled.";
            }                                                                
                        
        } else{
            $message = "Client name or Email invalid.";
        } 
    }

    if(isset($_REQUEST['delete'])){
        $id3 = $_REQUEST['delete'];
        $e_name = $_REQUEST['ename'];
        $time = date("Y-m-d H:i:s");
        $query3 = "UPDATE client_pay_settle_email SET status = 2, time = '$time' WHERE id = '$id3'";
        $run2 = mysqli_query($conn, $query3);

        if(mysqli_affected_rows($conn)==1){
            $successmessage = "Cleint Email $e_name Deleted Permanently from system.";
        }
    }
    if(isset($_REQUEST['deactive'])){
        $id3 = $_REQUEST['deactive'];
        $time = date("Y-m-d H:i:s");
        $query3 = "UPDATE client_pay_settle_email SET status = 1, time = '$time' WHERE id = '$id3'";
        $run2 = mysqli_query($conn, $query3);

        if(mysqli_affected_rows($conn)==1){
            $successmessage = "Client Email Deactivated.";
        }
    }
    if(isset($_REQUEST['activate'])){
        $id3 = $_REQUEST['activate'];
        $time = date("Y-m-d H:i:s");
        $check_emp_query = "SELECT * FROM client_pay_settle_email WHERE id = $id3 AND status = 1";
        $check_emp_run = mysqli_query($conn, $check_emp_query);
        if(mysqli_num_rows($check_emp_run) == 1){
            $check_emp_row = mysqli_fetch_array($check_emp_run);
            
            $query3 = "UPDATE client_pay_settle_email SET status = NULL, time = '$time' WHERE id = '$id3'";
            $run2 = mysqli_query($conn, $query3);

            if(mysqli_affected_rows($conn)==1){
                $successmessage = "Client Email Activated.";
            }                      
        }   
    }
}
include "header.php";
?>
<div id="page-wrapper">

<a style = 'float:right;' class='btn btn-danger tbl_e_narrow' href='client-email-pay-settle.php'>Back</a>

<h3>Client Email Payment Settings</h3>

<br>   
<?php 
    if(isset($successmessage)){
        if(strlen($successmessage) > 0){   
            echo '<div class="alert alert-success">
            <strong>'. $successmessage .'</strong></div>';
        } 
    }

    if(isset($message)){
        if(strlen($message) > 0){
            echo '<div class="alert alert-danger">
                <strong>'. $message .'</strong>
            </div>';
        }
    }
?>              
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                <h3>
        <?php if(isset($_REQUEST['spe'])){ echo "Edit Client Email Details<a style = 'float:right;' class='btn btn-warning' href='client-email-pay-settle-settings.php'>Cancel</a>";}else{
            echo "Add Client Email";
        } ?> </h3>
        </div>
              <!--   </div> -->
                                                                  

             <div class="panel-body">
                      
             <div class = "row">                   
      
                  
<div class="col-lg-12">

               <?php 
               if(isset($_REQUEST['spe'])){
                echo "<form action = 'client-email-pay-settle-settings.php?edit=$id2' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
               }else{
                echo '<form action = "client-email-pay-settle-settings.php?insert=" method = "POST" class = "form-group" enctype = "multipart/form-data">';
               }
               ?>

<div class="row">

    <div class="form-group col-lg-6">
            <label for="client_name">Client Name</label>               

        <?php if(isset($_REQUEST['spe'])){ ?> 

       <select name="client_name" class="form-control" required autofocus>   
        <?php              
             echo "<option value='$client_id_int' selected='selected'>$client_name_int</option>";  
                 
               $query00 = "SELECT * FROM client WHERE client_is_delete IS NULL";
               $run00 = mysqli_query($conn,$query00);

               if(mysqli_num_rows($run00)>0){
                while($row00 = mysqli_fetch_array($run00)){  
                    $row_client_id = $row00['client_id'];  
                    if($row_client_id != $client_id_int){
                        echo "<option value='".$row00['client_id']."'>". $row00['client_name']."</option>";
                    }                 
                                    
                }
               }  
               ?>
        
        </select>  

        <?php  }else{?>        
        <select name="client_name" class="form-control" required autofocus>   
        <?php              
             echo '<option value="" disabled="disabled" selected="selected">Select Client </option>';         

                 
               $query00 = "SELECT * FROM client WHERE client_is_delete IS NULL";
               $run00 = mysqli_query($conn,$query00);

               if(mysqli_num_rows($run00)>0){
                while($row00 = mysqli_fetch_array($run00)){                     
                    echo "<option value='".$row00['client_id']."'>". $row00['client_name']."</option>";                
                }
               }  
               ?>
        
        </select>  
   <?php }   
    
    ?> 
   
</div> 
<div class = "form-group col-lg-6">
<label for="client_email">Client Email</label>
 <?php if(isset($_REQUEST['spe'])){
        echo "<input type='text' class='form-control' name='client_email' id='client_email' value='$client_email_int' required='required'>";

    }else{
        echo '<input type="text" class="form-control" name="client_email" id="client_email" required="required">';
    }
    ?> 
   </div>
      </div>
 <div class = "row">
     
<div class = "form-group col-lg-6">
<input type="submit" name = "submit" value = "Submit" class="btn btn-success">
</div>
<div class = "form-group col-lg-6">
</div>
</div>
</form>
 
 <br>

</div>
</div>
                </div><!-- panel-body  -->
            </div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row -->
                            
     <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
            <?php if(isset($_REQUEST['view_deactivate'])) { ?>
<h4>Deactivated Emails List</h4>
     <?php }elseif(!isset($_REQUEST['view_deactivate'])) { ?>
        <h4>Active Emails List</h4>
        <?php } ?>
        </div> 
            <div class="panel-body">
            <?php if(isset($_REQUEST['view_deactivate'])) { ?>
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-emp">
                            <a href="<?php 
  
  if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
      $link = "https"; 
  else
      $link = "http"; 
    
  $link .= "://"; 
    
  $link .= $_SERVER['HTTP_HOST']; 
    
  $link .= $_SERVER['REQUEST_URI']; 
  $link = substr($link, 0, strpos($link, "?"));
        
  echo $link; 
  ?>" style="float:right;margin-bottom:2px" class="btn btn-success tbl_e_narrow">View Active Emails</a>
                                    <thead>
                                        <tr>  
                                                <th>
                                                Client Name
                                                </th><th>
                                                Email
                                                </th>
                                            <th>Activate</th>
                                            <th>Delete</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM client_pay_settle_email WHERE status = 1 ORDER BY id";
        $run = mysqli_query($conn, $query);
        if($run){
            while($row = mysqli_fetch_array($run)){
                $id = $row['id'];
                $client_id = $row['client_id']; 
                $client_email = $row['email']; 
                $client_name = '';
                $query_maps = "SELECT * FROM client WHERE client_id = $client_id";
                $run_maps = mysqli_query($conn, $query_maps);
                $row_maps = mysqli_fetch_array($run_maps);
                $client_name = $row_maps['client_name'];
                echo '<tr class="odd gradeX">';
                echo '<td>' .$client_name. '</td>';
                echo '<td>' .$client_email. '</td>';
                echo "<td><a href='client-email-pay-settle-settings.php?activate=$id&view_deactivate='>Activate</a> </td>";              
                echo "<td><a href='client-email-pay-settle-settings.php?delete=$id&view_deactivate=&ename=$client_name' class=\"delete22\" data-confirm=\"Are you sure to permanently delete $client_name?\">Delete</a> </td>";
                echo '</tr>';
            }
        }
    }
?>
                                            
                                            </tbody>
                                    
                                    </table>
                                    <?php } ?>

<?php if(!isset($_REQUEST['view_deactivate'])) { ?>
    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-emp">
    <a href="<?php 
  
  if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
      $link = "https"; 
  else
      $link = "http"; 
    
  $link .= "://"; 
    
  $link .= $_SERVER['HTTP_HOST']; 
    
  $link .= $_SERVER['REQUEST_URI']; 
  $link = substr($link, 0, strpos($link, "?"));
        
  echo $link; 
  ?>?view_deactivate" style="float:right;margin-bottom:2px" class="btn btn-danger tbl_e_narrow">View Deactivated Emails</a>
                                    <thead>
                                        <tr>
                                                <th>
                                                Client Name
                                                </th>
                                                <th>
                                                Client Email
                                                </th>
                                            <th>Edit</th>
                                            <th>Deactivate</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM client_pay_settle_email WHERE status IS NULL ORDER BY id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $id = $row['id'];
                $client_id = $row['client_id']; 
                $client_email = $row['email']; 
                $client_name = '';
                $query_maps = "SELECT * FROM client WHERE client_id = $client_id";
                $run_maps = mysqli_query($conn, $query_maps);
                $row_maps = mysqli_fetch_array($run_maps);
                $client_name = $row_maps['client_name'];
                echo '<tr class="odd gradeX">';
                echo '<td>' .$client_name. '</td>';
                echo '<td>' .$client_email. '</td>';
                echo "<td><a href='client-email-pay-settle-settings.php?spe=$id'>Edit</a> </td>";
                echo "<td><a href='client-email-pay-settle-settings.php?deactive=$id'>Deactivate</a> </td>";
                echo '</tr>';
            }
        }

    }

?>
                                            
                                            </tbody>
                                    
                                    </table>
<?php } ?>
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
    </div> <!--  page wrapper -->
                              
       
        <script>
    var deleteLinks = document.querySelectorAll('.delete22');

for (var i = 0; i < deleteLinks.length; i++) {
  deleteLinks[i].addEventListener('click', function(event) {
	  event.preventDefault();

	  var choice = confirm(this.getAttribute('data-confirm'));

	  if (choice) {
	    window.location.href = this.getAttribute('href');
	  }
  });
}
    </script>
    
<?php  include "footer.php";   ?>