@extends('layouts.master')
@section('content')
<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="{{ route('home') }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-building me-2"></i> Add Supplier Account</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('supplier.accounts.store') }}" method="POST" id="supplierAccountForm">
                        @csrf
                        <input type="hidden" name="id" id="accountId">
                        <div id="timeout">
                            @if(Session::has('success'))
                                <p class="alert alert-info">{{ Session('success') }}</p>
                            @endif
                            @if(Session::has('error'))
                                <p class="alert alert-danger">{{ Session('error') }}</p>
                            @endif
                        </div>
                        
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label"><strong>Supplier Code:</strong></label>
                                <input class="form-control" type="text" name="supplier_code" id="supplier_code" required />
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>Supplier Name:</strong></label>
                                <input class="form-control" type="text" name="supplier_account" id="supplier_account" required />
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label"><strong>Contact Person:</strong></label>
                                <input class="form-control" type="text" name="contact_person" id="contact_person" />
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>Contact 1:</strong></label>
                                <input class="form-control" type="text" name="contact_1" id="contact_1" />
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label"><strong>Contact 2:</strong></label>
                                <input class="form-control" type="text" name="contact_2" id="contact_2" />
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>Email Address:</strong></label>
                                <input class="form-control" type="email" name="email" id="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" />
                            </div>
                            
                            <div class="col-12">
                                <label class="form-label"><strong>Physical Address:</strong></label>
                                <textarea class="form-control" name="address" id="address"></textarea>
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label"><strong>Category:</strong></label>
                                <select name="category_id" id="category_id" class="form-control">
                                    <option value="">Select Category</option>
                                    @foreach($supplierCategories as $cat)
                                        <option value="{{ $cat->id }}">{{ $cat->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label"><strong>Age Analysis:</strong></label>
                                <select name="age_analysis_id" id="age_analysis_id" class="form-control">
                                    <option value="">Select Age Analysis</option>
                                    @foreach($ageingRecords as $ageing)
                                        <option value="{{ $ageing->id }}">{{ $ageing->description }}</option>
                                    @endforeach
                                </select>
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label"><strong>Area:</strong></label>
                                <select name="area_id" id="area_id" class="form-control">
                                    <option value="">Select Area</option>
                                    @foreach($areas as $area)
                                        <option value="{{ $area->id }}">{{ $area->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        
                        <div class="d-flex justify-content-end mt-4">
                            <input type="submit" id="submitButton" class="btn btn-primary me-2" value="SUBMIT">
                            <input type="reset" class="btn btn-outline-secondary" onclick="resetForm()" value="REFRESH">
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Table Section -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i> Supplier Accounts</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Supplier Code</th>
                                    <th>Supplier Name</th>
                                    <th>Contact Person</th>
                                    <th>Contact 1</th>
                                    <th>Contact 2</th>
                                    <th>Email</th>
                                    <th>Address</th>
                                    <th>Category</th>
                                    <th>Area</th>
                                    <th>Age Analysis</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($supplierAccounts as $record)
                                <tr>
                                    <td>{{ $record->supplier_code }}</td>
                                    <td>{{ $record->supplier_account }}</td>
                                    <td>{{ $record->contact_person }}</td>
                                    <td>{{ $record->contact_1 }}</td>
                                    <td>{{ $record->contact_2 }}</td>
                                    <td>{{ $record->email }}</td>
                                    <td>{{ $record->address }}</td>
                                    <td>{{ $record->category->name ?? '' }}</td>
                                    <td>{{ $record->area->name ?? '' }}</td>
                                    <td>{{ $record->ageAnalysis->description ?? '' }}</td>
                                    <td class="text-center">
                                        <a href="#" class="btn btn-sm btn-primary"
                                            onclick="editAccount(event,
                                                '{{ $record->id }}',
                                                '{{ $record->supplier_code }}',
                                                '{{ $record->supplier_account }}',
                                                '{{ $record->contact_person }}',
                                                '{{ $record->contact_1 }}',
                                                '{{ $record->contact_2 }}',
                                                '{{ $record->email }}',
                                                '{{ $record->address }}',
                                                '{{ $record->category_id }}',
                                                '{{ $record->area_id }}',
                                                '{{ $record->age_analysis_id }}'
                                            )">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="{{ route('supplier.accounts.delete', ['id' => $record->id]) }}" class="btn btn-sm btn-danger deleteBtn">
                                            <i class="fas fa-trash-alt"></i>
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title">Confirm Deletion</h5>
            </div>
            <div class="modal-body">Are you sure you want to delete this supplier account?</div>
            <div class="modal-footer">
                <button id="cancelDeleteBtn" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button id="confirmDeleteBtn" type="button" class="btn btn-danger">Delete</button>
            </div>
        </div>
    </div>
</div>

<script>
   function editAccount(event, id, supplierCode, supplierAccount, contactPerson, contact1, contact2, email, address, categoryId, areaId, ageAnalysisId) {
        event.preventDefault();
        document.getElementById('accountId').value = id;
        document.getElementById('supplier_code').value = supplierCode;
        document.getElementById('supplier_account').value = supplierAccount;
        document.querySelector('[name="contact_person"]').value = contactPerson;
        document.querySelector('[name="contact_1"]').value = contact1;
        document.querySelector('[name="contact_2"]').value = contact2;
        document.querySelector('[name="email"]').value = email;
        document.querySelector('[name="address"]').value = address;
        document.querySelector('[name="category_id"]').value = categoryId;
        document.querySelector('[name="area_id"]').value = areaId;
        document.querySelector('[name="age_analysis_id"]').value = ageAnalysisId;

        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-building me-2"></i> Edit Supplier Account';
        document.getElementById('submitButton').value = 'Update';
        document.getElementById('supplierAccountForm').action = '{{ route("supplier.accounts.edit") }}';
    }

    function resetForm() {
        document.getElementById('accountId').value = '';
        document.getElementById('supplier_code').value = '';
        document.getElementById('supplier_account').value = '';
        document.querySelector('.card-header h5').innerHTML = '<i class="fas fa-building me-2"></i> Add Supplier Account';
        document.getElementById('submitButton').value = 'SUBMIT';
        document.getElementById('supplierAccountForm').action = '{{ route("supplier.accounts.store") }}';
        document.getElementById('timeout').innerHTML = '';
        document.querySelector('[name="category_id"]').value = '';
        document.querySelector('[name="area_id"]').value = '';
        document.querySelector('[name="age_analysis_id"]').value = '';
    }

    document.addEventListener('DOMContentLoaded', function(){
        let deleteUrl = null;
        const modalEl = document.getElementById('deleteConfirmModal');
        const bsModal = new bootstrap.Modal(modalEl);

        document.querySelectorAll('.deleteBtn').forEach(btn => {
            btn.addEventListener('click', function(e){
                e.preventDefault();
                deleteUrl = this.getAttribute('href');
                bsModal.show();
            });
        });

        document.getElementById('confirmDeleteBtn').addEventListener('click', function(){
            bsModal.hide();
            window.location.href = deleteUrl;
        });

        document.getElementById('cancelDeleteBtn').addEventListener('click', function(){
            bsModal.hide();
        });
    });
</script>

<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
</style>
@endsection