{% extends "/base.html" %}

{% block title %} UI Tables {% endblock %} 

<!-- Specific CSS goes HERE -->
{% block stylesheets %}
<style>.non-match-type {
    background-color: #ec8f8fe3;
}
.not-allowed {cursor: not-allowed;}
.btn-arrow {
       
        font-size: 16px;
        text-decoration: none;
    }

/* Base styling for the overlay */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent black overlay */
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999; /* Make sure the overlay is above other elements */
}

/* Loader styling */
.loader {
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite; /* Safari */
  animation: spin 2s linear infinite;
}

/* Spinner animation keyframes */
@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}


 
</style>
{% endblock stylesheets %}

{% block content %}
<div id="flash-container">
    {% with messages = get_flashed_messages() %}
        {% if messages %}
            <div class="flash">
                {{ messages[0] }}
            </div>
        {% endif %}
    {% endwith %}
</div>
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="uploadModalLabel">Upload File</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Your existing form goes here -->
                <form role="form" id="uploadForm" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>File:</label>
                        <input type="file" name="excelFile" accept=".xlsx, .xls" required>
                    </div>
                    <!-- Rest of your form fields go here -->
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                {% if session.get('role') == 'checker' %}
                    <button id="submitBtn_checker" type="button" class="btn btn-primary">Submit_checker</button>
                {% else %}
                    <button id="submitBtn" type="button" class="btn btn-primary">Submit_user</button>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="history-modal-show" tabindex="-1" role="dialog" aria-labelledby="uploadModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="uploadModalLabel">History</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body history-modal">
            </div>
            
        </div>
    </div>
</div>


    <!-- [ Main Content ] start -->
    <section class="pcoded-main-container">
        <div class="pcoded-wrapper">

            <div class="pcoded-content">
                <div class="pcoded-inner-content">
                    <!-- [ breadcrumb ] start -->
                    <div class="page-header">
                        <div class="page-block">
                            <div class="row align-items-center">
                                <div class="col-md-12">
                                    <div class="page-header-title">
                                        <h5 class="m-b-10"></h5>
                                    </div>
                                    <ul class="breadcrumb">
                                        <li class="breadcrumb-item"><a href="/dashboard"><i class="feather icon-home"></i></a></li>
                                        <li class="breadcrumb-item"><a href="">File List</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- [ breadcrumb ] end -->
                    <div class="main-body">
                        <div class="page-wrapper">
                            <div class="row">
                                <!-- [ basic-table ] start -->
                                <div class="col-xl-12">
                                    <div class="card">
                                        <div class="card-header">
                                            <h5>File List</h5>
                                        </div>
                                        <div style="text-align: end">
                                            {% if session.get('role') == 'checker' or session.get('role') == 'user' %}
                                            <button type="submit" class="btn btn-secondary" title="Create User" onclick="window.location.href='/download_excel'">Download</button>
                                            {% endif %}
                                            <button type="button" class="btn btn-secondary" title="Create User" data-toggle="modal" data-target="#uploadModal">
                                                Upload File
                                            </button>
                                        </div>
                                        
                                        <!-- <div style="text-align: end"><button type="submit" class="btn btn-secondary" title="Create User" onclick="window.location.href='/add_db'">Add Data Source</button></div>-->
                                        <div class="card-block table-border-style"> 
                                            <div class="table-responsive">
                                                <table class="table">
                                                    <thead>
                                                        <tr>
                                                            <th>#</th>
                                                            <th>File Name</th>
                                                            <th>Date</th>
                                                            <th>Inserted Rows</th>
                                                            <th>Updated Rows</th>
                                                            <th>view</th>
                                                            <th>History</th>
                                                            <th>Download</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <!-- Your rows inside the table HERE: -->
                                                        {% for item in data_dicts %}
                                                        <tr>
                                                            <th scope="row">{{ item.id }}</th>
                                                            <td contenteditable="false" class="editable" data-field="file_path">{{ item.file_path.split("\\")[-1] }}</td>
                                                            <td contenteditable="false" class="editable" data-field="username">{{ item.created_at.strftime('%d %B %Y') }}</td>
                                                            <td>{{ item.inserted_rows }}</td>
                                                            <td>{{ item.updated_rows }}</td>
                                                            <td>
                                                                <a href="/show_data/{{ item.id }}" class="fa fa-eye"></a>
                                                            </td>
                                                            <td>
                                                                <a  class="fa fa-info-circle history" id="{{ item.id }}"></a>
                                                            </td>
                                                            <td>
                                                                <a type="submit" class="fa fa-download" title="Create User" onclick="window.location.href='/download_excel_file/{{ item.id }}'"></a>
                                                            </td>
                                                        </tr>
                                                        {% endfor %}
                                                    </tbody>
                                                </table>
                                                <div class="overlay" id="loadingGif" style="display: none;">
                                                    <div class="loader"></div>
                                                </div> 
                                              
                                                </div>
                                
                                                </div>
                            
                                            </div>
                                </div>
                            </div>
                        </div>
                        </div>
                        </div>
                    </div>
    </section>
    <!-- [ Main Content ] end -->

{% endblock content %}

<!-- Specific Page JS goes HERE  -->
{% block javascripts %}
<script>
    function toggleLoader(show) {
        if (show) {
            $('#loadingGif').show();
        } else {
            $('#loadingGif').hide();
        }
    }
    document.addEventListener('DOMContentLoaded', function() {
        const flashMessage = document.querySelector(".flash");
        if (flashMessage) {
            toastMixin.fire({
                animation: true,
                title: flashMessage
            });
        }
    });
</script>
<script>
    
    $(document).ready(function() {
    $(".history").click(function(event) {
        event.preventDefault();
        var item_id = $(this).attr('id');
        console.log('e', item_id);
        
        $.ajax({
            url: `/history`,
            type: 'POST',
            data: { item_id: item_id },
            success: function(data) {
                console.log(data);
                var tableRows = data.map((val, index) => `
                    <tr>
                        <td>${val.id}</td>
                        <td>${val.column_name}</td>
                        <td>${val.row_id}</td>
                        <td>${val.old_value}</td>
                        <td>${val.new_value}</td>
                    </tr>
                `).join('');
                
                var table = `
                    <table class="table">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Column Name</th>
                                <th>Row Id</th>
                                <th>Old Value</th>
                                <th>New Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${tableRows}
                        </tbody>
                    </table>
                `;
                
                console.log(table);
                $(".history-modal").html(table);
                $("#history-modal-show").modal("show");
            },
            error: function(xhr, status, error) {
                console.error("Error fetching data:", error);
            }
        });
    });
});

</script>
<script>
     function displayFlashMessage(message) {
            $("#flash-container").html('<div class="flash">' + message + '</div>');
        }
     function toggleLoader(show) {
        if (show) {
            $('#loadingGif').show();
        } else {
            $('#loadingGif').hide();
        }
    }
    $(document).ready(function() {
    $('#submitBtn').click(function() {
        alert('fff')
        var formData = new FormData($("#uploadForm")[0]);
        console.log(formData);
        toggleLoader(true);
        $.ajax({
            type: 'POST',
            url: '/save_file_user', // Specify your server endpoint
            data: formData,  // Use the formData variable here
            processData: false,
            contentType: false,
            success: function(response) {
                console.log(response);
                toggleLoader(false);
                if (response) {
                        displayFlashMessage(response);
                    }
                // Handle successful response from the server
                console.log('File uploaded successfully!', response);
                window.location.reload();
            },
            error: function(xhr, status, error) {
                // Handle error response from the server
                console.error('Error uploading file:', error);
            }
        });
    });
        $('#submitBtn_checker').click(function() {
            alert('sss');
            var formData = new FormData($("#uploadForm")[0]);
            console.log(formData);
            // return false;
            // return false
            toggleLoader(true);
            $.ajax({
                type: 'POST',
                url: '/update_file_ckecker', // Specify your server endpoint
                data: formData,  // Use the formData variable here
                processData: false,
                contentType: false,
                success: function(response) {
                    toggleLoader(false);
                    // Handle successful response from the server
                    console.log('File uploaded successfully!', response);
                    window.location.reload();
                },
                error: function(xhr, status, error) {
                    // Handle error response from the server
                    console.error('Error uploading file:', error);
                }
            });
    });
        });
    </script>
{% endblock javascripts %}

