# encoding: UTF-8
module Admin  
  class OrdersController < Admin::ApplicationController
    before_filter :set_relation_options, only: [:new, :edit, :create, :update]
    # GET /orders
    # GET /orders.json
    def index
      @orders = Order.all

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @orders }
      end
    end

    # GET /orders/1
    # GET /orders/1.json
    def show
      @order = Order.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @order }
      end
    end

    # GET /orders/new
    # GET /orders/new.json
    def new
      @order = Order.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @order }
      end
    end

    # GET /orders/1/edit
    def edit
      @order = Order.find(params[:id])
    end

    # POST /orders
    # POST /orders.json
    def create
      @order = Order.new(order_params)

      respond_to do |format|
        if @order.save
          format.html { redirect_to [:admin, @order], notice: t('action.create.successfull', model: Order.model_name.human) }
          format.json { render json: @order, status: :created, location: @order }
        else
          format.html { render action: "new" }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end

    # PUT /orders/1
    # PUT /orders/1.json
    def update
      @order = Order.find(params[:id])

      respond_to do |format|
        if @order.update_attributes(order_params)
          format.html { redirect_to [:admin, @order], notice: t('action.update.successfull', model: Order.model_name.human) }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /orders/1
    # DELETE /orders/1.json
    def destroy
      @order = Order.find(params[:id])
      @order.destroy

      respond_to do |format|
        format.html { redirect_to admin_orders_url, notice: t('action.destroy.successfull', model: Order.model_name.human) }
        format.json { head :no_content }
      end
    end

    private

    def set_relation_options
      @lists = List.all
      @suppliers = Supplier.all
    end

    def order_params
      params.require(:order).permit!
    end
  end
end
