module Admin::SeminarsFieldHelper

  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::FormOptionsHelper
  include ActionView::Helpers::DateHelper
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::RenderingHelper
  include ActionView::Context
  include OrbitBasis::RenderAnywhere

  include OrbitFormHelper

  def block_helper(seminar_main,index,disable = false,attribute_type=nil,signup=nil, to_require=true)
    unless self.disabled
      @index = index
      @require = to_require
      @markup_options = markup_options.merge(:disabled=>disable, :required => to_require)
      @seminar_main = seminar_main
      @attribute_value = @seminar_main.get_value_from_field_id(id,signup)
      @attribute_type = attribute_type
      @new_attribute = @attribute_value.nil?
      @attribute_value = @attribute_value || signup.seminar_signup_values.build(seminar_signup_field_id: id)
      @prefiled_value = @attribute_value.value rescue nil
      return instance_eval("render_#{markup}(#{required})") rescue ""
    end
  end

  def seminar_datetime_picker(object_name, value, format, time=false)
    content_tag :div, :class => "input-append datetimepick", "data-date-format"=>format, "data-picktime"=>"#{time}" do
      concat text_field_tag(object_name, value, :placeholder=>format)
      concat (content_tag :span, :class => 'add-on clearDate' do
                content_tag :i, nil, :class => 'icons-cross-3'
              end)
      concat (content_tag :span, :class => 'add-on iconbtn' do
                content_tag :i, nil, 'data-time-icon' => 'icons-clock', 'data-date-icon' => 'icons-calendar', :class=>"icons-calendar"
              end)
    end
  end

  def render_seminar_preferred_session(required = false)
    @prefiled_value ||=[]
    @prefiled_value = Array(@prefiled_value)
    control_group_wrapper_for_sm do
      @seminar_main.summary_chioices.map.with_index do |value,key|
        key = key.to_s
        label_tag("#{get_basic_field_name_org}_#{self.id}_#{key}",radio_button_tag(get_field_name_base, key ,  ((@prefiled_value.include?(key) || @prefiled_value.include?(key.to_i)) ? true : false), {:required=>@require,:id=>"#{get_basic_field_name_org}_#{self.id}_#{key}"})+value,@markup_options.merge(:class=>"radio inline"))
      end.join.html_safe
    end if @seminar_main.enable_summary_choice
  end

protected

  def get_basic_field_name_org
    "seminar_signup_values"
  end

  def get_basic_field_name
    "seminar_signup_field"
  end

  def get_basic_field_name_base
    "#{get_basic_field_name_org}[#{@index}]"
  end

  def can_muti_lang_input_for_sm?
    if self.markup == "address"
      return true
    else
      $seminar_list[:markups][markup]["muti_lang_input_supprt"] and !(get_data["cross_lang"] == "true")  
    end
  end
  def show_set_field_for_sm(field_sets,key_field,key_index,field,markup='text_field')
    @show_set_field_for_sm = true
    def self.can_muti_lang_input_for_sm?
      true
    end
    def self.can_add_more
      false
    end
    @markup = markup
    def self.markup
      @markup
    end
    def self.add_more
      false
    end
    @new_attribute = false
    @key_index = key_index
    def self.key
      @key_index
    end
    def self.form_label
      ''
    end
    def self.end_block
      unless @attribute_value.new_record?
        hidden_field_tag("seminar_main[#{@key_field}][#{@key_index}]"+"[id]",
          @attribute_value.id,:for=>"field_#{@key_index}", :id=>nil)
      else
        ""
      end
    end
    @key_field = key_field
    @field = field
    def self.get_basic_field_name_base
      "seminar_main[#{@key_field}][#{@key_index}][#{@field}]"
    end
    def self.get_field_name_base
      "seminar_main[#{@key_field}][#{@key_index}][#{@field}]"
    end
    @attribute_value = field_sets
    @prefiled_value = field_sets[field]
    a = control_group_wrapper_for_sm do |key,value|
      add_more_blank = ""
      if markup=='text_field'
        if key_field == 'seminar_email_sets'
          inside = text_field_tag([get_field_name_base,"[#{key}]"].join,value,:class=>'input-medium form-control')
        else 
          inside = text_field_tag([get_field_name_base,"[#{key}]"].join,value)
        end
      else
        inside = cktext_area("seminar_main[#{@key_field}][#{@key_index}][#{@field}]","#{key}",value:value)
      end
      inside
    end
    @key_field = nil
    @key_index = nil
    @field = nil
    @show_set_field_for_sm = nil
    return a.html_safe
  end
end