
@extends('layouts.nav')
@section('title')
Tenants
@endsection
@section('content')
@php
$PermissionsAddTenants = App\Models\PermissionsUserModel::getPermission('Add Tenants', Auth::user()->id);
$PermissionsEditTenants = App\Models\PermissionsUserModel::getPermission('Edit Tenants', Auth::user()->id);
$PermissionsDeleteTenants = App\Models\PermissionsUserModel::getPermission('Delete Tenants', Auth::user()->id);
@endphp

<div class="d-sm-flex align-items-center justify-content-between mb-4">
                        <h1 class="h3 mb-0 text-gray-800">System Tenants</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">Add tenants</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

                            @if(!empty($PermissionsAddTenants))
                            <form class="card-body" action="{{route('addtenants')}}" method="post"  id="accountTypeForm">
                            @csrf
                            <input type="hidden" id="accountId" name="id" value="">

                            <div class="form-group row">
                                   <div class="col-sm-4 mb-3 mb-sm-0">
                                        <input type="text" class="form-control form-control-user" id="AccCode" placeholder="Account Code" name="AccCode"  value="{{ $account }}" required readonly>
                                    </div>
                                    <div class="col-sm-4 mb-3 mb-sm-0">
                                        <input type="text" class="form-control form-control-user" id="AccName" placeholder="Tenants Account" name="AccName" value="Tenant Account" required readonly>
                                    </div>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control form-control-user" id="code" placeholder="Tenant Code" name="code"  value="{{ old('code') }}" required>
                                    </div>
                                </div>

                                <div class="form-group row">
                                <div class="col-sm-4 mb-3 mb-sm-0">
                                        <input type="text" class="form-control form-control-user" id="name" placeholder="Tenant Name" name="name"  value="{{ old('name') }}" required>
                                    </div>
                                    <div class="col-sm-4 mb-3 mb-sm-0">
                                        <input type="text" class="form-control form-control-user" id="contactperson" placeholder="Contact Person" name="contactperson"  value="{{ old('contactperson') }}" required>
                                    </div>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control form-control-user" id="contact1" placeholder="Contact" name="contact1"  value="{{ old('contact1') }}" required>
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <input type="text" class="form-control form-control-user" id="email" placeholder="Email" name="email"  value="{{ old('email') }}" required>

                                    </div>
                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <input type="text" class="form-control form-control-user" id="Address" placeholder="Address" name="Address"  value="{{ old('Address') }}" required>


                                    </div>
                                    <div class="col-sm-4">


                                    <input type="text" class="form-control form-control-user" id="vat" placeholder="VAT" name="vat"  value="{{ old('vat') }}" required>


                                </div>
                                </div>

                                <div class="form-group row">
                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <input type="text" class="form-control form-control-user" id="tin" placeholder="TIN" name="tin"  value="{{ old('tin') }}" required>

                                    </div>

                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <select class="form-control form-control-user" id="floor" name="floor"  value="{{ old('floor') }}"  required>
                                        <option value="" disabled selected>Select Floor</option>

                                        @foreach ($floors as $floor)

                                        <option value="{{ $floor->FloorNumber }}">{{ $floor->FloorNumber }}</option>

                                        @endforeach
                                    </select>

                                      </div>
                                      <div class="col-sm-4">


                                      <input type="text" class="form-control form-control-user" id="space" placeholder="Space Occupied" name="space"  value="{{ old('space') }}" required>


                                  </div>



                                </div>




                                <div class="form-group row">
                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <input type="text" class="form-control form-control-user" id="rooms" placeholder="Rooms" name="rooms"  value="{{ old('rooms') }}" required>

                                    </div>
                                    <div class="col-sm-4 mb-3 mb-sm-0">

                                    <input type="number" step=0.01 class="form-control form-control-user" id="rooms" placeholder="Rent Amount" name="rent_amount"  value="{{ old('rent_amount') }}" required>

                                    </div>
                                    <div class="col-sm-4 mb-3 mb-sm-0">
                                        <select class="form-control form-control-user" id="ageCode" name="ageCode" value="{{ old('ageCode') }}" required>
                                            <option value="" disabled selected>Select Ageing</option>
                                            @foreach ($ageingAnalyses as $analysis)
                                            <option value="{{ $analysis->age_code }}">
                                                {{ $analysis->age_code }} - {{ $analysis->description }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </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('','', '','', '','', '','', '', '','', '')"   type="reset" value="Refresh" class="btn btn-secondary btn-user btn-block"  onclick="hideMessages()"/>

                               </div>

                                <hr>
                             </form>
                             @endif



                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th>Entry Date</th>
                                            <th>Tenant Code</th>
                                            <th>Name</th>
                                            <th>Contact Person</th>
                                            <th>Contact</th>
                                            <th>Email</th>
                                            <th>Address</th>
                                            <th>VAT</th>
                                            <th>TIN</th>
                                            <th>Floor</th>
                                            <th>Space</th>
                                            <th>Rooms</th>
                                                                                        <th>Rent Amount</th>

                                            <th>Action</th>

                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>


                                        <th>Entry Date</th>
                                            <th>Tenant Code</th>
                                            <th>Name</th>
                                            <th>Contact Person</th>
                                            <th>Contact</th>
                                            <th>Email</th>
                                            <th>Address</th>
                                            <th>VAT</th>
                                            <th>TIN</th>
                                            <th>Floor</th>
                                            <th>Space</th>
                                            <th>Rooms</th>
                                            <th>Rent Amount</th>
                                            <th>Action</th>


                                        </tr>
                                    </tfoot>
                                    <tbody>

                                    @foreach ($tenants as $tenant)
                                            <tr>

                                                <td>{{ $tenant->EntryDate }}</td>
                                                <td>{{ $tenant->TenantCode }}</td>
                                                <td>{{ $tenant->Name }}</td>
                                                <td>{{ $tenant->ContactPerson }}</td>
                                                <td>{{ $tenant->Contact }}</td>
                                                <td>{{ $tenant->Email }}</td>
                                                <td>{{ $tenant->Address }}</td>
                                                <td>{{ $tenant->VAT }}</td>
                                                <td>{{ $tenant->TIN }}</td>
                                                <td>{{ $tenant->Floor }}</td>
                                                <td>{{ $tenant->Space }}</td>
                                                <td>{{ $tenant->Rooms }}</td>
                                                <td>{{ $tenant->rent_amount }}</td>
                                                <td style="justify-content: center; align-items: center;">
                                                  @if(!empty($PermissionsEditTenants))
                                                  <a href="" onclick="editAccountType(event,'{{ $tenant->ID }}', '{{ $tenant->TenantCode }}', '{{ $tenant->Name }}', '{{ $tenant->ContactPerson }}', '{{ $tenant->Contact }}', '{{ $tenant->Email }}', '{{ $tenant->Address }}', '{{ $tenant->VAT }}', '{{ $tenant->TIN }}', '{{ $tenant->Floor }}', '{{ $tenant->Space }}', '{{ $tenant->Rooms }}')"  class="btn btn-primary btn-circle" >
                                                  <i class="fas fa-edit"></i>
                                                  </a>
                                                  @endif
                                                  @if(!empty($PermissionsDeleteTenants))
                                                  <a href="{{route('deletetenants',['id'=>$tenant->ID])}}" class="btn btn-danger btn-circle"   onclick="return confirmDelete('{{ $tenant->Name. ' => '.$tenant->ContactPerson }}');" >
                                                      <i class="fas fa-trash"></i>
                                                  </a>
                                                  @endif
                                              </td>
                                            </tr>
                                        @endforeach

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

                    </div>

                    <script>
                           function editAccountType(event,id,code,name,contactperson,contact1,email,Address,vat,tin,floor,space,rooms)
                             {
                                event.preventDefault();

                                document.getElementById('accountId').value = id;
                                document.getElementById('code').value = code;
                                document.getElementById('name').value = name;
                                document.getElementById('contactperson').value = contactperson;
                                document.getElementById('contact1').value = contact1;
                                document.getElementById('vat').value = vat;
                                document.getElementById('tin').value = tin;
                                document.getElementById('email').value = email;
                                document.getElementById('Address').value = Address;
                                document.getElementById('floor').value = floor;
                                document.getElementById('space').value = space;
                                document.getElementById('rooms').value = rooms;
                                document.getElementById('formHeader').innerText = 'Edit Tenant';
                                document.getElementById('submitButton').value = 'Update';
                                document.getElementById('accountTypeForm').action = '{{route("edittenants")}}';
                            }

                            function refreshAccountType(id,code,name,contactperson,contact1,email,Address,vat,tin,floor,space,rooms)
                             {

                                document.getElementById('accountId').value = id;
                                document.getElementById('code').value = code;
                                document.getElementById('name').value = name;
                                document.getElementById('contactperson').value = contactperson;
                                document.getElementById('contact1').value = contact1;
                                document.getElementById('vat').value = vat;
                                document.getElementById('tin').value = tin;
                                document.getElementById('email').value = email;
                                document.getElementById('Address').value = Address;
                                document.getElementById('floor').value = floor;
                                document.getElementById('space').value = space;
                                document.getElementById('rooms').value = rooms;
                                document.getElementById('formHeader').innerText = 'Create Tenant';
                                document.getElementById('submitButton').value = 'Create';
                                document.getElementById('accountTypeForm').action = '{{route("addtenants")}}';
                                hideMessages();

                            }

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





                </script>

                    @endsection
