
@extends('layouts.nav')
@section('title')
Customers
@endsection
@section('content')               
@php
$PermissionsAddCustomers = App\Models\PermissionsUserModel::getPermission('Add Customers', Auth::user()->id);
$PermissionsEditCustomers = App\Models\PermissionsUserModel::getPermission('Edit Customers', Auth::user()->id);
$PermissionsDeleteCustomers = App\Models\PermissionsUserModel::getPermission('Delete Customers', 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 Customers</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 Customers</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($PermissionsAddCustomers))
                            <form class="card-body" action="{{route('addcustomers')}}" 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="Customer Account" name="AccName" value="Customer Account" required readonly>
                                    </div>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control form-control-user" id="code" placeholder="Customer 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="Customer 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 1" 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="contact2" placeholder="Contact 2" name="contact2"  value="{{ old('contact2') }}" required>
                                    
                                    </div>
                                    <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">
                                        <input type="text" class="form-control form-control-user" id="Address" placeholder="Address" name="Address"  value="{{ old('Address') }}" 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('','', '','', '','', '')"   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>Customer Code</th>
                                            <th>Name</th>
                                            <th>Contact Person</th>
                                            <th>Contact 1</th>
                                            <th>Contact 2</th>
                                            <th>Email</th>
                                            <th>Address</th>
                                            <th>Action</th>
                                         
                                        </tr>
                                    </thead>
                                    <tfoot>
                                        <tr>
                                        
                                        
                                        <th>Entry Date</th>
                                            <th>Customer Code</th>
                                            <th>Name</th>
                                            <th>Contact Person</th>
                                            <th>Contact 1</th>
                                            <th>Contact 2</th>
                                            <th>Email</th>
                                            <th>Address</th>
                                            <th>Action</th>
                                            

                                        </tr>
                                    </tfoot>
                                    <tbody>
                                      
                                    @foreach ($customers as $customer)
                                            <tr>
                                        
                                                <td>{{ $customer->EntryDate }}</td> 
                                                <td>{{ $customer->CustomeCode }}</td>
                                                <td>{{ $customer->Name }}</td> 
                                                <td>{{ $customer->ContactPerson }}</td>
                                                <td>{{ $customer->Contact1 }}</td> 
                                                <td>{{ $customer->Contact2 }}</td>
                                                <td>{{ $customer->Email }}</td> 
                                                <td>{{ $customer->Address }}</td>
                                                <td style="justify-content: center; align-items: center;">
                                                 @if(!empty($PermissionsEditCustomers)) 
                                                  <a href="" onclick="editAccountType(event,'{{ $customer->ID }}', '{{ $customer->CustomeCode }}', '{{ $customer->Name }}', '{{ $customer->ContactPerson }}', '{{ $customer->Contact1 }}', '{{ $customer->Contact2 }}', '{{ $customer->Email }}', '{{ $customer->Address }}')"  class="btn btn-primary btn-circle" >
                                                  <i class="fas fa-edit"></i>
                                                  </a>
                                                  @endif
                                                  @if(!empty($PermissionsDeleteCustomers))
                                                  <a href="{{route('deletecustomers',['id'=>$customer->ID])}}" class="btn btn-danger btn-circle"   onclick="return confirmDelete('{{ $customer->Name. ' => '.$customer->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,contact2,email,Address) 
                             {
                                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('contact2').value = contact2;
                                document.getElementById('email').value = email;
                                document.getElementById('Address').value = Address;
                                document.getElementById('formHeader').innerText = 'Edit Customer';
                                document.getElementById('submitButton').value = 'Update';
                                document.getElementById('accountTypeForm').action = '{{route("editcustomers")}}';
                            }
                
                            function refreshAccountType(id,code,name,contactperson,contact1,contact2,email,Address) 
                             {
                                
                                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('contact1').value = contact2;
                                document.getElementById('email').value = email;
                                document.getElementById('Address').value = Address;
                                document.getElementById('formHeader').innerText = 'Create Customer';
                                document.getElementById('submitButton').value = 'Create';
                                document.getElementById('accountTypeForm').action = '{{route("addcustomers")}}';
                                hideMessages();
                            
                            }

                            

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


                
                </script>

                    @endsection