<?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'];
	$kode = mysql_fetch_array(mysql_query("select max(id_orderform) as maxid from  orderform"));
	$maxid = $kode['maxid'];
	if($maxid == "")
	{
		$kode1 = "ORF1";
	
	}
	else
	{
		$total = $maxid+1;
		$kode1 = "ORF".$total;
	}
	if($act == 1)
	{
		$kode  = $_REQUEST['kode'];
		list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate']);
		$datepicker=$f."-".$e."-".$d;
		$description  = $_REQUEST['description'];
		$others_gold  = $_REQUEST['others_gold'];
		$others_choice  = $_REQUEST['others_choice'];
		$details  = $_REQUEST['details'];
		$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("insert into orderform (kode_orderform,orderform_date,details, description, user_update,status,picture,others_gold,others_choice,nama,manufacture) values ('$kode','$datepicker','$details','$description','$USERNAME','input','$filename','$others_gold','$others_choice','$nama','$manufacture')");						
						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("Add Order Form Success");
									document.location="orderform.php";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Order Form Failed");
									document.location="add_orderform.php";
								</script>
							<?php
						}
					}
				}
				else
				{
				?>
								<script language="javascript">
									alert("Image format invalid");
									document.location="add_orderform.php";
								</script>
				<?php
				}
			}
			else
			{
				$insert = mysql_query("insert into orderform (kode_orderform,orderform_date,details, description, user_update,status,others_gold,others_choice,nama,manufacture) values ('$kode','$datepicker','$details','$description','$USERNAME','input','$others_gold','$others_choice','$nama','$manufacture')");						
						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("Add Order Form Success");
									document.location="orderform.php";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Order Form Failed");
									document.location="add_orderform.php";
								</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>Add 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="add_orderform.php?act=1" 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 $kode1; ?>"  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" >
                          </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 date('d-m-Y'); ?>">
                          </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">Local&nbsp;&nbsp;
                           <input type="radio" name="manufacture" id="optionsRadios1" value="overseas" checked>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">
                            <input type="checkbox" name="gold[]" id="gold[]" value="750 White Gold" />750 White Gold
                             </label>
                             <label class="checkbox-inline">
                             <input type="checkbox" name="gold[]" id="gold[]" value="750 Yellow Gold" />750 Yellow Gold
                             </label>
                              <label class="checkbox-inline">
                             <input type="checkbox" name="gold[]" id="gold[]" value="750 Rose Gold" />750 Rose Gold
                             </label>
                             </br> </br>
                              <label class="checkbox-inline">
                              <input type="checkbox" name="gold[]" id="gold[]" value="others"/>Others</label> <input type="text" name="others_gold" id="others_gold"  class="form-control" />
                              
                         
                          </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"><input type="checkbox" name="choice[]" id="choice[]" value="Ring" />Ring</label>
                           <label class="checkbox-inline"> <input type="checkbox" name="choice[]" id="choice[][]" value="Bracelet" />Bracelet</label>
                             <label class="checkbox-inline"><input type="checkbox" name="choice[]" id="choice[]" value="Bangle" />Bangle</label>
                            <label class="checkbox-inline"> <input type="checkbox" name="choice[]" id="choice[]" value="Pendant" />Pendant</label>
                           <label class="checkbox-inline"> <input type="checkbox" name="choice[]" id="choice[]" value="Earing" />Earing</label>
                             <label class="checkbox-inline"><input type="checkbox" name="choice[]" id="choice[]" value="Necklace" />Necklace</label>
                             </br> </br>
                             <label class="checkbox-inline"> <input type="checkbox" name="choice[]" id="choice[]" value="others"/>Measurement (Ring, Size, Cm for Necklace/Bangle/Bracelet) </label><input type="text" name="others_choice" id="others_choice" class="form-control"  />
                          </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"></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"></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">
                           <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>