//
//  YHGCMineSchemeViewController.swift
//  galaxy
//
//  Created by alexzzw on 2024/11/12.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHGCMineSchemeViewController: YHBaseViewController {
    var tableView: UITableView!
    var dataSource: [YHScemeItemModel]? = []

    lazy var reqSchemeVM: YHGCMySchemeViewModel = {
        let vm = YHGCMySchemeViewModel()
        return vm
    }()

    lazy var headView: YHGCSchemeTableHeadView = {
        let headView = YHGCSchemeTableHeadView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 422))
        headView.update(type: .typeA, name: "--")
        return headView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        gk_navTitle = "我的方案"
        gk_navTitleColor = .white
        gk_navBarAlpha = 1
        gk_navBackgroundImage = UIImage(named: "my_scheme_nav")
        gk_backImage = UIImage(named: "nav_icon_back_white")
        setView()
        loadData()
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

extension YHGCMineSchemeViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let dataSource = dataSource, dataSource.count > indexPath.row else {
            return UITableViewCell()
        }
        let cell = tableView.dequeueReusableCell(withClass: YHGCSchemeTableViewCell.self)
        let model = dataSource[indexPath.row]
        cell.dataSource = model
        cell.schemeBlock = {
            /*
            if let orderID = UserDefaults.standard.value(forKey: "orderIdForPreview"), model.title == "资料清单" {
                let vc = YHMineCertificateEntryViewController()
                vc.orderId = orderID as? Int ?? 0
                UIViewController.current?.navigationController?.pushViewController(vc)
            }
            */
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 199
        } else if indexPath.row == 1 {
            return 251
        } else if indexPath.row == 2 {
            return 190
        }
        return 0
    }
}

extension YHGCMineSchemeViewController {
    func getData() {
        let model1 = YHScemeItemModel(title: "职业界别", mainMessage: "根据您目前给来的材料，初步建议您申请的行业：\n商业及贸易\n", lightMessage: "\n商业及贸易\n", subMessage: "后续会根据您文书准备的补充情况来最终确定，如有修改会再告知您。")
        let model2 = YHScemeItemModel(title: "资料清单", buttonTitle: "资料清单", mainMessage: "这是您的资料清单，请您前往查看。请您在1-2周内上传基础类证件哦，需要重新办理的可以晚些提供。需要注意的点如下：", lightMessage: "1-2周内", subMessage: "(1)港澳通如未办理，请尽快办理好反馈过来；\n(2)如为国内学校，需要尽快办理学位认证报告；\n(3)如为海外学校，需提供成绩单副本")
        let model3 = YHScemeItemModel(title: "文书清单", buttonTitle: "文书写作", mainMessage: "这是您的文书清单，包括推荐信、赴港计划书，我写好后会发在微信里，与您一起沟通进行哈。3周-4周左右完成，需要咱们共同配合完成的噢", lightMessage: "3周-4周")
        dataSource = [model1, model2, model3]
        tableView.reloadData()
    }

    func updateDataSource() {
    }

    func setView() {
        view.backgroundColor = .contentBkgColor
        tableView = {
            let tableView = UITableView(frame: .zero, style: .plain)
            if #available(iOS 11.0, *) {
                tableView.contentInsetAdjustmentBehavior = .never
            }
            tableView.backgroundColor = .clear
            tableView.separatorStyle = .none
            tableView.delegate = self
            tableView.dataSource = self
            tableView.tableFooterView = YHSchemeTableFooterView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 112))

            tableView.tableHeaderView = headView
            tableView.register(cellWithClass: YHGCSchemeTableViewCell.self)
            tableView.bounces = false
            return tableView
        }()
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.left.right.bottom.equalToSuperview()
        }
    }

    func loadData() {
        if let orderID = UserDefaults.standard.value(forKey: "orderIdForPreview") {
            let param = ["order_id": orderID]
            reqSchemeVM.getMySchemeData(params: param) { [weak self] success, error in
                guard let self = self else {
                    return
                }
                if success == true {
                    guard let model = self.reqSchemeVM.schemeModel else { return }

                    let industry = model.industry.defaultStringIfEmpty()
                    let model1 = YHScemeItemModel(title: "职业界别", mainMessage: "根据您目前给来的材料，初步建议您申请的行业：" + industry, lightMessage: industry, subMessage: "后续会根据您文书准备的补充情况来最终确定，如有修改会再告知您。", lightFont: UIFont.PFSC_B(ofSize: 16), hideTopLine: true)
                    let contentStr = model.content.reduce("") { partialResult, current in
                        partialResult + "\n" + current
                    }
                    let model2 = YHScemeItemModel(title: "资料清单", buttonTitle: "资料清单", mainMessage: "这是您的资料清单，请您前往查看。请您在1-2周内上传基础类证件哦，需要重新办理的可以晚些提供。需要注意的点如下：", lightMessage: "1-2周内", subMessage: "按照您填报的学历信息， 证明文件如下：\(contentStr)")
                    self.dataSource = [model1, model2]
                    self.tableView.reloadData()
                    let type = YHGCApplicationType(rawValue: model.application_type) ?? .typeA
                    self.headView.update(type: type, name: model.username.defaultStringIfEmpty())
                } else {
                    YHHUD.flash(message: error?.errorMsg ?? "请求出错")
                }
            }
        } else {
            printLog("error : orderID 为空")
        }
    }
}
