
@extends('layouts.nav')
@section('title')
Sub Accounts
@endsection
@section('content')               


<div class="d-sm-flex align-items-center justify-content-between mb-4">
                        <h1 class="h3 mb-0 text-gray-800">Sub Accounts</h1>
                      </div>

                  
                    <div class="card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary"   id="formHeader">Create Sub Accounts</h6>
                        </div>
                        @if (session('success'))
                            <div class="alert alert-success" id="successMessage"  style="margin: 20px;">
                                {{ session('success') }}
                            </div>
                        @endif
                      
                        @if ($errors->any())

                                    <div class="form-group"  id="successMessage"  style="margin: 20px;">
                                        <div class="card bg-warning text-white shadow">
                                            <div class="card-body">
                                        
                                                @foreach ($errors->all() as $error)
                                                <div style="color: red; ">{{ $error }}</div>
                                                @endforeach
                                        
                                            </div>
                                        </div>
                                    </div>
                                    @endif
                            <form class="card-body" action="{{route('addSubAccount')}}" method="post"  id="accountTypeForm">
                            @csrf
                            <input type="hidden" id="accountId" name="id" value="">
                                
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                    <input type="text" class="form-control form-control-user" id="AccCode" placeholder="Account Code" name="AccCode"  value="{{ $AccCode }}" required readonly>
                                   
                                    </div>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control form-control-user" id="AccName" placeholder="Account Name" name="AccName"  value="{{ $AccName }}" required readonly>
                                    </div>
                                    
                                </div>

                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                   
                                    <input type="text" class="form-control form-control-user" id="SubAccCode" placeholder="Account Code" name="SubAccCode"  value="{{$generatedCodeValue }}" required readonly  >
                                    
                                    </div>
                                    <div class="col-sm-6">
                                    <input type="text" class="form-control form-control-user" id="SubAccName" placeholder="Sub Account Name" name="SubAccName"  value="{{ old('SubAccName') }}" required>
                                   
                                    
                                    </div>
                                    
                                </div>

                               <div style="display: flex;">
                               <input type="submit" style="width: 100px;height: 40px; margin-right: 10px; margin-top: 6px" value="Create" class="btn btn-primary btn-user btn-block" id="submitButton"/>

                                <input style="width: 100px;" onclick="refreshAccountType('','', {{$generatedCodeValue }})"  type="reset" value="Refresh" class="btn btn-secondary btn-user btn-block"  onclick="hideMessages()"/>
                                    
                               </div>
                                
                                <hr>
                             </form>
                   
                        
                        
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th>Account Code</th>
                                            <th>Account Name</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>
                                        
                                        
                                        <th>Account Code</th>
                                            <th>Account Name</th>
                                            <th>Action</th>

                                        </tr>
                                    </tfoot>
                                    <tbody>
                                      
                                    @foreach ($SubAccounts as $SubAccount)
                                            <tr>
                                        
                                                <td>{{ $SubAccount->SubName }}</td> 
                                                <td>{{ $SubAccount->SubCode }}</td>
                            
                                                <td style="justify-content: center; align-items: center;">
                                                  
                                                    <a href="" onclick="editAccountType(event,'{{ $SubAccount->ID }}', '{{ $SubAccount->SubCode }}', '{{ $SubAccount->SubName }}')"  class="btn btn-primary btn-circle" >
                                                    <i class="fas fa-edit"></i>
                                                    </a>
                                                    <a href="{{ route('deleteSubAccount', ['id' => $SubAccount->ID,'AccCode' => $AccCode,'AccName' => $AccName]) }}" class="btn btn-danger btn-circle"   onclick="return confirmDelete('{{ $SubAccount->SubName }}');" >
                                                        <i class="fas fa-trash"></i>
                                                    </a>
                                                </td>
                                                
                                            </tr>
                                        @endforeach

                                    </tbody>
                                </table>
                            </div>
                        </div>
                        
                    </div>

                    <script>
                             function editAccountType(event, id,SubAccCode, SubAccName) 
                             {
                                event.preventDefault();
                                document.getElementById('accountId').value = id;
                                document.getElementById('SubAccCode').value = SubAccCode;
                                document.getElementById('SubAccName').value = SubAccName;
                                document.getElementById('formHeader').innerText = 'Edit Sub Account';
                                document.getElementById('submitButton').value = 'Update';
                                document.getElementById('accountTypeForm').action = '{{route("editSubAccount")}}';
                            }
                
                            function refreshAccountType(id, AccName,SubAccCode) 
                            {
                                document.getElementById('accountId').value = id;
                                document.getElementById('SubAccCode').value = SubAccCode;
                                document.getElementById('SubAccName').value = SubAccName;
                                document.getElementById('formHeader').innerText = 'Create Sub Accounts';
                                document.getElementById('submitButton').value = 'Create';
                                document.getElementById('accountTypeForm').action = '{{route("addSubAccount")}}';
                                hideMessages();
                            
                            }

                            function confirmDelete(Name) {
                                return confirm("Are you sure you want to delete the Account Type: " + Name + "?");
                            }


                
                </script>

                    @endsection