//
//  AV_DetalisVC.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/4/16.
//

import UIKit
import WCDBSwift
import Alamofire
import FirebaseRemoteConfig

class AV_DetalisVC: AV_RootVC {
    @IBOutlet weak var imageV: UIImageView!
    
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var contentLabel: UILabel!
    
    
    @IBOutlet weak var wsichsel: UISwitch!
    
    @IBOutlet weak var bgView: UIView!
    
 
    var retryAttempt = 0.0
    
    var model:UserModel?
    override func viewDidLoad() {
        super.viewDidLoad()
//        bgView.clipsToBounds = true
        bgView.layer.cornerRadius = 20
        imageV.layer.cornerRadius = 20
        // 设置阴影属性
        bgView.layer.shadowColor = UIColor.hexSting(color: "#9ABDE6", alpha: 0.25).cgColor
        bgView.layer.shadowOpacity = 0.5
        bgView.layer.shadowOffset = CGSize(width: 0, height: 2) // 设置向下偏移
        bgView.layer.shadowRadius = 4
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
        let image = model?.getImage()
        self.imageV.image = image
        self.timeLabel.text = model?.date
        self.contentLabel.text = model?.name
        self.wsichsel.isOn = ((model?.notificationEnabled) != nil)
        
    }
    
    @IBAction func editBtn(_ sender: UIButton) {
        let vc = AV_AddVC()
        vc.model = self.model
        vc.typeedit = 1
        vc.dataUpdateClosure = { [self] updatedModels in
            // 在闭包中接收更新的模型数组
            self.model = updatedModels
            // 在这里处理接收到的模型数组，例如重新加载数据或者刷新界面
            let image = model!.getImage()
            self.imageV.image = image
            self.timeLabel.text = model?.date
            self.contentLabel.text = model?.name
            self.wsichsel.isOn = ((model?.notificationEnabled) != nil)
        }
        navigationController?.pushViewController(vc, animated: true)
        
        
    }
    
    
    @IBAction func deleteBtn(_ sender: UIButton) {

        LuxADManager.shareInstance().showADByRandomMode(callback: {
            actionType  in
            if let uid = self.model?.uid {
                // 构建删除条件
                let condition: Condition = UserModel.Properties.uid == uid
                
                // 调用删除方法
                PrWCDBBaseManager.shared.deleteFromDb(fromTable: userModelTableName, where: condition)
            } else {
                print("Error: Model UID is nil")
            }
            self.navigationController?.popViewController(animated: true)
        })
    }
    
    
    @IBAction func back(_ sender: Any) {

        LuxADManager.shareInstance().showADByRandomMode(callback: {
            actionType  in
            self.navigationController?.popViewController(animated: true)
        })
    }

    
    
}
