@extends('layouts.master')
@section('content')
<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="{{ route('main.sub.account.add', $mainSubAccount->parent_id) }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Main Sub Accounts
        </a>
    </div>

    <div class="row">
        <!-- FORM SECTION -->
        <div class="col-lg-5">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white py-3">
                    <h5 id="formHeader" class="mb-0"><i class="fas fa-layer-group me-2"></i> Add Sub Account 1</h5>
                </div>
                <div class="card-body">
                    <form id="subAccount1Form" action="{{ route('sub.account.1.store') }}" method="POST">
                        @csrf
                        <input type="hidden" id="subAccount1Id" name="id">
                        <input type="hidden" id="parentId" name="parent_id" value="{{ $mainSubAccount->id }}">

                        <div id="timeout">
                            @if(Session::has('success'))
                                <div class="alert alert-success">{{ Session('success') }}</div>
                            @endif
                            @if(Session::has('error'))
                                <div class="alert alert-danger">{{ Session('error') }}</div>
                            @endif
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Main Account:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   value="{{ $mainSubAccount->mainAccount->code }} - {{ $mainSubAccount->mainAccount->account_name }}"
                                   readonly />
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Main Sub Account:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   value="{{ $mainSubAccount->account_code }} - {{ $mainSubAccount->sub_name }}"
                                   readonly />
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Sub Account 1 Code:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   id="accountCode"
                                   name="account_code"
                                   required />
                        </div>

                        <div class="mb-3">
                            <label class="form-label"><strong>Sub Account 1 Name:</strong></label>
                            <input class="form-control"
                                   type="text"
                                   id="subName"
                                   name="sub_name"
                                   required />
                        </div>

                        <div class="d-flex justify-content-end mt-4">
                            <input id="submitButton" type="submit" class="btn btn-primary me-2" value="SUBMIT">
                            <input type="reset" class="btn btn-outline-secondary" value="REFRESH" onclick="resetSubAccount1Form()">
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- TABLE SECTION -->
        <div class="col-lg-7 mt-4 mt-lg-0">
            <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> Sub Accounts 1</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Sub Account 1 Code</th>
                                    <th>Sub Account 1 Name</th>
                                    <th>Main Sub Account</th>
                                    <th style="width:150px">Sub Account 2</th>
                                    <th style="width:100px">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($subAccounts1 as $subAccount1)
                                <tr>
                                    <td>{{ $subAccount1->account_code }}</td>
                                    <td>{{ $subAccount1->sub_name }}</td>
                                    <td>{{ $subAccount1->mainSubAccount->sub_name ?? 'N/A' }}</td>
                                    <td>
                                        <a href="{{ route('sub.account.2.add', $subAccount1->id) }}" class="btn btn-sm btn-info">
                                            &rarr; Sub Acc 2
                                        </a>
                                    </td>
                                    <td class="text-center">
                                        <a href="#" class="btn btn-sm btn-primary" onclick="editSubAccount1(
                                            event,
                                            '{{ $subAccount1->id }}',
                                            '{{ $subAccount1->account_code }}',
                                            '{{ $subAccount1->sub_name }}'
                                        )">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button class="btn btn-sm btn-danger deleteSubAccount1Btn" 
                                                data-id="{{ $subAccount1->id }}">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- DELETE MODAL -->
<div class="modal fade" id="deleteSubAccount1ConfirmModal" 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>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete this sub account 1?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <form id="deleteSubAccount1Form" method="POST" action="">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function editSubAccount1(event, id, account_code, sub_name) {
    event.preventDefault();

    document.getElementById('subAccount1Id').value = id;
    document.getElementById('accountCode').value = account_code;
    document.getElementById('subName').value = sub_name;

    document.getElementById('formHeader').innerHTML = '<i class="fas fa-layer-group me-2"></i> Edit Sub Account 1';
    document.getElementById('submitButton').value = 'UPDATE';
    document.getElementById('subAccount1Form').action = '{{ route("editSubAccount1") }}';
}

function resetSubAccount1Form() {
    document.getElementById('subAccount1Id').value = '';
    document.getElementById('accountCode').value = '';
    document.getElementById('subName').value = '';
    document.getElementById('formHeader').innerHTML = '<i class="fas fa-layer-group me-2"></i> Add Sub Account 1';
    document.getElementById('submitButton').value = 'SUBMIT';
    document.getElementById('subAccount1Form').action = '{{ route("sub.account.1.store") }}';
    document.getElementById('timeout').innerHTML = '';
}

document.addEventListener('DOMContentLoaded', function(){
    const deleteButtons = document.querySelectorAll('.deleteSubAccount1Btn');
    const deleteForm = document.getElementById('deleteSubAccount1Form');
    const modal = new bootstrap.Modal(document.getElementById('deleteSubAccount1ConfirmModal'));
    
    deleteButtons.forEach(button => {
        button.addEventListener('click', function() {
            const accountId = this.getAttribute('data-id');
            const actionUrl = "{{ route('sub.account.1.delete', ['id' => ':id']) }}".replace(':id', accountId);
            
            deleteForm.action = actionUrl;
            modal.show();
        });
    });
});
</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