<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('4','creates','R');     
$_SESSION['cuPages']='dai_dsrcreate.php';   
$cuPage='dai_dsrcreate.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='daily';
$menuh='Daily Process';
$phead='dsradd';
$page='Add DSR ';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php
   $dyna1='';
  $d = rand(1, 10000); 
  $invoice_no = "DSR".date("dmY").$d;
?>

<?php 
if(isset($_POST['code'])){
    $code = remove_junk(escape($_POST['code']));

    $stf_id = remove_junk(escape($_POST['stf_id']));
    $note = remove_junk(escape($_POST['note']));
    $apdate = remove_junk(escape($_POST['apdate']));
	
	if(isset($_POST['code'])){
	$ducode = mysqli_query($con,"SELECT * FROM tbl_requisition WHERE code = '$code'");
	}
	
	if($ducode->num_rows > 0) {
		save_msg('i','DSR number alrady exists! Plz try another');
		echo "<script>window.location='dai_dsrcreate.php'</script>";
	}else{
    $sql="INSERT INTO tbl_requisition (code,stf_id,note,apdate,uid,brid,date) 
    VALUES ('$code','$stf_id','$note','$apdate','$aid','$brid',now());";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $item_name = $_POST["item"];
    $item_code = $_POST["oty"];
    $imei_code = $_POST["imei"];
    $query = '';


    
    for($count = 0; $count<count($item_code); $count++)
    {
     $item_name_clean = mysqli_real_escape_string($con, $item_name[$count]);
     $item_code_clean = mysqli_real_escape_string($con, $item_code[$count]);
     $imei_code_clean = mysqli_real_escape_string($con, $imei_code[$count]);
     if($item_name_clean != '' && $item_code_clean != '')
     {
      $query .=' 
      INSERT INTO tbl_reqitem(item,oty,imei,code)
      VALUES("'.$item_name_clean.'", "'.$item_code_clean.'","'.$imei_code_clean.'","'.$code.'");
      ';
      
     }
    }
    if($query != ''){
        $sqlr=mysqli_multi_query($con, $query) or die(mysqli_error($con));
        while(mysqli_next_result($con)){;}
    }

    $efid=mysqli_affected_rows($con);    
    if($efid>0){
    $act =remove_junk(escape('DSR Number: '.$code));    
    write_activity($aid,'DSR','New DSR has been Added',$act);
    save_msg('s','Data Successfully Saved!');    
    }else{
    save_msg('w','Data Fail to Saved!');    
    }
    echo "<script>window.location='dai_dsrcreate.php'</script>";  
	}  
}
?>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<div class="content-wrapper">
    <?php include('../layout/page_head.php'); ?>
    <!-- Main content -->
        <section class="content">
            
            <div class="row">
                <div class="col-md-12">
                    <div class="box box-solid">
                        <div class="box-header with-border">
                            <h3 class="box-title">Add New DSR</h3>
                        </div>
                        <div class="box-body">
                            <?php echo display_msg();?>    
                            <form id="dynamic_form" action='dai_dsrcreate.php'   enctype="multipart/form-data" method="post" accept-charset="utf-8">
                                <div class="col-md-12 popup_details_div">
                                    <div class="col-md-1"></div>
                                    <div class="col-md-10">
                                        <div class="col-md-12">

                                                <div class="row">
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                           <label>Staff Name</label>   
                                                            <select class="form-control select2" name="stf_id" id="stf_id">
                                                            <option value="">-Select-</option>
                                                                <?php									
                                                                $querye=mysqli_query($con,"SELECT * FROM tbl_employe ORDER BY id ASC")or die(mysqli_error($con));
                                                                while ($rowse=mysqli_fetch_array($querye)){
                                                                ?>
                                                                <option value="<?php echo $rowse['id'];?>"><?php echo $rowse['name'];?></option>
                                                                <?php } ?>
                                                            </select>
                                                        </div>    
                                                    </div>  
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label>Voucher No.</label>    
                                                            <input type="text" name="code" maxlength="15" value="<?php echo $invoice_no; ?>" id="code" class="form-control" placeholder="e.g. ABA/SU/001" readonly/>
                                                        </div>    
                                                    </div> 
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label>Date</label>    
                                                            <input type="text"class="form-control datetimepicker" name="apdate" id="apdate" value="<?php if(isset($_SESSION['axes_expdata']['date'])){echo $_SESSION['axes_expdata']['date'];}else{ echo date('Y-m-d');}?>" placeholder="Date:" autocomplete="off" readonly/>
                                                        </div>    
                                                    </div>    
                                                </div>
                                                
                                                <div class="row">
                                                    <div class="col-md-12">
                                                            <table class="table table-bordered table-striped" id="user_table">
                                                                <thead>
                                                                    <tr>
                                                                        <th>Product</th>
                                                                        <th>Quantity</th>
                                                                        <th>IMEI</th>
                                                                        <th>Action</th>
                                                                    </tr>
                                                                </thead>
                                                                <tbody>

                                                                </tbody>
                                                                <tfoot>
                                                                    
                                                                </tfoot>
                                                            </table>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-13"> 
                                                        <div class="form-group">    
                                                            <label>Note</label>
                                                            <textarea class="form-control" name="note" id="note" maxlength="200" rows="4" placeholder="Write Note..."></textarea>    
                                                        </div>
                                                    </div>    
                                                
                                                </div> 
                                            
                                        </div>    
                                    </div>    
                                    <div class="col-md-1"></div>    
                                </div>
                                <div class="clearfix" ></div>
                                <div class="col-md-12 nopadding widgets_area"></div>    
                                <div class="row"style="margin-top: 15px" >
                                    <div class="col-md-8 ">
                                    </div>
                                    <div class="col-md-4 text-right" >
                                        <input type="submit" name="save" id="save" class="btn btn-flat bg-purple btn-sm" value="Save"/> <a href="dai_dsrlist.php" class="btn btn-flat bg-gray  ">Close</a>
                                    </div> 
                                </div>     
                            </form>    
                        </div>
                    </div>
                </div>
                <!-- <div class="col-md-3">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box box-solid">
                                <div class="box-header">
                                    <h3 class="box-title">History </h3>
                                </div>

                                <div class="box-body" >
                                  <?php if($_SESSION['access']=='1'){echo read_activity($aid,'DSR','A');}else{echo read_activity($aid,'DSR','U');}?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> -->
            </div>
            <?php									
                $queryp=mysqli_query($con,"SELECT * FROM tbl_item ORDER BY id ASC")or die(mysqli_error($con));
                while ($rowsp=mysqli_fetch_array($queryp)){
            
                    $id = $rowsp["id"];
                    $name = $rowsp["name"];
            
                    $dyna1 .= ' <option value="'. $id.'">'. $id.','.$name.'</option>';
                }
            ?>
            <script>
                
                var number=0;
                $(document).ready(function(){

                var count = 1;

                dynamic_field(count);

                function dynamic_field(number)
                {

                html = '<tr>';
                        html += '<td> <select name="item[]"  class="form-control"><option selected>Choose Please.....</option><?php echo $dyna1; ?></select></td>';
                        html += '<td><input type="text" name="oty[]" id="oty[]" class="form-control" /></td>';
                        html += '<td  style="width: 30%"> <textarea class="form-control" name="imei[]" id="imei[]" maxlength="200" rows="2" placeholder="1254455555555,455566666666"></textarea></td>';
                        if(number > 1)
                        {
                            html += '<td><button type="button" name="remove" id="" class="btn btn-danger remove">Remove</button></td></tr>';
                            $('tbody').append(html);
                        }
                        else
                        {
                            html += '<td><button type="button" name="add" id="add" class="btn btn-success">Add</button></td></tr>';
                            $('tbody').html(html);
                        }
                }

                $(document).on('click', '#add', function(){
                count++;
                dynamic_field(count);
                });

                $(document).on('click', '.remove', function(){
                count--;
                $(this).closest("tr").remove();
                });

               

                });


                $(function () {
                $("select").select2();
                });




            </script>
         
            
        </section>
    <!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>



</html>    