//
//  SHRecordDetailsVC.swift
//  ShorthandMaster
//
//  Created by 明津李 on 2020/9/23.
//  Copyright © 2020 明津李. All rights reserved.
//

import UIKit

class SHRecordDetailsVC: SHBaseViewController {

    @IBOutlet weak var tableView: UITableView?
    @IBOutlet weak var tableFooterView: UIView?
    
    let bottomView = SHRecordDetailsBottomView.loadFromNibAndClass(SHRecordDetailsBottomView.self)
    var edit: Bool = false
    var currentModel = SHRecordFolderModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    override func setupUI() {
        self.navigationItem.title = currentModel.name
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "编辑", style: .done, target: self, action: #selector(editClick))
        self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(hexStr: "4183F4")
        
        tableView?.estimatedRowHeight = 44
        tableView?.rowHeight = UITableView.automaticDimension
        tableView?.separatorStyle = .none
        
        bottomView?.frame = CGRect.init(x: -tableFooterView!.frame.width, y: 0, width: tableFooterView!.frame.width*2, height: tableFooterView!.frame.height)
        tableFooterView?.addSubview(bottomView!)
    }
    
    @objc func editClick(){
        edit = !edit
        if edit {
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "完成", style: .done, target: self, action: #selector(editClick))
            
            UIView.animate(withDuration: 0.25) {
                self.bottomView?.frame = CGRect.init(x: 0, y: 0, width: self.tableFooterView!.frame.width*2, height: self.tableFooterView!.frame.height)
            }
            
        }else{
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "编辑", style: .done, target: self, action: #selector(editClick))
            UIView.animate(withDuration: 0.25) {
                self.bottomView?.frame = CGRect.init(x: -self.tableFooterView!.frame.width, y: 0, width: self.tableFooterView!.frame.width*2, height: self.tableFooterView!.frame.height)
            }
        }
        
        self.tableView?.reloadData()
    }
}

extension SHRecordDetailsVC: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentModel.dataSources.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "SHRecordDetailsCell", for: indexPath) as! SHRecordDetailsCell

        cell.model = currentModel.dataSources[indexPath.row]
        cell.edit = edit
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                
//        let show = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordShowViewController") as! SHRecordShowViewController
//        if self.searchBarVC.isActive {
//            show.model = searchDataSource[indexPath.row]
//        }else{
//            show.model = dataSources[indexPath.row]
//        }
//        self.navigationController?.pushViewController(show, animated: true)
    }
}
