class Admin::TitleProviderGroupNamesController < ApplicationController

  before_filter :superadmin_login_required

  def index
    params.reject!{|k,v| v.blank?}
    if !params[:country_id].nil?
      @country_id = params[:country_id]
      @title_provider_group_names = TitleProviderGroupName.order("name ASC").where(:country_id => params[:country_id])
    else
      @title_provider_group_names = TitleProviderGroupName.order("name ASC").all
    end
    @title_provider_group_name_id = params[:id]
    @titles = Title.where(title_provider_group_name_id: @title_provider_group_name_id)
    render layout: nil
  end

  def show
    @title_provider_group_name = TitleProviderGroupName.find(params[:id])
    @country_name = Country.find(@title_provider_group_name.country_id).name
    get_permission_label
    render layout: nil

  end

  def new
    get_permission_label
    @title_provider_group_name = TitleProviderGroupName.new(country_id: params[:country_id])
    Provider.order('name asc').each do |p|
      @title_provider_group_name.title_provider_groups.build(provider_id: p.id)
    end
    render action: 'edit'
  end

  def create
    get_permission_label
    ## modif Patrick rend création impossible, chercher pourquoi?
    #@title_provider_group_name = TitleProviderGroupName.new(permitted_params)
    @title_provider_group_name = TitleProviderGroupName.new({name: params[:title_provider_group_name][:name], country_id:  params[:title_provider_group_name][:country_id]})
    flash[:notice] = I18n.t('successfully_created') if @title_provider_group_name.save
    flash[:notice] = I18n.t('successfully_created') if @title_provider_group_name.update_attributes(permitted_params)
    render layout: nil
    #redirect_to admin_title_provider_group_name_path(@title_provider_group_name, format: :js) unless @title_provider_group_name.nil?
  end

  def edit
    get_permission_label
    @title_provider_group_name = TitleProviderGroupName.find(params[:id])
    new_provs_id = Provider.order('name asc').collect{|p| p.id} - @title_provider_group_name.providers.collect{|p| p.id}
    new_provs_id.each do |p_id|
      @title_provider_group_name.title_provider_groups.build(provider_id: p_id)
    end
  end

  def update
    @title_provider_group_name = TitleProviderGroupName.find(params[:id])
    flash[:notice] = I18n.t('successfully_updated') if @title_provider_group_name.update_attributes(permitted_params)
    #redirect_to action: :edit, format: :js
  end

  def destroy
    tpgn = TitleProviderGroupName.find(params[:id])
    titles = Title.where(title_provider_group_name_id: tpgn.id)
    tpgn.destroy if titles.empty?
    flash[:notice] = I18n.t('successfully_updated')
    render layout: nil
  end

  private

  def get_permission_label
    @all_permission_labels = PermissionLabel.where(id: 3)
    @all_permission_labels += PermissionLabel.where(id: 1)
    @all_permission_labels += PermissionLabel.where(id: 2)
  end

  def permitted_params
    params.require(:title_provider_group_name).permit!
  end

end
