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

import UIKit
import SVProgressHUD
import Photos

class AV_HomeVC: AV_RootVC {
    
    
    @IBOutlet weak var noneImagV: UIImageView!
    
    
    @IBOutlet weak var serchView: UIView!
    
    
    @IBOutlet weak var serchtextF: UITextField!
    
    
    @IBOutlet weak var tableView: UITableView!
    
    
    @IBOutlet weak var titleLabel: UILabel!
    
    var arr = [UserModel]()
    
    var searchResults: [UserModel] = []
    
    var typesearch:Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        serchView.layer.cornerRadius = 25
        setTableViewCell()
        PrWCDBBaseManager.shared.createTable(table: "UserModel", of: UserModel.self)
        serchtextF.delegate = self
//        requestPhotoLibraryPermission()
//        setNetwork()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
        let users = PrWCDBBaseManager.shared.qureyFromDb(fromTable: userModelTableName, cls: UserModel.self)
        if let users = users {
            self.arr = users
            print("---\(users)")
        } else {
            
        }
        
        titleLabel.text = "My Task \(self.arr.count)"
        //            // 更新视图
        updateUI()
    }
    
    
    
    // 根据输入的文本进行查询
    func search(withText searchText: String) {
        // 查询数据库中匹配的用户数据
        let condition = UserModel.Properties.name.like("%\(searchText)%")
        let results = PrWCDBBaseManager.shared.qureyFromDb(fromTable: userModelTableName, cls: UserModel.self, where: condition)
        
        if let results = results {
            searchResults = results
            // 更新 UI，例如刷新表格视图
            tableView.reloadData()
        }
    }
    
    
    func setTableViewCell(){
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "AV_TaskCell", bundle: nil), forCellReuseIdentifier: "AV_TaskCell")
        
    }
    
    func updateUI() {
        if arr.isEmpty {
            noneImagV.isHidden = false
        } else {
            noneImagV.isHidden = true
        }
        tableView.reloadData()
    }
    
    
    
    
    
}

extension AV_HomeVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.typesearch == 0{
            return arr.count
        }else{
            return searchResults.count
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AV_TaskCell", for: indexPath) as! AV_TaskCell
        //        cell.dateLabel.text = "May\n20"
        if self.typesearch == 0{
            cell.model = self.arr[indexPath.row]
        }else{
            cell.model = self.searchResults[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = AV_DetalisVC()
        if self.typesearch == 0{
            vc.model = self.arr[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            vc.model = self.searchResults[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}
extension AV_HomeVC: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // 获取输入的文本
        guard let text = textField.text else { return true }
        
        // 计算最终输入的文本
        let newText = (text as NSString).replacingCharacters(in: range, with: string)
        
        if newText.count <= 0 {
            self.typesearch = 0
        }else{
            self.typesearch = 1
        }
        
        // 执行搜索操作
        search(withText: newText)
        
        return true
    }
}
