class Organizations::ProjectsController < Organizations::BaseController
  before_action :load_organization

  def index
    public_projects_sql = @organization.projects.secret_and_visible.to_sql
    private_projects_sql = @organization.projects
                               .where(is_public: false)
                               .joins(team_projects: {team: :team_users})
                               .where(team_users: {user_id: current_user.id}).to_sql

    if params[:is_public].present? && params[:is_public].to_s == "public"
      @projects = Project.from("( #{ public_projects_sql}) AS projects")
      @projects = @projects.secret_and_visible
    else
      @projects = Project.from("( #{ public_projects_sql} UNION #{ private_projects_sql } ) AS projects")
    end
    keywords = params[:search].to_s.each_char.select { |c| c.bytes.first < 240 }.join('')
    @projects = @projects.where(id: params[:pm_project_repository_ids].split(',')) if params[:pm_project_repository_ids].present?
    @projects = @projects.where.not(id: params[:exclude_ids].to_s.split(",")) if params[:exclude_ids].present?
    @projects = @projects.where(project_type: ['mirror', 'common']).where("gpid is not null") if params[:actived].present?
    @projects = @projects.ransack(name_or_identifier_cont: keywords).result if params[:search].present?
    @projects = @projects.includes(:owner)
    if params[:choosed].present? && params[:choosed].is_a?(Array)
      @projects = @projects.order("FIELD(projects.id, #{params[:choosed].reverse.join(",")}) desc")
    else
      @projects = @projects.order("projects.#{sort} #{sort_direction}")
    end
    @projects = paginate(@projects)
  end

  def search
    tip_exception("请输入搜索关键词") if params[:search].nil?
    # 表情处理
    keywords = params[:search].to_s.each_char.select { |c| c.bytes.first < 240 }.join('')
    public_projects_sql = @organization.projects.where(is_public: true).to_sql
    private_projects_sql = @organization.projects
                               .where(is_public: false)
                               .joins(team_projects: {team: :team_users})
                               .where(team_users: {user_id: current_user.id}).to_sql
    @projects = Project.from("( #{ public_projects_sql} UNION #{ private_projects_sql } ) AS projects")

    @projects = @projects.ransack(name_or_identifier_cont: keywords).result
    @projects = @projects.includes(:owner).order("projects.#{sort} #{sort_direction}")
  end

  private

  def load_organization
    @organization = Organization.find_by(login: params[:organization_id]) || Organization.find_by(id: params[:organization_id])
    return render_not_found("组织不存在") if @organization.nil?
    return render_forbidden("没有查看组织的权限") if org_limited_condition || org_privacy_condition
  end

  def sort
    Project.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'updated_on'
  end

  def sort_direction
    %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
  end
end