class LiveBroad < ActiveRecord::Base
    has_paper_trail
    self.table_name = "live_broads"
    validates :room_id,:title,:begin_time,:end_time, presence: true
    attr_accessor :v_cover,:rank_str
    before_save :before_save
    has_and_belongs_to_many :depart_record
    serialize :rank_str, Array
    RANK_ENUM= [["会员",0], ["群主",1], ["店长", 2], ["区代", 3]]
    RANK_HASH=["会员","群主","店长","区代"]

    def before_save
      #自动取消其他置顶项
      if self.show
        live_broads = LiveBroad.find_by_sql("select * from live_broads ")
        # 创建商品图片
        live_broads.each do |l|
          if l.id != self.id
            l.show=false
            l.save
          end
        end
      end
      str=""
      i=0
      if !self.rank_str.blank? && self.rank_str.length>0
        self.rank_str.each do |item|
          if item.length<=0
            next
          end
          if i==0
            str=item.to_s
          else
            str=str+","+item.to_s
          end
          i=i+1
        end
      end
        # self.rank_strs=self.rank_str.map { |i| "" + i.to_s + "" }.join(",")
        self.rank_strs=str
    end

    def rank_name_str
      arr=self.rank_strs.split(",")
      str=""
      i=0
      if self.rank_strs.length>0
        arr.each do |item|
          if item.length<=0
            next
          end
          if !RANK_HASH[item.to_i].blank?
            if i==0
              str=RANK_HASH[item.to_i]
            else
              str=str+","+RANK_HASH[item.to_i]
            end
          end

          i=i+1
        end
      else
        str="全部"
      end

      return str

    end

    IMG_STORE_PATH = "live_broads"

    rails_admin do
        navigation_label '直播管理'
        weight -100
        list do 
          filters [:id,:title]
          field :id      
          field :title
          field :room_id
          field :cover do
            formatted_value do
              bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
              :style => '`width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
              :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
            end
          end
          field :begin_time
          field :end_time
          field :depart_record
          field :show
          field :recommend
          field :rank_name_str
          field :remark
          field :created_at

        end

        show do
          field :id
          field :title
          field :room_id
          field :cover do
            formatted_value do
              bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
              :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
            end
          end
          field :begin_time
          field :end_time
          field :depart_record
          field :show
          field :recommend
          field :rank_name_str
          field :remark
          field :created_at
          field :updated_at
        end

        edit do
          field :title
          field :room_id
          field :v_cover, :file_upload do
            pretty_value do
              bindings[:view].tag(:img, {:src => bindings[:object].get_cover_img, :class => 'preview'})
            end
          end
          field :begin_time
          field :end_time
          field :depart_record
          field :show
          field :recommend

          field :rank_str do
            render do
              bindings[:form].select( "rank_str",RANK_ENUM, {}, { :multiple => true })
            end
          end
          field :remark
        end
    end

    def get_cover_img
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.cover}"
      return url
    end

    def v_cover=file
      unless file.blank?
        clear_cover_img
        file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
        file_path = "#{IMG_STORE_PATH}/#{file_name}"
        Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
        self.cover = file_path
        self.save
      end
    end

    def clear_cover_img
      file_path = "#{self.cover}"
      Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
    end

    def after_destroy
      clear_cover_img
    end
end