class Admins::ProjectsController < Admins::BaseController
  before_action :find_project, only: [:edit, :update, :sync_phenglei_user]

  def index
    sort_by = Project.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_on'
    sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
    search = params[:search].to_s.strip
    projects = Project.where("name like ?", "%#{search}%").order("#{sort_by} #{sort_direction}")
    @projects = paginate projects.includes(:owner, :members, :issues, :versions, :attachments, :project_score, :license)
  end

  def edit ;end

  def update
    respond_to do |format|
      if @project.update_attributes(project_update_params)
        format.html do 
          redirect_to admins_projects_path
          flash[:sucess] = "更新成功"
        end
        format.js {render_ok}
      else 
        format.html do 
          redirect_to admins_projects_path
          flash[:danger] = "更新失败"
        end
        format.js {render_js_error}
      end
    end
  end

  def destroy
    project = Project.find_by!(id: params[:id])
    ActiveRecord::Base.transaction do
      Gitea::Repository::DeleteService.new(project.owner, project.identifier).call
      project.destroy!
      # render_delete_success
      redirect_to admins_projects_path
      flash[:success] = "删除成功"
    end
    rescue Exception => e
      redirect_to admins_projects_path
      flash[:danger] = "删除失败"
  end

  def export
    sort_by = Project.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_on'
    sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
    search = params[:search].to_s.strip
    projects = Project.where("name like ?", "%#{search}%").order("#{sort_by} #{sort_direction}")
    @projects = projects.includes(:owner, :members, :issues, :versions, :attachments, :project_score, :license)
    filename = ["项目列表", Time.zone.now.strftime('%Y%m%d%H%M%S')].join('-') << '.xlsx'
    @domain_url = EduSetting.get('host_name')
    render xlsx: 'export', filename: filename
  end

  def sync_phenglei_user
    if @project.is_secret
      SyncPhengleiUserJob.perform_later(@project.id)
      redirect_to admins_phenglei_users_path
      flash[:success] = "已开启后台同步任务"
    else
      redirect_to admins_phenglei_users_path
      flash[:danger] = "非风雷协议项目"
    end
  end

  private 
  def find_project
    @project = Project.find_by_id(params[:id])
  end

  def project_update_params 
    params.require(:project).permit(:is_pinned, :recommend, :recommend_index)
  end
end