class Projects::ProjectVisitorsController < Projects::BaseController
  before_action :require_login

  def index 
    project_visitors = @project.project_visitors.includes(:user).order(:created_at)
    project_visitors = project_visitors.where("contact like ? OR contact_phone like ? OR contact_unit like ?", "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%")
    @project_visitors = kaminari_paginate(project_visitors)
  end

  def create 

    return render_error('项目访问记录已存在！', 1) if @project.project_visitors.find_by(user_id: current_user.id).present?
    @project_visitor = @project.project_visitors.new(project_visitor_params.merge!(user_id: current_user.id) )
    if @project_visitor.save!
      render_ok
    else
      render_error(@project_visitor.errors.full_messages)
    end
  rescue Exception => e
    uid_logger_error(e.message)
    tip_exception(e.message)
  end

  private
  def project_visitor_params
    params.require(:project_visitor).permit(:contact, :contact_phone, :contact_unit)
  end


end