//
//  GongdanpiciTool.swift
//  OrderScheduling
//
//  Created by 中道 on 2026/3/4.
//

import Foundation
import RxSwift
import PopupDialog

class GongdanpiciTool : NSObject {
    public static let `default` = GongdanpiciTool()
    private let disposeBag = DisposeBag()
    
    func selectCountBySupplierId() {
        guard let supplierId = UserData.default.supplierId else {
            return
        }

        RQ.selectCountBySupplierId(parameters: SelectCountBySupplierIdPrameters(supplierId: supplierId,remindApp: 1,userId: UserData.default.userId))
            .observe(on: MainScheduler.instance)
            .subscribe(onSuccess: { selectCountModel in
                if selectCountModel?.success == true {
                    if let count = selectCountModel?.data, count > 0 {
                        let popup = PopupDialog(title: nil, message: "工单批次中有\(count)条【未生成财务批次】数据，请及时开票。",buttonAlignment: .horizontal,transitionStyle: .zoomIn,tapGestureDismissal: false,
                                                panGestureDismissal: false,
                                                hideStatusBar: false){

                        }
                        let vc = popup.viewController as! PopupDialogDefaultViewController
                        vc.titleColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
                        vc.messageColor = UIColor(red: 0.13, green: 0.19, blue: 0.33, alpha: 1)
                        vc.titleFont = UIFont.boldSystemFont(ofSize: 16)
                        vc.messageFont = UIFont.systemFont(ofSize: 16)
                        
                        vc.titleText = "提示"
                        vc.titleFont = UIFont.boldSystemFont(ofSize: 18)
                        let buttonOne = CancelButton(title: "稍后处理") {
                        }
                        buttonOne.titleFont = UIFont.boldSystemFont(ofSize: 16)
                        buttonOne.titleColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.7)
                        
                        let buttonTwo = DefaultButton(title: "去处理", dismissOnTap: true) {
                            if let supplierId = UserData.default.supplierId {
                                WEBTOOL.open(name: .invoicingNotify, appending: "&supplierId=\(supplierId)")
                            }
                        }
                        buttonTwo.titleFont = UIFont.boldSystemFont(ofSize: 16)
                        buttonTwo.titleColor = UIColor(red: 29/255.0, green: 100/255.0, blue: 210/255.0, alpha: 0.7)

                        popup.addButtons([buttonOne, buttonTwo])
                        let tabBarVc = UIApplication.shared.dd_keyWindow.rootViewController as? UITabBarController
                        let nav = tabBarVc?.selectedViewController as? UINavigationController
                        nav?.topViewController?.present(popup, animated: true)
                    }
                }else{
                    let tabBarVc = UIApplication.shared.dd_keyWindow.rootViewController as? UITabBarController
                    let nav = tabBarVc?.selectedViewController as? UINavigationController
                    nav?.topViewController?.view.dd_makeToast(selectCountModel?.msg)
                }
            })
            .disposed(by: disposeBag)
    }

    func refreshCount() {
        UserDefaults.standard.set(0, forKey: "selectCountBySupplierId")
    }
}
