//
//  PaymentRecordGroupView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 单日收款记录分组视图，含日期汇总与明细卡片。
final class PaymentRecordGroupView: UIView {

    private let headerStack = UIStackView()
    private let dateLabel = UILabel()
    private let summaryLabel = UILabel()
    private let itemsStack = UIStackView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func apply(group: RepaymentCollectionRecordGroup) {
        dateLabel.text = group.analyse.date
        summaryLabel.text = "共收款\(group.analyse.orderCount)笔, 当日收益：￥\(group.analyse.orderAmountSum)"

        itemsStack.arrangedSubviews.forEach {
            itemsStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }

        group.items.forEach { item in
            let card = PaymentRecordItemView()
            card.apply(item: item)
            itemsStack.addArrangedSubview(card)
        }
    }

    private func setupUI() {
        headerStack.axis = .horizontal
        headerStack.alignment = .center
        headerStack.distribution = .equalSpacing

        dateLabel.font = .systemFont(ofSize: 14)
        dateLabel.textColor = AppColor.textTertiary

        summaryLabel.font = .systemFont(ofSize: 14)
        summaryLabel.textColor = UIColor(hex: 0x4B5563)
        summaryLabel.textAlignment = .right
        summaryLabel.numberOfLines = 2

        itemsStack.axis = .vertical
        itemsStack.spacing = AppSpacing.sm

        addSubview(headerStack)
        addSubview(itemsStack)
        headerStack.addArrangedSubview(dateLabel)
        headerStack.addArrangedSubview(summaryLabel)

        headerStack.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
        }
        itemsStack.snp.makeConstraints { make in
            make.top.equalTo(headerStack.snp.bottom).offset(6)
            make.leading.trailing.bottom.equalToSuperview()
        }
    }
}

/// 单条收款记录卡片。
final class PaymentRecordItemView: UIView {

    private let amountLabel = UILabel()
    private let orderNumberLabel = UILabel()
    private let phoneLabel = UILabel()
    private let timeLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func apply(item: RepaymentCollectionRecordItem) {
        amountLabel.text = "¥ \(item.orderAmount)"
        orderNumberLabel.text = item.orderNumber
        phoneLabel.text = item.userPhone
        timeLabel.text = item.createTime
    }

    private func setupUI() {
        backgroundColor = .white
        layer.borderColor = UIColor(hex: 0xF3F4F6).cgColor
        layer.borderWidth = 1
        layer.cornerRadius = AppRadius.lg

        amountLabel.font = .systemFont(ofSize: 18, weight: .bold)
        amountLabel.textColor = AppColor.primary

        orderNumberLabel.font = .systemFont(ofSize: 14)
        orderNumberLabel.textColor = AppColor.textSecondary
        orderNumberLabel.textAlignment = .right

        phoneLabel.font = .systemFont(ofSize: 14)
        phoneLabel.textColor = AppColor.textSecondary

        timeLabel.font = .systemFont(ofSize: 14)
        timeLabel.textColor = AppColor.textSecondary
        timeLabel.textAlignment = .right

        let topRow = UIStackView(arrangedSubviews: [amountLabel, orderNumberLabel])
        topRow.axis = .horizontal
        topRow.distribution = .fillEqually

        let bottomRow = UIStackView(arrangedSubviews: [phoneLabel, timeLabel])
        bottomRow.axis = .horizontal
        bottomRow.distribution = .fillEqually

        let stack = UIStackView(arrangedSubviews: [topRow, bottomRow])
        stack.axis = .vertical
        stack.spacing = AppSpacing.xs

        addSubview(stack)
        stack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(AppSpacing.md)
        }
    }
}
