<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('4','views','R');     
$_SESSION['cuPages']='dai_dsrlist.php';   
$cuPage='dai_dsrlist.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='daily';
$menuh='Daily Process';
$phead='dsrlist';
$page='DSR List';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php 
if(isset($_POST['delreq'])){
$id=$_POST['delreq'];
$sql1="SELECT * FROM tbl_requisition WHERE id='$id'";
$query1=mysqli_query($con,$sql1)or die(mysqli_error($con));
$row=mysqli_fetch_array($query1);
$status=$row['status'];
$code=$row['code'];
// echo $status;
// exit();


$sql="DELETE FROM tbl_reqitem WHERE id='$code'";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
$sql2="DELETE FROM tbl_requisition WHERE id='$id'";
$query2=mysqli_query($con,$sql2)or die(mysqli_error($con));

$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('DSR Number: '.$code));    
write_activity($aid,'DSR','DSR has been deleted',$act);       
save_msg('s','DSR Successfully Deleted!!!');
}else{
save_msg('w','DSR Fail to Delete!!!');   
}
echo "<script>window.location='dai_dsrlist.php'</script>";     
}
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 
?>
<div class="content-wrapper">
    <?php include('../layout/page_head.php'); ?> 
    <!-- Main content -->
        <section class="content">
            
            <div class="row">
                <div class="col-md-8">
                    <div class="box box-solid">
                        <div class="box-header with-border">
                            <h3 class="box-title">DSR List</h3>
                        </div>
                        <div class="box-body">
                            <?php echo display_msg();?>    
                            <div class="col-md-12 table-responsive">
                                <table class="table table-bordered table-striped" id="datarec">
                                    <thead class="text-uppercase">
                                        <tr>
                                            <th width="30px">SN</th>
                                            <th width="60px">Voucher No</th>  
                                            <th width="60px">Staff Name</th>
                                            <th width="60px">Date</th>    
                                            <th width="60px">Action</th>  
                                        </tr>
                                    </thead>    
                                    <tbody>
                                        <?php
                                        $sql="SELECT * FROM tbl_requisition ORDER BY id ASC";    
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                        $id=$row['id'];
                                        $stf_id=$row['stf_id'];
                                        $sql1="SELECT * FROM tbl_employe where id= $stf_id";
                                        $query1=mysqli_query($con,$sql1);
                                        $stf=mysqli_fetch_array($query1);
                                        $stfname =$stf['name'];
                                        ?>
                                            <tr>
                                                <td width="30px"><?php echo count_id();?></td>
                                                <td width="60px"><?php echo $row['code'];?></td>       
                                                <td width="65px"><?php echo $stfname;?></td>
                                                <td width="65px"><?php echo $row['apdate'];?></td>
                                                <td nowrap="">
                                                    <a class="btn btn-flat bg-purple details-invoice" href="#" id="inv_<?php echo $row['id']; ?>" data-toggle="tooltip" title="View" ><i class="fa fa-eye cat-child"></i></a>    
                                                    <a class="btn btn-flat bg-purple" href="#" onclick="edit_item('ED_<?php echo $row['code'];?>')" data-toggle="tooltip" title="Edit"><i class="fa fa-edit"></i></a>   
                                                    <a class="btn btn-flat bg-purple" href="#" onclick="remove_item('DL_<?php echo $row['id'];?>')" data-toggle="tooltip" title="Delete"><i class="fa fa-trash"></i></a>
                                                    <form action="dai_dsrlist.php" id="DL_<?php echo $row['id'];?>" method="post" >
                                                    <input type="hidden" name="delreq" value="<?php echo $row['id']; ?>" />
                                                    </form>
                                                    <form action="dai_dsredit.php" id="ED_<?php echo $row['code'];?>" method="post" >
                                                    <input type="hidden" name="editdsr" value="<?php echo $row['code']; ?>" />
                                                    </form> 
                                                </td>     
                                            </tr> 
                                        <?php }?>     
                                    </tbody>   
                                </table>
                            </div>
                            <div class="clearfix" ></div>  
                            <div class="row"style="margin-top: 15px" >
                                <div class="col-md-12 table-responsive">    
                                    <div class="col-md-8"></div>
                                    <div class="col-md-4 text-right" >
                                        <a href="dai_dsrcreate.php" class="btn btn-flat bg-purple">Add DSR</a>   
                                    </div>
                                </div>    
                            </div>    
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <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 include('../layout/print.php'); ?>  
        </section>
    <!-- /.main content -->  
       
</div>
<script type="text/javascript">

$("#print").click(function() {    
var mode = 'iframe'; //popup
var close = mode == "popup";
var options = {
mode: mode,
popClose: close
};
$("div.printableArea").printArea(options);
});
    
function pagelist(){    
var perPage = 10;

new List("test-list", {
valueNames: ["pino","name"],
page: perPage,
plugins: [

ListPagination({
paginationClass: "pagination-layout",
left: 2,
right: 2
})
]
}).on("updated", function(list) {
var isFirst = list.i == 1;
var isLast = list.i > list.matchingItems.length - list.page;

// make the Prev and Nex buttons disabled on first and last pages accordingly
$(".pagination-prev.disabled, .pagination-next.disabled").removeClass(
"disabled"
);
if (isFirst) {
$(".pagination-prev").addClass("disabled");
}
if (isLast) {
$(".pagination-next").addClass("disabled");
}

// hide pagination if there one or less pages to show
if (list.matchingItems.length <= perPage) {
$(".pagination-wrap").hide();
} else {
$(".pagination-wrap").show();
}
});
}
    
$(".pagination-next").click(function(e) {
$(".pagination-layout .active")
.next()
.trigger("click");
e.preventDefault();    
});
    
$(".pagination-prev").click(function(e) {
$(".pagination-layout .active")
.prev()
.trigger("click");
e.preventDefault();    
});    
    
$("#print").click(function() {    
var mode = 'iframe'; //popup
var close = mode == "popup";
var options = {
mode: mode,
popClose: close
};
$("div.printableArea").printArea(options);
});    
        
function setPrinterConfig(){
resolution = $('#resolution').val()!=undefined?$('#resolution').val():"A4";
rotate = $('#rotate').val()!=undefined?$('#rotate').val():"portrait";

$('.page').removeClass('A2 A4 A5 Letter Legal');
$('.page').addClass(resolution);
$('.page').removeClass('portrait landscape');
$('.page').addClass(rotate);

w = "21cm"; h = "29.7cm";
if( resolution == "A4" ){
w = "21cm"; h = "29.7cm";     
}else if( resolution == "A5" ){
w = "14.8cm"; h = "21cm";
}else if( resolution == "Letter" ){
w = "21.6cm"; h = "27.9cm";
}else if( resolution == "Legal" ){
w = "21.6cm"; h = "35.6cm";
}
if( rotate == "landscape" ){
$('#pageInit').html("@page{size: "+h+" "+w+"}");
}else{
$('#pageInit').html("@page{size: "+w+" "+h+"}");
}
scaleTemplate();
};

function getPageHeight(){
resolution = $('#resolution').val()!=undefined?$('#resolution').val():"A4";
rotate = $('#rotate').val()!=undefined?$('#rotate').val():"portrait";

w = 21; h = 29.7;
if( resolution == "A4" ){
w = 21; h = 29.7;      
}else if( resolution == "A5" ){
w = 14.8; h = 21;
}else if( resolution == "Letter" ){
w = 21.6; h = 27.9;
}else if( resolution == "Legal" ){
w = 21.6; h = 35.6;
}
if( rotate == "landscape" ){
return w;
}else{
return h;
}
};

function getPageWidth(){
resolution = $('#resolution').val()!=undefined?$('#resolution').val():"A4";
rotate = $('#rotate').val()!=undefined?$('#rotate').val():"portrait";

w = 21; h = 29.7;
if( resolution == "A4" ){
w = 21; h = 29.7;      
}else if( resolution == "A5" ){
w = 14.8; h = 21;
}else if( resolution == "Letter" ){
w = 21.6; h = 27.9;
}else if( resolution == "Legal" ){
w = 21.6; h = 35.6;
}
if( rotate == "landscape" ){
return h;
}else{
return w;
}
};

function scaleTemplate(){
$.each($('[id=wrap_invoice]'), function(i, wrap_invoice){
var scale = 1;
if( $(wrap_invoice).parent().is(".wrapper") ){
$(wrap_invoice).unwrap();
}
var parent = $(wrap_invoice).parent();
var padding = $(parent).outerWidth()-$(parent).width();
var outer_height = $(parent).height();
var inner_height = $(wrap_invoice).outerHeight();
var outer_width = $(parent).width();
var inner_width = $(wrap_invoice).outerWidth();
if( outer_width < inner_width ){
if( padding == 0 ){
scale = parseFloat(outer_width/(inner_width+20));
padding = 20;
}else{
scale = parseFloat(outer_width/inner_width);
padding = 0;
}
var x = padding/2;
var origin = x.toFixed(2)+"px 0px 0px";
$(wrap_invoice).css({'-webkit-transform': 'scale('+(scale.toFixed(2))+')', '-webkit-transform-origin': origin});
var wrapper = $("<div class='wrapper'></div>");
$(wrapper).css({'width': inner_width*scale, 'height': inner_height*scale, "overflow": "hidden"});
$(wrap_invoice).wrap(wrapper);
}else{
$(wrap_invoice).css({'-webkit-transform': '', '-webkit-transform-origin': ""});
}
});
};

document.table_border = false;
document.table_strip = false;
document.table_border = true;
document.table_strip = true;

$(document).ready(function(){
$('#wrap_invoice table').removeClass();
$('#wrap_invoice table').addClass('table_invoice table_invoice-condensed');

$('#wrap_invoice table').addClass('table_invoice-bordered');
  
$('#wrap_invoice table').addClass('table_invoice-striped');
  
  

setPrinterConfig();
$('body').on('keyup', function(ev){
if( ev.keyCode == 27 ){
$('#close_page').click();
}
});

$('#close_page').click(function(){
window.close();
window.parent.window.close();
return false;
});
window.onresize = function() {
scaleTemplate();
}
scaleTemplate();
});
    
setTimeout(function() {
$(window).trigger("resize");
}, 100);
    
function sethead(){
var maxh = 0;
var ahe = $("#adheight").height();
var ihe = $("#inheight").height();

if(ahe>ihe){
maxh=ahe;   
}else{
maxh=ihe;    
}
if(maxh>ahe){
 $("#adheight").height(maxh);    
}else if(maxh>ihe){
 $("#inheight").height(maxh);        
}    
};
sethead();    
</script>
<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function() {
$('#datarec').DataTable({stateSave: true});   
});
function edit_item(id) {
   document.getElementById(id).submit(); 
}
function remove_item(id) {
        bootbox.confirm({
            title: "Confirm",
            message: "Are you sure to delete ?",
            buttons: {
                confirm: {
                    label: 'Yes',
                    className: 'btn-flat bg-purple'
                },
                cancel: {
                    label: 'Cancel',
                    className: 'btn-flat bg-gray'
                }
            },
            callback: function (result) {
                if (result == true) {
                    document.getElementById(id).submit();
                }
            }
        });

    }

$(".details-invoice").click(function (e) {
id_arr = $(this).attr('id');
id = id_arr.split("_");

$.ajax({
url: 'dai_viewdsrlist.php',
method: "POST",
data:{ 
invid: id[1]
},
success: function(data){
$('#listitem').html(data);
pagelist();    
}
});    
    
$.ajax({
url: 'dai_viewdsr.php',
method: "POST",
data:{ 
print: id[1]
},
success: function(data){
$('#invhold').html(data);
sethead();    
}
});
    
$('.right-side').toggle('slide', { direction: 'right' }, 300);    

e.preventDefault();
});

$(document).on('click', '#closepop', function() {
if($('.right-side').is(':visible')) { $('.right-side').toggle('slide', { direction: 'right' }, 300); }   
});    

$('#listitem').on('click','li', function(){
   $(this).addClass('active').siblings().removeClass('active');
});
    
$(document).on('click', '.invdsr', function() {
id_arr = $(this).attr('id');
id = id_arr.split("_");
var ids = id[1];
$.ajax({
url: 'dai_viewdsr.php',
method: "POST",
data:{ 
print: ids
},
beforeSend: function() {
$("#invhold").fadeOut('slow');
},    
success: function(data){    
$('#invhold').html(data);
$("#invhold").fadeIn('slow');    
sethead();    
}
});     
}); 


</script>
<!-- /page script -->
</html>    