//
//  UpcomingViewController.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2020/2/10.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit
/// 事务类型
enum BusinessType: String{
    ///待办
    case UpcomingType = "待办"
    ///已办
    case DoneType = "已办"
}
/// 统一代办 数据类型
enum UnifiedAgencyType: String{
    ///待办
    case UpcomingUnifiedAgencyType = "1"
    ///已办
    case DoneUnifiedAgencyType = "2"
}
class UpcomingViewController: BaseViewController {

    
    lazy var tixingImage: UIImageView = {
        let tixingImage = UIImageView.init()
        tixingImage.frame = CGRect(x:0, y: 0, width:SCREEN_WHIDTH() , height:SCREEN_HEIGHT() )
        tixingImage.image = UIImage.init(named: "upcoming_image")
        tixingImage.contentMode = .scaleAspectFit
        tixingImage.isHidden = true
        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.isHidden = true
        tixingLabel.textColor = UIColorFromRGB(rgbValue: 0x23262A)
        return tixingLabel
    }()
    
    
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        btnViewWidth = SCREEN_WHIDTH()/CGFloat(cellNum)
        btnViewHeight = btnViewWidth*0.9
        //self.dataSource = xinfo.detaileArray
        //最小行间距
        layout.minimumLineSpacing = 0
        //设置垂直间距
        layout.minimumInteritemSpacing = 0
        //设置 cell 上下左右的间距
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width:btnViewWidth, height:btnViewHeight)
        //layout.headerReferenceSize = CGSize(width: SCREEN_WHIDTH(), height: 26*HEIGHT_RATIO())
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
            collectionView.scrollIndicatorInsets = collectionView.contentInset
            collectionView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, HEIGHT_TAB_BAR()+HEIGHT_BOTTOM_MARGIN(), 0)
        }else{
            collectionView.contentInset = UIEdgeInsetsMake(44+UIApplication.shared.statusBarFrame.size.height, 0, 0, 0)
        }
        collectionView.collectionViewLayout = layout
        registerCollectinViewCell(collectionView, cell: UpcomingCollectionViewCell.self)
        registerCollectinViewCell(collectionView, cell: MineCollectionViewCell.self)
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    var dataSource:NSMutableArray!
    var delegate = UpcomingViewControllerDelegate()
    var layout = UICollectionViewFlowLayout()
    var cellNum:Double!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    var businessType:BusinessType = .UpcomingType
    
    //顶部刷新
    var header = MJRefreshNormalHeader()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    var httpListData = ServiceHttpListModelData()
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
    var selecClassModel:ApplicationHttpListModel!
    /// 待办数目 数组
    var upcomingCountData = NSMutableArray()
    ///获取待办的 key
    var upcomingKey = String()
    var httpArray = NSMutableArray()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.businessType == .UpcomingType {
            self.wr_setNavBarShadowImageHidden(true)
            getUpcomingCountHttpPostCmd()
        }
        self.appViewsSstatistics(title: self.businessType.rawValue, scope: AppClickType.OftenAppType.rawValue)
        self.wr_setNavBarBackgroundAlpha(1)
        actionHttpGCD()
        self.getUnReadNumberHttpPostCmd()
//        addNotificationCenter()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        NotificationCenter.default.removeObserver(self)
//        addNotificationCenter()
        addRemoveObserver()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initTitleBar()
        initDragCollectionView()
        addTitleView()
//        actionHttpGCD()
        addHeaderFooter()
        
    }
    //左侧返回按钮
    override func initReturnBtn(){
        if self.businessType == .DoneType {
            let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "return_image")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(arrowResponse))
            self.navigationItem.leftBarButtonItem = leftN
            return
        }
        let titleBtn:UIButton = UIButton.init(type: .custom)
        titleBtn.frame = CGRect(x:0, y: 0, width:36 , height:36)
        titleBtn.contentMode = .scaleAspectFit
        titleBtn.setTitle("我的待办", for: .normal)
        titleBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20.67)
        titleBtn.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 20.67)
        titleBtn.setTitleColor(UIColorFromRGB(rgbValue: 0x23262A), for: .normal)
        let messageN:UIBarButtonItem = UIBarButtonItem.init(customView: titleBtn)
        self.navigationItem.leftBarButtonItem = messageN
        
    }
    //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.collectionView.mj_header = self.header
        //self.collectionView.mj_footer = self.footer
    }
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        actionHttpGCD()
        self.tixingImage.isHidden = true
        self.tixingLabel.isHidden = true
    }
    //MARK:上拉加载
    @objc func downPlullLoadData(){
        
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getAllApplicationHttpListCmd()
        })
//        queue.async(group: group, execute: {[weak self] in
//            self?.group.enter()
//            self?.getUpcomingCountHttp()
//        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.setBiaoji()
            self?.delegate.dataSource = self?.dataSource
            if self?.dataSource.count == 0 {
                self?.tixingImage.isHidden = false
                self?.tixingLabel.isHidden = false
            } else {
                self?.tixingImage.isHidden = true
                self?.tixingLabel.isHidden = true
            }
            self?.collectionView.delegate = self?.delegate
            self?.collectionView.dataSource = self?.delegate
            self?.collectionView.reloadData()
            self?.collectionView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }
    /// 设置红色数目标记
    func setBiaoji(){
//        for index in 0..<self.upcomingCountData.count {
//            let model:UpcomingNewCountModel = self.upcomingCountData[index] as! UpcomingNewCountModel
//            for i in 0..<self.httpArray.count {
//                let cellModel:UpcomingCollectionViewCellModel = self.httpArray[i] as! UpcomingCollectionViewCellModel
//                if (cellModel.key == model.key) {
//                    cellModel.biaojiNum = numBigData(numStr: model.count)
//                }
//                self.dataSource.add(cellModel)
//            }
//        }
        self.dataSource = []
        for index in 0..<self.httpArray.count {
            let cellModel:UpcomingCollectionViewCellModel = self.httpArray[index] as! UpcomingCollectionViewCellModel
            for i in 0..<self.upcomingCountData.count {
                let model:UpcomingNewCountModel = self.upcomingCountData[i] as! UpcomingNewCountModel
                if (cellModel.key == model.key) {
                    cellModel.biaojiNum = numBigData(numStr: model.count)
                }
            }
            self.dataSource.add(cellModel)
        }
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
    override func gotoIntelligentCustomerService(){
        self.getApplicationByBelongAction(applicationName: "IT_ROBOT")
    }
}
