#  fq
class BidsController < ApplicationController
  #Added by young
  menu_item :respond
  menu_item :project, :only => :show_project
  #Ended by young
  before_filter :find_bid, :only => [:show, :show_project, :create, :destroy, :more, :back, :add, :new]

  helper :watchers
  def index
    # Modified by nie
    # @requirement_title = "4"
    @offset, @limit = api_offset_and_limit({:limit => 5})
    @bids = Bid.visible
    @bids = @bids.like(params[:name]) if params[:name].present?
    @bid_count = @bids.count
    @bid_pages = Paginator.new @bid_count, @limit, params['page']
    @offset ||= @bid_pages.reverse_offset
    #@bids = @bids.offset(@offset).limit(@limit).all.reverse
    unless @offset == 0
      @bids = @bids.offset(@offset).limit(@limit).all.reverse
    else
      limit = @bid_count % @limit
      @bids = @bids.offset(@offset).limit(limit).all.reverse
    end
  # @limit = api_offset_and_limit({:limit => 5})
  # @bids = Bid.visible
  # @bids = @bids.like(params[:name]) if params[:name].present?
  # @bid_count = @bids.count
  # @bid_pages = Paginator.new @bid_count, @limit, params['page']
  # @offset ||= @bid_pages.reverse_offset
  # #@bids = @bids.offset(@offset).limit(@limit).all.reverse
  # unless @offset == 0
  # @bids = @bid_count.offset(@offset).limit(@limit).all.reverse
  # else
  # limit = @bid_count%@limit
  # @bids = @bid_count.offset(@offset).limit(limit).all.reverse
  # end
  # #end
  end

  def show
    @state = false
    @user = @bid.author
    @jour = @bid.journals_for_messages
    @jour.each_with_index {|j,i| j.indice = i+1}
    #
    # @journals = @bid.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
    #@journals.each_with_index {|j,i| j.indice = i+1}
    respond_to do |format|
      format.html {
        render :layout => 'base_bids'
      }
      format.api

    end
  end

  def show_project
    # flash[:notice] = ""
    @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current))
    @option = []
    @membership.each do |membership|
      @option << membership.project
    end
    # a = [1]
    # @project = Project.where("id in []", a)
    @user = @bid.author
    @bidding_project = @bid.biding_projects
    respond_to do |format|
      format.html {
        render :layout => 'base_bids'
      }
      format.api
    end
  end

  ###添加应标项目
  def add
    project = Project.where('name = ?', params[:bid]).first
    bid_message = params[:bid_for_save][:bid_message]
    if BidingProject.where("project_id = ? and bid_id = ?", project.id, @bid.id).size == 0
      if BidingProject.cerate_bidding(@bid.id, project.id, bid_message)
        flash[:notice] = l(:label_bidding_succeed)
      end
    else
      flash[:error] = l(:label_bidding_fail)
    end
    @bidding_project = @bid.biding_projects
    respond_to do |format|
    # format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
    # format.html
      format.html { redirect_to :back }
      format.js
    #format.api { render_api_ok }
    end
  end

  ## 新建留言
  def create

    if params[:bid_message].size>0
      message = params[:bid_message][:message]
      refer_user_id = params[:bid_message][:reference_user_id].to_i
      @bid.add_jour(User.current, message, refer_user_id)
    # if a_message.size > 5
    # @message = a_message[-5, 5]
    # else
    # @message = a_message
    # end
    # @message_count = a_message.count
    end
    @jour = @bid.journals_for_messages
    @jour.each_with_index {|j,i| j.indice = i+1}
    @bid.set_commit(@jour.count)

    respond_to do |format|
      format.js
    #format.api { render_api_ok }
    end

  end

  ##删除留言
  def destroy
    JournalsForMessage.delete_message(params[:object_id])
    @jour = @bid.journals_for_messages
    @jour.each_with_index {|j,i| j.indice = i+1}
    @bid.set_commit(@jour.count)
    # if a_message.size > 5
    # @message = a_message[-5, 5]
    # else
    # @message = a_message
    # end
    # @message_count = a_message.count

    respond_to do |format|
    # format.html
      format.js
    #format.api { render_api_ok }
    end
  end

  ##引用
  def new
    @jour = JournalsForMessage.find(params[:journal_id]) if params[:journal_id]
    if @jour
    user = @jour.user
    text = @jour.notes
    else
    user = @bid.author
    text = @bid.description
    end
    # Replaces pre blocks with [...]
    text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]')
    @content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> "
    @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
    @id = user.id
  rescue ActiveRecord::RecordNotFound
    render_404
    end

  ##新建需求
  def new_bid
    if params[:bid_title]
      if params[:bid_budget].to_s =~ /^(\d+)$|^(\d+).([0-9]{2})$/
        if params[:bid_deadline].to_s =~ /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/
          bid = Bid.creat_bids(params[:bid_budget], params[:bid_deadline], params[:bid_title] , params[:bid_description])
          unless bid.watched_by?(User.current)
            if bid.add_watcher(User.current)
              flash[:notice] = l(:label_bid_succeed)
            end
          end
        else
          flash[:error] = l(:label_wrong_date)
        end
      else
        flash[:error] = l(:label_wrong_budget)
      end
    end
    @limit = 5
    @bid_count = Bid.count
    @bid_pages = Paginator.new @bid_count, @limit, params['page']
    @offset ||= @bid_pages.offset
    @bids = Bid.offset(@offset).limit(@limit).all
    respond_to do |format|
    # format.html
      format.html { redirect_to :back }
      format.js
    # format.api { render_api_ok }
    end
  end

  def more
    @jour = @bid.journals_for_messages
    @jour.each_with_index {|j,i| j.indice = i+1}
    @state = true

    respond_to do |format|
      format.html { redirect_to :back }
      format.js
    #format.api { render_api_ok }
    end
  end

  def back
    @jour = @bid.journals_for_messages
    @jour.each_with_index {|j,i| j.indice = i+1}
    @state = false

    respond_to do |format|
      format.html { redirect_to :back }
      format.js
    #format.api { render_api_ok }
    end
  end

  private

  def find_bid
    if params[:id]
      @bid = Bid.find(params[:id])
    end
  rescue
    render_404
    end

end

