<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}elseif($user_type == 'staff'){
    $active_user_id = "employee_id = $employee_id,";
}
$vendor_name = '';
$vendor_company = '';
$vendor_email = '';
$vendor_phone = '';
$vendor_address = '';

function clean_cell_input_data($cellVal1){	
    include "include/db.php";
	$cellVal1 = trim($cellVal1);
	$cellVal1 = mysqli_real_escape_string($conn, $cellVal1);
	return $cellVal1;
}

if(isset($_REQUEST['delete'])){
    if(strlen($_REQUEST['delete'])> 0){
       $vendor_id = $_REQUEST['delete'];
       $vendor_name = $_REQUEST['vendor_name'];
       $query_vendor_insert = "UPDATE vendor SET $active_user_id vendor_is_delete = 1 WHERE vendor_id = $vendor_id";  
    
       $run_vendor_insert = mysqli_query($conn, $query_vendor_insert);
       if(mysqli_affected_rows($conn) > 0){
           $successmessage = "$vendor_name has been permanently deleted from further use in the system."; 
       }else{
           $message = "Something went wrong. Try Again";
       }

    }
}

if(isset($_REQUEST['deactivate'])){
    if(strlen($_REQUEST['deactivate'])> 0){
    $vendor_id = $_REQUEST['deactivate'];
    $vendor_name = $_REQUEST['vendor_name'];
    $query_vendor_insert = "UPDATE vendor SET $active_user_id vendor_is_delete = 2 WHERE vendor_id = $vendor_id";  
    
    $run_vendor_insert = mysqli_query($conn, $query_vendor_insert);
    if(mysqli_affected_rows($conn) > 0){
        $successmessage = "$vendor_name has been Deactivated. You can reactivate later from Deactivated vendor's page.";   
    }else{
        $message = "Something went wrong. Try Again";
    } 
    }
}


if(isset($_REQUEST['activate'])){
    if(strlen($_REQUEST['activate'])> 0){
    $vendor_id = $_REQUEST['activate'];
    $vendor_name = $_REQUEST['vendor_name'];
    $query_vendor_insert = "UPDATE vendor SET $active_user_id vendor_is_delete = NULL WHERE vendor_id = $vendor_id";  
    
    $run_vendor_insert = mysqli_query($conn, $query_vendor_insert);
    if(mysqli_affected_rows($conn) > 0){
        $successmessage = "$vendor_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['vendor_name']) && isset($_REQUEST['vendor_company']) && isset($_REQUEST['vendor_email']) && isset($_REQUEST['vendor_phone']) && isset($_REQUEST['vendor_address']) && isset($_REQUEST['prop_zip']) && isset($_REQUEST['prop_city']) && isset($_REQUEST['prop_county']) && isset($_REQUEST['prop_state'])){

    if(strlen($_REQUEST['vendor_name']) > 0 && strlen($_REQUEST['vendor_company'])  >0 && strlen($_REQUEST['vendor_email'])  >0 && strlen($_REQUEST['vendor_phone']) >0 && strlen($_REQUEST['vendor_address']) >0 && strlen($_REQUEST['prop_city']) >0 && strlen($_REQUEST['prop_state']) >0  && strlen($_REQUEST['prop_zip']) >0){
        $vendor_name = trim($_REQUEST['vendor_name']);
        $vendor_name = clean_cell_input_data($vendor_name);
        $vendor_company = trim($_REQUEST['vendor_company']);
        $vendor_company = clean_cell_input_data($vendor_company);
        $vendor_email = $_REQUEST['vendor_email'];
        $vendor_email = clean_cell_input_data($vendor_email);
        $vendor_phone = $_REQUEST['vendor_phone'];
        $vendor_phone = clean_cell_input_data($vendor_phone);
        $vendor_address = $_REQUEST['vendor_address'];
        $vendor_address = clean_cell_input_data($vendor_address);    
        $vendor_county = $_REQUEST['prop_county'];
        $vendor_county = clean_cell_input_data($vendor_county);
        $vendor_city = $_REQUEST['prop_city'];
        $vendor_city = clean_cell_input_data($vendor_city); 
        $vendor_zip = $_REQUEST['prop_zip'];
        $vendor_zip = clean_cell_input_data($vendor_zip);
        $vendor_state = $_REQUEST['prop_state'];
        $vendor_state = clean_cell_input_data($vendor_state);        
        $time = date("Y-m-d H:i:s");

        $query_vendor_insert = "INSERT INTO vendor SET vendor_name = '$vendor_name', vendor_company = '$vendor_company', vendor_email = '$vendor_email', vendor_phone = '$vendor_phone', vendor_address = '$vendor_address',vendor_zip = '$vendor_zip',vendor_city = '$vendor_city',vendor_county = '$vendor_county',vendor_state = '$vendor_state', vendor_time = '$time', is_approve = 1, $active_user_id approve_time = '$time'";  
                
        $run_vendor_insert = mysqli_query($conn, $query_vendor_insert);
        if(mysqli_affected_rows($conn) > 0){
            $vendor_id = mysqli_insert_id($conn);
            $successmessage = "$vendor_name has been added."; 
            if(isset($_REQUEST['vendor_pay']) && isset($vendor_id)){
                if(strlen($_REQUEST['vendor_pay']) > 0 && strlen($vendor_id) > 0){
                    $vendor_pay = $_REQUEST['vendor_pay'];
                    $vendor_pay = clean_cell_input_data($vendor_pay);
                    $insert_vendor_pay = "INSERT INTO vendor_pay SET vendor_id = $vendor_id , pay_date = '$time', pay_amount = $vendor_pay,$active_user_id time = '$time'";
                    $insert_vendor_pay_run = mysqli_query($conn, $insert_vendor_pay);
                }
            }        

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

    }
}

if(isset($_REQUEST['editadd']) && isset($_REQUEST['vendor_name']) && isset($_REQUEST['vendor_company']) && isset($_REQUEST['vendor_email']) && isset($_REQUEST['vendor_phone']) && isset($_REQUEST['vendor_address']) && isset($_REQUEST['prop_zip']) && isset($_REQUEST['prop_city']) && isset($_REQUEST['prop_county']) && isset($_REQUEST['prop_state'])){
    if(strlen($_REQUEST['editadd'])  >0 && strlen($_REQUEST['vendor_name'])  >0 && strlen($_REQUEST['vendor_company'])  >0 && strlen($_REQUEST['vendor_email'])  >0 && strlen($_REQUEST['vendor_phone']) >0 && strlen($_REQUEST['vendor_address']) >0 && strlen($_REQUEST['prop_city']) >0 && strlen($_REQUEST['prop_state']) >0  && strlen($_REQUEST['prop_zip']) >0){
        $vendor_id = $_REQUEST['editadd'];    
        $vendor_name = trim($_REQUEST['vendor_name']);
        $vendor_name = clean_cell_input_data($vendor_name);
        $vendor_company = trim($_REQUEST['vendor_company']);
        $vendor_company = clean_cell_input_data($vendor_company);
        $vendor_email = $_REQUEST['vendor_email'];
        $vendor_email = clean_cell_input_data($vendor_email);
        $vendor_phone = $_REQUEST['vendor_phone'];
        $vendor_phone = clean_cell_input_data($vendor_phone);
        $vendor_address = $_REQUEST['vendor_address'];
        $vendor_address = clean_cell_input_data($vendor_address);
        $vendor_county = $_REQUEST['prop_county'];
        $vendor_county = clean_cell_input_data($vendor_county);
        $vendor_city = $_REQUEST['prop_city'];
        $vendor_city = clean_cell_input_data($vendor_city); 
        $vendor_zip = $_REQUEST['prop_zip'];
        $vendor_zip = clean_cell_input_data($vendor_zip);
        $vendor_state = $_REQUEST['prop_state'];
        $vendor_state = clean_cell_input_data($vendor_state);
        $time = date("Y-m-d H:i:s");
        $query_vendor_insert = "UPDATE vendor SET $active_user_id vendor_name = '$vendor_name', vendor_company = '$vendor_company', vendor_email = '$vendor_email', vendor_phone = '$vendor_phone', vendor_address = '$vendor_address', vendor_zip = '$vendor_zip',vendor_city = '$vendor_city', vendor_county = '$vendor_county', vendor_state = '$vendor_state' WHERE vendor_id = $vendor_id";  
                
        $run_vendor_insert = mysqli_query($conn, $query_vendor_insert);
        if($run_vendor_insert){
            $successmessage = "Details of $vendor_name has been changed.";
        }else{
            $message = "Something went wrong. Try Again";
            $editfail = 'yes';
        }             
    }
}

if(isset($_REQUEST['edit']) || isset($_REQUEST['view'])){
    if(isset($_REQUEST['edit'])){
        $vendor_id = $_REQUEST['edit'];
    }elseif(isset($_REQUEST['view'])){
        $vendor_id = $_REQUEST['view'];
    }
    if(strlen($vendor_id)>0){
        $query_fetch_vendor = "SELECT * FROM vendor WHERE vendor_id = $vendor_id";
        $run_fetch_vendor = mysqli_query($conn, $query_fetch_vendor);
        if(mysqli_num_rows($run_fetch_vendor) == 1){
            $row_fetch_vendor = mysqli_fetch_array($run_fetch_vendor);
        }
        $vendor_name = $row_fetch_vendor['vendor_name'];
        $vendor_company = $row_fetch_vendor['vendor_company'];
        $vendor_email = $row_fetch_vendor['vendor_email'];
        $vendor_phone = $row_fetch_vendor['vendor_phone'];
        $vendor_address = $row_fetch_vendor['vendor_address'];
        $vendor_zip = $row_fetch_vendor['vendor_zip'];
        $vendor_county = $row_fetch_vendor['vendor_county'];
        $vendor_state = $row_fetch_vendor['vendor_state'];
        $vendor_city = $row_fetch_vendor['vendor_city'];
    }
}
include "header.php";
?>
<div id="page-wrapper">
<?php
if($user_type == 'admin'){
?>
<button style='float:right;padding:2px;font-size:14px;margin-bottom:0px' class= 'btn btn-success' id = 'addVendorModal'>Import Vendor Excel</button>
<?php
}
?>
<h3>Vendor Management</h3>

<br>   
<?php                                     
                if(isset($successmessage)){

                    echo '<div class="alert alert-success">
                    <strong>'. $successmessage .'</strong></div>';
                } 

                if(isset($message)){
                    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['edit'])|| isset($editfail)){ 
            echo "Edit Vendor Details of $vendor_name<a style = 'float:right;' class='btn btn-warning' href='vendors.php'>Cancel</a>";

        }elseif(isset($_REQUEST['add'])|| isset($addfail)){
            echo "Add New Vendor <a style = 'float:right;' class='btn btn-warning' href='vendors.php'>Cancel</a>";
        }else{
            echo '<a href="vendors.php?add=" class="btn btn-success" role="button">Add New Vendor</a>';
        } 
        ?> </h3>
        </div>                                                                  
              <?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 = 'vendors.php?editadd=$vendor_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
                                }          
                                elseif(isset($_REQUEST['add'])|| isset($addfail)){
                                    echo '<form action = "vendors.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="vendor_name">Vendor Name</label>
     <input type='text' class='form-control' name='vendor_name' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='vendor_name' value='<?php echo $vendor_name ?>' required='required' autofocus>
        </div>
        <div class = "form-group col-lg-6">
        <label for="vendor_company">Company</label>
          <input type='text' class='form-control' name='vendor_company' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='vendor_company' value='<?php echo $vendor_company ?>' required='required'>
        </div>
        </div> 

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

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

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

        </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 $vendor_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 $vendor_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 $vendor_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($vendor_state)){
echo "<option value='$vendor_state' selected = 'selected'>$vendor_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="vendor_address">Address</label>          
                <textarea rows="4" name="vendor_address" <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> class = "form-control" id="vendor_address" placeholder=""  maxlength="5000" required = "required"><?php echo $vendor_address; ?></textarea>
                </div>                
        
        </div>  


        </div>
    
        <?php if(!isset($_REQUEST['edit']) && !isset($editfail) && !isset($_REQUEST['view'])){?>
        <div class = "form-group col-lg-6">
       
        <label for="vendor_pay">Year to Date Pay</label>          
        <input type='text' class='form-control' name='vendor_pay' id='vendor_pay'>   
    
        </div>
        <?php 
    }
    ?>
        </div>                          

    <div class = "row">
        
    <div class = "form-group col-lg-6">
    <?php if(isset($_REQUEST['view'])){ echo "<a href= 'vendors.php?edit=$vendor_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 -->
   <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 Vendor List</h4>
     <?php }elseif(!isset($_REQUEST['view_deactivate'])) { ?>
        <h4>Active Vendor 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 Vendors</a>
                                    <thead>
                                        <tr> 
                                        <th>
                                             Vendor ID
                                                </th>                                       
                                                <th>
                                             Vendor Name
                                                </th>
                                                <th>
                                                Company
                                                 </th>  
                                                <th>
                                               Phone
                                                </th>
                                                <th>
                                                Email
                                                </th>
                                                <th>
                                                Address
                                                </th>
                                                <th>
                                                City
                                                </th>
                                                <th>
                                                State
                                                </th>
                                                <th>
                                                Zip
                                                </th>
                                                <th>
                                                County
                                                </th>
                                                <th>
                                                Year to Date Pay
                                                </th>                                              
                                                <th>
                                                 Activate
                                                </th>
                                            <th>Delete</th>
                                        </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM vendor WHERE vendor_is_delete = 2 ORDER BY vendor_id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $vendor_id = $row['vendor_id'];
                $vendor_name = $row['vendor_name']; 
                $vendor_company = $row['vendor_company']; 
                $vendor_address = $row['vendor_address']; 
                $vendor_county = $row['vendor_county'];       
                $vendor_city = $row['vendor_city'];       
                $vendor_state = $row['vendor_state'];       
                $vendor_zip = $row['vendor_zip'];       
                $vendor_phone = $row['vendor_phone'];
                $vendor_email = $row['vendor_email'];
                $vendor_is_delete = $row['vendor_is_delete'];
                $vendor_pay = 0;               
            
                $year1_month = strtotime("-1 year", time());
                $year1_stamp = date("Y-m-d H:i:s", $year1_month);   
                $fetch_vendor_pay = "SELECT * FROM vendor_pay WHERE vendor_id = $vendor_id AND is_delete IS NULL AND pay_date >= '$year1_stamp'";
                $fetch_vendor_pay_run = mysqli_query($conn, $fetch_vendor_pay);
                if(mysqli_num_rows($fetch_vendor_pay_run) > 0){
                    while($fetch_vendor_pay_row = mysqli_fetch_array($fetch_vendor_pay_run)){
                        $row_pay_amount = $fetch_vendor_pay_row['pay_amount'];   
                        $vendor_pay = $vendor_pay + $row_pay_amount; 
                    }
                }
                if($vendor_pay == 0){
                    $vendor_pay = '';
                }else{
                    $vendor_pay = number_format($vendor_pay, 2, '.', '');
                    $vendor_pay = '$ '. $vendor_pay;
                }
                echo '<tr class="odd gradeX">';
                echo '<td>' .$vendor_id. '</td>';
                echo '<td>' .$vendor_name. '</td>';
                echo '<td>' .$vendor_company. '</td>';
                echo '<td>' .$vendor_phone. '</td>';
                echo '<td>' .$vendor_email. '</td>';
                echo '<td>' .$vendor_address. '</td>';
                echo '<td>' .$vendor_city. '</td>';
                echo '<td>' .$vendor_state. '</td>';
                echo '<td>' .$vendor_zip. '</td>';
                echo '<td>' .$vendor_county. '</td>';
                echo '<td>' .$vendor_pay. '</td>';
                echo "<td><a href='vendors.php?activate=$vendor_id&vendor_name=$vendor_name&view_deactivate='>Activate</a> </td>";
                echo "<td><a href='vendors.php?delete=$vendor_id&vendor_name=$vendor_name&view_deactivate=' class=\"delete\" data-confirm=\"Are you sure to permanently delete $vendor_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 Vendors</a>
                                    <thead>
                                        <tr> 
                                        <th>
                                             Vendor ID
                                                </th>                                       
                                                <th>
                                             Vendor Name
                                                </th>
                                                <th>
                                                Company
                                                 </th>  
                                                <th>
                                               Phone
                                                </th>
                                                <th>
                                                Email
                                                </th>
                                                <th>
                                                Address
                                                </th>
                                                <th>
                                                City
                                                </th>
                                                <th>
                                                State
                                                </th>
                                                <th>
                                                Zip
                                                </th>
                                                <th>
                                                County
                                                </th>
                                                <th>
                                                Year to Date Pay
                                                </th>                                 <th>Deactivate</th>               
                                                <th>
                                                 Edit
                                                </th>       
                                        </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        $query = "SELECT * FROM vendor WHERE vendor_is_delete IS NULL ORDER BY vendor_id";
        $run = mysqli_query($conn, $query);

        if($run){
            while($row = mysqli_fetch_array($run)){
                $vendor_id = $row['vendor_id'];
                $vendor_name = $row['vendor_name']; 
                $vendor_company = $row['vendor_company']; 
                $vendor_address = $row['vendor_address']; 
                $vendor_county = $row['vendor_county'];       
                $vendor_city = $row['vendor_city'];       
                $vendor_state = $row['vendor_state'];       
                $vendor_zip = $row['vendor_zip'];       
                $vendor_phone = $row['vendor_phone'];
                $vendor_email = $row['vendor_email'];
                $vendor_is_delete = $row['vendor_is_delete'];
                $vendor_pay = 0;               
            
                $year1_month = strtotime("-1 year", time());
                $year1_stamp = date("Y-m-d H:i:s", $year1_month);   
                $fetch_vendor_pay = "SELECT * FROM vendor_pay WHERE vendor_id = $vendor_id AND is_delete IS NULL AND pay_date >= '$year1_stamp'";
                $fetch_vendor_pay_run = mysqli_query($conn, $fetch_vendor_pay);
                if(mysqli_num_rows($fetch_vendor_pay_run) > 0){
                    while($fetch_vendor_pay_row = mysqli_fetch_array($fetch_vendor_pay_run)){
                        $row_pay_amount = $fetch_vendor_pay_row['pay_amount'];   
                        $vendor_pay = $vendor_pay + $row_pay_amount; 
                    }
                }
                if($vendor_pay == 0){
                    $vendor_pay = '';
                }else{
                    $vendor_pay = number_format($vendor_pay, 2, '.', '');
                    $vendor_pay = '$'. $vendor_pay;
                }
                echo '<tr class="odd gradeX">';
                echo '<td>' .$vendor_id. '</td>';
                echo '<td>' .$vendor_name. '</td>';
                echo '<td>' .$vendor_company. '</td>';
                echo '<td>' .$vendor_phone. '</td>';
                echo '<td>' .$vendor_email. '</td>';
                echo '<td>' .$vendor_address. '</td>';
                echo '<td>' .$vendor_city. '</td>';
                echo '<td>' .$vendor_state. '</td>';
                echo '<td>' .$vendor_zip. '</td>';
                echo '<td>' .$vendor_county. '</td>';
                echo '<td>' .$vendor_pay. '</td>';  
                echo "<td><a href='vendors.php?deactivate=$vendor_id&vendor_name=$vendor_name'>Deactivate</a> </td>";   
                echo "<td><a href='vendors.php?edit=$vendor_id'>Edit</a> </td>";
                echo '</tr>';
            }
        }

    }

?>
                                            
                                            </tbody>
                                    
                                    </table>
                             
<?php } ?>       
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->

<!-- Modal -->
<div id="vendorModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Upload Vendor Excel</h4>
      </div>
      <div class="modal-body">
      <div class="alert alert-danger" role="alert" id = 'vendor-alert' style='display:none'>
    <p id='danger_text'></p> 
 </div>
 <div class="alert alert-info" role="alert" id = 'vendor-alert-warning' style='display:none'>
    <p id='danger_text_warning'>Excel is uploading.</p> 
 </div>

<div class="alert alert-success" role="alert" id = 'vendor-alert-success' style='display:none'>
 <p id='danger_text1'>
</p> </div>
<form id="bid_item_excel" enctype="multipart/form-data">
<div class = "row">
    
        <div class = "form-group col-lg-6">
        <label for="fileToUpload_vendor">Choose Excel to upload</label>

<input type="file" class="upload-file" name="fileToUpload_vendor" id="fileToUpload_vendor">  

        </div>
        <div class = "form-group col-lg-6">
        <a href="excels/vendor.xlsx" download class="btn btn-danger" style = 'float:right;'>
                       Vendor Excel Format
                   </a>         
       </div>
        </div>        
    </form>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
<!--  -->
    </div> <!--  page wrapper -->
                              
       
       
<?php  include "footer.php";   ?>