class Action::NodeSelectsController < ApplicationController

  before_action :require_admin, except: [:index]
  before_action :find_action_node

  def index
    @node_selects = @node.action_node_selects
    respond_to do |format|
      format.html
      format.json
    end
  end

  def create
    @node_select = Action::NodeSelect.new(node_select_params)
    @node_select.action_node = @node
    respond_to do |format|
      if @node_select.save
        format.html { redirect_to action_node_node_selects_path(@node), notice: '创建成功.' }
        format.json { render_ok(data: @node_select.as_json) }
      else
        format.html { render :new }
        format.json { render json: @node_select.errors, status: -1 }
      end
    end
  end

  def new

  end

  def show

  end

  def edit

  end

  def update
    @node_select.update(node_select_params)
    respond_to do |format|
      format.html { redirect_to action_node_node_selects_path(@node), notice: '更新成功.' }
      format.json { render_ok(data: @node_select.as_json) }
    end
  end

  def destroy
    if @node_select.destroy!
      flash[:success] = '删除成功'
    else
      flash[:danger] = '删除失败'
    end
    redirect_to "api/actions/nodes"
  end

  private

  def find_action_node
    @node = Action::Node.find(params[:node_id])
    if params[:id].present?
      @node_select = @node.action_node_selects.find(params[:id])
    else
      @node_select = Action::NodeSelect.new
    end

  end

  def node_select_params
    if params.require(:action_node_select)
      params.require(:action_node_select).permit(:name, :val, :val_ext, :description, :sort_no)
    else
      params.permit(:name, :val, :val_ext, :description, :sort_no)
    end
  end
end
