<?php  
session_start();
require_once '../../Server/connection.php';
if($_POST){	
  $client_type = $_POST['client_type'];
  $client_id = $_POST['client_id'];
  
  if($client_type==1){
	  
  $clientFname = addslashes($_POST['clientFname']);
  $clientLname = addslashes($_POST['clientLname']);
  $client_tin = $_POST['client_tin'];
  $client_phone = $_POST['client_phone'];
  $n = substr($client_phone,1);
  $n1 = '255';	
  $clientPhone = $n1.$n;
  $client_source_id = $_POST['client_source'];
  $client_email = addslashes(strtolower($_POST['client_email']));
  $client_address = addslashes($_POST['client_address']);
  $upl_status = $_POST['upl_status'];
  
  $fromEmail = 'info@brightvision.co.tz';
  $toEmail = $client_email;
  $space = " ";
  $subject = "User Account Creation";
  
  $link = "<a href='https://cms.brightvision.co.tz/'>CMS</a>"; 
  
 
  $passwd = addslashes(strtoupper($clientLname.'123'));
  $password = md5(addslashes(strtoupper($clientLname.'123')));
  $message = "Hello Customer".$space.$clientFname.$space.$clientLname.$space."You have been registered into Bright Vision Customer Management System - CMS, You will login using your TIN".$space.$client_tin.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
  $subject = "User Account Creation";
  
   $sql = "UPDATE clients SET client_fname = '$clientFname',client_lname = '$clientLname',client_tin='$client_tin',client_phone='$clientPhone',client_email='$client_email',client_address='$client_address',clientcate_id='$client_type',client_source_id='$client_source_id' WHERE client_id = {$client_id}";		
   $result = $conn->query($sql);
  
  if($upl_status=='Yes'&&$result){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO clientdocs (client_id,fileName) 
				VALUES ('$client_id','$location')";

		$resFSql1 = $conn->query($fSql);
	          }
			
           if($resFSql1){			
		if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){	
					
				$_SESSION['success_message'] = "Customer Has Been Edited Successfully";
                       header("Location: ../editCustDetails.php?i=$client_id");
                             exit();
					
					
				}
                }				
			  
              }else{
				  
	if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){	
					
					$_SESSION['success_message'] = "Customer Has Been Edited Successfully";
                       header("Location: ../editCustDetails.php?i=$client_id");
                             exit();
					
					
				}	
				  
				  
			  }
	  
  }else{
	  
	  
  $compamyName = addslashes($_POST['company_name']);
  $client_tin = $_POST['company_tin'];
  $client_phone = $_POST['company_phone'];
  $n = substr($client_phone,1);
  $n1 = '255';	
  $clientPhone = $n1.$n;
  $client_source_id = $_POST['company_source'];
  $client_email = addslashes(strtolower($_POST['company_email']));
  $client_address = addslashes($_POST['company_address']);
  $upl_status = $_POST['upl_status']; 
  
  $fromEmail = 'info@brightvision.co.tz';
  $toEmail = $client_email;
  $space = " ";
  $subject = "User Account Creation";
  
  $link = "<a href='https://cms.brightvision.co.tz/'>CMS</a>";  
  
  
  $passwd = addslashes(strtoupper($compamyName.'123'));
  $password = md5(addslashes(strtoupper($compamyName.'123')));
  $message = "Hello Customer".$space.$compamyName.$space."You have been registered into Bright Vision Customer Management System - CMS, You will login using your TIN".$space.$client_tin.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
   $subject = "User Account Creation";
   
   $sql = "UPDATE clients SET client_fname = '$compamyName',client_tin='$client_tin',client_phone='$clientPhone',client_email='$client_email',client_address='$client_address',clientcate_id='$client_type',client_source_id='$client_source_id' WHERE client_id = {$client_id}";		
   $result = $conn->query($sql);

  if($upl_status=='Yes'&&$result){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO clientdocs (client_id,fileName) 
				VALUES ('$client_id','$location')";

		$resFSql1 = $conn->query($fSql);
	          }
			
           if($resFSql1){			
		if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){	
					
				$_SESSION['success_message'] = "Customer Has Been Edited Successfully";
                       header("Location: ../editCustDetails.php?i=$client_id");
                             exit();
					
					
				}
                }				
			  
              }else{
				  
	if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){	
					
					$_SESSION['success_message'] = "Customer Has Been Edited Successfully";
                       header("Location: ../editCustDetails.php?i=$client_id");
                             exit();
					
					
				}	
				  
				  
			  }  
	  
	  
  }
  

	

}