@extends('layouts.dashboard')
@section('page-title')
    {{__('Timesheet')}}
@endsection
@push('script-page')
    <script type="text/javascript" src="{{ asset('js/html2pdf.bundle.min.js') }}"></script>
    <script>
        var filename = $('#filename').val();

        function saveAsPDF() {
            var element = document.getElementById('printableArea');
            var opt = {
                margin: 0.3,
                filename: filename,
                image: {type: 'jpeg', quality: 1},
                html2canvas: {scale: 4, dpi: 72, letterRendering: true},
                jsPDF: {unit: 'in', format: 'A4'}
            };
            html2pdf().set(opt).from(element).save();

        }

        $(document).ready(function () {
            var filename = $('#filename').val();
            $('#report-dataTable').DataTable({
                dom: 'lBfrtip',
                buttons: [
                    {
                        extend: 'excel',
                        title: filename
                    },
                    {
                        extend: 'pdf',
                        title: filename
                    }, {
                        extend: 'print',
                        title: filename
                    }, {
                        extend: 'csv',
                        title: filename
                    }
                ]
            });
        });

    </script>
@endpush
@section('content')
    <div class="main-content">
        <section class="section">
            <div class="section-header">
                <h1>{{__('Manage Timesheet')}}</h1>
                <div class="section-header-breadcrumb">
                    <div class="breadcrumb-item active"><a href="{{route('home')}}">{{__('Dashboard')}}</a></div>
                    <div class="breadcrumb-item">{{__('Timesheet')}}</div>
                </div>
            </div>
            <div class="section-body">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                                {{ Form::open(array('route' => array('report.timesheet'),'method'=>'get')) }}
                                <div class="row">
                                    <div class="col">
                                        <h5 class="h5 mb-0">{{__('Filter')}}</h5>
                                    </div>
                                    <div class="col-md-2">
                                        {{Form::label('start_date',__('Start Date'))}}
                                        {{Form::date('start_date',isset($_GET['start_date'])?$_GET['start_date']:date('Y-m-01'),array('class'=>'form-control'))}}
                                    </div>
                                    <div class="col-md-2">
                                        {{Form::label('end_date',__('End Date'))}}
                                        {{Form::date('end_date',isset($_GET['end_date'])?$_GET['end_date']:date('Y-m-t'),array('class'=>'form-control'))}}
                                    </div>
                                    <div class="col-md-2">
                                        {{ Form::label('branch', __('Branch')) }}
                                        {{ Form::select('branch', $branch,isset($_GET['branch'])?$_GET['branch']:'', array('class' => 'form-control select2')) }}
                                    </div>
                                    <div class="col-md-2">
                                        {{ Form::label('department', __('Department')) }}
                                        {{ Form::select('department', $department,isset($_GET['department'])?$_GET['department']:'', array('class' => 'form-control select2')) }}
                                    </div>
                                    <div class="col-auto apply-btn">
                                        {{Form::submit(__('Apply'),array('class'=>'btn btn-outline-primary btn-sm'))}}
                                        <a href="{{route('report.timesheet')}}" class="btn btn-outline-danger btn-sm">{{__('Reset')}}</a>
                                        <a href="#" class="btn btn-outline-warning btn-sm mb-1" onclick="saveAsPDF()" id="">{{__('Download')}}</a>
                                    </div>
                                </div>
                                {{ Form::close() }}
                            </div>
                        </div>
                    </div>
                </div>

                <div id="printableArea">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-wrap">
                                    <div class="card-body">
                                        <input type="hidden" value="{{$filterYear['branch'] .' '.__('Branch') .' '.__('Timesheet Report').' '}}{{$filterYear['start_date'].' to '.$filterYear['end_date'].' '.__('of').' '.$filterYear['department'].' '.'Department'}}" id="filename">

                                        <div class="row">
                                            <div class="col">
                                                {{__('Title')}} : <h6>{{__('Timesheet Report')}}</h6>
                                            </div>
                                            @if($filterYear['branch']!='All')
                                                <div class="col">
                                                    {{__('Branch')}} : <h6>{{($filterYear['branch']) }}</h6>
                                                </div>
                                            @endif
                                            @if($filterYear['department']!='All')
                                                <div class="col">
                                                    {{__('Department')}} : <h6>{{$filterYear['department'] }}</h6>
                                                </div>
                                            @endif
                                            <div class="col">
                                                {{__('Duration')}} : <h6>{{$filterYear['start_date'].' to '.$filterYear['end_date']}}</h6>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-6 col-md-6 col-sm-6 col-12">
                            <div class="card card-statistic-1">
                                <div class="card-wrap">
                                    <div class="card-header">
                                        <h4>{{__('Total Working Employee')}}</h4>
                                    </div>
                                </div>
                                <div class="card-stats">
                                    <div class="card-stats-title">
                                        <div class="progreess-status mt-2">
                                            <h6><strong>{{$filterYear['totalEmployee']}} </strong></h6>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-12">
                            <div class="card card-statistic-1">
                                <div class="card-wrap">
                                    <div class="card-header">
                                        <h4>{{__('Total Working Hours')}}</h4>
                                    </div>
                                </div>
                                <div class="card-stats">
                                    <div class="card-stats-title">
                                        <div class="progreess-status mt-2">
                                            <h6><strong>{{$filterYear['totalHours']}}</strong></h6>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        @foreach($timesheetFilters as $timesheetFilter)
                            <div class="col-lg-3 col-md-6 col-sm-6 col-12">
                                <div class="card card-statistic-1">
                                    <div class="card-wrap">
                                        <div class="card-header">
                                            <h6>{{$timesheetFilter->name}}</h6>
                                        </div>
                                    </div>
                                    <div class="card-stats">
                                        <div class="card-stats-title">
                                            <div class="progreess-status mt-2">
                                                {{__('Total Working Hours')}} : <h6><strong>{{$timesheetFilter->total}}</strong></h6>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <div class="table-responsive py-4">
                                    <table class="table table-striped mb-0" id="report-dataTable">
                                        <thead class="thead-light">
                                        <tr>
                                            <th>{{__('Employee ID')}}</th>
                                            <th>{{__('Employee')}}</th>
                                            <th>{{__('Date')}}</th>
                                            <th>{{__('Hours')}}</th>
                                            <th>{{__('Description')}}</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        @foreach($timesheets as $timesheet)
                                            <tr>
                                                <td><a href="{{route('employee.show',\Illuminate\Support\Facades\Crypt::encrypt($timesheet->employee_id))}}" class="btn btn-sm btn-primary">{{ \Auth::user()->employeeIdFormat($timesheet->employee_id) }}</a></td>
                                                <td>{{(!empty($timesheet->employee)) ? $timesheet->employee->name:''}}</td>
                                                <td>{{\Auth::user()->dateFormat($timesheet->date)}}</td>
                                                <td>{{$timesheet->hours}}</td>
                                                <td>{{$timesheet->remark}}</td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
@endsection

