<?php
session_name("AUTHENUSER");
session_start();
$USERNAME = $_SESSION['SES_USERNAME'];
if(!session_name("AUTHENUSER"))
	{
?>
						<script language="javascript">
						 alert("Waktu Login telah habis.Silahkan Login Kembali");
						document.location="index.php";
						</script>
<?php
	}
if(!$USERNAME)
	{
?>
						<script language="javascript">
						 alert("Harap Login Dahulu.");
						document.location="index.php";
						</script>
<?php
	}
	?>
	<?php 
	include "common/config.php";
	include "include/head.php";

	$act  = $_REQUEST['act'];
	$id_orderform  = $_REQUEST['id_orderform'];
	$fetch = mysql_fetch_array(mysql_query("select * from orderform where id_orderform='$id_orderform'"));
	$id_orderform = $fetch['id_orderform'];
	$kode_orderform = $fetch['kode_orderform'];
	$details = $fetch['details'];
	$others_gold  = $fetch['others_gold'];
	$others_choice  = $fetch['others_choice'];
	$status = $fetch['status'];
	list($y, $m, $d)=split ("[-]",$fetch['orderform_date']);
	$orderform_date=$d."-".$m."-".$y;
	$description  = $fetch['description'];
	$picture = $fetch['picture'];
	$nama  = $fetch['nama'];
	$manufacture  = $fetch['manufacture'];
	$loc = "ORF/";
	$all = $loc.$picture; 
	if($act == 1)
	{
		$id_orderform  = $_REQUEST['id_orderform'];
		$kode  = $_REQUEST['kode'];
		list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate']);
		$datepicker=$f."-".$e."-".$d;
		$details  = $_REQUEST['details'];
		$others_gold  = $_REQUEST['others_gold'];
		$others_choice  = $_REQUEST['others_choice'];
		$description  = $_REQUEST['description'];
		$nama  = $_REQUEST['nama'];
		$manufacture  = $_REQUEST['manufacture'];
		$uploaddir = 'ORF/';
		$uploadfile = $uploaddir.$_FILES['file']['name'];
		$filename = $_FILES['file']['name']; 
			
	if($filename != "")
			{
				if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 1000000))
				{
				  if ($_FILES["file"]["error"] > 0)
					{
						echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
					}
					else
					{
						move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
							$insert = mysql_query("update orderform set orderform_date='$datepicker',details='$details', description='$description', user_update='$USERNAME',others_choice='$others_choice',others_gold='$others_gold',picture='$filename',nama='$nama',manufacture='$manufacture' where id_orderform='$id_orderform'");

						if($insert)
						{
						$gold = count($_POST['gold']);
						$sql = mysql_query("delete from  orderform_gold where kode_orderform = '$kode' ");
						for($i=0; $i<count($_POST['gold']); $i++)
						{
						$sql1 = mysql_query("INSERT INTO orderform_gold (kode_orderform,gold) VALUES ('$kode',trim('".$_POST['gold'][$i]."'))");
						}
						
						$choice = count($_POST['choice']);
						$sql2 = mysql_query("delete from  orderform_choice where kode_orderform = '$kode' ");
						for($i=0; $i<count($_POST['gold']); $i++)
						{
						$sql3 = mysql_query("INSERT INTO orderform_choice (kode_orderform,choice) VALUES ('$kode',trim('".$_POST['choice'][$i]."'))");
						}
						?>
								<script language="javascript">
									 alert("Edit Order Form Success");
									document.location="orderform.php";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Edit Order Form Failed");
									document.location="edit_orderform.php?id_orderform=<?php echo $id_orderform; ?>";
								</script>
							<?php
						}
					}
				}
				else
				{
				?>
								<script language="javascript">
									alert("Image format invalid");
									document.location="add_gold.php";
								</script>
				<?php
				}
			}
			else
			{
				
							$insert = mysql_query("update orderform set orderform_date='$datepicker',others_choice='$others_choice',others_gold='$others_gold', description='$description', user_update='$USERNAME',details='$details',nama='$nama',manufacture='$manufacture' where id_orderform='$id_orderform'");

						if($insert)
						{
						$gold = count($_POST['gold']);
						$sql = mysql_query("delete from  orderform_gold where kode_orderform = '$kode' ");
						for($i=0; $i<count($_POST['gold']); $i++)
						{
						$sql1 = mysql_query("INSERT INTO orderform_gold (kode_orderform,gold) VALUES ('$kode',trim('".$_POST['gold'][$i]."'))");
						}
						
						$choice = count($_POST['choice']);
						$sql2 = mysql_query("delete from  orderform_choice where kode_orderform = '$kode' ");
						for($i=0; $i<count($_POST['gold']); $i++)
						{
						$sql3 = mysql_query("INSERT INTO orderform_choice (kode_orderform,choice) VALUES ('$kode',trim('".$_POST['choice'][$i]."'))");
						}
						?>
								<script language="javascript">
									 alert("Edit Order Form Success");
									document.location="orderform.php";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Edit Order Form Failed");
									document.location="edit_orderform.php?id_orderform=<?php echo $id_orderform; ?>";
								</script>
							<?php
						}
			}
	}

	?>
<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-bar-chart"></i>Edit Order Form</h1>
</div>

<!--<ol class="breadcrumb">
  <li><a href="#">Home</a></li>
  <li><a href="#">Bread</a></li>
  <li><a href="#">Crumbs</a></li>
  <li class="active">Example</li>
</ol>-->
          <div class="main-content">
            <div class="row">
                <div class="widget">
                  <div class="widget-content-white glossed">
                    <div class="padded">
                      <form method="post" action="edit_orderform.php?act=1&id_orderform=<?php echo $id_orderform; ?>" enctype="multipart/form-data" role="form" class="form-horizontal">
                        <div class="form-group">
                          <label class="col-md-4 control-label">Order Form ID</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="kode" id="kode"  value="<?php echo $kode_orderform; ?>"  readonly="readonly"  >
                          </div>
                        </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Customer Name</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="nama" id="nama" value="<?php echo $nama; ?>"  >
                          </div>
                        </div>
                   
                    <div class="form-group">
                          <label class="col-md-4 control-label">Order Form Date</label>
                          <div class="col-md-8">
                            <input type="text" class="inputDate" name="inputDate" id="inputDate" value="<?php echo $orderform_date; ?>">
                          </div>
                        </div>
                        
                        
                    <div class="form-group">
                          <label class="col-md-4 control-label">Manufacture</label>
                          <div class="col-md-8">
                           <input type="radio" name="manufacture" id="optionsRadios1" value="local" <?php if($manufacture == "local") {?> checked="checked" <?php } ?>>Local&nbsp;&nbsp;
                           <input type="radio" name="manufacture" id="optionsRadios1" value="overseas" <?php if($manufacture == "overseas") {?> checked="checked" <?php } ?>>Overseas
                          </div>
                        </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Gold</label>
                          <div class="col-md-8">
                             <label class="checkbox-inline">
                            <?php $sss = mysql_query("select * from orderform_gold where kode_orderform='$kode_orderform' and gold='750 White Gold'"); 
							?> <input type="checkbox" name="gold[]" id="gold[]" value="750 White Gold"  <?php if(mysql_num_rows($sss) > 0) {?> checked="checked" <?php } ?> />750 White Gold
                             </label>
                             <label class="checkbox-inline">
                        <?php $sss1 = mysql_query("select * from orderform_gold where kode_orderform='$kode_orderform' and gold='750 Yellow Gold'"); ?>
                             <input type="checkbox" name="gold[]" id="gold[]" value="750 Yellow Gold" <?php if(mysql_num_rows($sss1) > 0) {?> checked="checked" <?php } ?> />750 Yellow Gold
                             </label>
                              <label class="checkbox-inline">
                              <?php $sss2 = mysql_query("select * from orderform_gold where kode_orderform='$kode_orderform' and gold='750 Rose Gold'");  ?>
                             <input type="checkbox" name="gold[]" id="gold[]" value="750 Rose Gold" <?php if(mysql_num_rows($sss2) > 0) {?> checked="checked" <?php } ?> />750 Rose Gold
                             </label>
                             </br> </br>
                              <label class="checkbox-inline">
                               <?php $sss3 = mysql_query("select * from orderform_gold where kode_orderform='$kode_orderform' and gold='others'"); ?>
                              <input type="checkbox" name="gold[]" id="gold[]" value="others" <?php if(mysql_num_rows($sss3) > 0) {?> checked="checked" <?php } ?>/>Others</label> <input type="text" name="others_gold" id="others_gold"  class="form-control"  value="<?php echo $others_gold; ?>" />
                              
                         
                          </div>
                        </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Choice of order</label>
                          <div class="col-md-8">
                          <label class="checkbox-inline">
                          <?php $sss = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Ring'"); ?>
                          <input type="checkbox" name="choice[]" id="choice[]" value="Ring" <?php if(mysql_num_rows($sss) > 0) {?> checked="checked" <?php } ?> />Ring</label>
                           <label class="checkbox-inline"> <?php $sss1 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Bracelet'"); ?>
                           <input type="checkbox" name="choice[]" id="choice[][]" value="Bracelet" <?php if(mysql_num_rows($sss1) > 0) {?> checked="checked" <?php } ?> />Bracelet</label>
                             <label class="checkbox-inline">
                             <?php $sss2 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Bangle'"); ?>
                     <input type="checkbox" name="choice[]" id="choice[]" value="Bangle" <?php if(mysql_num_rows($sss2) > 0) {?> checked="checked" <?php } ?> />Bangle</label>
                            <label class="checkbox-inline"> 
                             <?php $sss3 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Pendant'"); ?>
                 <input type="checkbox" name="choice[]" id="choice[]" value="Pendant" <?php if(mysql_num_rows($sss3) > 0) {?> checked="checked" <?php } ?> />Pendant</label>
                           <label class="checkbox-inline"> <?php $sss4 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Earing'"); ?>
                           <input type="checkbox" name="choice[]" id="choice[]" value="Earing"  <?php if(mysql_num_rows($sss4) > 0) {?> checked="checked" <?php } ?> />Earing</label>
                             <label class="checkbox-inline">
                              <?php $sss5 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='Necklace'"); ?>
                             <input type="checkbox" name="choice[]" id="choice[]" value="Necklace"  <?php if(mysql_num_rows($sss5) > 0) {?> checked="checked" <?php } ?>/>Necklace</label>
                             </br> </br>
                             <label class="checkbox-inline"> 
                              <?php $sss6 = mysql_query("select * from orderform_choice where kode_orderform='$kode_orderform' and choice='others'"); ?>
                             <input type="checkbox" name="choice[]" id="choice[]" value="others" <?php if(mysql_num_rows($sss6) > 0) {?> checked="checked" <?php } ?>/>Measurement (Ring, Size, Cm for Necklace/Bangle/Bracelet) </label><input type="text" name="others_choice" id="others_choice" class="form-control" value="<?php echo $others_choice; ?>"  />
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Engraving Details</label>
                          <div class="col-md-8">
                           <textarea cols="80" rows="5" name="details" id="details" class="form-control"><?php echo $details; ?></textarea>
                          </div>
                        </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Description</label>
                          <div class="col-md-8">
                           <textarea cols="80" rows="5" name="description" id="description" class="form-control"><?php echo $description; ?></textarea>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-md-4 control-label">Picture *max file 1MB and format file only JPEG/GIF</label>
                          <div class="col-md-8">
                          <a class="image" href="ORF/<?php echo $picture; ?>">
							<img src="<?php echo $all; ?>" alt="" height="69" width="117"/></a>&nbsp;
                           <input type="file" name="file" id="file" /> 
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="col-md-offset-4 col-md-8">
                            <button type="submit" class="btn btn-default">Submit</button>
                            <button type="reset" class="btn btn-default">Reset</button>
                          </div>
                        </div>
                      </form>
                    </div>
                    </div>
                    </div>
                    </div>
                    </div>
                   

<?php include "include/foot.php"; ?>
<script src='assets/js/for_pages/dashboard.js'></script>
</body>

</html>