@extends('layouts.master')

@push('css')
<style>
    /* ── Report Wrapper ── */
    .payroll-wrap {
        font-family: Arial, sans-serif;
        font-size: 13px;
    }

    /* ── Report Header ── */
    .payroll-header {
        text-align: center;
        margin-bottom: 10px;
        line-height: 1.5;
    }
    .payroll-header .co-name  { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
    .payroll-header .co-sub   { font-size: 15px; font-weight: 600; }
    .payroll-header .co-range { font-size: 12px; color: #444; margin-top: 3px; }

    /* ── Main Table ── */
    .payroll-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 12.5px;
        margin-top: 14px;
    }
    .payroll-table thead th {
        border: 1px solid #000;
        padding: 7px 8px;
        text-align: center;
        font-weight: 700;
        font-size: 12px;
        background: #fff;
        white-space: nowrap;
        vertical-align: bottom;
    }
    .payroll-table thead th small {
        display: block;
        font-weight: 600;
        font-size: 11px;
    }

    /* Solid side borders, dashed bottom — matches image */
    .payroll-table tbody td {
        border-left:   1px solid #000;
        border-right:  1px solid #000;
        border-top:    none;
        border-bottom: 1px dashed #888;
        padding: 6px 8px;
        vertical-align: middle;
    }

    /* Department group header */
    .payroll-table tr.dept-row td {
        font-weight: 700;
        font-size: 13px;
        color: #cc0000;
        border-left:  1px solid #000;
        border-right: 1px solid #000;
        border-bottom: none;
        padding: 8px 8px 2px;
        background: #fff;
        letter-spacing: 0.5px;
    }

    /* Sub-total row */
    .payroll-table tr.subtotal-row td {
        border-top:    1px dashed #888;
        border-bottom: 1px dashed #888;
        font-size: 12px;
        color: #cc0000;
        font-weight: 600;
        padding: 5px 8px;
    }

    /* Grand Total */
    .payroll-table tr.grand-row td {
        border-top:    2px solid #000 !important;
        border-bottom: 2px solid #000 !important;
        font-weight: 700;
        font-size: 12px;
        background: #f0f0f0;
        padding: 7px 8px;
    }

    /* Number alignment */
    .payroll-table td.num  { text-align: right;  white-space: nowrap; }
    .payroll-table th.num  { text-align: center; }
    .payroll-table td.ctr  { text-align: center; }

    .text-red { color: #cc0000; }
</style>
@endpush

@section('content')

<div class="card">
    <div class="card-header">
        <h3>Payroll History Report</h3>
    </div>
    <div class="card-body">

        {{-- ── Filter Form ── --}}
        <form class="row" method="GET" action="{{ route('payroll-history.index') }}">
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>From Date</label>
                    <input type="date" name="from_date" class="form-control"
                           value="{{ $fromDate }}">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>Till Date</label>
                    <input type="date" name="to_date" class="form-control"
                           value="{{ $toDate }}">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>Department</label>
                    <select name="department_id" class="form-control">
                        <option value="">— All Departments —</option>
                        @foreach($departments as $dept)
                            <option value="{{ $dept->id }}"
                                {{ $departmentId == $dept->id ? 'selected' : '' }}>
                                {{ $dept->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group mt-2">
                    <label>Employee</label>
                    <select name="employee_id" class="form-control">
                        <option value="">— All Employees —</option>
                        @foreach($employees as $emp)
                            <option value="{{ $emp->id }}"
                                {{ $employeeId == $emp->id ? 'selected' : '' }}>
                                {{ $emp->first_name }} {{ $emp->last_name }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="col-12 mt-3 d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-search"></i> View
                </button>
                @if($reportData && count($reportData['departments']))
                    <a href="{{ route('payroll-history.print', [
                            'from_date'     => $fromDate,
                            'to_date'       => $toDate,
                            'department_id' => $departmentId,
                            'employee_id'   => $employeeId,
                        ]) }}"
                       target="_blank" class="btn btn-warning">
                        <i class="fa fa-print"></i> Print
                    </a>
                @endif
            </div>
        </form>

        <hr />

        @if($reportData !== null)
            @if(empty($reportData['departments']))
                <div class="alert alert-info text-center">No records found.</div>
            @else

                {{-- ── Summary Cards ── --}}
                <div class="row mb-3">
                    <div class="col-md-3">
                        <div class="card border-primary">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Gross Salary</small>
                                <strong class="text-primary">{{ number_format($reportData['grandGross'], 0) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-danger">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Deductions</small>
                                <strong class="text-danger">{{ number_format($reportData['grandDeductions'], 0) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-success">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Net Payable</small>
                                <strong class="text-success">{{ number_format($reportData['grandNet'], 0) }}</strong>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card border-secondary">
                            <div class="card-body py-2 text-center">
                                <small class="text-muted d-block">Total Absent Days</small>
                                <strong>{{ $reportData['grandAbsentDays'] }}</strong>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- ── Payroll Table ── --}}
                <div class="payroll-wrap">

                    {{-- Report Header --}}
                    <div class="payroll-header">
                        <div class="co-name">M.D.S</div>
                        <div class="co-sub">Payroll History Report</div>
                        <div class="co-range">
                            From: {{ \Carbon\Carbon::parse($fromDate)->format('d/m/y') }},
                            Till: {{ \Carbon\Carbon::parse($toDate)->format('d/m/y') }}
                            @if($selectedEmployee)
                                , Employee: {{ $selectedEmployee->first_name }} {{ $selectedEmployee->last_name }}
                            @endif
                            @if($selectedDepartment)
                                , Dept: {{ $selectedDepartment->name }}
                            @endif
                        </div>
                    </div>

                    <table class="payroll-table">
                        <thead>
                            <tr>
                                <th>Employee</th>
                                <th>EmpID</th>
                                <th>Salary<br><small>Type</small></th>
                                <th>From</th>
                                <th>To</th>
                                <th class="num">Salary</th>
                                <th class="num">OT<br><small>Hours</small></th>
                                <th class="num">Over Time</th>
                                <th class="num">Award</th>
                                <th class="num">Absent<br><small>Days</small></th>
                                <th class="num">Absent<br><small>Amount</small></th>
                                <th class="num">Other<br><small>Ded.</small></th>
                                <th class="num">Advance<br><small>Ded.</small></th>
                                <th class="num">Loan<br><small>Ded.</small></th>
                                <th class="num">Net Payable</th>
                            </tr>
                        </thead>
                        <tbody>

                            @foreach($reportData['departments'] as $deptName => $deptData)

                                {{-- Department Group Header --}}
                                <tr class="dept-row">
                                    <td colspan="15">{{ $deptName }}</td>
                                </tr>

                                @foreach($deptData['rows'] as $row)
                                    <tr>
                                        <td>{{ $row['employee_name'] }}</td>
                                        <td class="ctr">{{ $row['employee_no'] }}</td>
                                        <td class="ctr">{{ $row['salary_type'] }}</td>
                                        <td class="ctr">{{ $row['from'] }}</td>
                                        <td class="ctr">{{ $row['to'] }}</td>
                                        <td class="num">{{ $row['salary'] ? number_format($row['salary'], 0) : '' }}</td>
                                        <td class="num">{{ $row['ot_hours'] ?: '' }}</td>
                                        <td class="num">{{ $row['overtime'] ? number_format($row['overtime'], 0) : '' }}</td>
                                        <td class="num">{{ $row['award'] ? number_format($row['award'], 0) : '' }}</td>
                                        <td class="ctr">{{ $row['absent_days'] ?: '' }}</td>
                                        <td class="num">{{ $row['absent_amount'] ? number_format($row['absent_amount'], 0) : '' }}</td>
                                        <td class="num">{{ $row['other_ded'] ? number_format($row['other_ded'], 0) : '' }}</td>
                                        <td class="num">{{ $row['advance_ded'] ? number_format($row['advance_ded'], 0) : '' }}</td>
                                        <td class="num">{{ $row['loan_ded'] ? number_format($row['loan_ded'], 0) : '' }}</td>
                                        <td class="num">{{ $row['net_payable'] ? number_format($row['net_payable'], 0) : '' }}</td>
                                    </tr>
                                @endforeach

                                {{-- Sub Total --}}
                                <tr class="subtotal-row">
                                    <td colspan="5" class="text-red">Sub Total = &gt;</td>
                                    <td class="num text-red">{{ number_format($deptData['subSalary'], 0) }}</td>
                                    <td class="num text-red">{{ $deptData['subOtHours'] ?: '' }}</td>
                                    <td class="num text-red">{{ $deptData['subOvertime'] ? number_format($deptData['subOvertime'], 0) : '' }}</td>
                                    <td class="num text-red">{{ $deptData['subAward'] ? number_format($deptData['subAward'], 0) : '' }}</td>
                                    <td class="ctr text-red">{{ $deptData['subAbsentDays'] ?: '' }}</td>
                                    <td class="num text-red">{{ $deptData['subAbsentAmount'] ? number_format($deptData['subAbsentAmount'], 0) : '' }}</td>
                                    <td class="num text-red">{{ $deptData['subOtherDed'] ? number_format($deptData['subOtherDed'], 0) : '' }}</td>
                                    <td class="num text-red">{{ $deptData['subAdvanceDed'] ? number_format($deptData['subAdvanceDed'], 0) : '' }}</td>
                                    <td class="num text-red">{{ $deptData['subLoanDed'] ? number_format($deptData['subLoanDed'], 0) : '' }}</td>
                                    <td class="num text-red">{{ number_format($deptData['subNet'], 0) }}</td>
                                </tr>

                            @endforeach

                            {{-- Grand Total --}}
                            <tr class="grand-row">
                                <td colspan="5">Grand Total = &gt;</td>
                                <td class="num">{{ number_format($reportData['grandGross'], 0) }}</td>
                                <td class="num">{{ $reportData['grandOtHours'] ?: '' }}</td>
                                <td class="num">{{ $reportData['grandOvertime'] ? number_format($reportData['grandOvertime'], 0) : '' }}</td>
                                <td class="num">{{ $reportData['grandAward'] ? number_format($reportData['grandAward'], 0) : '' }}</td>
                                <td class="ctr">{{ $reportData['grandAbsentDays'] ?: '' }}</td>
                                <td class="num">{{ $reportData['grandAbsentAmount'] ? number_format($reportData['grandAbsentAmount'], 0) : '' }}</td>
                                <td class="num">{{ $reportData['grandOtherDed'] ? number_format($reportData['grandOtherDed'], 0) : '' }}</td>
                                <td class="num">{{ $reportData['grandAdvanceDed'] ? number_format($reportData['grandAdvanceDed'], 0) : '' }}</td>
                                <td class="num">{{ $reportData['grandLoanDed'] ? number_format($reportData['grandLoanDed'], 0) : '' }}</td>
                                <td class="num">{{ number_format($reportData['grandNet'], 0) }}</td>
                            </tr>

                        </tbody>
                    </table>

                </div>{{-- .payroll-wrap --}}

            @endif
        @endif

    </div>
</div>

@endsection