# -*- encoding : utf-8 -*-

class Admin::PArticleSerialNumsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "default"
  end
  
  def index
    @p_article_serial_nums = PArticleSerialNum.all

    if params[:search][:p_serial_num_type_name].present?
      @p_article_serial_nums = @p_article_serial_nums.where(p_serial_num_type_id: params[:search][:p_serial_num_type_name])

    end

    if params[:search][:p_product_ref_cc_name]
      @p_article_serial_nums = @p_article_serial_nums.joins(:p_article, :p_product_ref).where("cc_name LIKE ?","#{params[:search][:p_product_ref_cc_name]}%")
    end

    if params[:search][:value]
      @p_article_serial_nums = @p_article_serial_nums.where("value LIKE ?","#{params[:search][:value]}%")
    end

    @p_article_serial_nums = sort_by_sorting(@p_article_serial_nums, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @p_article_serial_nums = @p_article_serial_nums.page(page).per(per_page)
      
      }
    end
  end

  def show
    @p_article_serial_num = PArticleSerialNum.find(params[:id])

  end

  def new
    @p_article_serial_num = PArticleSerialNum.new
    # @p_article_serial_num.build_p_serial_num_value
  end

  def edit
    @p_article_serial_num = PArticleSerialNum.find(params[:id])
    
  end

  def create
    @p_article_serial_num = PArticleSerialNum.new(params.require(:p_article_serial_num).permit!)

    if @p_article_serial_num.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @p_article_serial_num = PArticleSerialNum.find(params[:id])


    if @p_article_serial_num.update_attributes(params.require(:p_article_serial_num).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_article_serial_num = PArticleSerialNum.find(params[:id])
    @p_article_serial_num.destroy
    
  end
end
