# == Schema Information
#
# Table name: identity_verifications
#
#  id              :integer          not null, primary key
#  user_id         :integer          not null
#  number          :string(255)      not null
#  name            :string(255)      not null
#  card_front      :string(255)
#  card_back       :string(255)
#  hold_card_front :string(255)
#  hold_card_back  :string(255)
#  state           :integer          default("0")
#  description     :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#
# Indexes
#
#  index_identity_verifications_on_user_id  (user_id)
#

class IdentityVerification < ApplicationRecord
    belongs_to :user
    enum state: { "待审核": 0, "已通过": 1, "已拒绝": 2}
    after_create do
        Attachment.where(uuid:[card_front,card_back,hold_card_front,hold_card_back]).update_all(is_public:0)
    end
    
    after_save do
        if state == "已通过"
            user.update(id_card_verify: true, website_permission: true)
        end    
    end

    def card_front_attachment
        Attachment.where_id_or_uuid(card_front).first
    end
    
    def card_back_attachment
        Attachment.where_id_or_uuid(card_back).first
    end

    def hold_card_front_attachment
        Attachment.where_id_or_uuid(hold_card_front).first
    end

    def hold_card_back_attachment
        Attachment.where_id_or_uuid(hold_card_back).first
    end
end
