<?php

    $getId = base64_decode(anti_injection($_GET['id']));  
    if(@$getId){ 
        $query = mysql_query("SELECT * FROM customers WHERE id = + $getId");
        $result = mysql_fetch_array($query);
    }
    
    if(isset ($_POST['save'])){
	$name = $_POST['name'];
	$company = $_POST['company'];
	$cpf_cnpj = $_POST['cpf_cnpj'];
	$email = $_POST['email'];
	$pass = md5($_POST['pass']);
	$passemail = $_POST['pass'];
	$salt = base64_encode($_POST['pass']);
	$phrase = $_POST['phrase'];
	$address = $_POST['address'];
	$number = $_POST['number'];
	$neighborhood = $_POST['neighborhood'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipcode = $_POST['zipcode'];
	$obs = $_POST['obs'];
	$status = 'S';
	
	$crud = new crud('customers');  // table
    $crud->inserir("name,company,cpf_cnpj,email,pass,salt,phrase,address,number,neighborhood,city,state,zipcode,obs,status", "'$name','$company','$cpf_cnpj','$email','$pass','$salt','$phrase','$address','$number','$neighborhood','$city','$state','$zipcode','$obs','$status'");
	
	// SEND MAIL 
	require_once("config/mail/class.phpmailer.php");
	$query_tpl = mysql_query("SELECT * FROM email_template WHERE id = '16'");
    $template_mail = mysql_fetch_array($query_tpl);
	
	$mail = new PHPMailer();
	$mail->IsSMTP();		
	$mail->SMTPDebug = 0;		
	$mail->SMTPAuth = true;		
	$mail->IsHTML(true);
	$mail->Host = $config['smtp_host'];	
	$mail->Port = $config['smtp_port'];		
	$mail->Username = $config['smtp_user'];	
	$mail->Password = $config['smtp_pass'];	

	$mail->From = $template_mail['from_mail'];  
	$mail->FromName = $template_mail['name_from']; 

	$mail->AddAddress($email);
	$mail->IsHTML(true); 
	$mail->CharSet = 'iso-8859-1'; 
	$mail->Subject  = $template_mail['subject']; 
	
	$corpoDoEmail = $template_mail['msg']; 
	$signature = $config['signature'];
	$logo = $config['logo'];
	
	// MAIL Variables
    $corpoDoEmail = str_replace( '%NOME%', $name, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%SENHA%',  $passemail, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%EMPRESA%',  $company, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%CPFCNPJ%',  $cpf_cnpj, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%ENDERECO%',  $address, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%CIDADE%',  $city, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%EMAIL%',  $email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%SIGNATURE%',  $signature, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%LOGO%',  $logo, $corpoDoEmail );
    
    // END Variables
	$mail->Body = $corpoDoEmail;
	$SendOK = $mail->Send();
	$mail->ClearAllRecipients();
	$mail->ClearAttachments();
	if ($SendOK) {
	header("Location: index.php?mod=Customers&y=1"); // redirect send mail ok
    } else {
	echo "<script>
        alert ('Error sending email, please check the e-mail settings!');
        document.location.href = ('index.php?mod=Customers&y=1');
	</script>";
    }
	// END SEND 
	
	}
	
	if(isset($_POST['update'])) {
	$name = $_POST['name'];
	$company = $_POST['company'];
	$cpf_cnpj = $_POST['cpf_cnpj'];
	$email = $_POST['email'];
	$pass = md5($_POST['pass']);
	$passemail = $_POST['pass'];
	$salt = base64_encode($_POST['pass']);
	$phrase = $_POST['phrase'];
	$address = $_POST['address'];
	$number = $_POST['number'];
	$neighborhood = $_POST['neighborhood'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipcode = $_POST['zipcode'];
	$obs = $_POST['obs'];
	$status = $_POST['status'];	
	$registerID = $_POST['registerID'];
	
	
	$crud = new crud('customers'); // Table
    $crud->atualizar("name='$name',company='$company',cpf_cnpj='$cpf_cnpj',email='$email',pass='$pass',salt='$salt',phrase='$phrase',address='$address',number='$number',neighborhood='$neighborhood',city='$city',state='$state',zipcode='$zipcode',obs='$obs'", "id='$registerID'");
	
	header("Location: index.php?mod=Customers&y=2");
	}
	
    if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = base64_decode($_GET['id']); 
    $crud = new crud('customers'); // Table
    $crud->excluir("id = $id"); // ID
    
    header("Location: index.php?mod=Customers&y=3");
    }
   
?>


    <!-- JAVASCRIPT -->
     <script>
     $().ready(function() {

		$("#CustomerForm").validate({
			rules: {
				
				company: "required",
				name: {
					required: true,
					minlength: 4
				},
				pass: {
					required: true,
					minlength: 5
				},
				pass2: {
					required: true,
					minlength: 5,
					equalTo: "#pass"
				},
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				company: "<?php echo $lang['VALIDATE_COMPANY']; ?>",
				name: {
					required: "<?php echo $lang['VALIDATE_NAME']; ?>",
					minlength: "<?php echo $lang['VALIDATE_NAME_MIN']; ?>"
				},
				pass: {
					required: "<?php echo $lang['VALIDATE_PASS']; ?>",
					minlength: "<?php echo $lang['VALIDATE_PASS_MIN']; ?>"
				},
				pass2: {
					required: "<?php echo $lang['VALIDATE_PASS']; ?>",
					minlength: "<?php echo $lang['VALIDATE_PASS_MIN']; ?>",
					equalTo: "<?php echo $lang['VALIDATE_PASS_CONFIRM']; ?>"
				},
				email: "<?php echo $lang['VALIDATE_EMAIL']; ?>"
			}
		});

	});
	
	</script>
    <!-- END -->  
      
    <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <?php echo $lang['TITLE_NEW_CUSTOMER']; ?>
            <small><?php echo $lang['NEW_CUSTOMER']; ?></small>
          </h1>
          
        </section>

       
       <!-- Main content -->
        <section class="content">
          <div class="row">
			  
			  
            <!-- left column -->
            <div class="col-md-6">
              <!-- general form elements -->
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title"><?php echo $lang['SLOGAN_REGISTER_CUSTOMER']; ?></h3>
                </div><!-- /.box-header -->
                <!-- form start -->
                <form role="form" method="POST" id="CustomerForm" name="CustomerForm">
				<input type="hidden" name="len" id="pass-len" value="8">
					
                  <div class="box-body">
					  
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['NAME']; ?></label>
                      <input type="text" name="name" value="<?php echo @$result['name']; ?>" id="name" class="form-control" placeholder="<?php echo $lang['NAME_PLACEHOLDER']; ?>">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['COMPANY']; ?></label>
                      <input type="text" class="form-control" name="company" id="company" value="<?php echo @$result['company']; ?>" placeholder="<?php echo $lang['COMPANY_PLACEHOLDER']; ?>">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['CPF_CNPJ']; ?></label>
                      <input type="text" class="form-control" name="cpf_cnpj" value="<?php echo @$result['cpf_cnpj']; ?>">
                    </div>
                   
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['EMAIL']; ?></label>
                      <input type="email" class="form-control" name="email" value="<?php echo @$result['email']; ?>" placeholder="<?php echo $lang['EMAIL_PLACEHOLDER']; ?>">
                    </div>
                   
                   <div class="form-group">
                   <label for="Input"><?php echo $lang['PASS']; ?></label>
                   <div class="input-group ">
					
                    <input type="text" class="form-control" name="pass" id="pass" value="<?php echo @base64_decode($result['salt']); ?>" placeholder="<?php echo $lang['PASS_PLACEHOLDER']; ?>">
                    <span class="input-group-btn">
                      <button class="btn btn-info btn-flat" name="generate" id="generatePassword" type="button">Go!</button>
                    </span>
                  </div>
                   </div>
                   
                   
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['PASS_CONFIRM']; ?></label>
                      <input type="text" class="form-control" name="pass2" id="pass2" placeholder="<?php echo $lang['PASS_CONFIRM_PLACEHOLDER']; ?>">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['PHRASE']; ?></label>
                      <input type="text" class="form-control" name="phrase" value="<?php echo @$result['phrase']; ?>">
                    </div>
                    
                    
                    
                   
                  </div><!-- /.box-body -->

                 
                
                
              </div><!-- /.box -->
           </div><!-- /.col -->
           
           
           <!-- left column -->
            <div class="col-md-6">
              <!-- general form elements -->
              <div class="box box-info">
                <div class="box-header with-border">
                  <h3 class="box-title"><?php echo $lang['SLOGAN_REGISTER_ADDRESS']; ?></h3>
                </div><!-- /.box-header -->
                <!-- form start -->
   
                  <div class="box-body">
					 
					<div class="form-group">
                      <label for="Input"><?php echo $lang['ADDRESS']; ?></label>
                      <input type="text" name="address" value="<?php echo $result['address']; ?>" class="form-control">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['NUMBER']; ?></label>
                      <input type="text" name="number" value="<?php echo $result['number']; ?>" class="form-control">
                    </div> 
					  
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['NEIGHBORHOOD']; ?></label>
                      <input type="text" name="neighborhood" value="<?php echo $result['neighborhood']; ?>" class="form-control">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['CITY']; ?></label>
                      <input type="text" name="city" value="<?php echo $result['city']; ?>" class="form-control">
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['STATE']; ?></label>
                      <input type="text" name="state" value="<?php echo $result['state']; ?>" class="form-control">
                    </div>
                    
                     <div class="form-group">
                      <label for="Input"><?php echo $lang['ZIPCODE']; ?></label>
                      <input type="text" name="zipcode" value="<?php echo $result['zipcode']; ?>" class="form-control">
                    </div>
                   
                  </div><!-- /.box-body -->

                  <div class="box-footer">
					  
		  <?php if (@$result['id'] <> '') { ?>

		  <input type="submit" value="<?php echo $lang['UPDATE']; ?>" name="update" style="text-transform:uppercase" class="btn btn-info btn-lg btn-block">
		  <input type="hidden" name="registerID" value="<?php echo @$result['id']; ?>"> 
		  <? } else { ?>
		  <input type="submit" value="<?php echo $lang['REGISTER']; ?>" name="save" style="text-transform:uppercase" class="btn btn-success btn-lg btn-block">
		  <?php } ?>
					  
                  </div>
                  
                 
                </form>
                
              </div><!-- /.box -->
           </div><!-- /.col -->
           
          </div><!-- /.row -->
        </section><!-- /.content -->
       
        
      </div><!-- /.content-wrapper -->
   
   
   
   
