<div class="right_col mt-sm-3" role="main">
  <div class="row main_row max-w-1440">
    <a class="back-link" href="/policies"><span class="fa fa-chevron-left mr-2"></span>Back to policies</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="updatePolicy" action="/policies/update" method="post" class="form-horizontal form-label-left"
          novalidate>
          <input type="hidden" name="policy_id" value="<%=data.id %>">
          <div class="row">
            <div class="col-md-12">
              <div class="row">
                <div class="col-sm-12">
                  <div class="form-group">
                    <label class="col-form-label label-align" for="title">Title<span class="required">*</span>
                    </label>
                    <div class="input-group">
                      <input type="text" name="title" class="form-control" placeholder="Enter Title.."  id="title" value="<%=data.title %>" maxlength="150">
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="form-group drop-file">
                <label class="col-form-label label-align" for="policy_attachment">Featured
                  Attachment<span class="required">*</span>
                </label>
                <input type="file" name="policy_attachment" id="policy_attachment" class="form-control dropify h-auto" <% if(data.attachment !='' && fs.existsSync("assets/Policies/"+data.attachment)) { %> data-default-file="/Policies/<%=data.attachment%>" <% }%> data-allowed-file-extensions="pdf" data-max-file-size-preview="5M">
              </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="updatePolicyBtn" class="btn theme-btn form-button">Update</button>
          </div>

        </form>
      </div>
    </div>
  </div>
</div>

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

<script type="text/javascript">
  $("#updatePolicy").submit(function (e) {
    e.preventDefault();
    $(document).find("span.text-danger").remove();
    var form = $('#updatePolicy');
    var formData = new FormData(form[0]);

    let is_image_removed = $("#is_image_removed").val();
    if(is_image_removed === "1"){
      formData.append('policy_attachment', this.new_attachments)
    }
    
    var url = $('#updatePolicy').attr('action');
    $("#updatePolicyBtn").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) {
          $("#policy_attachment").parents().find('.dropify-wrapper').css('border','2px dashed #201b53');

          if(error.param !== 'policy_attachment'){
          $(document).find('[id=' + error.param + ']').after('<span class="help-block text-danger">' + error.msg + '</span>');
          }else{
            $("#policy_attachment").parents().find('.dropify-wrapper').css('border','2px dashed red');
          }
        });
      },
      complete: function () {
        $("#updatePolicyBtn").prop('disabled', false);
      },
    })
  });
</script>