{% load static %}
<!DOCTYPE html>
<html lang="en"> <!--begin::Head-->

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Upload</title><!--begin::Primary Meta Tags-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="title" content="CQT | Upload">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource/source-sans-3@5.0.12/index.css"
        integrity="sha256-tXJfXfp6Ewt1ilPzLDtQnJV4hclT9XuaZUKyUvmyr+Q=" crossorigin="anonymous">
    <!--end::Fonts--><!--begin::Third Party Plugin(OverlayScrollbars)-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/styles/overlayscrollbars.min.css"
        integrity="sha256-dSokZseQNT08wYEWiz5iLI8QPlKxG+TswNRD8k35cpg=" crossorigin="anonymous">
    <!--end::Third Party Plugin(OverlayScrollbars)--><!--begin::Third Party Plugin(Bootstrap Icons)-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.min.css"
        integrity="sha256-Qsx5lrStHZyR9REqhUF8iQt73X06c8LGIUPzpOhwRrI=" crossorigin="anonymous">
    <!--end::Third Party Plugin(Bootstrap Icons)--><!--begin::Required Plugin(AdminLTE)-->
    <link rel="stylesheet" href="{% static './css/adminlte.css' %}"><!--end::Required Plugin(AdminLTE)--><!-- apexcharts -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/apexcharts@3.37.1/dist/apexcharts.css"
        integrity="sha256-4MX+61mt9NVvvuPjUWdUdyfZfxSB1/Rf9WtqRHgG5S0=" crossorigin="anonymous">
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="{% static './css/select2-bootstrap4.min.css' %}">
    <link rel="stylesheet" href="{% static './css/custom.css' %}">
    <style>
        .select2-container .select2-search--inline .select2-search__field {
            position: absolute;
            top: 3px;
            font-size: 14px;
        }
        #full-page-loader {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.6); /* semi-transparent black */
            z-index: 9999;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .loader-overlay .spinner-border {
            width: 3rem;
            height: 3rem;
        }

    </style>
</head>

<body class="layout-fixed sidebar-expand-lg sidebar-mini app-loaded sidebar-collapse">
    <!--begin::App Wrapper-->
    <div class="app-wrapper"> <!--begin::Header-->
        {% include 'header.html' %}
        {% include 'sidebar.html' %}
        <main class="app-main"> <!--begin::App Content Header-->
            <div class="app-content-header"> <!--begin::Container-->
                <div class="container-fluid"> <!--begin::Row-->
                    <div class="row">
                        <div class="col-sm-6">
                            <h3 class="mb-0">📂 File Upload</h3>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-end">
                                <li class="breadcrumb-item"><a href="{% url 'file-upload' %}">Home</a></li>
                                <li class="breadcrumb-item active" aria-current="page"><a href="{% url 'content-scorecard' %}"></a>
                                   📂 File Upload</a>
                                </li>
                            </ol>
                        </div>
                    </div> <!--end::Row-->
                </div> <!--end::Container-->
            </div>
            <div class="app-content"> <!--begin::Container-->
                <div class="container-fluid"> <!-- Info boxes -->
                    <div id="full-page-loader" style="display: none;">
                    <div class="loader-overlay">
                        <div class="spinner-border text-light" role="status">
                            <!-- <span class="sr-only">Loading...</span> -->
                        </div>
                    </div>
                </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card mb-4">
                                <div class="card-header">
                                    <h5 class="card-title">Upload</h5>
                                    <!-- <a href="#" class="btn btn-sm btn-primary float-sm-end me-2">Download Template</a> -->
                                </div> <!-- /.card-header -->
                                <div class="card-body p-0">
                                    <!-- {% if success %}
                                        <div class="alert alert-success mt-3">
                                            File uploaded successfully! {{ file_url }}View File</a>
                                        </div>
                                    {% endif %} -->

                                    <form id="uploadForm" method="POST"  enctype="multipart/form-data"> <!--begin::Body-->
                                        {% csrf_token %}
                                        <div class="card-body">
                                            
                                            <div class="form-group row mb-3">
                                                <label for="file" class="col-sm-2 col-form-label">Select Input
                                                    File <span class="text-danger">*</span></label>
                                                <div class="col-sm-6">
                                                    <div class="input-group mb-3"> 
                                                        <input type="file" class="form-control"
                                                            id="fileInput" name="file" required
                                                            >
<!-- onchange="enableSubmitButton()" -->
                                                        <!-- <input type="file"
                                                            onchange="$(this).parents('.form-group').next().removeClass('d-none')"
                                                            class="form-control" id="file" required> 
                                                        </div> -->
                                                </div>
                                            </div>

                                            <div class="form-group row mb-3 ">
                                                <label for="file" class="col-sm-2 col-form-label"></label>
                                                <div class="col-sm-6">
                                                    <button type="submit" class="btn btn-primary" id="submitBtn" disabled>Upload</button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    
                                    <div id="responseMessage" class="mt-3"></div>

                                </div>
                            </div> <!-- /.card -->
                        </div> <!-- /.col -->
                    </div> <!--end::Row--> <!--begin::Row-->
                    
                </div> <!--end::Container-->
            </div> <!--end::App Content-->
        </main> <!--end::App Main--> <!--begin::Footer-->
        {% include 'footer.html' %}

    </div> 
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/browser/overlayscrollbars.browser.es6.min.js"
        integrity="sha256-H2VM7BKda+v2Z4+DRy69uknwxjyDRhszjXFhsL4gD3w=" crossorigin="anonymous"></script>
    <!--end::Third Party Plugin(OverlayScrollbars)--><!--begin::Required Plugin(popperjs for Bootstrap 5)-->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
        integrity="sha256-whL0tQWoY1Ku1iskqPFvmZ+CHsvmRWx/PIoEvIeWh4I=" crossorigin="anonymous"></script>
    <!--end::Required Plugin(popperjs for Bootstrap 5)--><!--begin::Required Plugin(Bootstrap 5)-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
        integrity="sha256-YMa+wAM6QkVyz999odX7lPRxkoYAan8suedu4k2Zur8=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <!--end::Required Plugin(Bootstrap 5)--><!--begin::Required Plugin(AdminLTE)-->
    <script src="{% static './js/adminlte.js' %}"></script>
    <!--end::Required Plugin(AdminLTE)--><!--begin::OverlayScrollbars Configure-->
    <script>
        const SELECTOR_SIDEBAR_WRAPPER = ".sidebar-wrapper";
        const Default = {
            scrollbarTheme: "os-theme-light",
            scrollbarAutoHide: "leave",
            scrollbarClickScroll: true,
        };
        document.addEventListener("DOMContentLoaded", function () {
            const sidebarWrapper = document.querySelector(SELECTOR_SIDEBAR_WRAPPER);
            if (
                sidebarWrapper &&
                typeof OverlayScrollbarsGlobal?.OverlayScrollbars !== "undefined"
            ) {
                OverlayScrollbarsGlobal.OverlayScrollbars(sidebarWrapper, {
                    scrollbars: {
                        theme: Default.scrollbarTheme,
                        autoHide: Default.scrollbarAutoHide,
                        clickScroll: Default.scrollbarClickScroll,
                    },
                });
            }
        });
        $(document).ready(function () {
            $('.select2').select2({
                theme: 'bootstrap4',
                placeholder: 'Select Competitors'
            });
        });
    </script> <!--end::OverlayScrollbars Configure--> <!-- OPTIONAL SCRIPTS --> <!-- apexcharts -->
    <script src="https://cdn.jsdelivr.net/npm/apexcharts@3.37.1/dist/apexcharts.min.js"
        integrity="sha256-+vh8GkaU7C9/wbSLIcwq82tQ2wTf44aOHA8HlBMwRI8=" crossorigin="anonymous"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
        const form = document.getElementById('uploadForm');
        const fileInput = document.getElementById('fileInput');
        const submitBtn = document.getElementById('submitBtn');
        const responseDiv = document.getElementById('responseMessage');

        // Enable submit button when file is selected
        fileInput.addEventListener('change', function () {
            submitBtn.disabled = fileInput.files.length === 0;
        });

        // Handle form submission
        form.addEventListener('submit', function (e) {
            e.preventDefault();
            $('#full-page-loader').show();
            // Disable button during upload
            submitBtn.disabled = true;
            submitBtn.textContent = 'Uploading...';

            const formData = new FormData(form);

            fetch('/core/api/upload-rules/', {
                method: 'POST',
                body: formData,
                headers: {
                    'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    responseDiv.innerHTML = `<div class="alert alert-success">✅ ${data.message}</div>`;
                    fileInput.value = ''; // Clear file input
                    submitBtn.disabled = true; // Keep disabled until new file selected
                    $('#full-page-loader').hide();
                } else {
                    responseDiv.innerHTML = `<div class="alert alert-danger">❌ ${data.error}</div>`;
                    submitBtn.disabled = false;
                    $('#full-page-loader').hide();
                }
            })
            .catch(error => {
                responseDiv.innerHTML = `<div class="alert alert-danger">❌ Upload failed: ${error}</div>`;
                submitBtn.disabled = false;
                $('#full-page-loader').hide();
            })
            .finally(() => {
                submitBtn.textContent = 'Upload';
                $('#full-page-loader').hide();

                // Remove message after 5 seconds
                setTimeout(() => {
                    responseDiv.innerHTML = '';
                    window.location.href = "/content-scorecard";
                }, 3000);
            });
        });
    });
</script>

</body><!--end::Body-->

</html>