﻿@extends('layouts.admin')
@section('page-title')
{{ __('Manage Appraisal') }}
@endsection
@push('css-page')
<style>
    @import url({{ asset('css/font-awesome.css')}});
</style>
@endpush
@push('script-page')
<script src="{{ asset('js/bootstrap-toggle.js') }}"></script>

@push('script-page')
<script src="{{ asset('js/bootstrap-toggle.js') }}"></script>
<script>
    function filterByDate() {
        const start = document.getElementById('start_date').value;
        const end = document.getElementById('end_date').value;

        const params = new URLSearchParams(window.location.search);
        if (start) params.set('start_date', start);
        if (end) params.set('end_date', end);

        window.location.href = "{{ route('appraisal.index') }}?" + params.toString();
    }
</script>
@endpush
@endpush

@section('breadcrumb')
<li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('Home') }}</a></li>
<li class="breadcrumb-item">{{ __('Appraisal') }}</li>
@endsection

@section('action-button')
@can('Create Appraisal')
<a href="#" data-url="{{ route('appraisal.create') }}" data-ajax-popup="true" data-size="lg"
    data-title="{{ __('Create New Appraisal') }}" data-bs-toggle="tooltip" title="" class="btn btn-sm btn-primary"
    data-bs-original-title="{{ __('Create') }}">
    <i class="ti ti-plus"></i>
</a>
@endcan
@endsection

@section('content')
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-header card-body table-border-style">
                <form method="GET" class="mb-3 row g-3">
                    <div class="col-md-4">
                        <label for="start_date" class="form-label">{{ __('Start Date') }}</label>
                        <input type="date" id="start_date" name="start_date" value="{{ request('start_date') }}" class="form-control">
                    </div>
                    <div class="col-md-4">
                        <label for="end_date" class="form-label">{{ __('End Date') }}</label>
                        <input type="date" id="end_date" name="end_date" value="{{ request('end_date') }}" class="form-control">
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">{{ __('Filter') }}</button>
                        <a href="{{ route('appraisal.index') }}" class="btn btn-secondary">{{ __('Reset') }}</a>
                    </div>
                </form>

                {{-- <h5> </h5> --}}
                <div class="table-responsive">
                    <table class="table" id="pc-dt-simple">
                        <thead>
                            <tr>
                                <th>{{ __('Branch') }}</th>
                                <th>{{ __('Department') }}</th>
                                <th>{{ __('Designation') }}</th>
                                <th>{{ __('Employee') }}</th>
                                <th>{{ __('Target Rating') }}</th>
                                <th>{{ __('Overall Rating') }}</th>
                                <th>{{ __('Appraisal Date') }}</th>
                                @if (Gate::check('Edit Appraisal') || Gate::check('Delete Appraisal') || Gate::check('Show Appraisal'))
                                <th width="200px">{{ __('Action') }}</th>
                                @endif
                            </tr>
                        </thead>
                        <tbody>

                            @foreach ($appraisals as $appraisal)
                            @php
                            $designation = !empty($appraisal->employees)
                            ? $appraisal->employees->designation->id
                            : '-';
                            $targetRating = \App\Models\Utility::getTargetrating(
                            $designation,
                            $competencyCount,
                            );
                            if (!empty($appraisal->rating) && $competencyCount != 0) {
                            $rating = json_decode($appraisal->rating, true);
                            $starsum = array_sum($rating);
                            $overallrating = $starsum / $competencyCount;
                            } else {
                            $overallrating = 0;
                            }
                            @endphp
                            @php
                            if (!empty($appraisal->rating)) {
                            $rating = json_decode($appraisal->rating, true);
                            if (!empty($rating)) {
                            $starsum = array_sum($rating);
                            $overallrating = $starsum / count($rating);
                            } else {
                            $overallrating = 0;
                            }
                            } else {
                            $overallrating = 0;
                            }
                            @endphp
                            <tr>
                                <td>{{ !empty($appraisal->branches) ? $appraisal->branches->name : '' }}</td>
                                <td>{{ !empty($appraisal->employees) ? $appraisal->employees->department->name : '-' }}
                                </td>
                                <td>{{ !empty($appraisal->employees) ? $appraisal->employees->designation->name : '-' }}
                                </td>
                                <td>{{ !empty($appraisal->employees) ? $appraisal->employees->name : '-' }}</td>
                                <td>
                                    @for ($i = 1; $i <= 5; $i++)
                                        @if ($targetRating < $i)
                                        @if (is_float($targetRating) && round($targetRating)==$i)
                                        <i class="text-warning fas fa-star-half-alt"></i>
                                        @else
                                        <i class="fas fa-star"></i>
                                        @endif
                                        @else
                                        <i class="text-warning fas fa-star"></i>
                                        @endif
                                        @endfor

                                        <span class="theme-text-color">({{ number_format($targetRating, 1) }})</span>
                                </td>
                                <td>

                                    @for ($i = 1; $i <= 5; $i++)
                                        @if ($overallrating < $i)
                                        @if (is_float($overallrating) && round($overallrating)==$i)
                                        <i class="text-warning fas fa-star-half-alt"></i>
                                        @else
                                        <i class="fas fa-star"></i>
                                        @endif
                                        @else
                                        <i class="text-warning fas fa-star"></i>
                                        @endif
                                        @endfor
                                        <span class="theme-text-color">({{ number_format($overallrating, 1) }})</span>
                                </td>
                                <td>{{ $appraisal->appraisal_date }}</td>
                                <td class="Action">
                                    @if (Gate::check('Edit Appraisal') || Gate::check('Delete Appraisal') || Gate::check('Show Appraisal'))
                                    @can('Show Appraisal')
                                    <div class="action-btn me-2">
                                        <a href="#" class="mx-3 btn btn-sm bg-warning align-items-center"
                                            data-size="lg"
                                            data-url="{{ route('appraisal.show', $appraisal->id) }}"
                                            data-ajax-popup="true" data-size="md"
                                            data-bs-toggle="tooltip" title=""
                                            data-title="{{ __('Appraisal Detail') }}"
                                            data-bs-original-title="{{ __('View') }}">
                                            <span class="text-white"><i class="ti ti-eye"></i></span>
                                        </a>
                                    </div>
                                    @endcan


                                    @can('Edit Appraisal')
                                    <div class="action-btn me-2">
                                        <a href="#" class="mx-3 btn btn-sm bg-info align-items-center"
                                            data-size="lg"
                                            data-url="{{ route('appraisal.edit', $appraisal->id) }}"
                                            data-ajax-popup="true" data-size="md"
                                            data-bs-toggle="tooltip" title=""
                                            data-title="{{ __('Edit Appraisal') }}"
                                            data-bs-original-title="{{ __('Edit') }}">
                                            <span class="text-white"><i class="ti ti-pencil"></i></span>
                                        </a>
                                    </div>
                                    @endcan

                                    @can('Delete Appraisal')
                                    <div class="action-btn">
                                        {!! Form::open([
                                        'method' => 'DELETE',
                                        'route' => ['appraisal.destroy', $appraisal->id],
                                        'id' => 'delete-form-' . $appraisal->id,
                                        ]) !!}
                                        <a href="#"
                                            class="mx-3 btn btn-sm bg-danger align-items-center bs-pass-para"
                                            data-bs-toggle="tooltip" title=""
                                            data-bs-original-title="Delete" aria-label="Delete"><span class="text-white"><i
                                                    class="ti ti-trash"></i></span></a>
                                        </form>
                                    </div>
                                    @endcan
                                    </span>
                </div>
                @endif
                </td>
                </tr>
                @endforeach
                </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
</div>
@endsect