@extends('backend.layouts.app')
@section('title', @$data['title'])

@section('style')
    <style>
        #appealDescription {
            min-height: 400px;
        }
    </style>
@endsection

@section('content')
    {!! breadcrumb([
        'title' => @$data['title'],
        route('admin.dashboard') => _trans('common.Dashboard'),
        '#' => @$data['title'],
    ]) !!}
    
    
    <div class="row">
        <div class="col-lg-7">
            <div class="table-content table-basic mb-3">
                <div class="card">
                    <div class="card-body">
                        <div class="p-3">
                            <small class="text-muted">{{ _trans('common.Employee Info') }}</small>
                            <br>
                            <div class="table-responsive">
                                <table>
                                    <tr>
                                        <th>{{ _trans('common.Employee ID') }}</th>
                                        <td>: {{ @$data['deduction']->user->employee_id }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Employee Name') }}</th>
                                        <td>: {{ @$data['deduction']->user->name }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Employee Type') }}</th>
                                        <td>: {{ @$data['deduction']->user->employee_type }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Department') }}</th>
                                        <td>: {{ @$data['deduction']->user->department->title }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Designation') }}</th>
                                        <td>: {{ @$data['deduction']->user->designation->title }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Shift') }}</th>
                                        <td>: 
                                            {{ @$data['deduction']->user->shift->name }} 
                                            [{{ \Carbon\Carbon::parse(@$data['deduction']->user->dutySchedule->start_time)->format('h:i:A') . ' - ' . \Carbon\Carbon::parse(@$data['deduction']->user->dutySchedule->end_time)->format('h:i:A') }}]
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Joining Date') }}</th>
                                        <td>: {{ @$data['deduction']->user->joining_date }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Email') }}</th>
                                        <td>: {{ @$data['deduction']->user->email }}</td>
                                    </tr>
                                    <tr>
                                        <th>{{ _trans('common.Phone') }}</th>
                                        <td>: {{ @$data['deduction']->user->phone }}</td>
                                    </tr>
                                </table>
                            </div>
                            <br>
                            <div class="card">
                                <div class="card-body py-3">
                                    <form  
                                        @if ($data['deduction']->consider_amount == 0 && request('for_consider') && isAdmin())
                                            action="{{ route('deduction.consider', $data['deduction']->id) }}" 
                                        @else
                                            action="#"
                                        @endif
                                        method="POST" 
                                        class="d-flex flex-column justify-content-end py-3" 
                                        autocomplete="off"
                                    >
                                        @csrf
                                        @if ($errors->any())
                                            <div class="d-flex flex-column gap-2 alert alert-danger">
                                                @foreach ($errors->all() as $error)
                                                    <span class="ps-3">{{ $error }}</span>
                                                @endforeach
                                            </div>
                                        @endif
                                        <small class="text-muted mb-1">{{ _trans('common.Deduction Details') }}</small>
                                        <div class="table-responsive">
                                            <table class="table table-striped table-bordered">
                                                <thead class="thead">
                                                    <tr>
                                                        <th>{{ _trans('common.Deduction Type') }}</th>
                                                        <th>{{ _trans('common.Tardy Rule') }}</th>
                                                        <th>{{ _trans('common.Purpose') }}</th>
                                                        <th class="text-center">{{ _trans('common.Days') }}</th>
                                                        <th>{{ _trans('common.Amount') }}</th>
                                                        <th class="text-center">{{ _trans('common.Applicable') }}</th>
                                                        <th>{{ _trans('common.Comment') }}</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @foreach ($data['deduction']->deductionDetails ?? [] as $deductionDetail)
                                                        <tr>
                                                            <td>{{ $deductionDetail->deduction_type }}</td>
                                                            <td>{{ @$deductionDetail->tardyRule->tardy_type ?? _trans('common.N/A') }}</td>
                                                            <td>{{ $deductionDetail->purpose }}</td>
                                                            <td class="text-center">{{ $deductionDetail->days }}</td>
                                                            <td class="text-end deduction-amount">{{ $deductionDetail->amount }}</td>
                                                            <td>
                                                                <div class="form-check form-switch d-flex justify-content-center">
                                                                    <input class="form-check-input" 
                                                                        name="not_applicable_ids[]" 
                                                                        value="{{ $deductionDetail->id }}" 
                                                                        type="checkbox" 
                                                                        role="switch" 
                                                                        id="isApplicable{{ $deductionDetail->id }}" 
                                                                        {{ !$deductionDetail->considered_by ? 'checked' : '' }}
                                                                        {{ ($data['deduction']->consider_amount > 0) || !request('for_consider') || isHr() ? 'disabled' : '' }}
                                                                        @if ($data['deduction']->consider_amount == 0 && request('for_consider') && isAdmin())
                                                                            onchange="calculateTotalConsiderAmount(this)"
                                                                        @else
                                                                            disabled
                                                                        @endif
                                                                    >
                                                                    <label class="form-check-label" for="isApplicable{{ $deductionDetail->id }}"></label>
                                                                </div>
                                                            </td>
                                                            <td>{{ $deductionDetail->comment }}</td>
                                                        </tr>
                                                    @endforeach
                                                </tbody>
                                                <tfoot>
                                                    <tr>
                                                        <th colspan="3" class="text-end">
                                                            {{ _trans('common.Total') }}
                                                        </th>
                                                        <th class="text-center">{{ array_sum(array_column($data['deduction']->deductionDetails->toArray(), 'days')) }}</th>
                                                        <th class="text-end total-deduction-amount">{{ array_sum(array_column($data['deduction']->deductionDetails->toArray(), 'amount')) }}</th>
                                                        <th class="text-center total-consider-amount">{{ array_sum(array_column($data['deduction']->deductionDetails->toArray(), 'amount')) - array_sum(array_column($data['deduction']->deductionDetails->whereNotNull('considered_by')->toArray(), 'amount')) }}</th>
                                                        <th></th>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                        @if ($data['deduction']->consider_amount == 0 && request('for_consider') && isAdmin())
                                            <div class="d-grid">
                                                <button class="btn btn-gradian mt-3">
                                                    <i class="las la-check-circle"></i>
                                                    {{ _trans('common.Submit') }}
                                                </button>
                                            </div>
                                        @endif
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-5">
            <div class="table-content table-basic">
                <div class="card">
                    <div class="card-body">
                        @if(\Carbon\Carbon::parse($data['deduction']->created_at)->addDays(2)->format('Y-m-d') >= date('Y-m-d') && !$data['deduction']->is_appealed)
                            <h3 class="bg-light p-3">{{ _trans('common.Appeal Form') }}</h3>
                            <form action="{{ route('deduction.appeal', $data['deduction']->id) }}" method="POST" enctype="multipart/form-data">
                                @csrf
                                <div class="mb-3">
                                    <textarea name="appeal_details" class="form-control" id="appealDescription" placeholder="{{ _trans('common.Provide the reason for your appeal') }}" required></textarea>
                                    @if ($errors->has('appeal_details'))
                                        <span class="error text-danger">
                                            {{ $errors->first('appeal_details') }}
                                        </span>
                                    @endif
                                </div>

                                <div class="mb-3">
                                    <div class="ot_fileUploader left-side">
                                        <input class="form-control" type="text" placeholder="{{ _trans('common.Attachment') }}" readonly="" id="placeholder3">
                                        <div class="primary-btn-small-input">
                                            <label class="btn btn-lg ot-btn-primary" for="fileBrouse3">{{ _trans('common.Browse') }}</label>
                                            <input type="file" class="d-none form-control" name="attachment" id="fileBrouse3">
                                        </div>
                                    </div>
                                    @if ($errors->has('attachment'))
                                        <span class="error text-danger">
                                            {{ $errors->first('attachment') }}
                                        </span>
                                    @endif
                                </div>

                                <button class="btn btn-gradian w-100">
                                    <i class="las la-check-circle"></i>
                                    {{ _trans('common.Submit') }}
                                </button>
                            </form>
                        @elseif ($data['deduction']->is_appealed)
                            <h3 class="bg-light p-3 mb-3">{{ _trans('common.Appeal Details') }}</h3>
                            <p id="appealDescription">{!! nl2br($data['deduction']->appeal_details) !!}</p>
                            @if ($data['deduction']->appeal_attachment_id)
                                <a class="text-danger" href="{{ uploaded_asset($data['deduction']->appeal_attachment_id) }}" target="_blank">
                                    <i class="las la-paperclip"></i>
                                    {{ _trans('common.Attachment') }}
                                </a>
                            @endif
                        @else
                            <h3 class="bg-light text-danger p-3">{{ _trans('common.No Appeal!') }}</h3>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    <script>
        const calculateTotalConsiderAmount = (obj) => {
            const totalConsiderAmount = Number($('.total-consider-amount').text());
            let considerAmount = Number($(obj).closest('tr').find('.deduction-amount').text());
            let totalConsider = 0;

            if ($(obj).is(":checked")) {
                totalConsider = totalConsiderAmount + considerAmount;
                $('.total-consider-amount').text(totalConsider.toFixed(2));
            } else {
                totalConsider = totalConsiderAmount - considerAmount;
                $('.total-consider-amount').text(totalConsider.toFixed(2));  
            }
        }
    </script>
@endsection
