class IdentityVerificationsController < ApplicationController
    before_action :require_login
    before_action :require_profile_completed, only: [:create]

    def index
        @id_verify = current_user.identity_verification
        return render_ok({data:nil}) unless @id_verify
    end

    def create
        return tip_exception(-1, "您已提交过身份审核，请勿重复提交") if IdentityVerification.exists?(user:current_user)
        return tip_exception(-1, "身份证输入有误")unless create_params[:number] =~ User::VALID_NUMBER_REGEX
        @id_verify = IdentityVerification.new(create_params)
        @id_verify.user = current_user
        @id_verify.save
    end

    def update
        return tip_exception(-1, "身份证输入有误")unless create_params[:number] =~ User::VALID_NUMBER_REGEX
        current_user.identity_verification.update(create_params.merge({ state: 0 }))
        current_user.update(id_card_verify: false)
        @id_verify = current_user.identity_verification
    end

    private 
    def create_params
        params.permit(:number, :name, :card_front, :card_back, :hold_card_front, :hold_card_back)
    end
end
