{{$current_value := (raw_value_of .ResourceValue .Meta)}}
{{$is_existing_record := (not (is_new_record $current_value))}}

<div class="qor-field">
  <label class="qor-field__label" for="{{.InputId}}">
    {{meta_label .Meta}}

  </label>

  <div class="qor-field__show">{{.Value}}</div>

  {{if .Meta.Config.Select2ResultTemplate}}
    <script name="select2-result-template" type="x-tmpl-mustache">
      {{.Meta.Config.Select2ResultTemplate}}
    </script>
  {{end}}

  {{if .Meta.Config.Select2SelectionTemplate}}
    <script name="select2-selection-template" type="x-tmpl-mustache">
      {{.Meta.Config.Select2SelectionTemplate}}
    </script>
  {{end}}

  {{if (eq .Meta.Config.SelectMode "bottom_sheet")}}
    <script name="select-one-selected-template" type="x-tmpl-mustache">
      <p class="qor-field__selected" data-primary-key="[[ primaryKey ]]">
        <span class="qor-selected__change">[[ &displayName ]]</span>
        <a href="#" class="qor-selected__remove"><i class="material-icons">remove_circle_outline</i></a>
      </p>
    </script>

    <script name="select-one-selected-icon" type="x-tmpl-mustache">
      <span class="qor-select__select-icon"><i class="material-icons">check_circle</i></span>
    </script>
  {{end}}

  <div class="qor-field__block qor-field__edit  qor-field__selectone" {{if (eq .Meta.Config.SelectMode "bottom_sheet")}} data-toggle="qor.selectone"{{end}}>
    {{if (eq .Meta.Config.SelectMode "bottom_sheet")}}
      {{if $is_existing_record}}
        <p class="qor-field__selected" data-primary-key="{{primary_key_of $current_value}}">
          <span class="qor-selected__change">{{.Value}}</span>
          <a href="javascripr://" class="qor-selected__remove"><i class="material-icons">remove_circle_outline</i></a>
        </p>
      {{end}}

      <a href="javascript://" {{if $is_existing_record}}style="display:none"{{end}} class="mdl-button mdl-button--colored qor-field__selectone-trigger" data-select-modal="one" {{if .InputId}}data-select-id="#{{.InputId}}"{{end}} data-selectone-url="{{url_for .Meta.Config.RemoteDataResource}}" data-select-listing-url="{{url_for .Meta.Config.RemoteDataResource}}" data-select-creating-url="{{new_resource_path .Meta.Config.RemoteDataResource}}" data-select-default-creating="{{.Meta.Config.DefaultCreating}}" {{if .Meta.Config.PrimaryField}}data-remote-data-primary-key="{{.Meta.Config.PrimaryField}}"{{end}}>{{meta_placeholder .Meta .Context "Click to Select"}}</a>

      <select id="{{.InputId}}" class="qor-field__input qor-field__selectone-input hidden" name="{{.InputName}}" {{if (not (has_change_permission .Meta)) }}disabled{{end}}>
        {{if $is_existing_record}}
          <option value="{{primary_key_of $current_value}}" selected>{{.Value}}</option>
        {{end}}
      </select>
    {{else}}
      <select id="{{.InputId}}" class="qor-field__input hidden" {{if .Meta.Config.RemoteDataResource}}{{if $is_existing_record}}chooser-selected="true"{{end}}{{end}} data-toggle="qor.chooser" data-placeholder="{{meta_placeholder .Meta .Context "Select an Option"}}" name="{{.InputName}}" {{if  (not (has_change_permission .Meta)) }}disabled{{end}} {{if .Meta.Config.AllowBlank}}data-allow-clear="true"{{end}} {{if .Meta.Config.RemoteDataResource}}data-remote-url="{{url_for .Meta.Config.RemoteDataResource}}" data-remote-data="true" {{if .Meta.Config.RemoteDataHasImage}}data-remote-image="true"{{end}} {{if .Meta.Config.PrimaryField}}data-remote-data-primary-key="{{.Meta.Config.PrimaryField}}"{{end}}{{end}}>
        {{if .Meta.Config.RemoteDataResource}}
          {{if $is_existing_record}}
            <option value="{{primary_key_of $current_value}}" selected>{{.Value}}</option>
          {{end}}
        {{else}}
          {{if .Meta.Config.AllowBlank}}
            <option></option>
          {{end}}
          {{range $values := (.Meta.Config.GetCollection .ResourceValue .Context)}}
            {{if (is_equal $current_value (index $values 0))}}
              <option value="{{index $values 0}}" selected>{{index $values 1}}</option>
            {{else}}
              <option value="{{index $values 0}}">{{index $values 1}}</option>
            {{end}}
          {{end}}
        {{end}}
      </select>
    {{end}}

    {{if has_change_permission .Meta}}<input type="hidden" name="{{.InputName}}" value="">{{end}}
  </div>
</div>
