<div class="right_col mt-sm-3" role="main">
  <div class="row main_row max-w-1440">
    <a class="back-link" href="/topics"><span class="fa fa-chevron-left mr-2"></span>Back to main topics</a>
    <div class="x_panel form_panel mt-3">
      <div class="x_title">
        <h2 class="title-text mb-0">Edit Topic</h2>
      </div>
      <div class="x_content">
        <form id="updateTopic" action="/topics/update" method="post" class="form-horizontal form-label-left"
          novalidate>
          <input type="hidden" name="topic_id" value="<%=data.id %>">
          <div class="row">
            <div class="col-md-12">
              <div class="row">
                <div class="col-sm-6">
                  <div class="form-group">
                    <label class="col-form-label label-align" for="name">Topic Name<span class="required">*</span></label>
                    <div class="input-group">
                      <!-- <span class="input-group-text"><img src="/images/categories.svg" class="mCS_img_loaded"></span> -->
                      <input type="text" name="name" class="form-control" placeholder="Enter Topic Name.."
                        required="required" id="name" value="<%=data.name %>" maxlength="150">
                    </div>
                  </div>
                </div>
                <div class="col-sm-6">
                  <div class="form-group">
                    <label class="col-form-label label-align" for="status">Status<span class="required">*</span></label>
                    <div class="input-group flex-nowrap">
                      <!-- <span class="input-group-text"><img src="/images/status.svg" class="mCS_img_loaded"></span> -->
                      <select class="form-control" name="status" id="status" tabindex="-1" single>
                        <option value="1" <%=data.status=='1' ? 'selected' : '' %>>Active</option>
                        <option value="0" <%=data.status=='0' ? 'selected' : '' %>>Deactive</option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="col-sm-12">
                  <div class="form-group">
                    <label class="col-form-label label-align" for="note">Note</span></label>
                    <textarea name="note" rows="5" class="form-control" placeholder="Enter Note.." id="note"><%=data.note %></textarea>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="form-group">
                <label class="col-form-label label-align" for="topic_image">Featured Image</label>
                <div class="drop-file">
                  <input type="file" name="topic_image" id="topic_image" class="form-control dropify h-auto" <% if
                  (data.topic_image !='' && fs.existsSync("assets/TopicImage/"+data.topic_image)) { %>
                data-default-file="/TopicImage/<%=data.topic_image%>" <% }%>
                    data-allowed-file-extensions="jpg png jpeg" data-max-file-size-preview="5M"
                    <% if (data.topic_image !='' && fs.existsSync("assets/TopicImage/"+data.topic_image)) { %>
                      value=" <%=data.topic_image %>" <% } else { %> value="" <% } %> >
                </div>
              </div>
              <input type="hidden" name="is_remove" id="is_image_removed" value="0">
            </div>
          </div>

          <div class="ln_solid"></div>
          <div class="text-right">
            <button type="submit" id="updateTopicBtn" class="btn theme-btn form-button">Update</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<%- include('./form_script'); %>

<script type="text/javascript">
  $("#updateTopic").submit(function (e) {
    e.preventDefault();
    $(document).find("span.text-danger").remove();
    var form = $('#updateTopic');
    var formData = new FormData(form[0]);
    formData.append('topic_image', this.new_attachments)
    var url = $('#updateTopic').attr('action');
    $("#updateTopicBtn").prop('disabled', true);
    $.ajax({
      type: "POST",
      url: url,
      dataType: "json",
      data: formData,
      processData: false,
      contentType: false,
      success: function (response) {
        if (response.success == true) {
          window.location = response.redirectUrl;
        }
      },
      error: function (response) {
        let errorMessages = '';
        $.each(response.responseJSON.errors, function (field_name, error) {
          $(document).find('[id=' + error.param + ']').after('<span class="help-block text-danger">' + error.msg + '</span>');
        })
      },
      complete: function () {
        $("#updateTopicBtn").prop('disabled', false);
      },
    })
  });
</script>