<?php
	include "admin_top_menu.php";
	include "admin_side_menu.php";
	 
	if($temp_id = isset($_GET['temp_id']) ? $_GET['temp_id'] : '')
	{
		$row=mysqli_query($conn,"SELECT * FROM `swatch` WHERE sid='$temp_id'");
		$data=mysqli_fetch_array($row);
	}
	else{
		$data['sid']="";
		$data['snumber']="";
		$data['sname']="";
		$data['scategory']="";
		$data['scolor']="";
		$data['scolor2']="";
		$data['sfabric']="";
		$data['spattern']="";
		$data['sgsm']="";
		$data['smfg']="";
		$data['sdescription']="";
		$data['simage']="";
	}
?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Product Details
        <small>Enter Details</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Product</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <!-- SELECT2 EXAMPLE -->
      <form name="frmtrack" action="swatch_code.php" method="post" enctype="multipart/form-data">
      <div class="box box-info">
        <div class="box-header with-border">
          <h3 class="box-title">Product Details</h3>
		  
          <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button>
          </div>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
          <div class="row">
            <div class="col-md-4">
              <div class="form-group">
                <label>Product Id</label>
                <input type="hidden" name="txt_sid" value="<?php echo $data['sid'];?>">
                <input type="text" class="form-control" name="txt_snumber" id="txt_snumber" placeholder="Swatch Id" value="<?php echo $data['snumber'];?>" required="" readonly="">
              </div>
            </div>
			
            <div class="col-md-4">
              <div class="form-group">
                <label>Product Name</label>
				<?php if($data['sid']==''){ ?>
                <input type="text" class="form-control" name="txt_sname" id="txt_sname" placeholder="Swatch Name" value="<?php echo $data['sname'];?>" required="" onkeyup="generateId();">
				<?php } else {?>
				<input type="text" class="form-control" name="txt_sname" id="txt_sname" placeholder="Swatch Name" value="<?php echo $data['sname'];?>" required="">
				<?php } ?> 
              </div>
            </div>
			
			<div class="col-md-4">
              <div class="form-group">
                <label>Product Category</label>
                <select class="form-control" name="txt_scategory">
                <option value="<?php echo $data['scategory'];?>"><?php echo $data['scategory'];?></option>
                <?php 
                  $query=mysqli_query($conn,"SELECT * FROM `category`")or die(mysqli_error($conn));
                  while($row=mysqli_fetch_array($query))
                  {
                ?>
                  <option value="<?php echo $row['ctname'];?>"><?php echo $row['ctname'];?></option>
                <?php  } ?>
              </select>
              </div>
            </div>

            <div class="col-md-4">
              <div class="form-group">
                <label>Product Fabric</label>
                <select class="form-control" name="txt_sfabric">
                <option value="<?php echo $data['sfabric'];?>"><?php echo $data['sfabric'];?></option>
                <?php 
                  $query=mysqli_query($conn,"SELECT * FROM `fabric`")or die(mysqli_error($conn));
                  while($row=mysqli_fetch_array($query))
                  {
                ?>
                  <option value="<?php echo $row['fname'];?>"><?php echo $row['fname'];?></option>
                <?php  } ?>
              </select>
              </div>
            </div>

			<div class="col-md-4">
              <div class="form-group">
                <label>Fabric Pattern</label>
                <select class="form-control" name="txt_spattern">
					<option value="<?php echo $data['spattern'];?>"><?php echo $data['spattern'];?></option>
					<option value="Plain">Plain</option>
					<option value="Chex">Chex</option>
					<option value="Lines">Lines</option>
					<option value="Cross">Cross</option>
					<option value="Gradient">Gradient</option>
					<option value="Dots">Dots</option>
					<option value="Twill">Twill</option>
				</select>
              </div>
            </div>
			
			<div class="col-md-4">
              <div class="form-group">
                <label>Product GSM</label>
                <select class="form-control" name="txt_sgsm">
                <option value="<?php echo $data['sgsm'];?>"><?php echo $data['sgsm'];?></option>
                <?php 
                  $query=mysqli_query($conn,"SELECT * FROM `gsm`")or die(mysqli_error($conn));
                  while($row=mysqli_fetch_array($query))
                  {
                ?>
                  <option value="<?php echo $row['gname'];?>"><?php echo $row['gname'];?></option>
                <?php  } ?>
              </select>
              </div>
            </div>

            <div class="col-md-12">
              <div class="form-group">
                <label>Product Color 1</label><br>
                <?php 
                  $query=mysqli_query($conn,"SELECT * FROM `color`")or die(mysqli_error($conn));
                  while($row=mysqli_fetch_array($query))
                  { ?>
                  <input type="radio" name="txt_scolor" value="<?php echo $row['cname'];?>" 
                  <?php if($row['cname']==$data['scolor']){echo "checked";}?>>
				  
				  <button class="btn btn-lg" style="background-color: <?php echo $row['hex'];?>"></button>
				  <?php echo $row['cname'];?>
                  &nbsp;&nbsp;&nbsp;&nbsp;
                <?php  } ?>
              </select>
              </div>
            </div>
			
			<div class="col-md-12">
              <div class="form-group">
                <label>Product Color 2</label><br>
                <?php 
                  $query=mysqli_query($conn,"SELECT * FROM `color`")or die(mysqli_error($conn));
                  while($row=mysqli_fetch_array($query))
                  { ?>
                  <input type="radio" name="txt_scolor2" value="<?php echo $row['cname'];?>" 
                  <?php if($row['cname']==$data['scolor2']){echo "checked";}?>>
				  
				  <button class="btn btn-lg" style="background-color: <?php echo $row['hex'];?>"></button>
				  <?php echo $row['cname'];?>
                  &nbsp;&nbsp;&nbsp;&nbsp;
                <?php  } ?>
              </select>
              </div>
            </div>
			
			<div class="col-md-12"></div>
			
			
			<div class="col-md-8">
				<div class="col-md-6">
				  <div class="form-group">
					<label>Manufactured By</label>
					<?php if($data['sid']==''){ ?>
					<select class="form-control" name="txt_smfg" id="txt_smfg" onchange="generateId();">
					<?php } else {?>
					<select class="form-control" name="txt_smfg" id="txt_smfg">
					<?php } ?>
					
					<option value="<?php echo $data['smfg'];?>"><?php echo $data['smfg'];?></option>
					<?php 
					  $query=mysqli_query($conn,"SELECT * FROM `manufacturer`")or die(mysqli_error($conn));
					  while($row=mysqli_fetch_array($query))
					  {
					?>
					  <option value="<?php echo $row['mill_name'];?>"><?php echo $row['mill_name'];?></option>
					<?php  } ?>
				  </select>
				 </div>
				</div>

				<div class="col-md-6">
				  <div class="form-group">
					<label>Product Image</label>
					<input type="file" class="form-control" name="swatchfile" placeholder="Swatch Image">
				  </div>
				</div>
				
				<div class="col-md-12">
				  <div class="form-group">
					<label>Description</label>
						<textarea class="form-control" name="txt_sdesc" placeholder="Description"><?php echo $data['sdescription'];?></textarea>
				  </div>
				</div>
			</div>
			
			<div class="col-md-4">
                <img src="<?php echo $data['simage'];?>" width="150" >
            </div>
			
          </div>
          <!-- /.row -->
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <?php if($data['sid']==''){ ?>
            <button type="submit" name="btn_ssubmit" class="btn btn-info pull-right">Submit</button>
      			<?php } else {?>
      				<button type="submit" name="btn_supdate" class="btn btn-info pull-right">Update</button> 
					<button type="button" name="print_button" id="print_button" onclick="PrintDiv();" class="btn btn-info pull-left">Print</button> 
					
      			<?php require_once"print_swatch.php"; } ?>
		</div>
      </div>
    
	
	<?php   
		if(isset($_GET['status'])&&$_GET['status']==1)
		echo "<font color=green>Data Successfully Insert..</font>";
			
		if(isset($_GET['status'])&&$_GET['status']==2)
		echo "<font color=green>Data Successfully Update..</font>";
			
		if(isset($_GET['status'])&&$_GET['status']==3)
		echo "<font color=red>Wrong click...</font>";   
	?>
      <!-- /.box -->
    </form>
	  
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  
<?php include "admin_footer.php"; ?>

<script type="text/javascript">
  onkeyup="generateId()"
  function generateId()
  {
    var sname = document.getElementById('txt_sname').value;
    var tsname = sname.substring(0, 2);
    var smfg = document.getElementById('txt_smfg').value;
    var tsmfg = smfg.substring(0, 2);

    var rand = Math.floor(Math.random() * 9999) + 1;
    var final = tsname.toUpperCase()+"-"+tsmfg.toUpperCase()+"-"+rand;
    document.getElementById('txt_snumber').value = final;
  }

function PrintDiv()
{    
	var divToPrint = document.getElementById('divToPrint');
	var popupWin = window.open('', '_blank', 'width=800,height=620');

	popupWin.document.open();
	popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
	popupWin.document.close();
}
</script>