<%
  is_persisted = Ecto.get_meta(@changeset.data, :state) == :loaded
%>

<%= form_for @changeset, @action, [multipart: true, csrf_token: @csrf_token, id: "Imagine-Properties-Form", class: "ui form" <> (if @changeset.action, do: " warning error", else: "")], fn f -> %>
  <input type="hidden" name="return_to" value="<%= assigns[:return_to] || "/#{@cms_page.path}" %>">

  <%= if @changeset.action do %>
    <div class="ui warning message">
      <p>Something went wrong, please check the errors below.</p>
    </div>
  <% end %>

  <div class="field">
    <%= label f, :title %>
    <%= text_input f, :title %>
    <%= error_tag f, :title %>
  </div>

  <div class="flex fields">
    <div class="field">
      <%= label f, :name %>
      <%= text_input f, :name, disabled: CmsPage.is_home_page?(@cms_page) %>
      <%= error_tag f, :name %>
    </div>

    <div class="field">
      <%= label f, :cms_template_id, "Template" %>
      <%= select f, :cms_template_id, cms_template_select_options(@cms_templates), class: "ui dropdown" %>
      <%= error_tag f, :cms_template_id %>
    </div>

    <%= if is_persisted do %>
    <div class="field">
      <%= label f, :published_version %>
      <%= select f, :published_version, cms_page_version_select_options(@cms_page.versions), class: "ui dropdown" %>
      <%= error_tag f, :published_version %>
    </div>
    <% end %>
  </div>

  <%= unless CmsPage.is_home_page?(@cms_page) do %>
  <div class="field">
    <%= label f, :parent_id %>
    <%= select f, :parent_id, cms_page_select_options(@cms_pages -- [@cms_page], @changeset.data), prompt: "– Select –", class: "ui search dropdown" %>
    <%= error_tag f, :parent_id %>
  </div>
  <% end %>

  <div class="fields">
    <div class="inline field">
      <div class="ui toggle checkbox">
        <%= checkbox f, :redirect_enabled %>
        <%= label f, :redirect_enabled %>
        <%= error_tag f, :redirect_enabled %>
      </div>
      <%= text_input f, :redirect_to, placeholder: "URL" %>
      <%= error_tag f, :redirect_to %>
    </div>
  </div>

  <div class="fields">
    <div class="field">
      <%= label f, :published_date %>
      <%= date_input f, :published_date, value: if(@cms_page.published_date, do: Timex.to_date(@cms_page.published_date)) %>
      <%= error_tag f, :published_date %>
    </div>

    <div class="field">
      <%= label f, :article_date %>
      <%= date_input f, :article_date, value: if(@cms_page.article_date, do: Timex.to_date(@cms_page.article_date)) %>
      <%= error_tag f, :article_date %>
    </div>

    <div class="field">
      <%= label f, :article_end_date %>
      <%= date_input f, :article_end_date, value: if(@cms_page.article_end_date, do: Timex.to_date(@cms_page.article_end_date)) %>
      <%= error_tag f, :article_end_date %>
    </div>

    <div class="field">
      <%= label f, :expiration_date %>
      <%= date_input f, :expiration_date, value: if(@cms_page.expiration_date, do: Timex.to_date(@cms_page.expiration_date)) %>
      <%= error_tag f, :expiration_date %>
    </div>
  </div>

  <div class="inline field">
    <div class="ui toggle checkbox">
      <%= checkbox f, :expires %>
      <%= label f, :expires %>
      <%= error_tag f, :expires %>
    </div>
  </div>

  <div class="flex fields">
    <div class="field">
      <%= label f, :thumbnail_path, "Thumbnail" %>
      <div style="height: 100px;">
        <%= if @changeset.data.thumbnail_path do %>
          <img src="<%= @changeset.data.thumbnail_path %>" style="max-width: 100%; max-height: 100px;">
        <% else %>
          [none]
        <% end %>
      </div>
      <%= file_input f, :thumbnail_file %>
      <%= text_input f, :thumbnail_path %>
      <%= error_tag f, :thumbnail_path %>
    </div>

    <div class="field">
      <%= label f, :feature_image_path, "Feature Image" %>
      <div style="height: 100px;">
        <%= if @changeset.data.feature_image_path do %>
          <img src="<%= @changeset.data.feature_image_path %>" style="max-width: 100%; max-height: 100px;">
        <% else %>
          [none]
        <% end %>
      </div>
      <%= file_input f, :feature_image_file %>
      <%= text_input f, :feature_image_path %>
      <%= error_tag f, :feature_image_path %>
    </div>
  </div>

  <div class="fields">
    <div class="field">
      <%= label f, :position %>
      <%= number_input f, :position %>
      <%= error_tag f, :position %>
    </div>
  </div>

  <div class="field">
    <%= label f, :summary %>
    <%= textarea f, :summary %>
    <%= error_tag f, :summary %>
  </div>

  <div class="field">
    <%= label f, :html_head, "HTML Head (Advanced)" %>
    <%= textarea f, :html_head %>
    <%= error_tag f, :html_head %>
  </div>
<% end %>
