<div class="course-enrolling">
  <p class="course-enrolling-title np-card-text" style='font-size: 16px !important;'>
    Recommend this course to your peers
  </p>
  <div class="course-enrolling-content">
    <div class="course-enrolling-content-left">
      <input class="course-enrolling-content-left-input" type="text" placeholder="Enter their email address">
      <button class="course-enrolling-content-left-button" onclick="sendCourseInvitation.call(this)">
        <span class="course-enrolling-content-left-button-text">
          Submit
        </span>
      </button>
    </div>
    <span class="course-enrolling-content-or">or</span>
    <div class="course-enrolling-content-right">
      <button class="course-enrolling-content-right-button" onclick="copyURL.call(this)">
        <span class="course-enrolling-content-right-button-text">
          Copy course URL
        </span>
      </button>
    </div>
  </div>
</div>

<script>
  function copyURL() {
    navigator.clipboard.writeText(`${ window.location.origin }/c/{{ course.enrollment_code }}`);
    this.innerHTML = 'Copied!';
    setTimeout(() => {
      this.innerHTML = 'Copy course URL';
    }, 3000);
  }

  function sendCourseInvitation() {
    let buttonElement = this;
    let inputElement = buttonElement.previousElementSibling;
    let email = inputElement.value;
    if (!email || !email.includes('@') || !email.includes('.') || email.length < 5) {
      inputElement.style.border = '2px solid red';
      setTimeout(() => {
        inputElement.style.border = 'none';
      }, 2000);
    }
    else{
      const url = 'https://www.workato.com/webhooks/rest/bd1a1eb7-7e79-4208-a1db-8e9c7440bcc9/lj-hooker-enroll-someone';
      buttonElement.disable = true;
      buttonElement.style.cursor = 'auto';
      buttonElement.style.background = 'lightgrey'
      buttonElement.children[0].innerText = 'Done!'
      buttonElement.style.color = 'black'
      inputElement.readOnly = true;
      const body = {
        course_uuid: '{{ course.id }}',
        sender_email: '{{ current_person.email }}',
        email_to_invite: email
      };
      var xhr = new XMLHttpRequest();
      xhr.open("POST", url, true);
      xhr.onload = function() {
        setTimeout(() => {
          buttonElement.children[0].innerText = 'Submit'
          buttonElement.disable = false;
          inputElement.readOnly = false;
          buttonElement.style.cursor = 'pointer';
          buttonElement.style.background = 'black'
          buttonElement.style.color = 'white'
        }, 3000);
      }
      xhr.send(JSON.stringify(body));
    }
  }
</script>

<style>
  .course-enrolling {
    position: relative;
    margin-top: 30px;
  }
  .course-enrolling-content {
    display: flex;
    margin: 5px 0 15px;
    align-items: center;
  }
  .course-enrolling-content-or {
    margin: 0 10px;
    font-weight: 800;
  }
  .course-enrolling-content-left {
    display: flex;
  }
  .course-enrolling-content-left-input {
    border-radius: 2px;
    padding: 5px 10px;
    text-align: center;
    font-weight: 600;
    font-size: 1.1rem;
    height: 32px;
    margin-right: 5px;
    border: 2px solid black;
    color: black;
    background: none;
  }
  .course-enrolling-content-left-input::placeholder {
    color: black;
    opacity: 0.8;
  }
  .course-enrolling-content-left-button {
    border: none;
    padding: 5px 10px;
    font-size: 1.1rem;
    background: black;
    color: white;
    font-weight: 600;
    border-radius: 2px;
    height: 32px;
    cursor: pointer;
    display: flex;
    margin: auto;
  }
  .course-enrolling-content-right-button {
    border: none;
    border-radius: 2px;
    padding: 5px 10px;
    text-align: center;
    color: white;
    background: black;
    font-weight: 600;
    font-size: 1.1rem;
    height: 32px;
    cursor: pointer;
    display: flex;
    margin: auto;
  }
  .course-enrolling-content-right-button-text {
    align-self: center;
  }
  .course-enrolling-content-left-button-text {
    align-self: center;
    align-items: center;
  }
  .course-enrolling-title {
    font-weight: bolder;
    margin-bottom: 10px;
  }

  @media only screen and (max-width: 1250px) {
    .course-enrolling-content {
      flex-direction: column;
      align-items: start;
    }
    .course-enrolling-content-or {
      margin: 10px 10px;
    }
  }
  @media only screen and (max-width: 768px) {
    .course-enrolling-content {
      flex-direction: row;
      align-items: center;
    }
    .course-enrolling {
      margin-left: 1.25rem;
      padding-left: 1.5rem;
    }
  }

  @media only screen and (max-width: 620px) {
    .course-enrolling-content-or {
      margin: 10px 10px;
    }
    .course-enrolling-content {
      flex-direction: column;
      align-items: start;
    }
  }
  @media only screen and (max-width: 410px) {
    .course-enrolling-content-left {
      flex-direction: column;
      align-items: start;
    }
    .course-enrolling-content-left-button {
      margin-left: 0;
      margin-top: 7px;
    }
  }

</style>