<?php include("_config/dbconfig.php");
include("_includes/head.php");?>
<title>Clients</title>
<style>
    .p_button{
        font-size: 11px !important;
        padding: 3px 5px 1px !important;
        background: #f7f7f7; /* Old browsers */
        background: -moz-linear-gradient(top, #f7f7f7 0%, #e7e7e7 100%); /* FF3.6-15 */
        background: -webkit-linear-gradient(top, #f7f7f7 0%,#e7e7e7 100%); /* Chrome10-25,Safari5.1-6 */
        background: linear-gradient(to bottom, #f7f7f7 0%,#e7e7e7 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f7f7f7', endColorstr='#e7e7e7',GradientType=0 ); /* IE6-9 */
        border-color: #c5c4c6;
        border-radius:0;
        margin-right: 4px !important;
    }
</style>
<?php include("_includes/header.php");


if(isset($_GET['del']) && !empty($_GET['del'])){
	
	$dd = base64_decode($_GET['del']);
	$obj_clients_class->deleteClientAndClientPersons($dd);
	echo "<script> window.location= 'clients.php'</script>";
    die();
	} 

function compareByName($a, $b) {
    return strcmp($a["company_name"], $b["company_name"]);
  }

    $all_clients = $obj_clients_class->getClients();
    usort($all_clients,"compareByName");
    
    /* echo '<pre>';
    print_r($all_clients); 
    echo '</pre>';  
    */

function change2($string){
            $tmp = preg_replace("/[\n\r]/","",$string);
            $string = str_replace('-', '*', $string);
            $string = preg_replace('/[^(\x20-\x7F)]*/','', $tmp);
            
            $string = strip_tags($string);
            $string = str_replace('*', '-', $string);
            return trim($string);
	}
        
function change($string){
            $tmp = preg_replace("/[\n\r]/","-",$string);
            $string = preg_replace('/[^(\x20-\x7F)]*/','', $tmp);
            $string = str_replace('-', '<br>', $string);
            $string = strip_tags($string);
            return trim($string);
	}
        
     $countt=0;
/*  $aa = $obj_clients_class->CheckClient1();
    foreach($aa as $check){
        if(!empty($check['email'])){
            $temE = change($check['email']); //explode(",", $check['email']);
            $match  = $obj_clients_class->CheckEmail2($temE);
            
                $new_emails =  change2($check['email']);
                if(!empty($match['email'])){
                    $old_email = $match['email'];
                    $rr = $obj_clients_class->updateEmail2($old_email, $new_emails);
                    if($rr == true){
                        $countt++;
                    }    
                }
                
                
            //echo  $check['email'].' ~ '.str_replace($s_email,change2($check['email']), $match['email']).' <br>';
            
            //foreach($temE as $tt){
              //  $s_email = change($tt);
                //if($match['email'] != $check['email'])
                    {
                       //echo  $check['email'].' ~ '.str_replace($s_email,change2($check['email']), $match['email']).' <br>';
                    }
           // }
            //echo "<br>";
        }
    }*/
    
    /*echo '<pre>';
    echo $countt;
    //print_r($match); 
    echo '</pre>'; 
    */
   
?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1>Clients</h1>
                </div>
                  <div class="col-md-4 col-sm-12">
			        <a href="client_controls.php" style="cursor: pointer;" class="btn btn-primary btn-o add-event"> Add New </a>
                </div>
            </div>
        </div>
        
        <ol class="breadcrumb white">
          <li><a href="dashboard.php">Dashboard</a></li>
          <li class="active">Clients</li>
          <?php  if($_SESSION['user_role_session']=="Admin"   ){ ?>
            <li class="right"><a href="client_import.php">Import Clients</a></li> 
          <?php  } ?>
        </ol>
</div>

<style>
    .modal-body table td{
        word-wrap: break-all;
        white-space: pre-wrap !important;
    } 
    strong {font-weight: 600; font-size: 13px;}

@media (min-width: 768px){
    .client-modal {
        width: 800px !important;
        margin: 30px auto;
    } 
}
.modal-body tr td {
    border-top: 1px solid #ddd !important;
}


.pointer{ cursor: pointer;}
</style>

<?php 
/*        echo "<pre>";
        print_r($all_clients);
        echo '</pre>';
   */     
?>
  	<div class="col-sm-12 form_inner">
			<div class="col-md-12 table_inner" style="padding:0;">
                <div class="table-responsive">
                    <table id="example" class="display nowrap lisitng_table" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                            	<th>Sr. </th>
                                <th style=" width:30% !important;">Company Name </th>
                                <th>Categories</th>
                                <!--<th>Product Category</th>-->
                                <th>City</th>
                                <th>Modify</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php $i=1; foreach($all_clients as $cat){ ?>
                            <tr>
                            	<td><?php echo $i ; ?></td>
                                <td><?php echo $cat['company_name']?> </td>
                                <td><?php echo $obj_category_class->getCategoriesName($cat['categories']);?></td>
                                <!--<td><?php // echo $cat['products']?> </td>-->
                                <td><?php echo $cat['city']?></td>
                                
                                <td>
                                    
<a data-toggle="modal" data-target="#view_<?php echo ($cat['client_id'])?>" style="cursor: pointer;"> View Details  </a> |
 <?php  
//if( isset($cat['persons'])>0)
    {  
//if( count($cat['persons'])>0)
        ?> 
<!-- Modal -->
<div id="view_<?php echo ($cat['client_id'])?>" class="modal fade" role="dialog">
  <div class="modal-dialog client-modal">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Client Details</h4>
      </div>
      <div class="modal-body">
        <table style="table-layout: fixed; width: 100%">
        <tr>
            <td width='50%' style="border-top:none !important; "><small>Company: </small> <strong> <?php echo $cat['company_name'] ?></strong> </td>
           <td width='50%' style="border-top:none !important; "><small>Owner's Name: </small> <strong> <?php echo $cat['owner_name'] ?></strong> </td>
        </tr>
            
        <tr>
                <td width='50%'><small>Categories: </small> <strong> <?php echo $obj_category_class->getCategoriesName(str_replace(', ', ',', $cat['categories'])); ?></strong></td>
                 <td width='50%'> <small>Website: </small> <strong> <?php echo $cat['website'] ?></strong> </td>
        </tr>
        <tr> 
             <td width='100%' colspan="2"><small>Products: </small> <strong> <?php echo $cat['products'] ?></strong></td>
        </tr>
        <tr>
            <td width='100%' colspan="2"><small>Office Address: </small> <strong> <?php echo $cat['office_add'] ?></strong> </td>
        </tr>
        <tr>  <td width='50%'><small>City: </small> <strong> <?php echo $cat['city'] ?></strong></td>
              <td  width='50%'> <small>Country: </small> <strong> <?php echo $cat['country'] ?></strong> </td>
        </tr>
        <tr>  <td colspan="2"><small>Lead Source: </small> <strong> <?php echo $cat['lead_source'] ?></strong></td>
        </tr>

        <tr>  <td width='50%'><br></td>
              <td  width='50%'> <br></td>
        </tr>
            
            
            <?php $cc=1; 
            
            if(isset($cat['persons']))
            if(count($cat['persons'])>0)
            foreach($cat['persons'] as $cat2){ ?>
            <tr> <td colspan="2" width="100%" style="background-color: #eee;"> <strong>Contact Persons <?php echo $cc++; ?></strong></td> </tr>
            <tr>
                <td width='50%'> <small>Name: </small> <strong> <?php echo $cat2['f_name'].' '.$cat2['l_name']; ?></strong> </td>
                <td width='50%'><small>Designation: </small> <strong> <?php echo $cat2['designation'] ?></strong></td>
            </tr>
            <tr>
                <td width='50%' colspan="2"> <small class="pointer btn p_button" onclick="copier('_<?php  echo $cat2['person_id'];  ?>_email');">Email: </small> <strong  id="_<?php  echo $cat2['person_id'];  ?>_email">   <?php echo str_replace(", ", ", ", $cat2['email']); ?> </strong> </td>
            </tr>
            
            <tr>
                <td width='50%'><small class="pointer btn p_button" onclick="copier('_<?php  echo $cat2['person_id'];  ?>_mobile');">Mobile: </small> <strong id="_<?php  echo $cat2['person_id'];  ?>_mobile"> <?php echo str_replace(", ", ", ",$cat2['mobile']); ?></strong></td>
                
                <td width='50%'> <small class="pointer" onclick="copier('_<?php  echo $cat2['person_id'];  ?>_phone');">Land Line: </small> <strong id="_<?php  echo $cat2['person_id'];  ?>_phone"> <?php echo str_replace(", ", ", ",$cat2['landline']); ?></strong> </td>
            </tr>
            <tr>
                <td width='50%' colspan="2" ><small>Ext: </small> <strong> <?php echo str_replace(", ", ", ",$cat2['line_ext']); ?></strong></td>
            </tr>            
            
            <?php } ?>
            
            
        </table>
        
        
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>                                    
<?php    } ?>
        <a href="client_view.php?edit=<?php echo base64_encode($cat['client_id'])?>"> Assign Exhi. </a> | 
        <a href="client_controls.php?edit=<?php echo base64_encode($cat['client_id'])?>"> Edit  </a> |
        <a href="clients.php?del=<?php echo base64_encode($cat['client_id'])?>" onclick="return window.confirm('Are you sure you want to Deactive this Client?'); " > Delete  </a>
                                </td>
                                <td></td>
                            </tr>
                        <?php   $i++; } 
                        ?> 
                        </tbody>
    				</table>
                </div>
            </div>
		</div>


<?php include("_includes/footer.php"); ?>
<script src="js/clipboard.js"></script>
<script>
$(document).ready(function() {
    $('#example').DataTable(); 
	
	/* {
        responsive: {
            details: {
                type: 'column',
                target: -1
            }
        },
        columnDefs: [ {
            className: 'control',
            orderable: false,
            targets:   -1
        }]
    });*/
	
});

 
function copier(tt){
            var vall = $("#"+tt).html();
            clipboard.copy(vall);
        }
         
</script>
<script>
document.getElementById("plain-copy").addEventListener("click", function() {
    
  
  
});
</script>