//
//  DepositOrderDetailViewController.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 店铺多点位旅拍订单详情。
final class DepositOrderDetailViewController: BaseViewController, UITableViewDataSource {

    private let orderNumber: String
    private let orderAPI: OrderAPI
    private var detail: StoreOrderDetailData?
    private let tableView = UITableView(frame: .zero, style: .insetGrouped)

    init(orderNumber: String, orderAPI: OrderAPI? = nil) {
        self.orderNumber = orderNumber
        self.orderAPI = orderAPI ?? NetworkServices.shared.orderAPI
        super.init(nibName: nil, bundle: nil)
    }

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

    override func setupNavigationBar() {
        title = "订单详情"
    }

    override func setupUI() {
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.snp.makeConstraints { $0.edges.equalToSuperview() }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        Task { await loadDetail() }
    }

    private func loadDetail() async {
        let storeId = AppStore.shared.session.currentStoreId
        guard storeId > 0 else {
            showToast("店铺信息异常")
            return
        }
        showLoading()
        defer { hideLoading() }
        do {
            detail = try await orderAPI.storeOrderDetail(storeId: storeId, orderNumber: orderNumber)
            tableView.reloadData()
        } catch {
            showToast(error.localizedDescription)
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int { 2 }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 { return 6 }
        return detail?.multiTravel?.shootingList.count ?? 0
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        section == 0 ? "基本信息" : "拍摄点位"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
        guard let detail else { return cell }
        if indexPath.section == 0 {
            switch indexPath.row {
            case 0: cell.textLabel?.text = "订单号"; cell.detailTextLabel?.text = detail.orderNumber
            case 1: cell.textLabel?.text = "项目"; cell.detailTextLabel?.text = detail.projectName
            case 2: cell.textLabel?.text = "状态"; cell.detailTextLabel?.text = detail.orderStatusName
            case 3: cell.textLabel?.text = "金额"; cell.detailTextLabel?.text = "¥\(detail.actualPayAmount)"
            case 4: cell.textLabel?.text = "手机号"; cell.detailTextLabel?.text = OrderPhoneMask.mask(detail.phone)
            default: cell.textLabel?.text = "创建时间"; cell.detailTextLabel?.text = detail.createdAt
            }
        } else if let item = detail.multiTravel?.shootingList[indexPath.row] {
            cell.textLabel?.text = item.scenicSpotName
            cell.detailTextLabel?.text = item.displayStaffName
        }
        return cell
    }
}
