class SchoolController < ApplicationController
  layout 'course_base'
  before_filter :require_admin, :only => :upload_logo
  
  def upload
    uploaded_io = params[:logo]
    school_id ||= params[:id]
    unless uploaded_io.nil? 
      File.open(Rails.root.join('public', 'images', 'school', school_id.to_s+'.png'), 'wb') do |file|
        file.write(uploaded_io.read)
      end
      s1 = School.find(school_id)
      s1.logo_link  = '/images/school/'+school_id.to_s+'.png'
      s1.save
    end
    redirect_to admin_schools_url(:school_name => params[:school_name])
  end  

  def upload_logo
    @school = School.find params[:id]
    @school_name = params[:school_name]
  end

  #获取制定学校开设的课程数
  def course_count school_id
     School.find(school_id).courses.count
  end
  
  
  def index
    
  end

  def get_province
    @provinces = School.find_by_sql("select distinct province from schools")

    options = ""

    @provinces.each do |p|
      options << "<option value = '#{p.province}' >#{p.province}</option>"
    end

    # 取id取学校名
    # 连接子表: 查询已添加用户的学校
    school = School.select("id, name").
              joins("RIGHT JOIN (
                      SELECT DISTINCT school_id 
                      FROM #{UserExtensions.table_name} 
                      WHERE school_id IS NOT NULL) AS sids ON schools.id = sids.school_id").
              where("#{School.table_name}.id IS NOT NULL")

    options_s = ""
    school.each do |s|
      options_s << "<li style = 'width: 33%; float: left'><a style='cursor: pointer;' id=#{s.id} onclick='test(this.id, this.text)'>#{s.name}</a></li>"
    end

    res = Hash.new
    res[:text] = options
    res[:text_s] = options_s

    render :json => res


  end
  
  def get_options
    @school = School.where("province = ?", params[:province])
    p = params[:province]
    ##@school = School.all
    options = ""
    
    @school.each do |s|
      #options << "<option value=#{s.id}>#{s.name}</option>"
      options << "<li style = 'width: 33%; float: left'><a style='cursor: pointer;' id=#{s.id} onclick='test(this.id, this.text)'>#{s.name}</a></li>"
    end
    
    
    render :text => options
    
  end

  def get_schoollist
     @school = School.where("province = ?", params[:province])
     
     options = ""
     @school.each do |s|
       options << "<li style = 'width: 33%; float: left'><a style='cursor: pointer;' id=#{s.id} onclick='test(this.id)'>#{s.name}</a></li>"
     end
     
     render :text => options
  end
  
  def search_school
    q = "%#{params[:key_word].strip}%"
    
    @school = School.where("name LIKE ?", q)
    @school = @school.where("province = ?", params[:province]) if (params[:province] != '0' )

    options = ""
    @school.each do |s|
      options << "<li style = 'width: 33%; float: left'> <a style='cursor: pointer;' id=#{s.id} onclick='test(this.id)'>#{s.name}</a></li>"
    end

    options = "<div class='flash error' id='flash_error'>#{l(:label_school_not_fount)}</div>" if options.blank?

    render :text => options    
  end
end
