class Users::ClasController <  Users::BaseController
    before_action :require_login
    before_action :private_user_resources!
    def index
        @user_clas = UserCla.where(user: current_user)
    end
    
    def show
        @user_cla = current_user.user_clas.find params[:id]
    end

    def create
        @user_cla = current_user.user_clas.find_by(cla_id: params[:cla_id])
        if @user_cla.nil?
            ActiveRecord::Base.transaction do
                Users::UserClaForm.new(user_cla_params).validate!
                @user_cla = UserCla.build(user_cla_params, current_user.id)
                
            end
        elsif @user_cla.state == "failed"
            @user_cla.update_by_params(user_cla_params)
        elsif @user_cla.state == "signed"
            return render_error('协议生效中，请勿重复签署') 
        end
        render_ok
        rescue Exception => e
            uid_logger_error(e.message)
            tip_exception(e.message)
    end

    def destroy
        @user_cla = current_user.user_clas.find params[:id]
        @user_cla.update_attributes(state: 2)
        render_ok
    end

    private 

    def user_cla_params
        params.permit(:email, :real_name, :cla_id)
    end

end
