class Panel::Location::BackEnd::LocationsController < OrbitBackendController
  include AdminHelper

  def index
    @locations = LocationInfo.all
    @categories = get_categories_for_index
    @location_infos = [];
    @locations.each do |loc|
      loc['color'] = Category.find(loc.category_id).custom_value
      @location_infos << loc
    end
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @locations }
    end
  end

  def new
    @location_info = LocationInfo.new
    @categories = get_categories_for_index

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

  def edit
    @location_info = LocationInfo.find(params[:id])
    @categories = get_categories_for_index

  end

  def show
    @location_info = LocationInfo.find(params[:id])
    respond_to do |format|
      format.html
      format.json { render json: @location }
    end
  end

  def create
    @location_info = LocationInfo.new(params[:location_info])
    @categories = get_categories_for_index
    if @location_info.save
      flash[:success] = "Success!!"
      redirect_to panel_location_back_end_locations_url
    else
      error_msg = @location_info.errors.full_messages
      render 'new'
    end
  end

  def update
    @location_info = LocationInfo.find(params[:id])
    @location_info.update_attributes(params[:location_info])
    redirect_to panel_location_back_end_locations_url
  end

  def destroy
    @location_info = LocationInfo.find(params[:id])
    @location_info.destroy
    redirect_to panel_location_back_end_locations_url

  end

  def get_locations
    location_infos = LocationInfo.all
    @data = Array.new

    location_infos.each do |location|
      picurl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.url}"
      thumburl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.thumb.url}"
      @data << { name: location.name,
                 pic_url: picurl,
                 thumb_url: thumburl,
                 longitude: location.longitude,
                 latitude: location.latitude,
                 description: location.description }
    end

    render :json => JSON.pretty_generate(@data)
  end

  def get_categories
    @data = Array.new
     @data << { name: "Department",
                link:  "http://#{request.host_with_port}"+"/panel/location/back_end/locations/get_locations?locale=en" }

      render :json => JSON.pretty_generate(@data)
  end
end
