@extends('layouts.master')

@section('content')
<!-- DataTables -->
<link rel="stylesheet" href="{{ asset('/assets/') }}/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="{{ asset('/assets/') }}/switch/rcswitcher.css">
<link rel="stylesheet" href="{{ asset('/assets/') }}/alert/pnotify.css">
<link rel="stylesheet" href="{{ asset('/assets/') }}/alert/sweetalert.css">
<link rel="stylesheet" href="{{ asset('/assets/') }}/alert/sweet-alert-animations.css">
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
                <header class="panel-heading" style="height:50px;">
                    <div class="pull-left">
                        {{$title}}
                    </div>
                    <div>
                        <a style="margin-left: 3px" href="{{ url('admin/leave_requests') }}" class="btn btn-sm btn-info pull-right"><i class="fa fa-list"></i> Leave List</a>
                    </div>
                </header>
                
                <div class="clearfix"></div>
            <div class="panel-body">
                <form action="{{ url('admin/leave_req_approved/'.$leave_req->id) }}" method="POST" class="form-horizontal" role="form">
                    {{ csrf_field() }}
                    <div class="col-md-7">
                        <div class="form-group">
                        <label for="purpose" class="col-sm-2">Name : </label>
                        <div class="col-sm-10">
                            @foreach($all_users as $user)
                                @if($user['id']==$leave_req->user_id)
                                   {{$user['name']}}
                                @endif
                            @endforeach
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="purpose" class="col-sm-2">Purpose : </label>
                        <div class="col-sm-10">
                            @foreach($purposes as $purp)
                                @if($purp['id']==$leave_req->purpose)
                                   {{$purp['name']}}
                                @endif
                            @endforeach
                        </div>

                    </div>
                    <div class="form-group">
                        <label for="description" class="col-sm-2">Application</label>
                        <div class="col-sm-7">
                            {{ $leave_req->description }}
                        </div>
                    </div>
                        
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                        <label for="due_date" class="col-sm-2">From :</label>
                        <div class="col-sm-8">
                            <input type='text' name="from" id="from" class="form-control date" placeholder="From Date" value="{{ date('Y-m-d', strtotime($leave_req->from)) }}" required="">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="due_date" class="col-sm-2">To :</label>
                        <div class="col-sm-8">
                            <input type='text' name="to" id="to" value="{{ date('Y-m-d', strtotime($leave_req->to)) }}" class="form-control date" placeholder="To Date" required="">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            @if($leave_req->approval == 0)
                                <button type="submit" class="btn btn-info">Approved</button>
                            @else
                                <span class="btn btn-info disabled">Already Approved</span>
                            @endif 
                            <a href="{{ url('admin/leave_req_discard') }}/{{ $leave_req->id }}" class='btn btn-info'> Discard </a>
                        </div>
                    </div>
                    </div>
                </form>
            </div>

        </div>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <header class="panel-heading" style="height:40px;">
                    <div class="pull-left">
                        Leave Summary 
                    </div>
                </header>
                <div class="clearfix"></div>
                <table class="table table-bordered">
                        <thead>
                            <tr>

                                <th class="text-right"> # </th>
                                <th class="text-center">
                                    Leave Head
                                </th>
                                <th class="text-center">
                                    Allowed
                                </th>
                                <th class="text-center">
                                   Taken 
                                </th>
                                <th class="text-center">
                                   Remaining
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                       
                            <?php $count=1; ?>
                            @foreach($purposes as $pur)
                                <tr>
                                    <td class="text-right">{{ $count++ }}</td>
                                    <td class="text-center">
                                        {{ $pur['name']}}
                                    </td>
                                    <td class="text-center">
                                        {{ $pur['approved_days'] }} 
                                    </td>
                                    <td class="text-center">
                                       @if(!empty($pur['taken']))
                                            {{ $pur['taken'] }}
                                        @else
                                        {{ 0 }}
                                        @endif
                                    </td>
                                    <td class="text-center">
                                        <span class="label label-primary">{{ $remaining = ($pur['approved_days'] - $pur['taken']) }} days</span>
                                    </td>
                                </tr>   
                            @endforeach
                        </tbody>
                    </table>
            </div>

        </div>
    </div><div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <header class="panel-heading" style="height:40px;">
                    <div class="pull-left">
                        Leave History 
                    </div>
                </header>
                <div class="clearfix"></div>
                <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="text-right"> # </th>
                                <th class="text-center">
                                    Name
                                </th>
                                <th class="text-center">
                                    Purpose
                                </th>
                                <th class="text-center">
                                   Leave From
                                </th>
                                <th class="text-center">
                                   Leave To
                                </th>
                                <th class="text-center">
                                   Days
                                </th>
                                <th class="text-center">
                                    Status
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                       
                            <?php $count=1; ?>
                            @foreach($all_leave_req as $info)
                                <tr>
                                    <td class="text-right">{{ $count++ }}</td>                                    
                                    <td class="text-center">
                                        @foreach($all_users as $user)
                                            @if($user['id']==$info->user_id)
                                               {{$user['name']}}
                                            @endif
                                        @endforeach
                                    </td>
                                    <td class="text-center">
                                        @foreach($purposes as $purp)
                                            @if($purp['id']==$info->purpose)
                                               {{$purp['name']}}
                                            @endif
                                        @endforeach
                                    </td>
                                    <td class="text-center">
                                        {{ date("jS-F-Y", strtotime($info->from)) }}
                                    </td>
                                    <td class="text-center">
                                        {{ date("jS-F-Y", strtotime($info->to)) }}
                                    </td>
                                    <td class="text-right">
                                        <?php
                                        /*$start = strtotime($info->from);
                                        $end = strtotime($info->to);

                                        $days_between = (ceil(abs( $end - $start) / 86400))+1;
                                        echo $days_between;*/
                                        ?>
                                        {{ $info->days }}
                                    </td>
                                    <td class="text-center">
                                        @if($info->approval == 0)
                                            <span class="label label-primary">Pending</span>
                                        @else
                                            <span class="label label-success">Approved</span>
                                        @endif
                                    </td>
                                </tr>   
                            @endforeach
                        </tbody>
                    </table>
            </div>

        </div>
    </div>

</div>
    


<div class="modal fade" id="modalPreview">
    <div class="modal-dialog" style='width: 50%;'>
    
    </div>
</div>
@stop

@section('style')
<style>
    .modal-dialog {
      width: 70% !important;
      height: 70% !important;
    }

    .modal-content {
      height: auto;
      min-height: 70% !important;
      border-radius: 0;
    }
</style>
@stop

@section('script')
<script src="{{ asset('/assets/') }}/js/jquery.dataTables.min.js"></script>
<script src="{{ asset('/assets/') }}/js/dataTables.bootstrap.min.js"></script>
<script src="{{ asset('/assets/') }}/switch/rcswitcher.js"></script>
<script src="{{ asset('/assets/') }}/alert/sweetalert.min.js"></script>
<script src="{{ asset('/assets/') }}/alert/pnotify-init.js"></script>
<script src="{{ asset('/assets/') }}/alert/notify.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
     $( ".date" ).datepicker({
          changeMonth: true,
          changeYear: true,
          dateFormat:"yy-mm-dd"
     });
});

</script>
<script type="text/javascript">
$(document).ready(function() {
    $('body').on('hidden.bs.modal', '.modal', function () {
      $(this).removeData('bs.modal');
    });
    var table=$("#example").DataTable({
         "searching": true,
         "lengthChange": true,
         "ordering": true,
         stateSave: true       
    });

});
</script>
<script type="text/javascript">
    jQuery(document).ready(function($)
    {
        $('.chk-active').click(function(event)
        {
            var id = $(this).data('id');
            var stat = $(this).data('value');
            /*
            if($(this).is(':checked'))
            {
                stat = 10;
            }
            else
            {
                stat = 0;
            }*/

            $.ajax({
                url: "{{url('admin/leave_accept')}}",
                type: 'POST',
                data: {id: id,stat: stat},
            })
            .done(function(data){
                alert(data);
                location.reload();
            });
            
        });
        
    });
</script>
@if(Session::has('msg'))
    <div class="alert alert-success center-block msg_alt" >
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <strong><i class="fa fa-check"></i></strong>&nbsp;
    {{Session::get('msg')}}
    </div>
@endif
@stop