﻿@extends('layouts.admin')

@section('page-title', __('Manage Appraisals'))

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

@section('action-button')
<a href="#" data-url="{{ route('appraisal_reviews.create') }}" data-ajax-popup="true"
    data-title="{{ __('Create Appraisal') }}" class="btn btn-sm btn-primary">
    <i class="ti ti-plus"></i>{{ __('Create Appraisal') }}
</a>
<a href="#" data-url="{{ route('appraisal_reviews.notify.create') }}" data-ajax-popup="true" data-title="{{ __('Send Review Reminders') }}" class="btn btn-sm btn-primary">
    <i class="ti ti-mail"></i> {{ __('Notify Employees') }}
</a>
@endsection

@section('content')
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body table-border-style">
                    <table class="table datatable">
                        <thead>
                            <tr>
                                <th>{{ __('Employee') }}</th>
                                <th>{{ __('Goals') }}</th>
                                <th>{{ __('Rating Type') }}</th>
                                <th>{{ __('Status') }}</th>
                                <th>{{ __('Final Rating') }}</th>
                                <th>{{ __('Process') }}</th>
                                <th width="120px">{{ __('Action') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($appraisals as $appraisal)
                                                                                    <tr>
                                                                                        <td>{{ optional($appraisal->employee)->name }}</td>
                                                                                        <td>
                                                                                            @foreach ($appraisal->goals as $goal)
                                                                                            <span class="badge bg-light text-dark mb-1">{{ $goal->title }}</span><br>
                                                                                            @endforeach
                                                                                        </td>
                                                                                        <td>{{ optional($appraisal->ratingType)->name }}</td>
                                                                                        <td>
                                                                                            @php
                                $statusColor = $appraisal->status === 'closed' ? 'success' : 'warning';
                                                                                            @endphp
                                                                                            <span class="badge bg-{{ $statusColor }} px-3 py-2 rounded-pill text-white">
                                                                                                {{ __(ucwords(str_replace('_', ' ', $appraisal->status))) }}
                                                                                            </span>
                                                                                        </td>
                                                                                        <td>
                                                                                            @if ($appraisal->final_score === 'ungraded')
                                                                                                <span class="text-info">{{ __('Ungraded') }}</span>
                                                                                            @elseif ($appraisal->final_score !== null)
                                                                                                {{ $appraisal->final_score }}
                                                                                            @else
                                                                                                <span class="text-muted">{{ __('N/A') }}</span>
                                                                                            @endif
                                                                                        </td>
                                                                                        <td>
                                                                                        @if($appraisal->status === 'closed' && !$isEmployee)
                                                                                            @if($appraisal->appraisalFinalScore)
                                                                                                <span class="badge bg-secondary">
                                                                                                    {{ ucwords(str_replace('_', ' ', $appraisal->appraisalFinalScore->action_type)) }}
                                                                                                </span>
                                                                                            @else
                                                                                                                                    {{-- Display the "Process" button if a record does not exist --}}
                                                                                                                                    <a href="#" class="btn btn-sm btn-success"
                                                                                                                                        data-url="{{ route('appraisal_reviews.process_final_score', $appraisal->id) }}"
                                                                                                                                        data-ajax-popup="true" data-title="{{ __('Performance Outcomes') }}">
                                                                                                                                        <span class="me-1">{{ __('Process') }}</span>
                                                                                                                                        <i class="ti ti-chart-line"></i>
                                                                                                                                    </a>
                                                                                            @endif
                                                                                        @endif
                                                                                    </td>
                                                                                        <td class="Action">
                                                                                            <div class="dt-buttons">
                                                                                                <span class="float-start">
                                                                                                    <div class="action-btn me-2">
                                                                                                        <a href="#" class="btn btn-sm bg-info"
                                                                                                            data-url="{{ route('appraisal_reviews.edit', $appraisal) }}"
                                                                                                            data-ajax-popup="true"
                                                                                                            data-title="{{ __('Edit Appraisal') }}">
                                                                                                            <i class="ti ti-pencil text-white"></i>
                                                                                                        </a>
                                                                                                    </div>

                                                                                                    @if ($appraisal->status === 'on_going' && $userIsAdmin)
                                                                                                                                                                        {{-- Added $userIsAdmin check here --}}
                                                                                                                                                                        <div class="action-btn me-2">
                                                                                                                                                                            {!! Form::open([
                                                                                                            'method' => 'POST',
                                                                                                            'route' => ['appraisal_reviews.close', $appraisal->id],
                                                                                                            'id' => 'close-form-' . $appraisal->id,
                                                                                                        ]) !!}
                                                                                                                                                                            <a href="#"
                                                                                                                                                                                class="btn btn-sm bg-danger align-items-center bs-pass-para"
                                                                                                                                                                                data-bs-toggle="tooltip"
                                                                                                                                                                                title="{{ __('Close Appraisal') }}"
                                                                                                                                                                                data-confirm="{{ __('Are You Sure?') . '|' . __('Closing this appraisal will finalize it and prevent further ratings/comments. Do you want to continue?') }}"
                                                                                                                                                                                data-text="{{ __('Closing this appraisal will finalize it and prevent further ratings/comments. Do you want to continue?') }}"
                                                                                                                                                                                data-confirm-yes="close-form-{{ $appraisal->id }}">
                                                                                                                                                                                <i class="ti ti-lock text-white"></i>
                                                                                                                                                                            </a>
                                                                                                                                                                            {!! Form::close() !!}
                                                                                                                                                                        </div>
                                                                                                    @endif
                                                                                                    @if (!$isEmployee)
                                                                                                                                                                        <div class="action-btn">
                                                                                                                                                                            {!! Form::open([
                                                                                                            'method' => 'DELETE',
                                                                                                            'route' => ['appraisal_reviews.destroy', $appraisal->id],
                                                                                                            'id' => 'delete-form-' . $appraisal->id,
                                                                                                        ]) !!}
                                                                                                                                                                            <a href="#"
                                                                                                                                                                                class="btn btn-sm bg-danger align-items-center bs-pass-para"
                                                                                                                                                                                data-bs-toggle="tooltip"
                                                                                                                                                                                title="{{ __('Delete') }}">
                                                                                                                                                                                <i class="ti ti-trash text-white"></i>
                                                                                                                                                                            </a>
                                                                                                                                                                            {!! Form::close() !!}
                                                                                                                                                                        </div>
                                                                                                    @endif
                                                                                                    </span>
                                                                                                    </div>
                                                                                                    </td>
                                                                                                    </tr>
                            @endforeach
                                                                        </tbody>
                                                                        </table>

                                                                        <div class="mt-3">{{ $appraisals->links() }}
                                                                        </div>
                                                                        </div>
                                                                        </div>
                                                                        </div>
                                                                        </div>
@endsecti