@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Relatório de Indicadores de TI'))

@section('content')
    <div class="card shadow-sm">

        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' =>
                "<i class='fa-solid fa-chart-bar'></i> " . __($headLine ?? 'Relatório de Indicadores de TI'),
            'routeBack' => 'admin.indicadoresTi.index',
            'routeBackParams' => [],
            'nameBack' => __('Voltar'),
            'permission' => 'SYSADMIN',
        ])

        <div class="card-body">

            @if (session('success'))
                <div class="alert alert-success mt-3 alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if ($errors->any())
                <div class="alert alert-danger mt-3 alert-dismissible fade show" role="alert">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $erro)
                            <li>{{ $erro }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            <div class="card shadow-sm mt-4">
                <div class="card-header fw-bold d-flex align-items-center gap-2">
                    <i class="fa-solid fa-filter text-primary"></i>
                    {{ __('Filtros do Relatório') }}
                </div>
                <div class="card-body">
                    <form method="GET" action="{{ route('admin.indicadoresTi.relatorio.index') }}"
                        id="form-filtros-relatorio">
                        <div class="row g-3">

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="data_inicio">
                                    <i class="fa-solid fa-calendar-day me-1 text-muted"></i>{{ __('Data Início') }}
                                </label>
                                <input type="date" class="form-control" id="data_inicio" name="data_inicio"
                                    value="{{ $filterParams['data_inicio'] ?? '' }}">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="data_fim">
                                    <i class="fa-solid fa-calendar-check me-1 text-muted"></i>{{ __('Data Fim') }}
                                </label>
                                <input type="date" class="form-control" id="data_fim" name="data_fim"
                                    value="{{ $filterParams['data_fim'] ?? '' }}">
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="setor_id">
                                    <i class="fa-solid fa-building me-1 text-muted"></i>{{ __('Setor') }}
                                </label>
                                <select class="form-select" id="setor_id" name="setor_id" id="rel-setor">
                                    <option value="">{{ __('Todos os setores') }}</option>
                                    @foreach ($setores as $setor)
                                        <option value="{{ $setor->id }}" @selected((int) ($filterParams['setor_id'] ?? 0) === $setor->id)>
                                            {{ $setor->nome }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="assunto_id">
                                    <i class="fa-solid fa-tags me-1 text-muted"></i>{{ __('Assunto') }}
                                </label>
                                <select class="form-select" id="assunto_id" name="assunto_id" id="rel-assunto">
                                    <option value="">{{ __('Todos os assuntos') }}</option>
                                    @foreach ($assuntos as $assunto)
                                        <option value="{{ $assunto->id }}" @selected((int) ($filterParams['assunto_id'] ?? 0) === $assunto->id)>
                                            {{ $assunto->nome }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="subassunto_id">
                                    <i class="fa-solid fa-tag me-1 text-muted"></i>{{ __('Subassunto') }}
                                </label>
                                <select class="form-select" id="subassunto_id" name="subassunto_id" id="rel-subassunto">
                                    <option value="">{{ __('Todos os subassuntos') }}</option>
                                    @foreach ($subassuntos as $sub)
                                        <option value="{{ $sub->id }}" @selected((int) ($filterParams['subassunto_id'] ?? 0) === $sub->id)>
                                            {{ $sub->nome }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="status">
                                    <i class="fa-solid fa-circle-half-stroke me-1 text-muted"></i>{{ __('Status') }}
                                </label>
                                <select class="form-select" name="status" id="status">
                                    <option value="">{{ __('Todos os status') }}</option>
                                    @foreach (\App\Models\IndicadoresTi\Chamado::STATUS as $valor => $label)
                                        <option value="{{ $valor }}" @selected(($filterParams['status'] ?? '') === $valor)>
                                            {{ __($label) }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="prioridade">
                                    <i class="fa-solid fa-exclamation-triangle me-1 text-muted"></i>{{ __('Prioridade') }}
                                </label>
                                <select class="form-select" name="prioridade" id="prioridade">
                                    <option value="">{{ __('Todas as prioridades') }}</option>
                                    @foreach (\App\Models\IndicadoresTi\Chamado::PRIORIDADE as $valor => $label)
                                        <option value="{{ $valor }}" @selected(($filterParams['prioridade'] ?? '') === $valor)>
                                            {{ __($label) }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label fw-semibold" for="tipo">
                                    <i class="fa-solid fa-shapes me-1 text-muted"></i>{{ __('Tipo') }}
                                </label>
                                <select class="form-select" name="tipo" id="tipo">
                                    <option value="">{{ __('Todos os tipos') }}</option>
                                    @foreach (\App\Models\IndicadoresTi\Chamado::TIPO as $valor => $label)
                                        <option value="{{ $valor }}" @selected(($filterParams['tipo'] ?? '') === $valor)>
                                            {{ __($label) }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                        </div>

                        <div class="d-flex gap-2 mt-3 flex-wrap align-items-center">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa-solid fa-magnifying-glass me-1"></i>{{ __('Visualizar Prévia') }}
                            </button>
                            <a href="{{ route('admin.indicadoresTi.relatorio.index') }}"
                                class="btn btn-outline-secondary">
                                <i class="fa-solid fa-xmark me-1"></i>{{ __('Limpar') }}
                            </a>
                        </div>
                    </form>
                </div>
            </div>

            @if ($resumo)
                {{-- KPIs --}}
                <div class="row g-3 mt-3">
                    <div class="col-6 col-md">
                        <div class="card text-center border-primary shadow-sm h-100">
                            <div class="card-body py-3">
                                <div class="display-6 fw-bold text-primary">{{ $resumo['total'] }}</div>
                                <small class="text-muted">{{ __('Total de Chamados') }}</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-6 col-md">
                        <div class="card text-center border-success shadow-sm h-100">
                            <div class="card-body py-3">
                                <div class="display-6 fw-bold text-success">{{ $resumo['atendidos'] }}</div>
                                <small class="text-muted">{{ __('Atendidos') }}</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-6 col-md">
                        <div class="card text-center border-warning shadow-sm h-100">
                            <div class="card-body py-3">
                                <div class="display-6 fw-bold text-warning">{{ $resumo['por_status']['pendente'] ?? 0 }}</div>
                                <small class="text-muted">{{ __('Pendentes') }}</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-6 col-md">
                        <div class="card text-center border-info shadow-sm h-100">
                            <div class="card-body py-3">
                                <div class="display-6 fw-bold text-info">{{ $resumo['por_status']['em_andamento'] ?? 0 }}</div>
                                <small class="text-muted">{{ __('Em Andamento') }}</small>
                            </div>
                        </div>
                    </div>
                    <div class="col-6 col-md">
                        <div class="card text-center border-secondary shadow-sm h-100">
                            <div class="card-body py-3">
                                <div class="display-6 fw-bold text-secondary">{{ $resumo['por_setor']->count() }}</div>
                                <small class="text-muted">{{ __('Setores') }}</small>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Gráficos --}}
                <div class="row g-3 mt-3"
                     id="relatorio-charts"
                     data-dias="{{ $resumo['dias_range'] ?? '' }}"
                     data-status='@json($resumo["por_status"])'
                     data-prioridade='@json($resumo["por_prioridade"])'
                     data-setor='@json($resumo["por_setor"])'
                     data-mes-labels='@json($resumo["por_mes_labels"])'
                     data-mes-data='@json($resumo["por_mes_data"])'
                     data-semana='@json($resumo["por_semana"])'>

                    <div class="col-md-4" data-chart-key="status">
                        <div class="card shadow-sm h-100">
                            <div class="card-header fw-semibold">
                                <i class="fa-solid fa-circle-half-stroke me-1 text-primary"></i>{{ __('Por Status') }}
                            </div>
                            <div class="card-body d-flex align-items-center justify-content-center">
                                <canvas id="chart-status" style="max-height:240px;"></canvas>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-4" data-chart-key="prioridade">
                        <div class="card shadow-sm h-100">
                            <div class="card-header fw-semibold">
                                <i class="fa-solid fa-exclamation-triangle me-1 text-warning"></i>{{ __('Por Prioridade') }}
                            </div>
                            <div class="card-body d-flex align-items-center justify-content-center">
                                <canvas id="chart-prioridade" style="max-height:240px;"></canvas>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-4" data-chart-key="setor">
                        <div class="card shadow-sm h-100">
                            <div class="card-header fw-semibold">
                                <i class="fa-solid fa-building me-1 text-success"></i>{{ __('Por Setor') }}
                            </div>
                            <div class="card-body">
                                <canvas id="chart-setor"></canvas>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-8" data-chart-key="mes">
                        <div class="card shadow-sm h-100">
                            <div class="card-header fw-semibold">
                                <i class="fa-solid fa-calendar me-1 text-info"></i>{{ __('Chamados por Mês') }}
                            </div>
                            <div class="card-body">
                                <canvas id="chart-mes" style="max-height:260px;"></canvas>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-4" data-chart-key="semana">
                        <div class="card shadow-sm h-100">
                            <div class="card-header fw-semibold">
                                <i class="fa-solid fa-calendar-week me-1 text-secondary"></i>{{ __('Chamados por Semana') }}
                            </div>
                            <div class="card-body">
                                <canvas id="chart-semana"></canvas>
                            </div>
                        </div>
                    </div>

                </div>
            @endif

            <div class="card shadow-sm mt-4">
                <div class="card-header fw-bold d-flex align-items-center gap-2">
                    <i class="fa-solid fa-file-export text-success"></i>
                    {{ __('Exportar Relatório') }}
                </div>
                <div class="card-body">
                    <p class="text-muted mb-3">
                        {{ __('Escolha o tipo de relatório e o formato de exportação. Os filtros acima serão aplicados.') }}
                    </p>

                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="card border-primary h-100">
                                <div class="card-body">
                                    <h5 class="card-title d-flex align-items-center gap-2">
                                        <i class="fa-solid fa-chart-pie text-primary"></i>
                                        {{ __('Relatório Resumido') }}
                                    </h5>
                                    <p class="text-muted small mb-3">
                                        {{ __('Totais agrupados por setor, assunto, status e prioridade. Ideal para visão gerencial.') }}
                                    </p>
                                    <div class="d-flex gap-2 flex-wrap">
                                        <form method="POST" action="{{ route('admin.indicadoresTi.relatorio.pdf') }}"
                                            class="d-inline">
                                            @csrf
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value)
                                                    <input type="hidden" name="{{ $key }}"
                                                        value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <input type="hidden" name="tipo_relatorio" value="resumido">
                                            <button type="submit" class="btn btn-danger">
                                                <i class="fa-solid fa-file-pdf me-1"></i>{{ __('Exportar PDF') }}
                                            </button>
                                        </form>
                                        <form method="POST" action="{{ route('admin.indicadoresTi.relatorio.csv') }}"
                                            class="d-inline">
                                            @csrf
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value)
                                                    <input type="hidden" name="{{ $key }}"
                                                        value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <input type="hidden" name="tipo_relatorio" value="resumido">
                                            <button type="submit" class="btn btn-success">
                                                <i class="fa-solid fa-file-csv me-1"></i>{{ __('Exportar CSV') }}
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="card border-secondary h-100">
                                <div class="card-body">
                                    <h5 class="card-title d-flex align-items-center gap-2">
                                        <i class="fa-solid fa-list-ul text-secondary"></i>
                                        {{ __('Relatório Detalhado') }}
                                    </h5>
                                    <p class="text-muted small mb-3">
                                        {{ __('Lista completa de chamados com todos os campos: solicitante, setor, assunto, subassunto, tempo, descrição.') }}
                                    </p>
                                    <div class="d-flex gap-2 flex-wrap">
                                        <form method="POST" action="{{ route('admin.indicadoresTi.relatorio.pdf') }}"
                                            class="d-inline">
                                            @csrf
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value)
                                                    <input type="hidden" name="{{ $key }}"
                                                        value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <input type="hidden" name="tipo_relatorio" value="detalhado">
                                            <button type="submit" class="btn btn-danger">
                                                <i class="fa-solid fa-file-pdf me-1"></i>{{ __('Exportar PDF') }}
                                            </button>
                                        </form>
                                        <form method="POST" action="{{ route('admin.indicadoresTi.relatorio.csv') }}"
                                            class="d-inline">
                                            @csrf
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value)
                                                    <input type="hidden" name="{{ $key }}"
                                                        value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <input type="hidden" name="tipo_relatorio" value="detalhado">
                                            <button type="submit" class="btn btn-success">
                                                <i class="fa-solid fa-file-csv me-1"></i>{{ __('Exportar CSV') }}
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-4">
                            <div class="card border-success h-100">
                                <div class="card-body">
                                    <h5 class="card-title d-flex align-items-center gap-2">
                                        <i class="fa-solid fa-table text-success"></i>
                                        {{ __('Relatório Matricial (Mensal/Anual)') }}
                                    </h5>
                                    <p class="text-muted small mb-3">
                                        {{ __('Total geral de chamados por mês e total de chamados por setor por mês, com total anual.') }}
                                    </p>
                                    <div class="d-flex gap-2 flex-wrap">
                                        <form method="POST"
                                            action="{{ route('admin.indicadoresTi.relatorio.pdf.matricial') }}"
                                            class="d-inline">
                                            @csrf
                                            @foreach ($filterParams as $key => $value)
                                                @if ($value)
                                                    <input type="hidden" name="{{ $key }}"
                                                        value="{{ $value }}">
                                                @endif
                                            @endforeach
                                            <button type="submit" class="btn btn-danger">
                                                <i
                                                    class="fa-solid fa-file-pdf me-1"></i>{{ __('Exportar PDF Matricial') }}
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>
@endsection
