//
//  UpcomingListVC.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/22.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class UpcomingListVC: BaseViewController {

    lazy var tixingImage: UIImageView = {
        let tixingImage = UIImageView.init()
        tixingImage.frame = CGRect(x:(SCREEN_WHIDTH()-70)/2, y: 150+STATUBAR_HEIGHT()+44, width:70 , height:70 )
        tixingImage.image = UIImage.init(named: "advisory_tixing")
        return tixingImage
    }()
    lazy var tixingLabel: UILabel = {
        let tixingLabel = UILabel.init()
        tixingLabel.textAlignment = .center
        tixingLabel.font = UIFont.systemFont(ofSize: 17)
        tixingLabel.text = "暂无数据"
        tixingLabel.numberOfLines = 0
        tixingLabel.textColor = UIColorFromRGB(rgbValue: 0x23262A)
        return tixingLabel
    }()
    lazy var headView:UpcomingListHeadView = {
        let headView = UpcomingListHeadView.init(frame: CGRect(x:0, y: 0, width:SCREEN_WHIDTH() , height:30 ))
        return headView
    }()
    lazy var myTableView: UITableView = {
        let myTableView = UITableView.init()
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        registerCell(myTableView, cell: UpcomingListTableViewCell.self)
        myTableView.separatorStyle = .none
        myTableView.backgroundColor = UIColorFromRGB(rgbValue: 0xF4F9FD)
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    var dataSource:NSMutableArray!
    var delegate:UpcomingListVCDelegate!
    var upcomingType:BusinessType = .UpcomingType
    var httpListData = UpcomingListModel()
    var appModel:ApplicationHttpListModel!
    
    //顶部刷新
    var header = MJRefreshNormalHeader()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    var page = 1
    var titleType:String!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if myTableView != nil{
            dataSource = NSMutableArray()
            self.page = 1
            actionHttpGCD()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge.bottom
        initTitleBar()
        
        addInitTableView()
        addTitleView()
        
        addHeaderFooter()
        // Do any additional setup after loading the view.
    }
    //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)
//        }
        if self.httpListData.list.count < 10 {
            self.myTableView.mj_footer.endRefreshing()
            self.footer.setTitle("已经到底啦", for: .idle)
        } else {
            self.footer.setTitle("", for: .idle)
            actionHttpGCD()
        }
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
//            self?.getMyUpcomingListHttpPostCmd()
            self?.getUpcomingNewListPostCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.initSetDataNew(httpArray: self?.httpListData.list ?? NSMutableArray())
            if self?.dataSource.count == 0 {
                self?.tixingImage.isHidden = false
                self?.tixingLabel.isHidden = false
            } else {
                self?.tixingImage.isHidden = true
                self?.tixingLabel.isHidden = true
            }
            self?.headView.numStr = self?.httpListData.total
            self?.delegate.dataSource = self?.dataSource
            self?.myTableView.reloadData()
            self?.myTableView.mj_footer.endRefreshing()
            self?.myTableView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }

    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
}
