<!DOCTYPE html>

<?php 
	session_start();
	@ob_start();
	include "../../config/koneksi.php";
	include "../../config/fungsi_rupiah.php";
	include "../head.php";

	$custt = $_SESSION['nama'];  
	if ($_SESSION['nama'] == ''){
		header('Location:index.php');
	}
	else{
?>
<html>

  <body class="hold-transition skin-blue sidebar-mini layout-boxed">
    <div class="wrapper">
		<?php include "../header.php";?>
      
		<?php include "../menu.php";?>
      

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            DATA ORDERAN
            <small>Data Orderan</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Orderan</a></li>
            <li class="active">Data Orderan</li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
            <div class="col-xs-12">
              
              <div class="box">
                <div class="box-header">
                  <h3 class="box-title">List Orderan</h3>
                </div><!-- /.box-header -->

					
						<div class="box-body">
							<?php 
							$id=$_GET['id'];	
							echo"
							<form name=form id='demoForm' method=POST action='aksisales.php?module=sales&act=ordadd&id=$id' class='form-horizontal'>	 ";?> 
								<div class="box-footer">
									<div class="form-group">
									  <label>Product</label>
									  
										<select name="category" class="form-control">
											<?php
												$cek = mysql_query("Select * from categories  order by category asc");
												while ($r=mysql_fetch_array($cek)){	
													echo"<option value = $r[pk]>$r[category]</option>";
												}	
											?>
										</select>

										<select name="choices[]" id="choices" class="form-control">
											<!-- js populates -->
										</select>
									</div>
									<div class="form-group">
									  <label>Qty</label>
									  <input type="text" name="qty" onkeypress="return isNumberKey(event)"/>
									</div>
			
									<button type="submit" class="btn btn-info">Add Order</button>
								</div><!-- /.box-footer -->
							</form>
						</div>

                <div class="box-body">
					<div class="box-header">
                  <h3 class="box-title">List Orderan</h3>
                </div><!-- /.box-header -->
                  <table id="example2" class="table table-bordered table-hover">
                    <thead>
                      <tr>
                        <th>Product</th>
                        <th>Qty</th>
                        <th>Price</th>
						<th>Total</th>
                        <th></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
						$gtot = 0;
						$cek = mysql_query("Select * from orderdetails where billno = '$_GET[id]' order by urut asc");
						while ($r=mysql_fetch_array($cek)){	
							$produk = mysql_result(mysql_query("select product from products where code = $r[code]"),0);
							$harga = mysql_result(mysql_query("select unit_rp from products where code = $r[code]"),0);
							$harga2 = format_rupiah($harga);
							$total = ($harga * $r['qty']);
							$total2 = format_rupiah($total);
							$gtot = $gtot + $total;
							echo "<tr>
									<td>$produk</td>
									<td>$r[qty]</td>
									<td>$harga2</td>
									<td>$total2</td>";

									echo"<td><a href='aksisales.php?module=sales&act=delprodadd&id=$id&urut=$r[urut]'><span class='label label-danger'>Delete</span></a></td>";

								  echo"</tr>";
						}
					?>
                        
                     
                    </tbody>
					<tfoot>
						<th>Total</th>
                        <th></th>
                        <th></th>
						<?php
						$gtot2 = format_rupiah($gtot);
						echo"<th>$gtot2</th>"
						?>
                        <th></th>
					</tfoot>
                  </table>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
		<?php include "../footer.php";?>
		

      <!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">

        <!-- Tab panes -->
        <div class="tab-content">
          <!-- Home tab content -->
          <div class="tab-pane" id="control-sidebar-home-tab">

          </div><!-- /.tab-pane -->

        </div>
      </aside><!-- /.control-sidebar -->
      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <div class="control-sidebar-bg"></div>
    </div><!-- ./wrapper -->

    <!-- jQuery 2.1.4 -->
    <script src="../../plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="../../bootstrap/js/bootstrap.min.js"></script>
    <!-- DataTables -->
    <script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- SlimScroll -->
    <script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="../../plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../../dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="../../dist/js/demo.js"></script>
    <!-- page script -->
    <script>
      $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
          "paging": true,
          "lengthChange": false,
          "searching": false,
          "ordering": true,
          "info": true,
          "autoWidth": false
        });
      });
    </script>
	<script type="text/javascript">
	
	function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode != 46 &&(charCode < 48 || charCode > 57)))
        return false;
    return true;
	}

function removeAllOptions(sel, removeGrp) {
    var len, groups, par;
    if (removeGrp) {
        groups = sel.getElementsByTagName('optgroup');
        len = groups.length;
        for (var i=len; i; i--) {
            sel.removeChild( groups[i-1] );
        }
    }
    
    len = sel.options.length;
    for (var i=len; i; i--) {
        par = sel.options[i-1].parentNode;
        par.removeChild( sel.options[i-1] );
    }
}

function appendDataToSelect(sel, obj) {
    var f = document.createDocumentFragment();
    var labels = [], group, opts;
    
    function addOptions(obj) {
        var f = document.createDocumentFragment();
        var o;
        
        for (var i=0, len=obj.text.length; i<len; i++) {
            o = document.createElement('option');
            o.appendChild( document.createTextNode( obj.text[i] ) );
            
            if ( obj.value ) {
                o.value = obj.value[i];
            }
            
            f.appendChild(o);
        }
        return f;
    }
    
    if ( obj.text ) {
        opts = addOptions(obj);
        f.appendChild(opts);
    } else {
        for ( var prop in obj ) {
            if ( obj.hasOwnProperty(prop) ) {
                labels.push(prop);
            }
        }
        
        for (var i=0, len=labels.length; i<len; i++) {
            group = document.createElement('optgroup');
            group.label = labels[i];
            f.appendChild(group);
            opts = addOptions(obj[ labels[i] ] );
            group.appendChild(opts);
        }
    }
    sel.appendChild(f);
}

// anonymous function assigned to onchange event of controlling select list
document.forms['demoForm'].elements['category'].onchange = function(e) {
    // name of associated select list
    var relName = 'choices[]';
    
    // reference to associated select list 
    var relList = this.form.elements[ relName ];
    
    // get data from object literal based on selection in controlling select list (this.value)
    var obj = Select_List_Data[ relName ][ this.value ];
    
    // remove current option elements
    removeAllOptions(relList, true);
    
    // call function to add optgroup/option elements
    // pass reference to associated select list and data for new options
    appendDataToSelect(relList, obj);
};

// object literal holds data for optgroup/option elements
var Select_List_Data = {
    
    // name of associated select list
    'choices[]': {
        
        <?php
				$cek = mysql_query("Select * from categories  order by category asc");
				while ($r=mysql_fetch_array($cek)){	
				echo"$r[pk]: {
				
				text: [";
				
				$cek2 = mysql_query("Select * from products where category = '$r[pk]' order by code asc");
				while ($rr=mysql_fetch_array($cek2)){	
					echo"'$rr[product]',";
				}
				echo"],value: [";
				$cek3 = mysql_query("Select * from products where category = '$r[pk]' order by code asc");
				while ($rrr=mysql_fetch_array($cek3)){	
					echo"'$rrr[code]',";
				}

				echo"]
				},";
				
				}
		?>

        
    }
    
};

// populate associated select list when page loads
window.onload = function() {
    var form = document.forms['demoForm'];
    
    // reference to controlling select list
    var sel = form.elements['category'];
    sel.selectedIndex = 0;
    
    // name of associated select list
    var relName = 'choices[]';
    // reference to associated select list
    var rel = form.elements[ relName ];
    
    // get data for associated select list passing its name
    // and value of selected in controlling select list
    var data = Select_List_Data[ relName ][ sel.value ];
    
    // add options to associated select list
    appendDataToSelect(rel, data);
};

</script>
  </body>
</html>
<?php 
	}
?>