class Action::NodeTypesController < ApplicationController
  before_action :require_admin, except: [:index]
  before_action :find_node_type, except: [:index, :create, :new]

  def index
    @node_types = Action::NodeType.all
    respond_to do |format|
      format.html
      format.json { render_ok(data: @node_types.as_json) }
    end
  end

  def create
    @node_type = Action::NodeType.new(node_types_params)
    respond_to do |format|
      if @node_type.save
        format.html { redirect_to action_node_types_path, notice: '创建成功.' }
        format.json { render_ok(data: @node_type.as_json) }
      else
        format.html { render :new }
        format.json { render json: @node_type.errors, status: -1 }
      end
    end
  end

  def show
    respond_to do |format|
      format.html
      format.json { render_ok(data: @node_type.as_json) }
    end
  end

  def new
    @node_type = Action::NodeType.new
  end

  def edit

  end

  def update
    @node_type.update(node_types_params)
    respond_to do |format|
      format.html { redirect_to action_node_types_path, notice: '更新成功.' }
      format.json { render_ok(data: @node_type.as_json) }
    end
  end

  def destroy
    if @node_type.destroy!
      flash[:success] = '删除成功'
    else
      flash[:danger] = '删除失败'
    end
    respond_to do |format|
      format.html { redirect_to action_node_types_path }
      format.json { render_ok }
    end
  end

  private

  def find_node_type
    @node_type = Action::NodeType.find(params[:id])
  end

  def node_types_params
    if params.require(:action_node_type)
      params.require(:action_node_type).permit(:name, :description, :sort_no)
    else
      params.permit(:name, :description, :sort_no)
    end
  end
end
