class Admins::PlatformCommunicatesController < Admins::BaseController
  before_action :get_communicate, only: [:edit, :update, :destroy, :show, :online_switch]

  def index
    params[:location] = "pc"
    sort_by = PlatformCommunicate.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'order_index'
    sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
    q = PlatformCommunicate.where(location: params[:location]).ransack(title_cont: params[:search])
    communicates = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
    @communicates = kaminari_paginate(communicates)
  end

  def applet
    params[:location] = "applet"
    sort_by = PlatformCommunicate.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'order_index'
    sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
    q = PlatformCommunicate.where(location: params[:location]).ransack(title_cont: params[:search])
    communicates = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
    @communicates = kaminari_paginate(communicates)
  end

  def show
    watchers = @communicate.watchers
    if params[:search].present?
      like_sql = 'users.lastname LIKE :keyword OR users.nickname LIKE :keyword OR users.mail LIKE :keyword OR users.phone LIKE :keyword OR users.login LIKE :keyword OR users.company_name LIKE :keyword'
      watchers = watchers.left_joins(:user).where(like_sql, keyword: "%#{params[:search]}%")
    end
    if params[:user_type].present?
      watchers = watchers.left_joins(:user).where("users.user_type = ?", params[:user_type])
    end
    if params[:professional_field].present?
      watchers = watchers.left_joins(:user).where("users.professional_field = ?", params[:professional_field])
    end
    @watchers = kaminari_paginate watchers
  end

  def new
    location = params[:location] || "pc"
    @communicate = PlatformCommunicate.new(location: location)
  end

  def create
    @communicate = PlatformCommunicate.new(communicate_params)
    if @communicate.save
      redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:success] = '创建成功'
    else
      redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:danger] = "创建失败"
    end
  end

  def edit

  end

  def update
    @communicate.attributes = communicate_params
    if @communicate.save
      redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:success] = '更新成功'
    else
      redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:danger] = '更新失败'
    end
  end

  def destroy
    location = @communicate.location
    if @communicate.destroy
      redirect_to location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:success] = '删除成功'
    else
      redirect_to location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
      flash[:danger] = '删除失败'
    end
  end

  def online_switch
    if @communicate.status
      @communicate.update_attributes!(status: false)
    else
      @communicate.update_attributes!(status: true)
    end
    # render_ok
  end

  private

  def get_communicate
    @communicate = PlatformCommunicate.find_by_id(params[:id])
  end

  def communicate_params
    params.require(:platform_communicate).permit!
  end
end