#
#= customers_users コントローラー
# Authors:: Kazunori Shimizu
# Copyright:: Copyright (C) OSS K.K.  2012.
#--
# date        name                   note
# 2012.3.8   Kazunori Shimizu        新規作成
#-------------------------------------------------------------------------------
#++
class CustomersUsersController < Comm::BaseController::General
  #== コンストラクタ
  #-----------------------------------------------------------------#++
  def initialize
    @mcls = CustomersUser
  end

  #== ロール一覧取得(指定ユーザーの所属情報付与）
  #-----------------------------------------------------------------#++
  def customers_index
    uid = params[:id]
    
    # 全有効ロール取得
    all_customers = Customer.set(MFIND_V).all.only_hashfy
    # ユーザーの所属ロール取得
    user_customers = User.find(uid).customers.only_hashfy
    
    # ユーザーの所属情報付与
    all_customers.each{|customer|
      idx = user_customers.search_val(customer['id'], 'id')
      customer['belong'] = idx ? true : false
    }
    
    # 返却
    respond_to do |format|
      format.ext_json{
        render :json => all_customers.to_ext_json('customers', nil)
      }
    end
  end

  #== 指定ユーザーのロール編集
  #-----------------------------------------------------------------#++
  def customers_update
    uid = params[:id]
    user = User.find(uid)
    
    # ロール編集
    result = user.update_customers(params[:data])
    
    # 返却
    render :json => Comm::Tool::Json.result_json(result, user.errmsg)
  end
end
