{% extends 'base.html.twig' %}

{% block title %}Entreprise | Secufinance{% endblock %}

{% block stylesheets %}
    <link rel="stylesheet" type="text/css" href="{{ asset('plugins/table/datatable/datatables.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ asset('plugins/table/datatable/dt-global_style.css') }}">
    <link href="{{ asset('assets/css/components/custom-modal.css') }}" rel="stylesheet" type="text/css"/>

    <link href="{{ asset('assets/css/scrollspyNav.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('plugins/file-upload/file-upload-with-preview.min.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('plugins/flatpickr/flatpickr.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('plugins/flatpickr/custom-flatpickr.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('plugins/summernote/summernote-bs4.css') }}" rel="stylesheet" type="text/css">

    <style>
        .flatpickr-wrapper{
            width: 100%;
        }
    </style>
{% endblock %}

{% block body %}

    <div class="layout-px-spacing">
        <div class="page-header">
            <div class="page-title">
                <h3>Gestion des demandes {{ requestType.name }} </h3>
            </div>
            <button type="button" class="btn btn-outline-primary mb-2 mr-2" data-toggle="modal"
                    data-target="#addRequest " style="float: right; margin-bottom: 16px; margin-top: 30px;">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
                     stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                     class="feather feather-file-text">
                    <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                    <polyline points="14 2 14 8 20 8"></polyline>
                    <line x1="16" y1="13" x2="8" y2="13"></line>
                    <line x1="16" y1="17" x2="8" y2="17"></line>
                    <polyline points="10 9 9 9 8 9"></polyline>
                </svg>
                {% if requestType.id == 5 %} Ajouter une demande interne {% else %} Ajouter une demande {% endif %}
            </button>

            {% if is_granted('ROLE_ADMIN') %}
                <a href="{{ url('request_object_management', {requestType: requestType.id}) }}" type="button"
                   class="btn btn-outline-dark mb-2 mr-2" style="float: right; margin-bottom: 16px; margin-top: 30px;">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
                         stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                         class="feather feather-file-text">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                        <polyline points="14 2 14 8 20 8"></polyline>
                        <line x1="16" y1="13" x2="8" y2="13"></line>
                        <line x1="16" y1="17" x2="8" y2="17"></line>
                        <polyline points="10 9 9 9 8 9"></polyline>
                    </svg>
                    Gestion des objets
                </a>
            {% endif %}
            {% if not isArchived %}
                <a href="{{ path('clientRequest', {requestType: requestType.id }) }}?archive=true"
                   class="btn btn-outline-warning mb-2 mr-2"
                   style="float: right; margin-bottom: 16px; margin-top: 30px;">
                    Archive
                </a>
            {% else %}
                <a href="{{ path('clientRequest', {requestType: requestType.id }) }}"
                   class="btn btn-outline-success mb-2 mr-2"
                   style="float: right; margin-bottom: 16px; margin-top: 30px;">
                    Retour à la liste
                </a>
            {% endif %}

            <!-- Modal -->
            <div class="modal fade" id="addRequest" tabindex="-1" role="dialog"
                 aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-xl" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">
                                {% if requestType.id == 5 %} Ajouter une demande interne {% else %} Ajouter une demande {% endif %}
                            </h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                     viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                                     stroke-linecap="round" stroke-linejoin="round" class="feather feather-x">
                                    <line x1="18" y1="6" x2="6" y2="18"></line>
                                    <line x1="6" y1="6" x2="18" y2="18"></line>
                                </svg>
                            </button>
                        </div>
                        <form method="post" enctype="multipart/form-data">
                            <input type="hidden" name="type" value="{{ app.request.get('requestType').id }}">
                            <div class="modal-body">
                                <div class="row">
                                    {% if requestType.id == 5 %}
                                        {% if is_granted('ROLE_ACCOUNTANT') or is_granted('ROLE_ADMIN') or is_granted('ROLE_LAWYER') or is_granted('ROLE_ACCOUNTINGASSISTANT') %}
                                            <div class="col-12 form-group mb-4">
                                                <label for="exampleFormControlSelect1">Choisir un
                                                    collaborateur...</label>
                                                <select class="form-control" name="collaborator" required>
                                                    <option selected="selected" disabled>collaborateurs...</option>
                                                    {% for collaborator in collaborators %}
                                                        {% if collaborator.id != app.user.id %}
                                                            <option value="{{ collaborator.id }}">
                                                                {% if  collaborator.firsName is defined and  collaborator.lastName is defined %}
                                                                    {{ collaborator.firsName }} {{ collaborator.lastName }}
                                                                {% else %}
                                                                    Admin
                                                                {% endif %}
                                                            </option>
                                                        {% endif %}
                                                    {% endfor %}
                                                </select>
                                            </div>
                                        {% endif %}
                                    {% else %}
                                        {% if is_granted('ROLE_ACCOUNTANT') or is_granted('ROLE_ADMIN') %}
                                            <div class="col-md-6 form-group mb-4">
                                                <select class="form-control" name="client" required>
                                                    <option selected="selected" disabled>clients</option>
                                                    {% for client in clients %}
                                                        <option value="{{ client.id }}">{{ client.company }}</option>
                                                    {% endfor %}
                                                </select>
                                            </div>
                                        {% endif %}
                                    {% endif %}

                                    <div class="col-md-6 form-group mb-4">
                                        <select class="form-control" name="object" required>
                                            <option selected="selected" disabled>Objets...</option>
                                            {% for object in objects %}
                                                {% if object.isActive %}
                                                    <option value="{{ object.id }}">{{ object.object }}</option>
                                                {% endif %}
                                            {% endfor %}
                                        </select>
                                    </div>

                                    <div class="col-12 form-group mb-4">
                                            <textarea type="text" name="message" class="form-control summernote"
                                                      placeholder="Corp du demande" required></textarea>
                                    </div>
                                    <div class="col-md-6 mt-4">
                                        <div class="form-group">
                                            <input id="dateTimeFlatpickr"
                                                   class="form-control flatpickr flatpickr-input active"
                                                   type="text" placeholder="Sélectionner une date de Deadline...">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="custom-file-container" data-upload-id="mySecondImage">
                                            <label style="display: block">charger vos pièce jointe
                                                <a href="javascript:void(0)"
                                                   class="custom-file-container__image-clear btn btn-sm btn-outline-danger float-right"
                                                   title="Effacer les documents">
                                                    <b>X</b>
                                                </a>
                                            </label>
                                            <label class="custom-file-container__custom-file">
                                                <input type="file" name="files[]"
                                                       class="custom-file-container__custom-file__custom-file-input"
                                                       multiple>
                                                {# <input type="hidden" name="MAX_FILE_SIZE" value="90485760" /> #}
                                                <span class="custom-file-container__custom-file__custom-file-control"></span>
                                            </label>
                                            <div class="custom-file-container__image-preview"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button class="btn" data-dismiss="modal">
                                    <i class="flaticon-cancel-12"></i> Annuler
                                </button>
                                <button type="submit" class="btn btn-outline-primary">Envoyer</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </div>

        <div class="row" id="cancel-row">
            <div class="col-xl-12 col-lg-12 col-sm-12  layout-spacing">
                <div class="widget-content widget-content-area br-6">
                    <div class="table-responsive mb-4 mt-4">
                        <table id="zero-config" class="table table-hover text-align-left" style="width:100%">
                            <thead>
                            <tr>
                                {% if (is_granted('ROLE_ADMIN') or is_granted('ROLE_ACCOUNTANT')) and requestType.id != 5 %}
                                    <th>client</th>
                                {% endif %}
                                <th>Titre</th>
                                <th>Date</th>
                                <th>Crée par</th>
                                <th>état</th>
                                <th class="no-content"></th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for clientrequest in clientrequests %}
                                <tr>
                                    {% if (is_granted('ROLE_ADMIN') or is_granted('ROLE_ACCOUNTANT')) and clientrequest.type.id != 5 %}
                                        <td>
                                            {% if clientrequest.client.logo is not null %}
                                                <img src="{{ asset('uploads/clients/' ~ clientrequest.client.logo ) }}"
                                                     alt="{{ clientrequest.client.logo }}" class="rounded"
                                                     width="60">
                                            {% else %}
                                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                     viewBox="0 0 24 24"
                                                     fill="none" stroke="currentColor" stroke-width="2"
                                                     stroke-linecap="round"
                                                     stroke-linejoin="round" class="feather feather-user text-info">
                                                    <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                                                    <circle cx="12" cy="7" r="4"></circle>
                                                </svg>
                                            {% endif %}
                                            <p class="mt-1 text-capitalize">
                                                <small>{{ clientrequest.client.company }}</small></p>
                                        </td>
                                    {% endif %}
                                    <td>
                                        <a href="{{ url('clientrequest_show', {'id': clientrequest.id} ) }}"
                                           title="Afficher la demande">
                                            <b>
                                                {% if clientrequest.object %}
                                                    {{ clientrequest.object.object }}
                                                {% else %}
                                                    {{ clientrequest.title }}
                                                {% endif %}
                                            </b>
                                        </a>
                                    </td>
                                    <td>{{ clientrequest.createdAt |date("d-m-Y h:i:s") }}</td>
                                    <td>
                                        {{ clientrequest.createdBy.email }}
                                    </td>
                                    <td>
                                        {% if clientrequest.isClosed %}
                                            <span class="badge badge-danger">Cloturé</span>
                                        {% else %}
                                                <span class="badge badge-success">En cours</span>
                                        {% endif %}
                                        {# ----------- Archiving -------- #}
                                        {% if clientrequest.isArchived  %}
                                            <span class="badge badge-success">Archivé</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if not clientrequest.isClosed and is_granted('ROLE_ADMIN') %}
                                            <a href="{{ url('clientrequest_close_request', {id : clientrequest.id, requestType : requestType.id}) }}"
                                               class="btn btn-danger"
                                               onclick="return confirm('Voulez-vous vraiment clôturer cette demande?');">cloturer</a>
                                        {% endif %}
                                        {% if not clientrequest.isArchived and is_granted('ROLE_ADMIN') %}
                                            <a href="{{ url('clientrequest_close_request', {id : clientrequest.id, requestType : requestType.id}) }}"
                                               class="btn btn-warning"
                                               onclick="return confirm('Voulez-vous vraiment archiver cette demande?');">Archiver</a>
                                        {% endif %}
                                        {# <a href="{{ path('client_edit', {'id': client.id}) }}"
                                               class="btn btn-warning mb-2 mr-2 rounded-circle" title="modifier">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                     viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                                                     stroke-linecap="round" stroke-linejoin="round"
                                                     class="feather feather-user-plus">
                                                    <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path>
                                                    <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path>
                                                </svg>
                                            </a>
                                            <form method="post" action="{{ path('client_delete', {'id': client.id}) }}"
                                                  onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer {{ client.email }}');"
                                                  style="display: inline">
                                                <input type="hidden" name="_method" value="DELETE">
                                                <input type="hidden" name="_token"
                                                       value="{{ csrf_token('delete' ~ client.id) }}">
                                                <button class="btn btn-danger mb-2 mr-2 rounded-circle" title="Supprimer">
                                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                         viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                                                         stroke-linecap="round" stroke-linejoin="round"
                                                         class="feather feather-trash-2">
                                                        <polyline points="3 6 5 6 21 6"></polyline>
                                                        <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                                                        <line x1="10" y1="11" x2="10" y2="17"></line>
                                                        <line x1="14" y1="11" x2="14" y2="17"></line>
                                                    </svg>
                                                </button>
                                            </form> #}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </div>
{% endblock %}

{% block javascripts %}
    <script src="{{ asset('plugins/table/datatable/datatables.js') }}"></script>


    <script src="{{ asset('assets/js/scrollspyNav.js') }}"></script>
    <script src="{{ asset('plugins/file-upload/file-upload-with-preview.min.js') }}"></script>
    <script src="{{ asset('plugins/summernote/summernote-bs4.min.js') }}"></script>
    <script src="{{ asset('plugins/flatpickr/flatpickr.js') }}"></script>
    <script src="{{ asset('plugins/flatpickr/custom-flatpickr.js') }}"></script>
    <script>
        $('.summernote').summernote({
            placeholder: 'détails de votre demande',
            tabsize: 2,
            height: 100
        });

        $('#zero-config').DataTable({
            "aaSorting": [],
            "oLanguage": {
                "oPaginate": {
                    "sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>',
                    "sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>'
                },
                "sInfo": "Afficher la page _PAGE_ de _PAGES_",
                "sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
                "sSearchPlaceholder": "Recherche...",
                "sLengthMenu": "Résultats :  _MENU_",
            },
            "stripeClasses": [],
            "lengthMenu": [10, 20, 50],
            "pageLength": 10
        });

        var secondUpload = new FileUploadWithPreview('mySecondImage')

        var f2 = flatpickr(document.getElementById('dateTimeFlatpickr'), {
            enableTime: true,
            dateFormat: "Y-m-d H:i",
            static: true,
            minDate: new Date()
        });
    </script>
{% endblock %}