//
//  YHOtherResidenceFillView.swift
//  galaxy
//
//  Created by davidhuangA on 2024/2/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHOtherResidenceFillView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var dataModel : Residence? {
        didSet {
            updateUI()
        }
    }
    
    let nationView : YHOtherSelecteItemView =  YHOtherSelecteItemView()
    let startView : YHOtherSelecteItemView =  YHOtherSelecteItemView()
    let endView : YHOtherSelecteItemView =  YHOtherSelecteItemView()
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        nationView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
        }
        
        startView.snp.makeConstraints { make in
            make.top.equalTo(nationView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        
        endView.snp.makeConstraints { make in
            make.top.equalTo(startView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
}

extension YHOtherResidenceFillView {
    func setupUI() {
        
        //1.国家
        let view = nationView
        view.updateKeyName(name: "国家", keyValue: "")
        view.type = 1
        addSubview(view)
        view.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
        }
        
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(tapCountry))
        view.addGestureRecognizer(tap1)
        
        
        //2.开始时间
        let view2 = startView
        view2.type = 1
        view2.updateKeyName(name: "开始时间", keyValue: "")
        addSubview(view2)
        view2.snp.makeConstraints { make in
            make.top.equalTo(view.snp.bottom)
            make.left.right.equalToSuperview()
//            make.height.equalTo(YHOtherSelecteItemView.getViewHBy(type: 1))
        }
        let tap2 = UITapGestureRecognizer(target: self, action: #selector(tapStartDate))
        view2.addGestureRecognizer(tap2)
        
        //3.结束时间
        let view3 = endView
        view3.type = 1
        view3.updateKeyName(name: "结束时间", keyValue: "")
        addSubview(view3)
        view3.snp.makeConstraints { make in
            make.top.equalTo(view2.snp.bottom)
            make.left.right.equalToSuperview()
//            make.height.equalTo(YHOtherSelecteItemView.getViewHBy(type: 1))
            
            make.bottom.equalToSuperview()
        }
        
        let tap3 = UITapGestureRecognizer(target: self, action: #selector(tapEndDate))
        view3.addGestureRecognizer(tap3)
    }
    
    
    func updateUI() {
        guard let dataModel = dataModel else { return }
        
        
        nationView.updateKeyName(name: "国家", keyValue: dataModel.country)
        nationView.needCheckFlag = dataModel.needCheckFlag
        
        startView.updateKeyName(name: "开始时间", keyValue: dataModel.start_time)
        startView.needCheckFlag = dataModel.needCheckFlag
        
        endView.updateKeyName(name: "结束时间", keyValue: dataModel.end_time)
        endView.needCheckFlag = dataModel.needCheckFlag
        
        layoutIfNeeded()
    }
    
}

extension YHOtherResidenceFillView {
    @objc func tapCountry(_ gestureRecognizer: UIPanGestureRecognizer) {
        let vc = YHSelectCountryViewController()
         vc.backLocationStringController = { country in
             printLog(country)
             self.nationView.updateKeyName(name: "国家", keyValue: country)
             self.dataModel?.country = country
             
             self.layoutIfNeeded()
         }
        UIViewController.current?.navigationController?.pushViewController(vc)
    }
    
    
    @objc func tapStartDate(_ gestureRecognizer: UIPanGestureRecognizer) {
        YHDatePickView.show(type: .yyyymmdd) { date in
           printLog(date)
            
            self.startView.updateKeyName(name: "开始时间", keyValue: date)
            self.dataModel?.start_time = date
            self.layoutIfNeeded()
        }
    }
    
    @objc func tapEndDate(_ gestureRecognizer: UIPanGestureRecognizer) {
        YHDatePickView.show(type: .yyyymmdd) { date in
           printLog(date)
            self.endView.updateKeyName(name: "结束时间", keyValue: date)
            self.dataModel?.end_time = date
            self.layoutIfNeeded()
        }
    }
}
