class AppliedProjectController < ApplicationController
  helper :watchers
  #申请加入项目
  def applied_join_project
    if params[:object_id]
      @project = Project.find_by_id(params[:object_id])
    end
    # @user_id = params[:user_id]
    # if params[:project_join]
    #   if @project
    #     user = User.find @user_id
    #     if user.member_of?(@project)
    #       @status = 3
    #     else
    #       @applieds = AppliedProject.where("user_id = ? and project_id = ?", params[:user_id],params[:project_id])
    #       if  @applieds.count == 0
    #         appliedproject = AppliedProject.create(:user_id => params[:user_id], :project_id => params[:project_id])
    #         Mailer.run.applied_project(appliedproject)
    #         @status = 2
    #       else
    #         @status = 1
    #       end
    #     end
    #   else
    #     @status = 0
    #   end
    #   respond_to do |format|
    #     format.js
    #   end
    #   return
    # end
    #
    # @applieds = AppliedProject.where("user_id = ? and project_id = ?", params[:user_id],params[:project_id])
    # if  @applieds.count == 0
    #   appliedproject = AppliedProject.create(:user_id => params[:user_id], :project_id => params[:project_id])
    #   Mailer.run.applied_project(appliedproject)
    # end
    #
    # #redirect_to project_path(params[:project_id])
    # #redirect_to_referer_or {render :text => ( 'applied success.'), :layout => true}
    # respond_to do |format|
    #   format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
    #   format.js { render :partial => 'set_applied'}
    # end
  end

  # @flage:提示语标志（1：邀请码错误；2：已经是项目成员； 3：角色没有选择; 4：申请成功）
  # role:成员角色 => 0（3：管理人员；4：开发人员；5：报告人员）
  # 申请成功则发送消息
  def applied_project_info
    if params[:project_id].nil?
      @project = Project.where(:invite_code => params[:invite_code]).first
    else
      @project = Project.find(params[:project_id])
    end
    if !@project || params[:invite_code].strip.upcase.to_s != @project.invite_code
      @flag = 1
    elsif User.current.member_of?(@project)
      @flag = 2
    elsif params[:member].nil?
      @flag = 3
    elsif !AppliedProject.where(:project_id => @project.id, :user_id => User.current.id).first.nil?
      @flag = 5
    elsif params[:member] == "member_reporter" && params[:invite_code].upcase.to_s == @project.invite_code
      # 项目报告人员直接加入项目
      @flag = 6
      members = []
      user_grades = []
      project_info = []
      members << Member.new(:role_ids => ["5"], :user_id => User.current.id)
      user_grades << UserGrade.new(:user_id => User.current.id, :project_id => @project.id)
      role = Role.find(5)
      project_info << ProjectInfo.new(:project_id => @project.id, :user_id => User.current.id) if role.allowed_to?(:is_manager)
      @project.members << members
      @project.project_infos << project_info
      @project.user_grades << user_grades unless user_grades.first.user_id.nil?

      ps = ProjectsService.new
      ps.send_wechat_join_project_notice User.current,@project,5,0
    else
      @flag = 4
      role = params[:member] == "member_manager" ? 3 : (params[:member] == "member_developer" ? 4 :5)
      applied_project = AppliedProject.create(:user_id => User.current.id, :project_id => @project.id, :role => role)
      # 申请成功则给项目管理员发送邮件及发送消息
      # Mailer.run.applied_project(applied_project)
    end
  end

  #取消申请
  def unapplied_join_project
    @project = Project.find(params[:project_id])
    #@applied = AppliedProject.find(params[:id])
    #@applied.destroy

    AppliedProject.deleteappiled(params[:user_id], params[:project_id])

    respond_to do |format|
      format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
      format.js { render :partial => 'set_applied' }
    end
  end

end
