@extends('layouts.master')
@section('title', 'Customer Profile')

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title"><strong>Customer Profile</strong></h1>
    </div>
    <div class="header-action">
        <nav class="nav">
            <a class="nav-link" href="{{ route('customer.index') }}">Customers</a>
        </nav>
    </div>
</header>
@endsection

@section('content')
<div class="col-md-12">

    <div class="card">
        <div class="card-body">

            <div class="row mb-4">
                <div class="col-md-3 text-center">
                    @if($customer->image)
                    <img src="{{ asset($customer->image) }}" width="150" class="img-thumbnail mb-2">
                    @else
                    <img src="{{ asset('uploads/default.png') }}" width="150" class="img-thumbnail mb-2">
                    @endif
                </div>

                <div class="col-md-9">
                     <a href="{{ route('customer.admissionFee.form', $customer->id) }}" class="btn btn-success mb-3">
                        Collect Admission Fee
                    </a>
                    <h4><strong>{{ $customer->name }}'s Profile</strong></h4>
                    <p><strong>Father:</strong> {{ $customer->father_name ?? '-' }}</p>
                    <p><strong>Mother:</strong> {{ $customer->mother_name ?? '-' }}</p>
                    <p><strong>Mobile:</strong> {{ $customer->mobile_no ?? '-' }}</p>
                    <p><strong>Address:</strong> {{ $customer->address ?? '-' }}</p>
                    <p><strong>Account No:</strong> {{ $customer->account_no ?? '-' }}</p>
                    <p><strong>Gender:</strong> {{ ucfirst($customer->gender) }}</p>
                    <p><strong>Join Date:</strong> {{ $customer->join_date ?? '-' }}</p>
                    <p><strong>Admission Fee:</strong>  {{ number_format($customer->admissionFees->sum('amount'), 2) }}</p>
                    <a href="{{ route('customer.edit', $customer->id) }}" class="btn btn-warning btn-sm">
                    <i class="fa fa-edit"></i> Edit Customer
                </a>
                </div>
                
            </div>

            {{-- Nominees --}}
            <div class="mb-4">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <h5>Nominees</h5>
                    <button class="btn btn-sm btn-primary" data-toggle="modal" data-target="#addNomineeModal">
                        <i class="fa fa-plus"></i> Add Nominee
                    </button>

                </div>

                @if($customer->nominees->count() > 0)
                <div class="table-responsive">
                    <table class="table table-bordered table-soft">
                        <thead>
                            <tr class="bg-secondary text-white">
                                <th>#</th>
                                <th>Name</th>
                                <th>Father Name</th>
                                <th>Mother Name</th>
                                <th>Address</th>
                                <th>Mobile No</th>
                                <th>NID Card</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($customer->nominees as $key => $nominee)
                            <tr>
                                <td>{{ ++$key }}</td>
                                <td>{{ $nominee->name }}</td>
                                <td>{{ $nominee->father_name }}</td>
                                <td>{{ $nominee->mother_name }}</td>
                                <td>{{ $nominee->address }}</td>
                                <td>{{ $nominee->mobile_no }}</td>
                                <td>
                                    @if($nominee->nid_card)
                                    <a href="{{ asset($nominee->nid_card) }}" target="_blank">View</a>
                                    @else
                                    -
                                    @endif
                                </td>
                                <td>
                                    <a href="{{ route('nominee.edit', $nominee->id) }}" class="btn btn-info btn-sm"><i
                                            class="fa fa-edit"></i></a>
                                    <a href="{{ route('nominee.destroy', $nominee->id) }}"
                                        class="btn btn-danger btn-sm delete"><i class="fa fa-trash"></i></a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @else
                <p>No nominee added yet</p>
                @endif
            </div>


            {{-- Documents --}}
            <div class="mb-4">
                <h5>Documents</h5>
                <form action="{{ route('customer.documents.upload', $customer->id) }}" method="POST"
                    enctype="multipart/form-data">
                    @csrf
                    <input type="file" name="documents[]" multiple class="form-control mb-2"
                        accept=".jpg,.jpeg,.png,.pdf">
                    <button type="submit" class="btn btn-primary btn-sm">Upload Documents</button>
                </form>

                <ul class="mt-3">
                    @foreach($customer->documents as $doc)
                    <li class="d-flex justify-content-between align-items-center mb-1">
                        <a href="{{ asset($doc->file_path) }}" target="_blank">{{ $doc->file_name }}</a>
                        <form action="{{ route('customer.documents.destroy', $doc->id) }}" method="POST"
                            onsubmit="return confirm('Are you sure you want to delete this document?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger btn-sm btn-icon"><i
                                    class="fa fa-trash"></i></button>
                        </form>
                    </li>
                    @endforeach
                </ul>
            </div>


        </div>
    </div>

</div>


<!-- Add Nominee Modal -->
<div class="modal fade" id="addNomineeModal" tabindex="-1" aria-labelledby="addNomineeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <form action="{{ route('nominee.store', $customer->id) }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addNomineeModalLabel">Add Nominee</h5>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label>Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" required>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label>Father Name</label>
                            <input type="text" name="father_name" class="form-control">
                        </div>

                        <div class="col-md-6 mb-3">
                            <label>Mother Name</label>
                            <input type="text" name="mother_name" class="form-control">
                        </div>

                        <div class="col-md-6 mb-3">
                            <label>Mobile No</label>
                            <input type="text" name="mobile_no" class="form-control">
                        </div>

                        <div class="col-md-12 mb-3">
                            <label>Address</label>
                            <textarea name="address" class="form-control" rows="2"></textarea>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label>NID Card</label>
                            <input type="file" name="nid_card" class="form-control" accept=".jpg,.jpeg,.png,.pdf">
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Save Nominee</button>
                </div>
            </div>
        </form>
    </div>
</div>

@endsection
