<?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='dsrlist';
$page='Update DSR ';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?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']));
    $item_name = $_POST["item"];
    $item_id = $_POST["id"];
    $item_code = $_POST["oty"];
    $imei_code = $_POST["imei"];
    $item_sqty = $_POST["sellqty"];
    $item_rqty = $_POST["returnqty"];
	
    $sql="UPDATE tbl_requisition SET stf_id='$stf_id',note='$note',apdate='$apdate' Where code='$code'";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));

   $idss=count($item_name);
//    for($i=0;$i<$idss;$i++) {
//     mysqli_query($con, "UPDATE tbl_reqitem set item='" . $item_name[$i] . "', oty='" . $item_code[$i] . "', sellqty='" . $item_code[$i] . "', returnqty='" . $item_code[$i] . "' WHERE code='" . $code . "'");
//    }

    $query = '';
    for($count = 0; $count<$idss; $count++)
    {
     $item_id_clean = mysqli_real_escape_string($con, $item_id[$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]);
     $item_sqty_clean = mysqli_real_escape_string($con, $item_sqty[$count]);
     $item_rqty_clean = mysqli_real_escape_string($con, $item_rqty[$count]);
      $query .='
        UPDATE tbl_reqitem SET item="'.$item_name_clean.'",oty="'. $item_code_clean.'", imei="'. $imei_code_clean.'",sellqty="'. $item_sqty_clean.'", returnqty="'. $item_rqty_clean.'"
        WHERE id="'.$item_id_clean.'";
       ';
      
    }
    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('Update DSR Number: '.$code));    
    write_activity($aid,'DSR','DSR has been Updated',$act);
    save_msg('s','Data Successfully Saved!');    
    }else{
    save_msg('w','Data Fail to Saved!');    
    }
    echo "<script>window.location='dai_dsrlist.php'</script>";   
}
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>


<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">Update DSR</h3>
                        </div>
                        <div class="box-body">
                            <?php echo display_msg();?>  
                            <?php 
                                if(isset($_POST['editdsr'])){
                                $ids = $_POST['editdsr'];
                                $sql="SELECT * FROM tbl_requisition WHERE code='".$ids."' ORDER BY id ASC LIMIT 1";    
                                $admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
                                $adm=mysqli_fetch_array($admin); 
                                
                            ?>  
                            <form id="dynamic_form" action='dai_dsredit.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 if($rowse['id']==$adm['stf_id']){?> Selected<?php } ?>><?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 $adm['code']; ?>" 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 echo $adm['apdate']; ?>" 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 Name</th>
                                                                        <th>Quantity</th>
                                                                        <th>IMEI</th>
                                                                        <th>Sell Quantity</th>
                                                                        <th>Return Quantity</th>
                                                                    </tr>
                                                                </thead>
                                                                <tbody>
                                                                <?php
                                                                    $sqlrei="SELECT * FROM tbl_reqitem WHERE code='$ids'";    
                                                                    $queryrei=mysqli_query($con,$sqlrei)or die(mysqli_error($con));
                                                                    $i=0;
                                                                    while ($reirow=mysqli_fetch_array($queryrei)){
                                                                        //$id=$reirow['id'];
                                                                        // $item=$reirow['item'];
                                                                        // $sqlitem="SELECT * FROM tbl_item WHERE id='$item'";
                                                                        // $queryitem=mysqli_query($con,$sqlitem)or die(mysqli_error($con));
                                                                        // $rowitem=mysqli_fetch_array($queryitem);
                                                                        // $itemname=$rowitem['name'];
                                                                    ?>
                                                                    <tr>
                                                                        <td> <select class="form-control select2" name="item[]" id="item">
                                                                                <option value="">-Select-</option>
                                                                                <?php									
                                                                                $queryi=mysqli_query($con,"SELECT * FROM tbl_item ORDER BY id ASC")or die(mysqli_error($con));
                                                                                while ($rowsi=mysqli_fetch_array($queryi)){
                                                                                ?>
                                                                                <option value="<?php echo $rowsi['id'];?>" <?php if($rowsi['id']==$reirow['item']){?> Selected<?php } ?>><?php echo $rowsi['name'];?></option>
                                                                                <?php } ?>
                                                                            </select>
                                                                        </td>
                                                                        <input type="hidden" name="id[]"  value="<?php echo $reirow['id'];?>"  class="form-control" />
                                                                        <td><input type="text" name="oty[]"  value="<?php echo $reirow['oty'];?>"  class="form-control" /></td>
                                                                        <td  style="width: 30%"><textarea class="form-control" name="imei[]" id="imei[]" maxlength="200" rows="2"><?php echo $reirow['imei'];?></textarea></td>
                                                                        <td><input type="text" name="sellqty[]"  value="<?php echo $reirow['sellqty'];?>" class="form-control" /></td>
                                                                        <td><input type="text" name="returnqty[]"  value="<?php echo $reirow['returnqty'];?>"  class="form-control" /></td>
                                                                    </tr>
                                                                    <?php } ?>

                                                                </tbody>
                                                            </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..."><?php echo $adm['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>    
                         <?php } ?>
                        </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>  
        </section>
    <!-- /.main content -->    
</div>

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



</html>    