   <?php
   // CRUD 
   @$getId = base64_decode(anti_injection($_GET['id']));  
    if(@$getId){ 
        $query = mysql_query("SELECT * FROM requests WHERE id = + $getId");
        $result = mysql_fetch_array($query);
    }
    
    if(isset ($_POST['update'])){
	$payment = $_POST['payment']; 
	$status = $_POST['status']; 
	$idRT = $_POST['idRT'];
    $registerID = $_POST['registerID'];
	$crud = new crud('requests');  // table
    $crud->atualizar("payment='$payment',status='$status'", "id='$registerID'");
	
	if($status == 'A') { 
	header("Location: index.php?mod=Orders&y=2"); // redirect send mail ok
    }
    if($status == 'S') { 
		
	$queryR = mysql_query("SELECT * FROM requests WHERE id = '$registerID'");
    $resultD = mysql_fetch_array($queryR);
    
    $customer = $resultD['customer'];
    $date = date('Y-m-d');
    
    $q = mysql_query("SELECT SUM(amount * qtd) as amount FROM requests_tbl WHERE request = '$registerID'");
 	$r = mysql_fetch_array($q);
	$totalorder = number_format($r["amount"],2,'.','');
    
    $dvc = $config['dias_faturas'];
	$payment_date = date('Y-m-d', strtotime("+$dvc days"));
	$arrayData = explode("-",$payment_date);
    $ano = $arrayData[0];
    $mes = $arrayData[1];
    $dia = $arrayData[2];
	
	$crud = new crud('invoices');  // table
    $crud->inserir("payment,customer,request,total,date,expiration_date,payment_date,dia,mes,ano,status", "'$payment','$customer','$registerID','$totalorder','$date','','$payment_date','$dia','$mes','$ano','open'");
	
	header("Location: index.php?mod=OrderAccepted&y=2"); // redirect send mail ok
    }
    if($status == 'N') { 
	header("Location: index.php?mod=OrderCanceled&y=2"); // redirect send mail ok
    }
	
	}
	
	if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = base64_decode($_GET['id']); 
    $crud = new crud('requests_tbl'); // Table
    $crud->excluir("id = $id"); // ID    
    $idRT = $_GET['idRT'];
    header("Location: index.php?mod=OrderEdit&id=$idRT&y=3");
    }
	
	$acao = $_POST['act'];
	if ($acao == "alterar")
    {
	$quant = $_POST['qtd'];
		if (is_array($quant))
		{	
			foreach($quant as $cod => $qtd)
			{
					$sql_alterar = "UPDATE requests_tbl SET qtd = '$qtd' WHERE id = '$cod'";
					$rs_alterar = mysql_query($sql_alterar);
			}
		}
      }
    ?>

    
    <script type="text/javascript">  
    $(function () {
        //Initialize Select2 Elements
        $(".select2").select2();       
        });
    </script> 
      
    <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <?php echo $lang['TITLE_UPDATE_ORDER']; ?>
          </h1>
          
        </section>

       
       <!-- Main content -->
        <section class="content">
          <div class="row">
			  
			  
            <!-- left column -->
            <div class="col-md-12">
              <!-- general form elements -->
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title"><?php echo $lang['ORDER']; ?></h3>
                </div><!-- /.box-header -->
                <!-- form start -->
                <form role="form" method="POST" id="Order" name="Order">
				
                  <div class="box-body">
				 
				 <table class="table table-striped">
				 <tr>
					 
				 
				 <td>
				 <div class="form-group">
                      <label for="Input"><?php echo $lang['ORDER_PAY']; ?></label>
                    <select id="payment" name="payment" class="form-control" style="width: 100%;">
					<option value="PayPal" <?php if (@$result['payment'] == 'PayPal') { echo "selected"; } ?>>PayPal</option>
                    <option value="PagSeguro" <?php if (@$result['payment'] == 'PagSeguro') { echo "selected"; } ?>>PagSeguro</option>
                    <option value="bCash" <?php if (@$result['payment'] == 'bCash') { echo "selected"; } ?>>bCash</option>
                    <option value="Boleto" <?php if (@$result['payment'] == 'Boleto') { echo "selected"; } ?>>Boleto</option>
				    </select>
                    </div>
				 </td>
				 
				
				 <td>
				 <div class="form-group">
                      <label for="Input"><?php echo $lang['STATUS']; ?></label>
                    <select id="status" name="status" class="form-control" style="width: 100%;">
					<option value="A" <?php if (@$result['status'] == 'A') { echo "selected"; } ?>><?php echo $lang['ORDER_STATUS_A']; ?></option>
                    <option value="S" <?php if (@$result['status'] == 'S') { echo "selected"; } ?>><?php echo $lang['ORDER_STATUS_S']; ?></option>
                    <option value="N" <?php if (@$result['status'] == 'N') { echo "selected"; } ?>><?php echo $lang['ORDER_STATUS_N']; ?></option>
				    </select>
                    </div>
				 </td>

				 </tr>
				 </table>	
				 <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']; ?>"> 
		  <input type="hidden" name="idRT" value="<?php echo $_GET['id']; ?>">
		  <input type="hidden" name="GT" value="<?php echo $_GET['sx']; ?>">
		  <? } 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 method="POST" name="qtd" id="qtd">
				  <input type="hidden" name="act" value="alterar">
				  
				 <table class="table table-striped table-hover">
                    <thead>
                      <tr>
                        <th><?php echo $lang['PRODUCT']; ?></th>
                        <th><?php echo $lang['DOMAIN']; ?></th>
                        <th><?php echo $lang['QTD']; ?></th>
                        <th><?php echo $lang['AMOUNT']; ?></th>
                        <th><?php echo $lang['ORDER_TOTAL']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
					$idRT = $result['id'];
 		  			$queryRF = mysql_query("SELECT p.*, c.product AS product FROM requests_tbl AS p INNER JOIN products AS c ON p.product = c.id WHERE p.request = '$idRT' ORDER BY p.id DESC");
 		  			while($qres = mysql_fetch_array($queryRF)){
					?>
		            <tr>
		            <td><?php echo $qres['product']; ?></td>
					<td><?php echo $qres['domain']; ?></td>
					<td><input type="number" onchange="this.form.submit();" min="1" max="100" autocomplete="off" class="form-control" style="width:60px;" name="qtd[<?php echo $qres['id']; ?>]" value="<?php echo $qres['qtd']; ?>"></td>
					<td><?php echo number_format($qres['amount'],2,'.',''); ?></td>
					<td><?php echo number_format($qres['amount'] * $qres['qtd'],2,'.',''); ?></td>
				    <td><a class="btn btn-danger" href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=OrderEdit&id=<?php echo base64_encode($qres['id']); ?>&idRT=<?php echo $_GET['id']; ?>&action=delete' } else { void('') };"><i class="fa fa-remove"></i></a></td>
		            </tr>
					<?php } ?>
					
                    </tbody>
                    <tfoot>
                      <tr>
                        <th><?php echo $lang['PRODUCT']; ?></th>
                        <th><?php echo $lang['DOMAIN']; ?></th>
                        <th><?php echo $lang['QTD']; ?></th>
                        <th><?php echo $lang['AMOUNT']; ?></th>
                        <th><?php echo $lang['ORDER_TOTAL']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </tfoot>
                  </table>
                
                </form>
                
               
                    
                   
                  </div><!-- /.box-body -->

                 
                
                
              </div><!-- /.box -->
           </div><!-- /.col -->
           
           
         
           
          </div><!-- /.row -->
        </section><!-- /.content -->
       
        
      </div><!-- /.content-wrapper -->
   
   
   
   
