//
//  YHResignAppointTimeCell.swift
//  galaxy
//
//  Created by edy on 2024/10/10.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import AttributedString

class YHResignAppointTimeSingleCell: UITableViewCell {

    static let cellReuseIdentifier = "YHResignAppointTimeSingleCell"
    var model = YHResignAppointTimeSettingModel()
    
    var switchBlock: ((Bool)->())?
    var refreshBlock: (()->())?
    
    lazy var whiteContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = kCornerRadius6
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel(frame: CGRect(x: 18, y: 16, width: KScreenWidth, height: 24.0))
        let mustTag: ASAttributedString = .init("* ", .font(UIFont.PFSC_M(ofSize: 17)),.foreground(UIColor.failColor))
        var title: ASAttributedString = .init("请您确认以下信息", .font(UIFont.PFSC_M(ofSize: 17)),.foreground(UIColor.mainTextColor))
        title = mustTag + title
        titleLabel.attributed.text = title
        return titleLabel
    }()
    
    lazy var submitTimeItemView: YHResignAppointSelectTimeItemView = {
        let view = YHResignAppointSelectTimeItemView(frame: .zero)
        view.title = "预约在港递交时间"
        view.placeHolder = "请选择"
        view.isNeedShowErrorTips = false
        view.errorTips = "请选择在港递交时间"
        view.detail = ""
        view.updateLineMargin(-18.0)
        view.clickBlock = {
            [weak self] in
            guard let self = self else { return }
            self.selectDetailDate(title: "选择在港递交时间") {
                [weak self] dateStr in
                guard let self = self else { return }
                self.model.ready_to_submit_at = "2024-11-04 17:00"
                self.refreshBlock?()
            }
        }
        return view
    }()
    
    lazy var leaveTimeItemView: YHResignAppointSelectTimeItemView = {
        let view = YHResignAppointSelectTimeItemView(frame: .zero)
        view.title = "预计离港时间"
        view.placeHolder = "请选择"
        view.errorTips = "请选择离港时间"
        view.isNeedShowErrorTips = false
        view.detail = ""
        view.clickBlock = {
            [weak self] in
            guard let self = self else { return }
            self.selectDetailDate(title: "选择离港时间") {
                [weak self] dateStr in
                guard let self = self else { return }
                self.model.leave_hk_at = "2024-12-04 17:00"
                self.refreshBlock?()
            }
        }
        return view
    }()
    
    lazy var leaveHKSwithView: YHResignLeaveHKSwitchItemView = {
        let view = YHResignLeaveHKSwitchItemView()
        view.title = "暂不离港"
        view.selectBlock = {
            [weak self] on in
            guard let self = self else { return }
            self.switchBlock?(on)
        }
        return view
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func updateModel(_ model: YHResignAppointTimeSettingModel) {
        self.model = model
        self.leaveHKSwithView.switchBtn.setOn(self.model.notleaveHK, animated: false)
        let isHiddenLeaveHKTime = self.model.notleaveHK
        
        // 时间控件显示
        submitTimeItemView.detail = model.getSubmitHKTime()
        leaveTimeItemView.detail = model.getLeaveHKTime()
        
        // 时间控件红色提示显示
        submitTimeItemView.isNeedShowErrorTips = (model.isClickSubmit && model.ready_to_submit_at.isEmpty)
        leaveTimeItemView.isNeedShowErrorTips = (model.isClickSubmit && model.leave_hk_at.isEmpty)
        
        leaveTimeItemView.removeFromSuperview()
        leaveTimeItemView.snp.removeConstraints()
        
        if isHiddenLeaveHKTime {
            leaveHKSwithView.snp.remakeConstraints { make in
                make.left.equalTo(18)
                make.right.equalTo(-18)
                make.top.equalTo(submitTimeItemView.snp.bottom)
                make.bottom.equalToSuperview()
            }
            
        } else {
            
            whiteContentView.addSubview(leaveTimeItemView)
            leaveTimeItemView.snp.remakeConstraints { make in
                make.left.equalTo(18)
                make.right.equalTo(-18)
                make.top.equalTo(submitTimeItemView.snp.bottom)
            }
            
            leaveHKSwithView.snp.remakeConstraints { make in
                make.left.equalTo(18)
                make.right.equalTo(-18)
                make.top.equalTo(leaveTimeItemView.snp.bottom)
                make.bottom.equalToSuperview()
            }
        }
        
        self.setNeedsLayout()
        self.layoutIfNeeded()
    }
    
    func selectDetailDate(title: String, selectBlock:((_ dateStr: String)->())?) {
        YHDatePickView.show(type: .yyyymmdd, title:title, lastIsTaday: false, currentDay:"") {
            [weak self] dateStr in
            guard let self = self else { return }
            selectBlock?(dateStr)
        }
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        self.contentView.addSubview(whiteContentView)
        whiteContentView.addSubview(titleLabel)
        whiteContentView.addSubview(submitTimeItemView)
        whiteContentView.addSubview(leaveTimeItemView)
        whiteContentView.addSubview(leaveHKSwithView)

        
        whiteContentView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(-16)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(16)
            make.height.equalTo(24)
        }
        
        submitTimeItemView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
        }
        
        leaveTimeItemView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(submitTimeItemView.snp.bottom)
        }
        
        leaveHKSwithView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(leaveTimeItemView.snp.bottom)
            make.bottom.equalToSuperview()
        }
    }
}
