<?php   include_once('../config/db.php');   include_once('../config/check.php');  

if (filter_has_var(INPUT_POST, "submit")) 
    try{
            $date =            htmlspecialchars (strtoupper($_POST['date']));
            $date =            date("Y-m-d", strtotime($date));
            $name =                  htmlspecialchars ( $_POST['name']);
            $code =                  htmlspecialchars ( $_POST['code']);
            $description =           htmlspecialchars ( $_POST['description']);
            $uomgroup =              htmlspecialchars ( $_POST['multiplestatus']);
            $uombase =               htmlspecialchars ( @$_POST['uombase']);
            if( $uombase=="")
            {
                $uombase =  0;
                $uomrate = 0.00;

            }
            else{

                $uombase =               htmlspecialchars ( $_POST['uombase']);
                $uomrate =               htmlspecialchars ( $_POST['uomrate']);
            }
            

            $sql = 'INSERT INTO aduom  (date,name,code,description,uomgroup,uombase,uomrate,uid)
                                VALUES (:date,:name,:code,:desc,:uomgroup,:uombase,:uomrate,:uid )';
            $conn->prepare($sql)->execute(['date' => $date,'name' => $name,'code' => $code,'desc' =>$description,
                                           'uomgroup' =>$uomgroup,'uombase' =>$uombase,'uomrate' =>$uomrate,'uid' => $uid    ]);
            header("location:uom.php");

        } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>  
</head>
<body <?php include_once('../inc/snav.php');  ?>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('../inc/sbrand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('../inc/snavbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('../inc/smenu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
        <section class="content-header">
          <h1 class="listheader" >   <i class="fa fa-barcode" style="color:green;"></i> ADD UOM   </h1>
            <ol class="breadcrumb">
            <!-- <li><a href="#"><i class="fa fa-dashboard"></i> </a></li>
            <li><a href="#"> </a></li>
            <li class="active"> </li> -->
            <li><a href="uoms" class="btn btn-sm btn-md"> <i class="fa fa-cubes"></i> ALL UOMS   </a></li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content container-fluid"> 
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                    <form class="form-horizontal  "  action="" method="post"  >                    
                            <div class="col-sm-2"> 
                                <label >  Date  </label>  
                                <input type="text"   autocomplete="off" class="form-control datepicker" name="date"   required=""   > 
                            </div>                                             
                            <div class="col-sm-3"> 
                                <label > Name  </label>  
                                    <input type="text"  name="name" value="" autocomplete="off" class="form-control"   required=""   > 
                            </div> 
                                                                                    
                            <div class="col-md-3">
                                <label> Code  </label>                          
                                <input type="text" class="form-control" name="code" required="" autocomplete="off" >
                            </div>  
                            <div class="col-md-4">
                                <label> Description  </label>                          
                                <input type="text" class="form-control" name="description" required="" autocomplete="off" >
                            </div>  
                            <div class="col-sm-2"> 
                                <label > IS UOM MULTIPLE?  </label>  
                                    <select class="form-control   "  required="" name="multiplestatus"  >                                       
                                                 <option selected value=0 > NOT A MULTIPLE </option>
                                                 <option  value=1 > IS A MULTIPLE </option>                                                
                                    </select>                                         
                            </div>

                            <div class="bp">                              
                                <div class="col-sm-3">  
                                    <label> BASE UOM </label>  
                                    <select class="form-control js-example-basic-single"   name="uombase" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM aduom where uomgroup=0  ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option></option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>    
                                             
                                     
                                </div>                                
                                <div class="col-sm-3">  
                                    <label>  BASE UNIT  </label>
                                    <input type="text"  class="form-control" autocomplete="off" name="uomrate"  >
                                </div>
                            </div>

                         

                                                                       
                                                                                            
                        <!-- submit button -->
                        <div class="form-group">                              
                            <div class="col-sm-offset-4 col-sm-10" style="padding:0.5rem;">
                                    <button type="submit"  name="submit" class="btn btn-success col-md-3 subbtn">  <i class="fa fa-circle-plus"></i>  Add UOM  </button> </div>
                        </div> 
                    </form>               
                </div>
            </div>
        </div>                       
        </section>

</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('../inc/sfooter.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('../inc/slinkjs.php') ?>

</body>
</html>

<script>
    $(document).ready(function() 
        {          
           $('.bp').hide(); 
            $("select[name='multiplestatus']") .change(function()
                   {    
                        var mstatus =  $("select[name='multiplestatus']").val(); 
                   //     alert(mstatus);
                        if( mstatus==1)
                               {   $('.bp').show();  }
                        else   {   $(".bp").hide();  }     
                   });
        });
</script>