{% extends "main.html" %}
{%load static%}
{% block content %}

<div class="w-full px-5 s:px-9 mb-5">
    <div class="w-full h-full shadow-md rounded-md mt-5 py-5 px-3 bg-white">
        <h1 class="text-3xl text-secondosiblue text-center font-semibold">
            Add Customer
        </h1>
        <form method="POST" action="{% url 'addcustomer' %}">
            {% csrf_token %}
            <div class="w-full flex flex-col gap-5 justify-center items-center mt-5">

                <div class="w-full">
                    <label class="text-gray-500">First Name:</label>
                    <input name="first_name" type="text"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1" required>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Last Name:</label>
                    <input name="last_name" type="text"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1" required>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Email:</label>
                    <input name="email" type="email"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1" required>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Mobile Number:</label>
                    <input name="mobile_number" type="number"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1" required>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Personal Website:</label>
                    <input name="personal_website" type="url"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1">
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Status:</label>
                    <select name="status"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1"
                        required>
                        <option value="Active">Active</option>
                        <option value="Suspended">Suspended</option>
                        <option value="Terminated">Terminated</option>
                    </select>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Reference:</label>
                    <select name="referenceid"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1"
                        required>
                        {% for reference in references %}
                        <option value="{{reference.id}}">{{reference.name}}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="w-full">
                    <label class="text-gray-500">Type:</label>
                    <select id="businessTypeSelect"
                        class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                        <option value="personal">Personal</option>
                        <option value="business">Business</option>
                    </select>
                </div>

                <div class="w-[80%] mx-auto border border-gray-300 rounded-md py-5 px-3 bg-white hidden"
                    id="addBusinessContainer">
                    <h1 class="text-3xl text-secondosiblue text-center font-semibold">
                        Select Business
                    </h1>

                    <div class="w-full flex justify-end items-center px-3 py-2">
                        <button type="button"
                            class="w-fit py-1 px-3 bg-white rounded-md outline-none text-osiblue border border-osiblue text-xl cursor-pointer hover:bg-osiblue duration-300 hover:text-white"
                            id="addBusinessButton">
                            Add Business
                        </button>
                    </div>

                    <div class="w-full">
                        <label class="text-gray-500">Business Name:</label>
                        <select name="business" id="businessSelect"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                            {% for business in businesses %}
                            <option value="{{business.id}}">{{business.name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>


                <!-- MAKE THE "ADDBUSINESSCONTAINER" VISIBLE WHEN CHOOSING THE CUSTOMER TYPE AS BUSINESS  -->
                <script>
                    const businessTypeSelect = document.getElementById('businessTypeSelect');
                    const addBusinessContainer = document.getElementById('addBusinessContainer');

                    businessTypeSelect.addEventListener('change', function () {
                        if (businessTypeSelect.value === 'business') {
                            addBusinessContainer.style.display = 'block';
                            addBusinessContainer.scrollIntoView({ behavior: 'smooth' });
                        } else {
                            addBusinessContainer.style.display = 'none';
                        }
                    });
                </script>

                <input name="password1" type="password" placeholder="Password"
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md" required>
                <input name="password2" type="password" placeholder="Password"
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md" required>

                <div class="w-full flex justify-center items-center mt-3">
                    <button type="submit"
                        class="w-fit py-1 px-3 bg-osiblue rounded-md outline-none text-white border border-osiblue text-xl cursor-pointer hover:bg-white hover:text-osiblue duration-300">Save</button>
                </div>
            </div>
        </form>


        <!-- THE "ADD BUSINESS" FORM THAT APPEARS WHEN CLICKING ON THE ADD BUSINESS BUTTON -->
        <div class="w-full p-3 rounded-md border border-gray-300 mt-5 hidden" id="addBusinessFormContainer">
            <h1 class="text-3xl text-secondosiblue text-center font-semibold">Add Business</h1>
            <form method="POST" action="{% url 'addbusinessmodal' %}" id="addBusinessFormModal"
                enctype="multipart/form-data">
                {% csrf_token %}

                <div class="w-full flex flex-col gap-5">
                    <div class="w-full">
                        <label class="text-gray-500">Name:</label>
                        <input name="name" type="text"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1"
                            required>
                    </div>


                    <div class="w-full">
                        <label class="text-gray-500">Business Logo:</label>
                        <div class="inbox-box border border-gray-300 w-full rounded-md px-3 mt-1">
                            <div class="flex items-center justify-between">
                                <input name="logo" required type="file" class="file-input" accept="image/*" hidden />
                                <span class="file-name text-gray-500 text-base focus:outline-none outline-none">Upload
                                    Logo</span>
                                <label
                                    class="file-label bg-transparent text-gray-500 border border-white h-14 cursor-pointer flex items-center">
                                    <i class="fa fa-upload" style="font-size: 25px;"></i>
                                </label>
                            </div>
                        </div>
                    </div>


                    <div class="w-full">
                        <label class="text-gray-500">Email:</label>
                        <input name="email" type="email"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1"
                            required>
                    </div>

                    <div class="w-full">
                        <label class="text-gray-500">Financial Number:</label>
                        <input name="financial_number" type="number"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1"
                            required>
                    </div>

                    <div class="w-full flex justify-start items-center gap-2 mt-1">
                        <input name="vat" type="checkbox" id="vatCheckbox" onchange="updateVatValue(this.checked)">
                        <label class="text-slate-800">Vat</label>
                    </div>


                    <script>
                        function updateVatValue(checked) {
                            const vatInput = document.querySelector('input[name="vat"]');
                            vatInput.value = checked ? "true" : "false";
                        }
                    </script>

                    <div class="w-full">
                        <label class="text-gray-500">Commercial Registration:</label>
                        <input name="commercial_registration" type="commercial_registration"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1"
                            required>
                    </div>

                    <div class="w-full">
                        <label class="text-gray-500">Mobile Number:</label>
                        <input name="phone_number" type="number"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1"
                            required>
                    </div>

                    <div class="w-full">
                        <label class="text-gray-500">Website:</label>
                        <input name="website" 
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1">
                    </div>


                    <div class="w-full">
                        <label class="text-gray-500">Business Type:</label>
                        <select name="type" id="business_type"
                            class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                            {% for business_type in business_types %}
                            <option value="{{ business_type.id }}">{{ business_type.name }}</option>
                            {% endfor %}
                        </select>
                    </div>


                    <div class="w-full flex justify-center items-center mt-3">
                        <button type="submit"
                            class="w-fit py-1 px-5 bg-osiblue rounded-md outline-none text-white border border-osiblue text-xl cursor-pointer hover:bg-white hover:text-osiblue duration-300"
                            id="addBusinessFormButton">Save</button>
                    </div>
                </div>

            </form>
        </div>

        <!-- TO MAKE THE "ADDBUSINESSFORMCONTAINER" VISIBLE WHEN CLICKING ON THE ADD BUSINESS BUTTON -->
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                const addButton = document.getElementById('addBusinessButton');
                const formContainer = document.getElementById('addBusinessFormContainer');
                const formButton = document.getElementById('addBusinessFormButton');

                addButton.addEventListener('click', function () {
                    formContainer.classList.toggle('hidden');

                    if (!formContainer.classList.contains('hidden')) {
                        formContainer.scrollIntoView({ behavior: 'smooth' });
                    }
                });

                formButton.addEventListener('click', function () {
                    formContainer.classList.add('hidden');
                });
            });

        </script>


        <!-- TO FETCH AND DISPLAY THE NEW ADDED BUSINESS IN THE BUSINESS SELECT TAG -->
        <script>
            document.getElementById('addBusinessFormModal').addEventListener('submit', function (event) {
                event.preventDefault(); // Prevent the form from actually submitting

                const form = this;
                const formData = new FormData(this);

                fetch("{% url 'addbusinessmodal' %}", {
                    method: "POST",
                    body: formData,
                })
                    .then(response => response.json())
                    .then(data => {
                        // console.log('Received JSON data:', data); // Check if data is received

                        form.reset();
                        const fileNameSpan = document.getElementById('file-name');
                        fileNameSpan.textContent = 'Upload Business Logo';


                        refreshSelectOptions(data);
                    })
                    .catch(error => {
                        console.error('Error fetching updated options:', error);
                    });
            });

            function refreshSelectOptions(updatedOptions) {
                const selectElement = document.getElementById('businessSelect');


                selectElement.innerHTML = '';

                // Add a default option
                const defaultOption = document.createElement('option');
                defaultOption.value = '';
                defaultOption.textContent = 'Business Name';
                defaultOption.disabled = true;
                selectElement.appendChild(defaultOption);

                // Add the new options in reverse order
                for (let i = updatedOptions.length - 1; i >= 0; i--) {
                    const business = updatedOptions[i];
                    const option = document.createElement('option');
                    option.value = business.id;
                    option.textContent = business.name;
                    selectElement.appendChild(option);
                }
            }
        </script>
    </div>
</div>
</div>

<!-- POPUP MODAL -->
<div class="w-full h-full bg-black bg-opacity-40 z-20 fixed justify-center items-center hidden" id="popUpModal">
    <div class="w-[95%] md:w-fit h-fit bg-white rounded-md p-9 relative">
        <button class="absolute top-3 right-5 text-slate-800 text-xl cursor-pointer outline-none border-none"
            id="closeModalButton">
            <i class="fa fa-close"></i>
        </button>
        <iframe id="popupModalFrame" frameborder="0"></iframe>
    </div>
</div>

<!-------------- JS SCRIPTS --------------->
<script type="text/javascript" src='{% static "js/upload-input-tag.js" %}'></script>

<script type="text/javascript" src='{% static "js/pop-modals.js" %}'></script>

</div>

{% endblock content %}