Newer
Older
LaserMethane / LaserMethane / ViewController / MainMenu / MainMenuViewController.swift
//
//  MainMenuViewController.swift
//  LaserMethane
//
//  Created by 203 on 2021/7/28.
//

import Alamofire
import DefaultsKit
import SwiftyJSON
import UIKit

class MainMenuViewController: UIViewController {
    @IBOutlet var inspectView: UIView!
    @IBOutlet var logSearchView: UIView!
    @IBOutlet var settingsView: UIView!

    private let defaults = Defaults.shared
    private var isSingleMode = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置导航栏背景和标题
        title = "主菜单"
        navigationController?.navigationBar.isTranslucent = false // 顶部导航不透明可见
        navigationController?.navigationBar.barTintColor = .systemBlue
        let dict: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = dict // title color

        isSingleMode = defaults.get(for: singleModeKey) ?? false
        if !isSingleMode {
            // 如果不是单机模式登录,则获取用户信息
            obtainUserInfo()
        }
        // 设置背景以及圆角
        initViewBackground()
    }

    func obtainUserInfo() {
        let token = defaults.get(for: tokenKey)!
        let obtainUserInfoURL = defaults.get(for: serverConfigKey)! + Constant.userInfo.rawValue
        Alamofire.request(obtainUserInfoURL, method: .get, headers: ["token": token]).responseJSON { [self] response in
            switch response.result {
            case let .success(value):
                let userModel = UserInfoModel(respJson: JSON(value))
                if userModel.code == 200 {
                    let userData = userModel.data!
                    // 将用户信息存起来
                    defaults.set(UserDataCacheModel(account: userData.account,
                            deptId: userData.deptId, deptName: userData.deptName, id: userData.id,
                            ipAddr: userData.ipAddr, name: userData.name, phone: userData.phone), for: userDataCacheModelKey)
                } else {
                    // 重新获取用户信息
                    reloadUserInfo()
                }
            case .failure:
                // 重新获取用户信息
                reloadUserInfo()
            }
        }
    }

    func reloadUserInfo() {
        let alertController = UIAlertController(title: "温馨提示", message: "获取用户信息失败,是否重试?", preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "是", style: .default, handler: { [self] _ in
            // 获取用户信息
            obtainUserInfo()
        })
        let cancelAction = UIAlertAction(title: "否", style: .default, handler: { [self] _ in
            dismiss(animated: true, completion: nil)
        })
        cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
        alertController.addAction(cancelAction)
        alertController.addAction(confirmAction)
        present(alertController, animated: true, completion: nil)
    }

    // 新建巡检
    @IBAction func toCreateInspectView(_ sender: UIButton) {
        if isSingleMode {
            navigationController?.pushViewController(SingModeMapViewController(), animated: true)
        } else {
            navigationController?.pushViewController(MapViewController(), animated: true)
        }
    }

    // 记录查询
    @IBAction func toSearchLogView(_ sender: UIButton) {
        // 弹框选择
        let selectController = UIAlertController(title: "选择记录类型", message: nil, preferredStyle: .alert)
        let inspectButton = UIAlertAction(title: "巡检记录", style: .default, handler: { [self] _ in
            if isSingleMode {
                navigationController?.pushViewController(SingModeInspectViewController(), animated: true)
            } else {
                navigationController?.pushViewController(InspectViewController(), animated: true)
            }
        })
        let eventLogButton = UIAlertAction(title: "事件记录", style: .default, handler: { [self] _ in
            if isSingleMode {
                navigationController?.pushViewController(SingModeEventViewController(), animated: true)
            } else {
                navigationController?.pushViewController(EventViewController(), animated: true)
            }
        })
        selectController.addAction(inspectButton)
        selectController.addAction(eventLogButton)
        present(selectController, animated: true, completion: nil)
    }

    // 系统设置
    @IBAction func toUserSettingsView(_ sender: UIButton) {
        if isSingleMode {
            navigationController?.pushViewController(SingModeSettingViewController(), animated: true)
        } else {
            navigationController?.pushViewController(SettingViewController(), animated: true)
        }
    }

    func initViewBackground() {
        LayerShadowHub.shared.setShadow(view: inspectView, sColor: [211, 211, 211].transferUIColor(), offset: CGSize(width: 0, height: 0), alpha: 1.0, radius: CGFloat(5.0))

        LayerShadowHub.shared.setShadow(view: logSearchView, sColor: [211, 211, 211].transferUIColor(), offset: CGSize(width: 0, height: 0), alpha: 1.0, radius: CGFloat(5.0))

        LayerShadowHub.shared.setShadow(view: settingsView, sColor: [211, 211, 211].transferUIColor(), offset: CGSize(width: 0, height: 0), alpha: 1.0, radius: CGFloat(5.0))
    }
}