<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}

$client_name = '';
$client_alias = '';
$client_email = '';
$client_phone = '';
$client_address = '';
$client_city = '';
$client_county = '';
$client_state = '';
$client_zip = '';
$client_discount = '';

$message = '';
$successmessage = '';
if(isset($_REQUEST['mess'])){
    $message = $_REQUEST['mess'];
}
if(isset($_REQUEST['smess'])){
    $successmessage = $_REQUEST['smess'];
}
if($user_type == 'admin'){
    if(isset($_REQUEST['delete'])){
        if(strlen($_REQUEST['delete'])> 0){
        $client_id = $_REQUEST['delete'];
        $client_name = $_REQUEST['client_name'];
        $query_client_insert = "UPDATE client SET $active_user_id client_is_delete = 1 WHERE client_id = $client_id";  
        
        $run_client_insert = mysqli_query($conn, $query_client_insert);
        if(mysqli_affected_rows($conn) > 0){
            $successmessage = "$client_name has been deleted from further use in the system.";   
        }else{
            $message = "Something went wrong. Try Again";
        } 
        }
    }

    if(isset($_REQUEST['deactivate'])){
        if(strlen($_REQUEST['deactivate'])> 0){
        $client_id = $_REQUEST['deactivate'];
        $client_name = $_REQUEST['client_name'];
        $query_client_insert = "UPDATE client SET $active_user_id client_is_delete = 2 WHERE client_id = $client_id";  
        
        $run_client_insert = mysqli_query($conn, $query_client_insert);
        if(mysqli_affected_rows($conn) > 0){
            $successmessage = "$client_name has been Deactivated. You can reactivate later from Deactivated client's page.";   
        }else{
            $message = "Something went wrong. Try Again";
        } 
        }
    }


    if(isset($_REQUEST['activate'])){
        if(strlen($_REQUEST['activate'])> 0){
        $client_id = $_REQUEST['activate'];
        $client_name = $_REQUEST['client_name'];
        $query_client_insert = "UPDATE client SET $active_user_id client_is_delete = NULL WHERE client_id = $client_id";  
        
        $run_client_insert = mysqli_query($conn, $query_client_insert);
        if(mysqli_affected_rows($conn) > 0){
            $successmessage = "$client_name has been Activated. It can be now used in the system.";   
        }else{
            $message = "Something went wrong. Try Again";
        } 
        }
    }



    if(isset($_REQUEST['insert']) && isset($_REQUEST['client_name']) && isset($_REQUEST['client_alias']) && isset($_REQUEST['client_email']) && isset($_REQUEST['client_discount']) && isset($_REQUEST['client_phone']) && isset($_REQUEST['client_address']) && isset($_REQUEST['prop_state']) && isset($_REQUEST['prop_county']) && isset($_REQUEST['prop_city']) && isset($_REQUEST['prop_zip'])){
        if(strlen($_REQUEST['client_name'])  >0 && strlen($_REQUEST['client_alias'])  >0 && strlen($_REQUEST['client_email'])  >0 && strlen($_REQUEST['client_phone']) >0 && strlen($_REQUEST['client_address']) >0 && strlen($_REQUEST['prop_state']) >0 && strlen($_REQUEST['prop_city']) >0 && strlen($_REQUEST['prop_zip']) >0){
        $client_name = trim($_REQUEST['client_name']);
        $client_alias = trim($_REQUEST['client_alias']);
        $client_email = trim($_REQUEST['client_email']);
        $client_discount = trim($_REQUEST['client_discount']);
        $discount_query = '';
        if(strlen($client_discount) > 0){
            if($client_discount > 0 && $client_discount < 100){
                $discount_query = ",is_discount = 1, discount = $client_discount";
            }
        }
        $client_phone = trim($_REQUEST['client_phone']);
        $client_address = trim($_REQUEST['client_address']);
        $client_state = trim($_REQUEST['prop_state']);
        $client_county = trim($_REQUEST['prop_county']);
        $client_city = trim($_REQUEST['prop_city']);
        $client_zip = trim($_REQUEST['prop_zip']);
        $time = date("Y-m-d H:i:s");
        $query_check_client = "SELECT * FROM client WHERE (client_name = '$client_name' AND client_alias = '$client_alias' AND client_is_delete IS NULL) OR (client_name = '$client_name' AND client_is_delete IS NULL) OR (client_alias = '$client_alias' AND client_is_delete IS NULL)";

        $run_check_client = mysqli_query($conn, $query_check_client);

            if(mysqli_num_rows($run_check_client) == 0){ 
                $query_client_insert = "INSERT INTO client SET client_name = '$client_name', client_alias = '$client_alias', client_email = '$client_email', client_phone = '$client_phone', client_address = '$client_address', client_zip = '$client_zip', client_state = '$client_state', client_county = '$client_county', client_city = '$client_city', client_time = '$time', is_approve = 1,$active_user_id approve_time = '$time'" . $discount_query;  
                
                $run_client_insert = mysqli_query($conn, $query_client_insert);
                if(mysqli_affected_rows($conn) > 0){
                    $successmessage = "$client_name has been added.";

                }else{
                    $message = "Something went wrong. Try Again";
                    $addfail = 'yes';
                }

            }else{
                $message = 'That client name or alias already exists.';
            }

        }
    }

    if(isset($_REQUEST['editadd']) && isset($_REQUEST['client_name']) && isset($_REQUEST['client_alias']) && isset($_REQUEST['client_email']) && isset($_REQUEST['client_phone']) && isset($_REQUEST['client_address']) && isset($_REQUEST['prop_state']) && isset($_REQUEST['prop_county']) && isset($_REQUEST['prop_city']) && isset($_REQUEST['prop_zip'])&& isset($_REQUEST['client_discount'])){
        if(strlen($_REQUEST['editadd'])  >0 && strlen($_REQUEST['client_name'])  >0 && strlen($_REQUEST['client_alias'])  >0 && strlen($_REQUEST['client_email'])  >0 && strlen($_REQUEST['client_phone']) >0 && strlen($_REQUEST['client_address']) >0 && strlen($_REQUEST['prop_state']) >0 && strlen($_REQUEST['prop_city']) >0 && strlen($_REQUEST['prop_zip']) >0){
            $client_id = $_REQUEST['editadd'];    
            $client_name = trim($_REQUEST['client_name']);
            $client_alias = trim($_REQUEST['client_alias']);
            $client_email = trim($_REQUEST['client_email']);
            $client_phone = trim($_REQUEST['client_phone']);
            $client_address = trim($_REQUEST['client_address']);
            $client_discount = trim($_REQUEST['client_discount']);
            $discount_query = '';
            if(strlen($client_discount) > 0){
                if($client_discount > 0 && $client_discount < 100){
                    $discount_query = ",is_discount = 1, discount = $client_discount";
                }
            }
            $client_state = trim($_REQUEST['prop_state']);
            $client_county = trim($_REQUEST['prop_county']);
            $client_city = trim($_REQUEST['prop_city']);
            $client_zip = trim($_REQUEST['prop_zip']);
            $time = date("Y-m-d H:i:s");
            
            $query_check_client1 = "SELECT * FROM client WHERE client_id = $client_id";
            $run_check_client1 = mysqli_query($conn, $query_check_client1);
            
            $row_check_client1 = mysqli_fetch_array($run_check_client1);
            $client_alias_old = $row_check_client1['client_alias'];
            $client_name_old = $row_check_client1['client_name'];

            $check_client = 'pass';

            if($client_name != $client_name_old){
                $query_check_client = "SELECT * FROM client WHERE client_name = '$client_name' AND client_is_delete IS NULL";

                $run_check_client = mysqli_query($conn, $query_check_client);
                if(mysqli_num_rows($run_check_client) > 0){
                    $check_client = 'fail';
                }

            }
            if($client_alias != $client_alias_old){
                $query_check_client1 = "SELECT * FROM client WHERE client_alias = '$client_alias' AND client_is_delete IS NULL";

                $run_check_client1 = mysqli_query($conn, $query_check_client1);

                if(mysqli_num_rows($run_check_client1) > 0){
                    $check_client = 'fail';
                }
            }
        
            if($check_client == 'pass'){
                
                $query_client_insert = "UPDATE client SET client_name = '$client_name', client_alias = '$client_alias', client_email = '$client_email', client_phone = '$client_phone', client_address = '$client_address', client_zip = '$client_zip', client_state = '$client_state', client_county = '$client_county',$active_user_id client_city = '$client_city' $discount_query WHERE client_id = $client_id";
                $run_client_insert = mysqli_query($conn, $query_client_insert);
                if($run_client_insert){
                    $successmessage = "Details of $client_name has been changed.";
                }else{
                    $message = "Something went wrong. Try Again";
                    $editfail = 'yes';
                }  
            }else{
                    $message = 'That client name or alias already exists.';
                    $editfail = 'yes';
                
            }

        }
    }

    if(isset($_REQUEST['edit']) || isset($_REQUEST['view'])){
        if(isset($_REQUEST['edit'])){
            $client_id = $_REQUEST['edit'];

        }elseif(isset($_REQUEST['view'])){
            $client_id = $_REQUEST['view'];

        }
        if(strlen($client_id)>0){
            $query_fetch_client = "SELECT * FROM client WHERE client_id = $client_id";
            $run_fetch_client = mysqli_query($conn, $query_fetch_client);
            if(mysqli_num_rows($run_fetch_client) == 1){
                $row_fetch_client = mysqli_fetch_array($run_fetch_client);
            }
            $client_name = $row_fetch_client['client_name'];
            $client_alias = $row_fetch_client['client_alias'];
            $client_email = $row_fetch_client['client_email'];
            $is_discount = $row_fetch_client['is_discount'];
            if(isset($is_discount)){
                if($is_discount == 1){
                    $client_discount = $row_fetch_client['discount'];
                    if(isset($client_discount)){
                        if(strlen($client_discount) > 0){
                            if($client_discount - floor($client_discount) > 0){
                            
                            }else{
                                $client_discount = number_format($client_discount, 0, '.', '');
                            }
                        }
                    }
                }
            }
            $client_phone = $row_fetch_client['client_phone'];
            $client_address = $row_fetch_client['client_address'];
            $client_city = $row_fetch_client['client_city'];
            $client_county = $row_fetch_client['client_county'];
            $client_state = $row_fetch_client['client_state'];
            $client_zip = $row_fetch_client['client_zip'];
        }
    }
}
include "header.php";
?>




<div id="page-wrapper">
<h3>Client Management</h3>

<br>   
<?php    
if($user_type == 'admin'){                                 
                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>';
                    }
                }
                    ?>              
    
        <?php 
        if(isset($_REQUEST['edit'])|| isset($editfail)){ ?>

<div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                <h3>

        <?php
            echo "Edit Client Details of $client_name<a style = 'float:right;' class='btn btn-warning' href='clients.php'>Cancel</a>";

?>
</h3>
        </div>

<?php


        }elseif(isset($_REQUEST['add'])|| isset($addfail)){?>

            <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                            <!-- <div class="col-lg-12"> -->
                            <h3>
            
                    <?php
            echo "Add New Client <a style = 'float:right;' class='btn btn-warning' href='clients.php'>Cancel</a>";
?>
</h3>
        </div>

<?php

        }else{ ?>
      <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>New Client Form </b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">          
          
                <a href="clients.php?add=" class="btn btn-success" role="button">Add New Client</a>
<br>
<br>
                  </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
           

            
        </div> <!--  col lg 6-->

         <div class="col-md-6">
         <?php          if($user_type == 'admin'){     ?>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>New Client Details Excel Upload </b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">
           
            <form action = 'client-import.php?upload_xl_client=' method = 'POST' enctype = 'multipart/form-data'>
                <div class="form-group col-md-6">
                   <br>
				    <input type="file" class="upload-file" required name="fileToUpload1" id="fileToUpload1">   
                </div>
                <div class="form-group col-md-6">
               
                   <input type="submit" class="btn btn-success" value = "Upload"> 
                   <a href="excels/client-data.xlsx" download class="btn btn-danger" style = 'float:right;'>
                       Client Excel Format
                   </a>
              
               </div>

                </form>
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
            <?php        }    ?>
        </div> <!--  col lg 6-->
          </div> <!-- row -->    


<!--  -->
<div class="row">
                  <div class="col-lg-12">
                      <div class="panel panel-default">           
        
        
        <?php
        } 
        ?>                                                                   
              <?php 
             if(isset($_REQUEST['edit']) || isset($_REQUEST['add'])|| isset($addfail)|| isset($editfail) || isset($_REQUEST['view'])){ ?>
             <div class="panel-body">                      
             <div class = "row">               
           
                                   
                                   
                    <div class="col-lg-12">
                    
                    <?php }



                              
                                if(isset($_REQUEST['edit'])|| isset($editfail)){
                                    echo "<form action = 'clients.php?editadd=$client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
                                }          
                                elseif(isset($_REQUEST['add'])|| isset($addfail)){
                                    echo '<form action = "clients.php?insert=" method = "POST" class = "form-group" enctype = "multipart/form-data">';
                                }elseif(isset($_REQUEST['view'])){
                                    echo '<form class = "form-group" enctype = "multipart/form-data">';
                                }
                              
                                if(isset($_REQUEST['edit']) || isset($_REQUEST['add'])|| isset($addfail)|| isset($editfail) || isset($_REQUEST['view'])){
                                   
                              ?>

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

     <input type='text' class='form-control' name='client_name' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='client_name' value='<?php echo $client_name ?>' required='required' autofocus>
     <input type="hidden" name='client_discount'>
        </div>
    
    
        <div class = "form-group col-lg-6">
        <label for="client_alias">Alias</label>
          

          <input type='text' class='form-control' name='client_alias' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='client_alias' value='<?php echo $client_alias ?>' required='required'>                          

        </div>
        </div> 

         <div class = "row">
        
        <div class = "form-group col-lg-6">
        <label for="client_phone">Phone</label>
          

     <input type='text' class='form-control' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> name='client_phone' id='client_phone' value='<?php echo $client_phone ?>' required='required'>
        </div>
    
    
        <div class = "form-group col-lg-6">
        <label for="client_email">Email</label>
          

          <input type='text' class='form-control' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> name='client_email' id='client_email' value='<?php echo $client_email ?>' required='required'>                          

        </div>

       <!--  <div class = "form-group col-lg-4">
        <label for="client_discount">Discount Fee</label>
          

          <input type='text' class='form-control' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> name='client_discount' id='client_discount' value='<?php /* echo $client_discount */ ?>'>                          

        </div> -->
        </div>

        <div class = "row">
        
        <div class = "form-group col-lg-6">
      
        <div class="row">
                    <div class="form-group col-lg-6">
                    <label for="prop_zip">Zipcode</label>
                            <input class='form-control' name='prop_zip' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> maxlength='5' minlength='5' id='prop_zip' value='<?php echo $client_zip ?>' required='required'> 

                        </div>
                        <div class="form-group col-lg-6">
                        <label for="prop_city">City</label>
                    

                            <input type='text' class='form-control' name='prop_city' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='prop_city' value='<?php echo $client_city ?>' required='required'> 
                           
                        
                        </div>
                       
                    
                    </div>

                    <div class="row">

                        <div class="form-group col-lg-6">
                        <label for="prop_county">County</label>
                    

                    <input type='text' class='form-control' name='prop_county' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='prop_county' value='<?php echo $client_county ?>'>  
                       
                        </div>
                        <div class="form-group col-lg-6">
                        <label for="prop_state">State</label>

<select class='form-control' name='prop_state'  id='prop_state' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> required='required'>
<?php
if(isset($client_state)){
echo "<option value='$client_state' selected = 'selected'>$client_state</option>";
}else{
echo '<option value="" disabled="disabled" selected="selected">Select State</option>';
}
?>

<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="PR">Puerto Rico</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>               

                        </div>

                    </div>
        <div class="row">
        <div class = "form-group col-lg-12">                  
         
            <label for="client_address">Address</label>          
            <textarea rows="4" name="client_address" <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> class = "form-control" id="client_address" placeholder=""  maxlength="5000" required = "required"><?php echo $client_address; ?></textarea>
        </div>                
         
        </div>  
    


        </div>
    
    
        <div class = "form-group col-lg-6">
                                 

        </div>
        </div>                          

    <div class = "row">
        
    <div class = "form-group col-lg-6">
    <?php if(isset($_REQUEST['view'])){ echo "<a href= 'clients.php?edit=$client_id' type='button' name = 'btn_edit'  class='btn btn-warning'>Edit Details</a>";}else{echo '    <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  --> <?php } ?>
            </div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row -->

    <?php } ?>
   <style>
       @media(min-width:768px) {
   th { font-size: 14 px; padding:2px!important;margin:0px;}
td { font-size: 14px;padding:2px!important;margin:0px; }
       }
   </style>                           
     <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 Client List</h4>
     <?php }elseif(!isset($_REQUEST['view_deactivate'])) { ?>
        <h4>Active Client 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-bidwizzz">
                            <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 Clients</a>
                                    <thead>
                                        <tr>
                                        <th>
                                        Client ID
                                                </th>
                                        
                                                <th>
                                                    Client Name
                                                </th>
                                                <th>
                                                Alias
                                                 </th>
                                               
                                                <th>
                                               Phone
                                                </th>
                                                <th>
                                                Email
                                                </th>
                                                <th>
                                                Address
                                                </th>
                                                <th>
                                                City
                                                </th>
                                                <th>
                                                State
                                                </th>
                                                <th>
                                                Zip
                                                </th>
                                                <th>
                                                County
                                                </th>
                                              <!--   <th>
                                                Discount
                                                </th> -->
                                                <?php          if($user_type == 'admin'){     ?>
                                                <th>
                                                Activate
                                                </th>
                                               
                                            <th>Delete</th>
                                                <?php          }     ?>
                                        </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM client WHERE client_is_delete = 2 ORDER BY client_id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $client_id = $row['client_id'];
                $client_name = $row['client_name']; 
                $client_alias = $row['client_alias']; 
                $client_address = $row['client_address']; 
                $client_county = $row['client_county'];       
                $client_zip = $row['client_zip'];       
                $client_city = $row['client_city'];       
                $client_state = $row['client_state'];             
                $client_phone = $row['client_phone'];
                $client_email = $row['client_email'];
                $client_is_delete = $row['client_is_delete'];
                $is_discount = $row['is_discount'];
                /* $discount_row = '';
                if(isset($is_discount)){
                    if($is_discount == 1){
                        $discount = $row['discount'];
                        if(isset($discount)){
                            if(strlen($discount) > 0){
                                if($discount - floor($discount) > 0){
                                    $discount_row = $discount . '%';
                                }else{
                                    $discount = number_format($discount, 0, '.', '');
                                    $discount_row = $discount . '%';
                                }
                            }
                        }
                    }
                }  
     */
                echo '<tr class="odd gradeX">';
                echo '<td>' .$client_id. '</td>';
                echo '<td>' .$client_name. '</td>';
                echo '<td>' .$client_alias. '</td>';
                echo '<td>' .$client_phone. '</td>';
                echo '<td>' .$client_email. '</td>';
                echo '<td>' .$client_address. '</td>';
                echo '<td>' .$client_city. '</td>';
                echo '<td>' .$client_state. '</td>';
                echo '<td>' .$client_zip. '</td>';
                echo '<td>' .$client_county. '</td>';
              /*   echo '<td>' .$discount_row. '</td>'; */
              if($user_type == 'admin'){ 
                echo "<td><a href='clients.php?activate=$client_id&client_name=$client_name&view_deactivate='>Activate</a> </td>";              
                echo "<td><a href='clients.php?delete=$client_id&client_name=$client_name&view_deactivate=' class=\"delete2\" data-confirm=\"Are you sure to permanently delete $client_name? It cannot be reactivated.\">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-bidwizzz">
                            <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 Clients</a>
                                    <thead>
                                        <tr>
                                        <th>
                                        Client ID
                                                </th>
                                        
                                                <th>
                                                    Client Name
                                                </th>
                                                <th>
                                                Alias
                                                 </th>
                                               
                                                <th>
                                               Phone
                                                </th>
                                                <th>
                                                Email
                                                </th>
                                                <th>
                                                Address
                                                </th>
                                                <th>
                                                City
                                                </th>
                                                <th>
                                                State
                                                </th>
                                                <th>
                                                Zip
                                                </th>
                                                <th>
                                                County
                                                </th>
                                              <!--   <th>
                                                Discount
                                                </th> -->
                                    <?php          if($user_type == 'admin'){     ?>
                                                <th>
                                                 Deactivate
                                                </th>
                                                <th>
                                                 Edit
                                                </th>
                                                   <!--   <th>Delete</th> -->
                                                <?php          }     ?>
                                       
                                           
                                        </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM client WHERE client_is_delete IS NULL ORDER BY client_id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $client_id = $row['client_id'];
                $client_name = $row['client_name']; 
                $client_alias = $row['client_alias']; 
                $client_address = $row['client_address']; 
                $client_county = $row['client_county'];       
                $client_zip = $row['client_zip'];       
                $client_city = $row['client_city'];       
                $client_state = $row['client_state'];             
                $client_phone = $row['client_phone'];
                $client_email = $row['client_email'];
                $client_is_delete = $row['client_is_delete'];
                $is_discount = $row['is_discount'];
                /* $discount_row = '';
                if(isset($is_discount)){
                    if($is_discount == 1){
                        $discount = $row['discount'];
                        if(isset($discount)){
                            if(strlen($discount) > 0){
                                if($discount - floor($discount) > 0){
                                    $discount_row = $discount . '%';
                                }else{
                                    $discount = number_format($discount, 0, '.', '');
                                    $discount_row = $discount . '%';
                                }
                            }
                        }
                    }
                }  
     */
                echo '<tr class="odd gradeX">';
                echo '<td>' .$client_id. '</td>';
                echo '<td>' .$client_name. '</td>';
                echo '<td>' .$client_alias. '</td>';
                echo '<td>' .$client_phone. '</td>';
                echo '<td>' .$client_email. '</td>';
                echo '<td>' .$client_address. '</td>';
                echo '<td>' .$client_city. '</td>';
                echo '<td>' .$client_state. '</td>';
                echo '<td>' .$client_zip. '</td>';
                echo '<td>' .$client_county. '</td>';
              /*   echo '<td>' .$discount_row. '</td>'; */
              if($user_type == 'admin'){ 
                echo "<td><a href='clients.php?deactivate=$client_id&client_name=$client_name'>Deactivate</a> </td>";
                echo "<td><a href='clients.php?edit=$client_id'>Edit</a> </td>";              
               /*  echo "<td><a href='clients.php?delete=$client_id&client_name=$client_name' class=\"delete2\" data-confirm=\"Are you sure to permanently delete $client_name? It cannot be reused.\">Delete</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('.delete2');

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";   ?>