<?php include("_config/dbconfig.php");
include("_includes/head.php");

$total_person = "";
$CompanyName = "";
$OwnerName = "";
$categories = array() ;
$website = "";
$products ="";
$address = "";
$city = "";
$country = "";
$lead_source = "";
$member_of = '';

function removeComma($string){
    return substr($string, 0, strlen($string)-2 );
}
if(isset($_SESSION['user_session'])){
    $agent_id = $_SESSION['user_session'];
} 

$cities = $obj_jobs_class->getCities();
  
if($_SESSION['user_role_session']=="Admin"){
            $event_all = $obj_events_class->getAgetEvent();      
  }else {
            $event_all = $obj_events_class->getAgetEvent($agent_id);
  }
  
$total_person_contacts = 0;


if(isset($_POST['assign_event'])){
    
    $event_id = base64_decode($_POST['event']);
    $client_id = base64_decode($_POST['client']);
    
    $res  = $obj_jobs_class->AssignClientsToEvent($event_id, $client_id, $agent_id);
    
    if($res<1){
        $mess = 0; "";
    }else {
        $mess = 1;  "";
    }
    $_SESSION['asign_mesg'] = $mess;
    
    echo "<script> window.location= 'client_view.php?edit=".base64_encode($client_id)."'</script>";
    die();
}



    if(isset($_GET['edit']) && !empty($_GET['edit'])){
    
            $id_        = base64_decode($_GET['edit']);
            $client     = $obj_clients_class->getClients($id_);
            
            $obj_category_class->getCategoriesName($client['categories']);
            
            if(isset($client['persons']))
            $total_person = count($client['persons']);
            $CompanyName = $client['company_name'];
            $OwnerName = $client['owner_name'];
            $categories_client = explode(", ", $client['categories']) ;
              
            $website = $client['website'];
            $products = $client['products'];
            $address = $client['office_add'];
            $city = $client['city'];
            $country = $client['country'];
            $fax = '';
            $add_by = '0';  
            $lead_source = $client['lead_source'];
            $page_title = "View Client";
            $member_of = $client['member_of'];
            
            echo '<title>Client View </title>';
            
            $total_person_contacts = 0;
            // die(); 
       } 
    else { 
         echo "<script> window.location= 'clients.php'</script>";
         die();
    }
         $categories = $obj_category_class->getAllCategories();     

?>
<style>
.view-box{
        border: none !important;
        font-weight: bold !important;
        background: #eee !important;
        min-height: 34px;
        height: auto;
}
    
</style>

<?php include("_includes/header.php"); ?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1><?php echo $page_title; ?></h1>
                </div>
            </div>
        </div>
        
        
     <ol class="breadcrumb white">
          <li><a href="dashboard.php">Dashboard</a></li>
          <li><a href="clients.php">Clients</a></li>
          <li class="active"><?php echo $page_title; ?></li>
    </ol>
    
    
</div>


<div class="col-sm-12 form_inner">
    
                <div class="row form-box">
                     <form  id="" method="post" action="" enctype="multipart/form-data">
                        <div class="col-md-3 col-sm-3">
                            <label for="CompanyName"><h4>Assign client to Exhibition</h4></label>
                        </div>
                         <div class="clearfix"></div> 
                        <div class="col-md-3 col-sm-3 form-group">
                                
                        <select class="chosen-select form-control" style="width:100%;" id="exi_id" tabindex="2" name="event" required="required" onchange="goEvent();">
                        <option value="">---Select Exhibition---</option>
                        <?php foreach($event_all as $tl){   $sel = '';
                            if (isset($_GET['event']) && !empty($_GET['event'])) {
                            $ev_id = base64_decode($_GET['event']);
                            if($ev_id == $tl['event_id']){$sel = "selected='selected'";}
                            }
                            ?>
                        <option value="<?php echo base64_encode($tl['event_id']);?>" <?= $sel ?>><?php echo $tl['event_name'];?></option>
                        <?php } ?>
                        </select>
                            <input type="hidden" value="<?php echo $_GET['edit']?>" name="client">
                        </div>
                         <div class="col-md-3 col-sm-3 form-group">
                             <input type="submit" value="Assign" name="assign_event" class="btn  btn-default" style="margin: 0px;" >
                         </div>
                         <div class="clearfix"></div>
                         
                         <?php if(isset($_SESSION['asign_mesg']) && $_SESSION['asign_mesg']==1 ){
                                    echo "<span class='text-green'> Client assigned successfully.  </span>";
                                }  
                                else if(isset($_SESSION['asign_mesg']) && $_SESSION['asign_mesg']==0 ){
                                    echo "<span class='text-red'> This client Already assigned.</span>";
                                }
                                
                         
                         ?>
                         
                             
                         
                         
                         
                         
                     </form>
                     <div class="clearfix"></div>
                    <hr>
                    <form  id="_form" method="post" action="" enctype="multipart/form-data">
		
                        <div class="col-md-4 col-sm-6 form-group">
                                <label for="CompanyName">Company Name</label>
                                <div class="view-box  form-control "><?= $CompanyName ?></div>
                        </div>
                        <div class="col-md-4 col-sm-6 form-group">
                                <label for="Owner">Owner's Name</label>
                                <div class="view-box  form-control "><?= $OwnerName ?></div> 
                        </div>
                    
                    
                        <div class="col-md-4 col-sm-6 form-group">
                                <label for="Website">Category</label> 
                                
                                <div class="view-box  form-control ">
                            <?php foreach($categories as $cats){
                                if(isset($cats['sub_cat']) && count($cats['sub_cat'])>0){
                                ?>
                                <?php foreach($cats['sub_cat'] as $cats2){$sel = "";
                                            if(in_array($cats2['cat_id'], $categories_client)){ echo $cats2['category'];}
                                    ?>
                                        <?php  ?>
                                
                                <?php } ?>
                                    
                            <?php }else {?>
                                    
                            <?php } }?>
                        </div>      
                                 
                        </div>
                        
                    <div class="clearfix"></div>
                        
                    <div class="col-md-4 col-sm-6 form-group">
                            <label for="Website">Website</label>
                            <div class="view-box  form-control "><?= $website?></div>
                    </div>
                    
                    <div class="col-md-4 col-sm-6 form-group">
                        <label for="Products">Products</label> 
                        <div class="view-box  form-control "><?= $products?></div>
                    </div>    
                    
                     <div class="col-md-4 col-sm-6 form-group">
                            <label for="address">Office Address</label>
                            <div class="view-box  form-control "><?= $address?></div> 
                    </div>
                    <div class="clearfix"></div>
                    
                    <div class="col-md-8" style="padding: 0px;">
     
                    <div class="col-md-4 col-sm-4 form-group">
                        <label for="City">City</label> 
                        <div class="view-box  form-control "> 
                        <?php foreach ($cities as $citii){
                                if(strtolower($citii['city']) == strtolower($city)){
                                    echo $city;
                                } 
                            } ?>
                        </div> 
                    </div>

                    <div class="col-md-4 col-sm-4 form-group">
                            <label for="ContactNum">Country</label> 
                            <div class="view-box  form-control ">Pakistan</div> 
                    </div>
                    
                    <div class="col-md-4 col-sm-4 form-group">
                            <label> Lead Source </label>
                            <div class="view-box  form-control "><?= $lead_source ?></div>   
                    </div>
                    </div>
                    <div class="col-md-4 col-sm-4 form-group">
                            <label> Member Of</label>
                            <div class="view-box  form-control "><?= $member_of ?></div>   
                    </div>
                    
                    
                    

                    <div class="clearfix"></div>
                    <div class="col-md-12 col-sm-12 form-group">
                        <h4>Contact Persons </h4>                        
                    </div>
                    <div class="clearfix"></div>   
                    
            <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?> 
                    <input type="hidden" value="<?php echo $total_person; ?>" id="total_person" name="total_person" >  
                    
            <?php $ii = 0;  
            if(isset($client['persons']))
            foreach($client['persons'] as $person){ $ii++; ?>

                    <div id="person_<?= $ii?>_row" class="client_border">
                        
                    <div class="col-md-5 col-sm-12 form-group">
                        
                        <div class="col-md-4 col-sm-12 form-group no-padds">
                                <label for="">First Name</label>
                                <div class="view-box  form-control "><?php echo $person['f_name']?></div>
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Last Name</label>
                                <div class="view-box  form-control "><?php echo $person['l_name']?></div>
                                
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Designation</label>
                                <div class="view-box  form-control "><?php echo $person['designation']?></div>
                        </div>
                    </div>

<div class="col-md-7 col-sm-12 form-group">
<div class="clearfix"></div>                        
                        <div id="more_contact_<?=$ii ?>_">

<?php   $_emails = explode(", ", $person['email']);
        $_mobile = explode(", ", $person['mobile']);
        $_landline = explode(", ", $person['landline']);
        $_line_ext = explode(", ", $person['line_ext']);
        $contact_person_row=0;
        //print_r($_emails);
        for($innr = 1; $innr<4; $innr++ ){

        if(isset($_emails[$innr-1]) ||  isset($_mobile[$innr-1]) || isset($_landline[$innr-1]) || isset($_line_ext[$innr-1]) ){ ?>
        <div id="row_<?= $innr-1;?>_" class="">
        <div class="col-md-3 col-sm-12 form-group">
                <label >Email</label>
                <div class="view-box  form-control "><?php if(isset($_emails[$innr-1])) echo $_emails[$innr-1]; ?></div>
                
        </div>

        <div class="col-md-3 col-sm-12 form-group">
                <label>Mobile #</label>
                <div class="view-box  form-control "><?php if(isset($_mobile[$innr-1])) echo $_mobile[$innr-1] ?></div>
                
        </div>                    

        <div class="col-md-3 col-sm-12 form-group">
                <label>Land Line #</label>
                <div class="view-box  form-control "><?php if(isset($_landline[$innr-1])) echo  $_landline[$innr-1] ?></div>
                
        </div>

        <div class="col-md-2 col-sm-12 form-group">
                <label for="Address">Ext # </label>
                
                <div class="view-box  form-control "><?php if(isset($_line_ext[$innr-1])) echo $_line_ext[$innr-1] ?></div>
                
        </div>
          
            
  <?php
        
        if($innr > 1){}  ?>
        </div>
    <?php if($innr == 1){?>

    <?php } 
                $contact_person_row++;
        }
        
        $total_person_contacts += ($innr-1);
        
    } ?><div class="clearfix"></div>
        
</div>
                        
                    <div class="clearfix"></div>
               </div>
               <div class="clearfix"></div> 
           </div>
            <?php } } ?>

            <div class="clearfix"></div> 
            
            
   
                         </form>                
		</div>
           
		</div>

<?php include("_includes/footer.php"); ?>

<script>
    
    $(document).ready(function(){
           $("#_form").validate();
       });

function thumbImg(){
	
            var imgg = $("#profile_img").val();
            arr = imgg.split(".");
            imgg = arr[1].toLowerCase();

            if(imgg == "jpg" || imgg =="jepg"  ||  imgg =="png"  || imgg =="gif"){
                $("#old_img").fadeOut(300);                    
            }
            else {
                    alert('Please select the valid file or format!');
                    $("#thumb_img").focus();
                    $("#thumb_img").val('');
            }
	}

var roww = <?= $total_person_contacts; ?>;

function addMoreContact(no_contact){
        var ro_val   = '';
        var ro_val11 = '';
        roww = roww+1;
        var ro_val11 = $("#person_"+no_contact+"_contact").val();
        
        console.log(ro_val11+"****************");
        
        var ro_val = parseInt(ro_val11)+1;
        $("#person_"+no_contact+"_contact").val(ro_val);
        
        var content =  '<div id="row_'+roww+'_" class="  form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control" type="email" name="person_'+no_contact+'_email'+ro_val+'" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control digits  " maxlength="12"  type="text" name="person_'+no_contact+'_mobile'+ro_val+'" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Land Line #</label><input class="form-control digits  " maxlength="12" type="text" name="person_'+no_contact+'_contact'+ro_val+'" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext # </label><input class="form-control " type="text digits  " maxlength="4" minlength="3"   name="person_'+no_contact+'_ext'+ro_val+'" value="" ></div><a class="col-md-1" style="float: right; font-size: 16px; cursor: pointer; padding: 5px;" onclick="remove_contact('+roww+', '+no_contact+');"><br><i class="fa fa-times"></i></a> <div class="clearfix"></div></div>';
        $( "#more_contact_"+no_contact+"_" ).append(content);
        if(ro_val>2){
            $("#person_"+no_contact+"_row #add_more").fadeOut(500);
        }
}


function remove_contact(row_id, person_row){
            
            $( "#person_"+person_row+"_row #row_"+row_id+"_" ).fadeOut(500, function(){
                        $( "#row_"+row_id+"_" ).html('');
            })

        var ro_val11 = $("#person_"+person_row+"_contact").val();
        console.log(ro_val11);
        
        ro_val= parseInt(ro_val11)-1;
        $("#person_"+person_row+"_contact").val(ro_val);
        
        if(ro_val<3){
            $("#person_"+person_row+"_row #add_more").fadeIn(500);
        }
}


function addPerson(){
    
    person  = $("#total_person").val();
    person_row = parseInt(person) + 1;
    $("#total_person").val(person_row);
        
    var new_content = '<div id="person_'+person_row+'_row" class="client_border" ><div class="col-md-5 col-sm-12 form-group"><div class="col-md-4 col-sm-12 form-group no-padds"><label >First Name</label><input class="form-control required" type="text" name="person'+person_row+'_f_name" value=""  ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Last Name</label><input class="form-control required" type="text" name="person'+person_row+'_l_name" value="" ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Designation</label><input class="form-control " type="text" name="person'+person_row+'_designation" value="" ></div></div><div class="col-md-7 col-sm-12 form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control " type="email" name="person_'+person_row+'_email1" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control digits" maxlength="12" type="text" name="person_'+person_row+'_mobile1" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Land Line #</label><input class="form-control digits" maxlength="12" type="text" name="person_'+person_row+'_contact1" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext </label><input class="form-control digits" maxlength="4"  minlength="3" type="text" name="person_'+person_row+'_ext1" value="" ></div><div class="clearfix"></div><div id="more_contact_'+person_row+'_"></div><div class="clearfix"></div><a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px;" onclick="addMoreContact('+person_row+');">Add More</a><div class="clearfix"></div></div><input type="hidden" id="person_'+person_row+'_contact" value="1"><div class="clearfix"></div> <a class="remove-btn" onclick="removePerson('+person_row+');">Remove Person</a></div>';
    
    $( "#more_persons").append(new_content);
}

function removePerson(remId){
        
            //alert(remId);
            $("#person_"+remId+"_row").remove();
            /*person  = $("#total_person").val();
            person_row = parseInt(person) - 1;
            $("#total_person").val(person_row);*/
            
}

function delPerson(remId, idd){ 
    
        var aaaa = confirm("Are you sure you want to Delete!"); 
        console.log(aaaa);
        if(aaaa == true){
            $.post('ajax.php?del_person='+idd+'&a=1', function(data){
                    $("#person_"+remId+"_row").remove();
                    console.log(data);
            })
        }
        
}

</script>
<?php  /* ?>
<div id="person_'+person_row+'_row"><div class="col-md-5 col-sm-12 form-group"><div class="col-md-4 col-sm-12 form-group no-padds"><label >First Name</label><input class="form-control required" type="text" name="person'+person_row+'_f_name" value=""  ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Last Name</label><input class="form-control required" type="text" name="person'+person_row+'_l_name" value="" ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Designation</label><input class="form-control required" type="text" name="person'+person_row+'_designation" value="" ></div></div><div class="col-md-7 col-sm-12 form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control required" type="email" name="person_'+person_row+'_email" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control required" type="text" name="person_'+person_row+'_mobile" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Contact #</label><input class="form-control " type="text" name="person_'+person_row+'_contact" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext </label><input class="form-control" type="text" name="person_'+person_row+'_fax" value="" ></div><div class="clearfix"></div><div id="more_contact_'+person_row+'_"></div><div class="clearfix"></div><a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px;" onclick="addMoreContact('+person_row+');">Add More</a><div class="clearfix"></div></div><input type="hidden" id="person_'+person_row+'_contact" value="1"><div class="clearfix"></div> </div>
<?php */ ?>

<?php if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
<script>
$(document).ready(function(){ 
        $('.form-control').attr('readonly', 'readonly'); 
   });
</script>
<?php } ?>