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

import UIKit

class AdvisoryViewController: BaseViewController {

    @IBOutlet weak var tixingLabel: UILabel!
    @IBOutlet weak var tixingImage: UIImageView!
    @IBOutlet weak var myTableView: UITableView!
    
    var dataSource:NSMutableArray!
    
    var delegate:AdvisoryViewControllerDelegate!
//    var sequenPageType:SequenPageType = .CompanyNews
    var httpListData = AdvisoryHttpListModelData()
    var type = String()
    
    
    //顶部刷新
    var header = MJRefreshNormalHeader()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    var page = 1
    
    
    class func createViewController(createArgs:AnyObject?) ->AnyObject{
        let storyboard:UIStoryboard=UIStoryboard(name: "AdvisoryViewController", bundle: nil)
        let vc:AdvisoryViewController=storyboard.instantiateViewController(withIdentifier: "AdvisoryViewController") as! AdvisoryViewController
        vc.createArgs=createArgs
        return vc
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        initReturnBtn()
        self.tixingImage.isHidden = true
        self.tixingLabel.isHidden = true
        self.dataSource = NSMutableArray()
        actionHttpGCD()
        addInitTableView()
        addHeaderFooter()
        addRemoveObserver()
        // Do any additional setup after loading the view.
    }
    override func addNotificationCenter() {
        
    }
    //左侧返回按钮
    override func initReturnBtn(){
        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: ""), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
    }
    override func arrowResponse(){
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func actionCustomerServiceVC(){

    }
    //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 = false
        self.footer.setTitle("", for: .idle)
        self.footer.isRefreshingTitleHidden = true
        self.myTableView.mj_header = self.header
        self.myTableView.mj_footer = self.footer
    }
    
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        self.page = 1
        self.tixingImage.isHidden = true
        self.tixingLabel.isHidden = true
        actionHttpGCD()
//        self.myTableView.reloadData()
    }
    //MARK:上拉加载
    @objc func downPlullLoadData(){
        if self.httpListData.isLastPage == "0" {
            self.footer.setTitle("", for: .idle)
            actionHttpGCD()
//            self.myTableView.reloadData()
        }else{
            self.myTableView.mj_footer.endRefreshing()
            self.footer.setTitle("已经到底啦", for: .idle)
        }
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getAdvisoryHttpCmd()
        })
        
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.initSetData(httpArray: self?.httpListData.httpModelArray ?? NSMutableArray())
            if self?.dataSource.count == 0 {
                self?.tixingImage.isHidden = false
                self?.tixingLabel.isHidden = false
            }
//            self?.delegate.dataSource = self?.dataSource
//            self?.myTableView.delegate = self?.delegate
//            self?.myTableView.dataSource = self?.delegate
            self?.myTableView.reloadData()
            self?.myTableView.mj_footer.endRefreshing()
            self?.myTableView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }
    
}
