// // PunchPointViewControllers.swift // suixinkan // // Created by Codex on 2026/6/26. // import SnapKit import UIKit private extension PunchPointItem { var displayAddress: String { region?.address ?? scenicSpotStr } } /// 打卡点列表页。 final class PunchPointListViewController: ModuleTableViewController { private let viewModel = PunchPointListViewModel() override func viewDidLoad() { title = "打卡点" navigationItem.rightBarButtonItem = UIBarButtonItem( title: "新建", style: .plain, target: self, action: #selector(createPunchPoint) ) super.viewDidLoad() wireViewModel(viewModel) { } } override func tableRowCount() -> Int { viewModel.items.count } override func configureCell(_ cell: TitleSubtitleTableViewCell, at indexPath: IndexPath) { let item = viewModel.items[indexPath.row] cell.configure(title: item.name, subtitle: item.displayAddress, detail: item.statusLabel) } override func didSelectTableRow(at indexPath: IndexPath) { let item = viewModel.items[indexPath.row] navigationController?.pushViewController( PunchPointDetailViewController(punchPointId: item.id, summary: item), animated: true ) } override func reloadContent() async { await viewModel.reload(scenicId: services.currentScenicId, api: services.punchPointAPI) } @objc private func createPunchPoint() { navigationController?.pushViewController(PunchPointEditorViewController(punchPointId: nil), animated: true) } } extension PunchPointListViewModel: ViewModelBindable {} /// 打卡点详情页。 final class PunchPointDetailViewController: ModuleTableViewController { private let punchPointId: Int private let summary: PunchPointItem? private let viewModel = PunchPointListViewModel() init(punchPointId: Int, summary: PunchPointItem?) { self.punchPointId = punchPointId self.summary = summary super.init(nibName: nil, bundle: nil) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { title = summary?.name ?? "打卡点详情" navigationItem.rightBarButtonItem = UIBarButtonItem( title: "二维码", style: .plain, target: self, action: #selector(showQR) ) super.viewDidLoad() viewModel.onChange = { [weak self] in self?.title = self?.viewModel.selectedDetail?.name ?? self?.summary?.name self?.reloadTable() } } override func tableRowCount() -> Int { guard viewModel.selectedDetail != nil || summary != nil else { return 0 } return 4 } override func configureCell(_ cell: TitleSubtitleTableViewCell, at indexPath: IndexPath) { let detail = viewModel.selectedDetail ?? summary guard let detail else { return } switch indexPath.row { case 0: cell.configure(title: "名称", subtitle: detail.name) case 1: cell.configure(title: "地址", subtitle: detail.displayAddress) case 2: cell.configure(title: "状态", subtitle: detail.statusLabel) default: cell.configure(title: "创建时间", subtitle: detail.createdAt) } } override func reloadContent() async { await viewModel.loadDetail(id: punchPointId, api: services.punchPointAPI) } @objc private func showQR() { guard let detail = viewModel.selectedDetail ?? summary else { return } navigationController?.pushViewController( PunchPointQRViewController(title: detail.name, qrURL: detail.mpQrcode), animated: true ) } } /// 打卡点编辑页。 final class PunchPointEditorViewController: ModuleTableViewController { private let viewModel = PunchPointEditorViewModel() private let nameField = UITextField() private let punchPointId: Int? init(punchPointId: Int?) { self.punchPointId = punchPointId super.init(nibName: nil, bundle: nil) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { title = punchPointId == nil ? "新建打卡点" : "编辑打卡点" navigationItem.rightBarButtonItem = UIBarButtonItem( title: "保存", style: .done, target: self, action: #selector(save) ) super.viewDidLoad() nameField.placeholder = "打卡点名称" nameField.borderStyle = .roundedRect nameField.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 52) tableView.tableHeaderView = nameField wireViewModel(viewModel) { [weak self] in if self?.nameField.text?.isEmpty != false { self?.nameField.text = self?.viewModel.name } } } override func tableRowCount() -> Int { 1 } override func configureCell(_ cell: TitleSubtitleTableViewCell, at indexPath: IndexPath) { cell.configure(title: "地址", subtitle: viewModel.address) } override func reloadContent() async { guard let punchPointId else { return } if let detail = try? await services.punchPointAPI.punchPointInfo(id: punchPointId) { viewModel.apply(detail) nameField.text = viewModel.name } } @objc private func save() { viewModel.name = nameField.text ?? "" Task { let success = await viewModel.submit( scenicId: services.currentScenicId, api: services.punchPointAPI, uploadService: services.ossUploadService ) if success { navigationController?.popViewController(animated: true) } } } } extension PunchPointEditorViewModel: ViewModelBindable {} /// 打卡点二维码页。 final class PunchPointQRViewController: UIViewController { private let pageTitle: String private let qrURL: String init(title: String, qrURL: String) { pageTitle = title self.qrURL = qrURL super.init(nibName: nil, bundle: nil) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() title = pageTitle view.backgroundColor = UIColor(hex: 0xF5F7FA) let urlLabel = UILabel() urlLabel.text = qrURL urlLabel.numberOfLines = 0 urlLabel.textAlignment = .center urlLabel.textColor = AppDesign.textSecondary view.addSubview(urlLabel) urlLabel.snp.makeConstraints { make in make.center.equalToSuperview() make.leading.trailing.equalToSuperview().inset(24) } } }