# encoding: utf-8
class SyllabusesController < ApplicationController
  include ApplicationHelper
  helper :attachments
  include AttachmentsHelper
  include CoursesHelper

  before_filter :is_logged, :only => [:index, :show, :edit, :new, :update, :destroy, :delete_syllabus]
  before_filter :find_syllabus, :only => [:show, :edit, :update, :destroy, :syllabus_courselist, :edit_syllabus_eng_name, :edit_syllabus_title, :update_base_info, :delete_syllabus, :delete_des, :members]
  def index
    user = User.current
    @syllabuses = user.syllabuses
  end

  def show
    respond_to do |format|
      format.js
      format.html{render :layout => 'base_syllabus'}
      format.api
    end
  end

  def new
    @syllabus = Syllabus.new
    render :layout => 'new_base'
  end

  def create
    if User.current.user_extensions.identity
      @syllabus = Syllabus.new
      @syllabus.title = params[:title]
      @syllabus.eng_name = params[:eng_name]
      @syllabus.user_id = User.current.id
      @syllabus.description = Message.where("id = 19412").first.nil? ? '' : Message.where("id = 19412").first.content
      if @syllabus && @syllabus.save
        member =  SyllabusMember.create(:user_id => @syllabus.user_id, :rank => 1)
        @syllabus.syllabus_members << member
        respond_to do |format|
          flash[:notice] = l(:notice_successful_create)
          format.html {redirect_to syllabus_path(@syllabus)}
          format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'syllabuses', :action => 'show', :id => @syllabus.id) }
        end
      else
        respond_to do |format|
          flash[:notice] = l(:notice_create_failed)
          format.html { redirect_to new_syllabus_path } #Added by young
          format.api { render_validation_errors(@syllabus) }
        end
      end
    end
  end

  def edit
    respond_to do |format|
      format.html{render :layout => 'base_syllabus'}
    end
  end

  def delete_syllabus
    respond_to do |format|
      format.js
    end
  end

  def update
    @syllabus.description = params[:syllabus][:description]
    @syllabus.des_status = 1
    @syllabus.save_attachments(params[:attachments])
    if @syllabus.save
      if params[:asset_id]
        ids = params[:asset_id].split(',')
        update_kindeditor_assets_owner ids,@syllabus.id,OwnerTypeHelper::SYLLABUS
      end
      redirect_to syllabus_path(@syllabus)
    else
      redirect_to syllabus_path(@syllabus)
    end
  end

  #删除课程大纲的描述
  def delete_des
    if @syllabus
      @syllabus.description = Message.where("id = 19412").first.nil? ? '' : Message.where("id = 19412").first.content
      @syllabus.des_status = 0
      @syllabus.attachments.destroy_all
      if @syllabus.save
        redirect_to syllabus_path(@syllabus)
      end
    end
  end

  def destroy
    if @syllabus && @syllabus.courses.not_deleted.empty?
      @syllabus.destroy
      redirect_to user_courselist_user_path(User.current.id)
    end
  end

  #班级列表
  def syllabus_courselist
    @order, @c_sort,@type = params[:order] || 1, params[:sort] || 1, params[:type] || 1

    #确定 sort_type
    if @order.to_i == @type.to_i
      @c_sort = @c_sort.to_i == 1 ? 2 : 1   #1升序 2降序
    else
      @c_sort = 2
    end

    sort_name = "updated_on"
    sort_type = @c_sort == 1 ? "asc" : "desc"

    @courses = @syllabus.courses.where("is_delete = ?", 0).select("courses.*,(SELECT MAX(updated_at)  FROM `course_activities` WHERE course_activities.course_id = courses.id) AS #{sort_name}").order("#{sort_name} #{sort_type}")

    #根据 作业+资源数排序
    if @order.to_i == 2
      @type = 2
      @courses.each do |course|
        course[:infocount] = (User.current.admin? || User.current.allowed_to?(:as_teacher,course)) ? (course.homework_commons.count + visable_attachemnts_incourse(course).count) : (course.homework_commons.where("publish_time <= '#{Date.today}'").count + visable_attachemnts_incourse(course).count)
        if course[:infocount] < 0
          course[:infocount] = 0
        end
      end
      @c_sort == 1 ? (@courses = @courses.sort{|x,y| x[:infocount] <=> y[:infocount] }) : (@courses = @courses.sort{|x,y| y[:infocount] <=> x[:infocount]})
      @courses = sortby_time_countcommon_nosticky @courses,sort_name
    else
      @type = 1
    end

    #分页
    @limit = 10
    @is_remote = true
    @atta_count = @courses.count
    @atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1
    @offset ||= @atta_pages.offset
    @courses = paginateHelper @courses,@limit

    respond_to do |format|
      format.js
      format.html{render :layout => 'base_syllabus'}
    end
  end

  #修改课程名称
  def edit_syllabus_title
    if @syllabus && params[:title] != ""
      @syllabus.update_column("title",params[:title])
    end
    respond_to do |format|
      format.js
    end
  end

  #修改英文名称
  def edit_syllabus_eng_name
    if @syllabus
      @syllabus.update_column("eng_name",params[:eng_name])
    end
    respond_to do |format|
      format.js
    end
  end

  #编辑属性
  def update_base_info
    if @syllabus
      @syllabus.update_attributes(:credit => params[:credit], :hours => params[:hours], :theory_hours => params[:theory_hours], :practice_hours => params[:practice_hours], :applicable_major => params[:applicable_major], :pre_course => params[:pre_course])
      @syllabus.update_attributes(:syllabus_type => params[:syllabus_type])
      respond_to do |format|
        format.js
      end
    end
  end

  def members
    @members = @syllabus.syllabus_members.includes(:user => {:user_extensions => [], :courses => []}).order("rank asc")
    @show = params[:show] ? params[:show].to_i : 0
    respond_to do |format|
      format.js
      format.html{render :layout => 'base_syllabus'}
    end
  end

  private
  def find_syllabus
    @syllabus = Syllabus.find params[:id]
  end

  def is_logged
    redirect_to signin_path unless User.current.logged?
  end
end
