% extends "templates/base.html" %}
{% block title %}Join a Course{% endblock %}

{% block head_include %}
<meta name="description" content="Join a Course"/>
<meta name="keywords" content="" />
{% endblock %}

{% block content %}
{% if frappe.session.user == "Guest" %}

<div class="page-card">
  <div class='page-card-head'>
    <span class='indicator blue password-box'>{{ _("Login Required") }}</span>
  </div>
  <div class=''>{{ _("Please log in to confirm joining the course" )}} {{ batch.course_title }}.</div>
  <a type="submit" id="login" class="btn btn-primary w-100"
    href="/login?redirect-to=/courses/{{ batch.course }}/join?batch={{ batch.name }}">{{_("Login")}}</a>
</div>

{% elif already_a_member %}

<div class="page-card">
  <div class='page-card-head'>
    <span class='indicator blue password-box'>{{ _("Already a member") }}</span>
  </div>
  <div class=''>{{ _("You are already a member of the batch") }} {{ batch.title }} {{ _("for the course") }} {{ batch.course_title }}.
  </div>
  <a type="submit" id="batch-home" class="btn btn-primary w-100" href="">{{_("Go to Batch Home")}}</a>
</div>

{% else %}

<div class="page-card">
  <div class='page-card-head'>
    <span class='indicator blue password-box'>{{ _("Confirm your membership") }}</span>
  </div>
  <div>{{ _("Please provide your confirmation to be a part of the batch") }} {{ batch.title }} {{ _("for the course") }}
    {{ batch.course_title }}.
  </div>
  <a type="submit" id="confirm" class="btn btn-primary w-100">{{_("Confirm")}}</a>
</div>

{% endif %}
{% endblock %}

{% block script %}
<script>
frappe.ready(() => {
    $("#confirm").click((e) => {
        frappe.call({
            "method": "lms.lms.doctype.lms_batch_membership.lms_batch_membership.create_membership",
            "args": {
                "batch": {{ batch.name }},
                "course": {{ batch.course }}
            },
            "callback": (data) => {
            if (data.message == "OK") {
                frappe.msgprint({
                    message: __("You are now a member of this batch!"),
                    clear: true
                });
                setTimeout(function () {
                    window.location.href = "/courses/{{ batch.course }}/home";
                }, 1000);
            }
            }
        });
    });
});
</script>
{% endblock %}
