<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <link rel='icon' href='/favicon.ico' type='image/x-icon'/>
    <title> <%= settings["WebsiteName"].Value %> || <%= title %></title>

    <!-- Font Awesome Icons -->
    <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/dist/css/adminlte.min.css">
    <!-- Google Font: Source Sans Pro -->
    <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
    <link rel="stylesheet" href="/plugins/datatables-bs4/css/dataTables.bootstrap4.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/colreorder/1.5.2/css/colReorder.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.1.6/css/fixedHeader.dataTables.min.css">
    <link rel="stylesheet" href="/css/portal.css">
<!-- Google Tag Manager -->

<!-- End Google Tag Manager -->
    <script>function format(time) {
            // Hours, minutes and seconds
            let hrs = ~~(time / 3600);
            let mins = ~~((time % 3600) / 60);
            let secs = ~~time % 60;

            // Output like "1:01" or "4:03:59" or "123:03:59"
            let ret = "";
            if (hrs > 0) {
                ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
            }
            ret += "" + mins + ":" + (secs < 10 ? "0" : "");
            ret += "" + secs;
            return ret;
        }

        <% let questions = {
            Projects: "What do you know about project management?",
            OD: "How to ensure that all teams are on the right track? Explain with a specific method.",
            TM: "How to enhance the performance of the members?",
            LD: "What is the capacity building program? Mention the steps to conduct a training.",
            CR: "List the benefits or values that you can offer to the sponsors/partners",
            RM: "What do you know about product cycle?",
            Events: "Mention & describe an idea for an event, And list the needed actions for implementing this idea.",
            Marketing: "Prepare an offline campaign (theme, title, objectives)",
            Social: "Prepare an online campaign (theme, title, objectives)",
            Visuals: "List the basic video editing concepts you're familiar with if any (timeline, layers, blending modes...etc.)",
            Camera: "What are the basics of photography?",
            Presentation: "Mention 5 pillars will lead person to be a good presenter?"
        } %>
        let Answers;
    </script>
    <!-- Custom CSS Block -->
    <%- block('css').toString() %>

</head>
<body class="hold-transition sidebar-mini">
<!-- Google Tag Manager (noscript) -->
<!-- End Google Tag Manager (noscript) -->
<div class="wrapper">

    <!-- Navbar -->
    <% include ../partials/navbar.ejs %>
    <!-- /.navbar -->

    <!-- Sidebar -->
    <% include ../partials/sidebar.ejs %>
    <!-- /.sidebar -->

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark"><%= title %></h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">Home</a></li>
                            <li class="breadcrumb-item active"><%= title %></li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">

                <div class="card card-primary">
                    <div class="card-header">
                        <h3 class="card-title"><%= title %></h3>
                        <!-- /.card-tools -->
                    </div>
                    <!-- /.card-header -->
                    <%
                    let label = {
                        Applied: "bg-primary",
                        Interviewed: "bg-warning",
                        Rejected: "bg-danger",
                        Accepted: "bg-success"
                    }
                    %>
                    <div class="card-body ">
                        <table id="ind" class="table table-bordered table-hover wrap" role="grid"
                               aria-describedby="example2_info">
                            <thead>
                            <tr role="row">
                                <th>#</th>
                                <th>Name</th>
                                <th>State</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Time</th>
                                <th>Preferences</th>
                                <th>Extra</th>
                            </thead>
                            <tbody>
                            <% applicants.forEach(app =>  { %>
                                <tr role="row" id="row-<% app.id %>">
                                    <td><%= app.id %></td>
                                    <td><%= app.Name %></td>
                                    <td>
                                        <div class="badge <%= label[app.State] %>"
                                             id="state-<%= app.id %>"><%= app.State %></div>
                                    </td>

                                    <td><a href="mailto:<%= app.Email %>"><%= app.Email %></a></td>
                                    <td><a href="tel:<%= app.Phone %>"><%= app.Phone %></a></td>
                                    <td><%= app.Time %></td>
                                    <td><%= app.First %>, <%= app.Second %></td>
                                    <td><span data-toggle="tooltip" data-placement="top" title="Info"><button
                                                    type="button"
                                                    class="btn btn-primary ModalButton"
                                                    data-id="<%= app.id %>"
                                                    data-toggle="modal"
                                                    data-target="#par">
                                            <i class="fa fa-info"></i>
                                        </button></span>
                                        <% if(["Admin", "President", "HR VP", "TD Team Leader", "OD Team Leader", "L&D Team Leader"].includes(user.Position?.Name) || user.isAdmin){ %>
                                            <span data-toggle="tooltip" data-placement="top" title="Actions"><button
                                                        type="button" class="btn btn-secondary EditButton"
                                                        data-toggle="modal"
                                                        data-target="#pare" data-id="<%= app.id %>">
                                            <i class="fa fa-cog"></i>
                                        </button></span>
                                            <form class="SheetSend"> <span data-toggle="tooltip" data-placement="top"
                                                                           title="Send To Sheet">
                                                <input type="hidden" name="id" value="<%= app.id %>">
                                                <button type="submit" class="btn btn-success"><i
                                                            class="fa fa-paper-plane"></i>
                                                </button> </span>
                                            </form>
                                        <% } %>
                                    </td>


                                </tr>
                            <% }); %>

                            </tbody>
                        </table>
                    </div>
                    <div class="modal fade" id="par" tabindex="-1" role="dialog"
                         aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content" id="InfoModalContent">
                            </div>
                        </div>
                    </div>
                    <div class="modal fade" id="pare" tabindex="-1" role="dialog"
                         aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content" id="EditModalContent">

                            </div>
                        </div>
                    </div>
                    <!-- /.card-body -->
                </div>


                <!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content -->
    </div>

    <!-- /.content-wrapper -->
    <!--    Footer-->
    <% include ../partials/footer.ejs %>

    <!-- Toasts -->
    <% include ../partials/toasts.ejs %>
    <!-- Custom JS Block -->
    <%- block('js').toString() %>
    <!-- AJAXSubmit -->
    <script type="text/javascript" src="/vendor/jquery.ajaxSubmit.min.js"></script>
    <script src="/plugins/datatables/jquery.dataTables.js"></script>
    <script src="/plugins/datatables/jquery.dataTables.js"></script>
    <script src="/plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>
    <script src="https://cdn.datatables.net/fixedheader/3.1.6/js/dataTables.fixedHeader.min.js"></script>
    <script src="https://cdn.datatables.net/colreorder/1.5.2/js/dataTables.colReorder.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>
    <script src="https://cdn.datatables.net/plug-ins/1.10.7/pagination/select.js"></script>

    <script type="text/javascript">
        let label = {
            Applied: "bg-primary",
            Interviewed: "bg-warning",
            Rejected: "bg-danger",
            Accepted: "bg-success",
            Overtime: "bg-dark"
        }
        $(document).on('submit', '.editApplicant', function () {
            let currentform;
            let badge;
            $.ajax({
                url: '/portal/editapplicant',
                type: 'POST',
                data: $(this).serialize(),
                tryCount: 0,
                retryLimit: 5,
                success: function (res) {
                    console.log(res.id + " " + res.State);
                    this.tryCount = 0;
                    badge = $(`#state-${res.id}`)
                    badge.text(res.State);
                    badge.removeClass('bg-primary bg-warning bg-danger bg-success');
                    badge.addClass(label[res.State]);
                    toastr.success(res.msg);
                },
                error: function (xhr, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        this.tryCount++;
                        toastr.error("Error! " + xhr.status + " " + textStatus + ", Retrying");
                        if (this.tryCount <= this.retryLimit) {
                            //try again
                            $.ajax(this);
                            return;
                        }
                        return;
                    }
                    if (xhr.status === 500) {
                        toastr.error("Error! " + xhr.status + " " + textStatus);
                    } else {
                        toastr.error("Error! " + xhr.status + " " + textStatus);
                    }
                }
            });
            return false;
        });
        $(document).on('submit', '.SheetSend', function () {
            $.ajax({
                url: '/portal/sendtosheet',
                type: 'POST',
                data: $(this).serialize(),
                tryCount: 0,
                retryLimit: 5,
                success: function (res) {
                    this.tryCount = 0;
                    toastr.success(res.msg);
                },
                error: function (xhr, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        this.tryCount++;
                        toastr.error("Error! " + xhr.status + " " + textStatus + ", Retrying");
                        if (this.tryCount <= this.retryLimit) {
                            //try again
                            $.ajax(this);
                            return;
                        }
                        return;
                    }
                    if (xhr.status === 500) {
                        toastr.error("Error! " + xhr.status + " " + textStatus);
                    } else {
                        toastr.error("Error! " + xhr.status + " " + textStatus);
                    }
                }
            });
            return false;
        });
        $(document).on('click', '.ModalButton', function () {
            let ParId = $(this).data("id");
            console.log(ParId);
            $("#InfoModalContent").load(`/portal/applicants/modal?id=${ParId}`);
        });
        $(document).on('click', '.EditButton', function () {
            let ParId = $(this).data("id");
            console.log(ParId);
            $("#EditModalContent").load(`/portal/applicants/edit?id=${ParId}`);
        });
        $('body').on('hidden.bs.modal', '.modal', function () {
            $(this).removeData('bs.modal');
        });
    </script>
    <script> $(function () {
            $("#ind").DataTable({
                stateSave: true,
                responsive: true,
                columnDefs: [
                    {responsivePriority: 1, targets: 0},
                    {responsivePriority: 2, targets: -1},
                    {responsivePriority: 3, targets: 1}
                ],
                fixedHeader: {
                    header: true,
                    footer: true
                },
                "autoWidth": false
            });
        })
        // let x = downloadData()

    </script>
</body>
</html>
