//
//  AppealInputViewController.swift
//  OrderScheduling
//
//  Created by 中道 on 2025/9/15.
//

import UIKit

final class AppealInputViewController: UIViewController {

    let titleLabel = UILabel()
    let textField = UITextField()

    init(prefill: String? = nil) {
        super.init(nibName: nil, bundle: nil)
        textField.text = prefill
    }
    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.layer.cornerRadius = 12
        view.clipsToBounds = true

        // 标题
        titleLabel.text = "申诉理由"
        titleLabel.textAlignment = .center
        titleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
        titleLabel.textColor = .black

        // 输入框
        textField.placeholder = "请输入申诉原因…"
        textField.borderStyle = .none
        textField.backgroundColor = UIColor(white: 0.98, alpha: 1)
        textField.layer.cornerRadius = 10
        textField.layer.borderWidth = 1
        textField.layer.borderColor = UIColor.systemBlue.withAlphaComponent(0.35).cgColor
        textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
        textField.leftViewMode = .always
        textField.clearButtonMode = .whileEditing
        textField.returnKeyType = .done
        textField.delegate = self

        let container = UIStackView(arrangedSubviews: [titleLabel, textField])
        container.axis = .vertical
        container.spacing = 14
        container.alignment = .fill
        container.distribution = .fill
        view.addSubview(container)
        container.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            container.topAnchor.constraint(equalTo: view.topAnchor, constant: 16),
            container.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            container.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            container.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16),
            textField.heightAnchor.constraint(equalToConstant: 40)
        ])
    }

    // 控制弹窗内容的尺寸（宽度由库控制，这里给出理想高度）
    override var preferredContentSize: CGSize {
        get { CGSize(width: 280, height: 140) }
        set { super.preferredContentSize = newValue }
    }
}

extension AppealInputViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
