   <?php
   // CRUD 
   @$getId = base64_decode(anti_injection($_GET['id']));  
    if(@$getId){ 
        $query = mysql_query("SELECT * FROM products WHERE id = + $getId");
        $result = mysql_fetch_array($query);
    }
    
    if(isset ($_POST['save'])){
	$server = $_POST['server'];
	$type = $_POST['type'];
	$group = $_POST['group'];
	$product = $_POST['product'];
	$module = $_POST['module'];
	$email_template = $_POST['email_template'];
	$description = $_POST['description'];
	$automation = $_POST['automation'];
	$price = $_POST['price'];
	$package = $_POST['package'];
	$status = $_POST['status'];
	
	 $crud = new crud('products');  // table
     $crud->inserir("server,p_type,p_group,product,module,email_template,description,automation,price,package,status", "'$server','$type','$group','$product','$module','$email_template','$description','$automation','$price','$package','$status'");
	
	
	header("Location: index.php?mod=Products&y=1"); // redirect send mail ok
   
	
	}
	
	if(isset($_POST['update'])) {
	$server = $_POST['server'];
	$type = $_POST['type'];
	$group = $_POST['group'];
	$product = $_POST['product'];
	$module = $_POST['module'];
	$email_template = $_POST['email_template'];
	$description = $_POST['description'];
	$automation = $_POST['automation'];
	$price = $_POST['price'];	
	$package = $_POST['package'];	
	$status = $_POST['status'];
	$registerID = $_POST['registerID'];
	
	
	$crud = new crud('products'); // Table
    $crud->atualizar("server='$server',p_type='$type',p_group='$group',product='$product',module='$module',package='$package',email_template='$email_template',description='$description',automation='$automation',price='$price',status='$status'", "id='$registerID'");
	
	header("Location: index.php?mod=Products&y=2");
	}
	
    if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = base64_decode($_GET['id']); 
    $crud = new crud('products'); // Table
    $crud->excluir("id = $id"); // ID
    
    header("Location: index.php?mod=Products&y=3");
    }
    
    
    ?>
    <!-- JAVASCRIPT -->
     <script>
     $().ready(function() {

		$("#CustomerForm").validate({
			rules: {
				price: "required",
				server: "required",
				group: "required",
				product: {
					required: true,
					minlength: 4
				},
				
			},
			messages: {
				price: "<?php echo $lang['VALIDATE_PRICE']; ?>",
				server: "<?php echo $lang['VALIDATE_REQUIRED']; ?>",
				group: "<?php echo $lang['VALIDATE_REQUIRED']; ?>",
				product: {
					required: "<?php echo $lang['VALIDATE_PRODUTO']; ?>",
					minlength: "<?php echo $lang['VALIDATE_PRODUTO_MIN']; ?>"
				}
				
			}
		});

	});
	
	</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_PRODUCT']; ?>
            <small><?php echo $lang['NEW_PRODUCT']; ?></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['PRODUCT']; ?></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['SERVER']; ?></label>
                      <select id="server" name="server" id="server" class="form-control select2" style="width: 100%;" id="passaValor" onchange="getValor(this.value, 0)" required>
						  <option value="">Select</option>
                    <?php
					$catsr = mysql_query("SELECT * FROM servers order by id DESC");
					while($srv = mysql_fetch_array($catsr)){ 
					?>
                    <option value="<?php echo $srv['id']; ?>" <?php if (@$result['server'] == $srv['id']) { echo "selected"; } ?>><?php echo $srv['server']; ?> - <?php echo $srv['ip']; ?> - <?php echo $srv['module']; ?></option>
				    <?php } ?> 
				    </select>
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['PRODUCT_GROUP']; ?></label>
                      <select id="group" name="group" id="group" class="form-control select2" style="width: 100%;" required>
						  <option value="">Select</option>
                    <?php
					$catgp = mysql_query("SELECT * FROM groups order by id DESC");
					while($grp = mysql_fetch_array($catgp)){ 
					?>
                    <option value="<?php echo $grp['id']; ?>" <?php if (@$result['p_group'] == $grp['id']) { echo "selected"; } ?>><?php echo $grp['name']; ?></option>
				    <?php } ?> 
				    </select>
                    </div>
                    
                     <div class="form-group">
                      <label for="Input"><?php echo $lang['TYPE']; ?></label>
                    <select name="type" class="form-control">
                    <option value="1" <?php if (@$result['p_type'] == '1') { echo "selected"; } ?>><?php echo $lang['TYPE_OPTION1']; ?></option>
                    <option value="2" <?php if (@$result['p_type'] == '2') { echo "selected"; } ?>><?php echo $lang['TYPE_OPTION2']; ?></option>
                    <option value="3" <?php if (@$result['p_type'] == '3') { echo "selected"; } ?>><?php echo $lang['TYPE_OPTION3']; ?></option>
                    <option value="4" <?php if (@$result['p_type'] == '4') { echo "selected"; } ?>><?php echo $lang['TYPE_OPTION4']; ?></option>
                    </select>
                    </div> 
					  
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['PRODUCT']; ?></label>
                      <input type="text" name="product" value="<?php echo @$result['product']; ?>" id="product" class="form-control" placeholder="<?php echo $lang['PRODUCT_PLACEHOLDER']; ?>">
                    </div>
                    
                    <script type="text/javascript">
					function getValor(valor){
						$("#recebeValor").html("<option value='0'>Loading...&nbsp;&nbsp;</option>");
						setTimeout(function(){
						$("#recebeValor").load("ajaxValor.php",{id:valor})
						}, 1000);
					};
					</script>
                    
                    <div class="form-group">
                      <label for="Input">Package cPanel</label>
                      
                      <select name="package" class="form-control" id="recebeValor">
					    <?php if (@$result['id'] <> '') { ?>
						<?php
						$pkp = $result['package'];
						$packages = mysql_query("SELECT * FROM packages WHERE name = '$pkp'");
						$srpackage = mysql_fetch_array($packages);
						?>
						<option value="<?php echo $srpackage['name']; ?>"><?php echo $srpackage['name']; ?></option>
						<? } else { ?>
						<option value="">Select Server&nbsp;&nbsp;</option>
						<?php } ?>
						</select>	  
					  </select>
                      </div>
                    
                    
                    
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['PRICE']; ?></label>
                      <input type="text"  class="form-control" onKeyUp="moeda(this);" name="price" id="price" value="<?php echo @$result['price']; ?>">
                    </div>
                    
                   <div class="form-group">
                      <label for="Input"><?php echo $lang['STATUS']; ?></label><br>
                      <label for="S"><input id="S" type="radio" name="status" value="S" <?php if (@$result['status'] == 'S') { echo "checked"; } if (@$result['status'] == '') { echo "checked"; } ?>> <?php echo $lang['ACTIVE']; ?></label>
                      <label for="N"><input id="N" type="radio" name="status" value="N" <?php if (@$result['status'] == 'N') { echo "checked"; } ?>> <?php echo $lang['BLOCK']; ?></label>
                    </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['DETAILS']; ?></h3>
                </div><!-- /.box-header -->
                <!-- form start -->
   
                  <div class="box-body">
					 
					<div class="form-group">
                      <label for="Input"><?php echo $lang['DESCRIPTION']; ?></label>
                    <textarea name="description" id="msg" class="form-control"><?php echo $result['description']; ?></textarea>
                    </div>
                    
                    <div class="form-group">
                      <label for="Input"><?php echo $lang['AUTOMATION']; ?></label>
                    <select name="automation" class="form-control">
                    <option value="1" <?php if (@$result['automation'] == '1') { echo "selected"; } ?>><?php echo $lang['AUTO_OPTION1']; ?></option>
                    <option value="2" <?php if (@$result['automation'] == '2') { echo "selected"; } ?>><?php echo $lang['AUTO_OPTION2']; ?></option>
                    <option value="3" <?php if (@$result['automation'] == '3') { echo "selected"; } ?>><?php echo $lang['AUTO_OPTION3']; ?></option>
                    </select>
                    </div> 
					
					<div class="form-group">
                      <label for="Input"><?php echo $lang['EMAIL_TEMPLATE']; ?></label>
                    <select id="email_template" name="email_template" class="form-control" style="width: 100%;" required>
                    <?php
					$cattp = mysql_query("SELECT * FROM email_template order by id DESC");
					while($tpl = mysql_fetch_array($cattp)){ 
					?>
                    <option value="<?php echo $tpl['id']; ?>" <?php if (@$result['email_template'] == $tpl['id']) { echo "selected"; } ?>><?php echo $tpl['name']; ?> - <?php echo $tpl['subject']; ?> - <?php echo $tpl['from_mail']; ?></option>
				    <?php } ?> 
				    </select>
                    </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 -->
   
   
   
   
