﻿@extends('layouts.admin')

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

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

@section('action-button')
<a href="#" data-url="{{ route('simple-goals.create') }}" data-ajax-popup="true"
    data-title="{{ __('Create Goal') }}" class="btn btn-sm btn-primary">
    <i class="ti ti-plus"></i>{{ __('Create Goal') }}
</a>
<a href="#" data-url="{{ route('goals.notify.create') }}" data-ajax-popup="true" data-title="{{ __('Send Goal 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>{{ __('Goal') }}</th>
                            <th>{{ __('Status') }}</th>
                            <th>{{ __('Approval Status') }}</th> {{-- New Column Header --}}
                            <th>{{ __('Due') }}</th>
                            <th width="120px">{{ __('Action') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($goals as $goal)
                        <tr>
                            <td>{{ $goal->title }}</td>
                            <td>
                                @php
                                $statusColors = [
                                'not_started' => 'secondary',
                                'in_progress' => 'secondary',
                                'on_hold' => 'warning',
                                'completed' => 'success',
                                'cancelled' => 'danger',
                                'archived' => 'dark',
                                ];

                                $color = $statusColors[$goal->status] ?? 'secondary';
                                @endphp

                                <span class="badge bg-{{ $color }} px-3 py-2 rounded-pill text-white">
                                    {{ __(ucwords(str_replace('_', ' ', $goal->status))) }}
                                </span>
                            </td>
                            <td> {{-- New Cell for Approval Status --}}
                                @if ($goal->is_active)
                                    <span class="badge bg-success px-3 py-2 rounded-pill text-white">{{ __('Active') }}</span>
                                @else
                                    <span class="badge bg-warning text-dark px-3 py-2 rounded-pill">{{ __('Pending Approval') }}</span>
                                @endif
                            </td>
                            <td>{{ optional($goal->due_date)->format('Y-m-d') ?? '—' }}</td>
                            <td class="Action">
                                <div class="dt-buttons">
                                    <span class="float-start">
                                        {{-- Edit Button --}}
                                        <div class="action-btn me-2">
                                            <a href="#" class="btn btn-sm bg-info"
                                                data-url="{{ route('simple-goals.edit', $goal) }}"
                                                data-ajax-popup="true"
                                                data-title="{{ __('Edit Goal') }}">
                                                <i class="ti ti-pencil text-white"></i>
                                            </a>
                                        </div>

                                        {{-- Activate Button (Only for Pending Goals & Authorized Users) --}}
                                        @if (!$goal->is_active && ($canApproveGoals ?? Auth::user()->can('manage goals')))
                                            <div class="action-btn me-2">
                                                {!! Form::open([
                                                    'method' => 'POST',
                                                    'route' => ['simple-goals.activate', $goal->id],
                                                    'id' => 'activate-form-' . $goal->id,
                                                    'onsubmit' => "return confirm('".__('Are you sure you want to activate this goal?')."');" // Simple JS confirmation
                                                ]) !!}
                                                <a href="#" class="btn btn-sm bg-success align-items-center"
                                                    data-bs-toggle="tooltip" title="{{ __('Activate Goal') }}"
                                                    onclick="document.getElementById('activate-form-{{ $goal->id }}').submit();">
                                                    <i class="ti ti-check text-white"></i>
                                                </a>
                                                {!! Form::close() !!}
                                            </div>
                                        @endif

                                        {{-- Delete Button --}}
                                        <div class="action-btn">
                                            {!! Form::open([
                                                'method' => 'DELETE',
                                                'route' => ['simple-goals.destroy', $goal->id],
                                                'id' => 'delete-form-' . $goal->id,
                                            ]) !!}
                                            <a href="#"
                                                class="btn btn-sm bg-danger align-items-center bs-pass-para"
                                                data-bs-toggle="tooltip"
                                                title="{{ __('Delete') }}"
                                                data-confirm="{{ __('Are You Sure?') . '|' . __('This action cannot be undone. Do you want to continue?') }}"
                                                data-text="{{ __('This action cannot be undone. Do you want to continue?') }}"
                                                data-confirm-yes="delete-form-{{ $goal->id }}">
                                                <i class="ti ti-trash text-white"></i>
                                            </a>
                                            {!! Form::close() !!}
                                        </div>
                                    </span>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>

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