//
//  MineInfoViewController.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/11.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class MineInfoViewController: BaseViewController,WXAuthDelegate,WXApiDelegate {
    
    lazy var myTableView: UITableView = {
        let myTableView = UITableView.init()
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, 0, 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        registerCell(myTableView, cell: MineInfoTableViewCell.self)
        registerCell(myTableView, cell: NormalTableViewCell.self)
        registerCell(myTableView, cell: MineInfoHeadTableViewCell.self)
        registerCell(myTableView, cell: SignOutTableViewCell.self)
//        myTableView.separatorStyle = .none
        myTableView.backgroundColor = UIColorFromRGB(rgbValue: 0xF4F9FD)
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    var dataSource:NSMutableArray!
    var delegate:MineInfoViewControllerDelegate!
    var inforHttpModel = MyInformationHttpModel()
    //顶部刷新
    var header = MJRefreshNormalHeader()
    var signOutButton:UIButton!
    
    var signOutBlock:selectBlock!
    
    var httpViewModel = WeChatViewModel()

   
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        self.view.backgroundColor = UIColorFromRGB(rgbValue: 0xF4F9FD)
        self.title = "个人信息"
        //addRightN()
        self.wr_setNavBarBackgroundAlpha(1)
        addInitTableView()
        actionHttpGCD()
        addHeaderFooter()
//        addInitLoginButton()
//        wr_setDefaultNavBarShadowImageHidden
        self.wr_setNavBarShadowImageHidden(false)
        // Do any additional setup after loading the view.
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.view.removeFromSuperview()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK:添加上拉加载 下拉刷新
    func addHeaderFooter(){
        self.header.setRefreshingTarget(self, refreshingAction: #selector(upPullLoadData))
        self.header.lastUpdatedTimeLabel.isHidden = true
        self.header.stateLabel.isHidden = true
        //self.footer.setRefreshingTarget(self, refreshingAction: #selector(downPlullLoadData))
        // self.footer.stateLabel.isHidden = true
        //self.footer.isRefreshingTitleHidden = true
        self.myTableView.mj_header = self.header
        //self.myTableView.mj_footer = self.footer
    }
    
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        
        actionHttpGCD()
        
    }
    func addInitLoginButton() {
        signOutButton = UIButton.init(type: .custom)
        signOutButton.frame = CGRect(x:10*WHIDTH_RATIO(), y: SCREEN_HEIGHT()-HEIGHT_RATIO()*250, width:SCREEN_WHIDTH()-20*WHIDTH_RATIO() , height:WHIDTH_RATIO()*40)
        //loginButton.centerX = SCREEN_WHIDTH()/2
        signOutButton.setBackgroundImage(UIImage.init(named: "loginBtn_image"), for: .normal)
        signOutButton.setTitle("退出登录", for: .normal)
        signOutButton.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
        signOutButton.addTarget(self, action: #selector(signOutButtonAction), for: .touchUpInside)
        //signOutButton.backgroundColor = UIColor.red
        self.myTableView .addSubview(signOutButton)
    }
    @objc func signOutButtonAction(){
//        showOneAlert(title: "版本更新", message: ("请更新到"+model.versionNumber!), titleSecondBtn: "确定", blockOtherBtn: {[weak self] in
//            self?.openUrl(urlString: model.downloadLink!)
//        })
        showAlert(title: "提示", message: "确定要退出APP？", titleCancelBtn: "取消", titleSecondBtn: "确定") {[weak self] in
            AccountInfo.sharedInstance().clearAccount()
            self?.deAllSelecCompanyDepartment()
            NotificationCenter.default.post(name: Notification.Name(rawValue: "setLoginWindow"), object: "0")
        }
    }
    
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyInformationHttpGetCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.delegate.dataSource = self?.dataSource
            self?.myTableView.delegate = self?.delegate
            self?.myTableView.dataSource = self?.delegate
            self?.myTableView.reloadData()
            self?.myTableView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .NOIsHomePushType)
    }
}
