ReportViewController.swift 7.8 KB
//
//  ReportViewController.swift
//  ParentAssistant
//
//  Created by 葛建军 on 2018/3/5.
//  Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//

import UIKit

class ReportViewController: UIViewController {
    var titleView:ReportHeaderView!//navigationBar自定义view
    @IBOutlet weak var reportTable: UITableView!
    @IBOutlet var dateViewHeight: NSLayoutConstraint!//顶部固定日期的高度
    @IBOutlet var calendarView: UIView!
    @IBOutlet weak var EmptyView: UIView!
    var dateView:LXCalendarView!
    var dateAry:(weeks:[String],days:[String]) = ([],[])
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //MARK: - 空白页面
        self.EmptyView.isHidden = true
        
        if !self.EmptyView.isHidden {
            //设置顶部按钮
            setupNavigationBar()
            //设置日期页面
            setupDateView()
        }
    }
    // MARK: - 设置顶部按钮
    func setupNavigationBar(){
        self.navigationController?.navigationBar.barStyle = UIBarStyle.default// UIColorFromRGB(0xC5DAFF)
        let backImage = UIImage(named: "navigationBar_backgrounImage")
        self.navigationController?.navigationBar.setBackgroundImage(backImage, for: UIBarMetrics.default)
        self.configTheme()
        self.navigationItem.title = "报告"
        setuptitleView()
        
    }
    func setuptitleView(){
        // MARK: - 设置顶部的titleView
        titleView = Bundle.main.loadNibNamed("ReportHeaderView", owner: nil, options: nil)![0] as! ReportHeaderView
        titleView.delegate = self
        titleView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 44)
        self.navigationItem.titleView = titleView
        titleView.layoutSubviews()
        //navigation左上角显示名称 家长未绑定孩子
        if AccountManager.shared.isOnline() {
            if AccountManager.shared.rawUserInfo!.contentData()["studentClass"].arrayValue.count == 0 {
                titleView.nameButton.setTitle("未绑定", for: UIControlState.normal)
            }else{
                titleView.nameButton.setTitle(AccountManager.shared.rawUserInfo!.contentData()["studentClass"][0]["studentName"].stringValue, for: UIControlState.normal)
            }
        }else{
            titleView.nameButton.setTitle("未登录", for: UIControlState.normal)
        }
    }
    // MARK: - 设置日期页面
    func setupDateView(){
        titleView.dateButton.setTitle("\(getCurrentDate())", for: UIControlState.normal)
        dateViewHeight.constant = getScreenWidth()/7
        //添加顶部固定一周日期
        dateAry = getCurrentWeeks()
        //添加日历
        dateView = LXCalendarView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth/2))
        dateView.isHaveAnimation = true
        dateView.isCanScroll = true
        dateView.isShowLastAndNextBtn = false
        dateView.isShowLastAndNextDate = true
        
        dateView.todayTitleColor = UIColor.red
        dateView.selectBackColor = UIColorFromRGB(0xC5DAFF)
        dateView.dealData()
        dateView.backgroundColor = UIColor.white
        calendarView.addSubview(dateView)
        dateView.selectBlock = { (year:NSInteger,month:NSInteger,day:NSInteger) -> () in
            self.calendarView.isHidden = true
            self.titleView.dateButton.setTitle("\(month)月\(day)日", for: UIControlState.normal)
            //选择日期后应隐藏日历并刷新界面
            appDelegate.window!.makeToast("\(year)年\(month)月\(day)日", duration: 2, position: CSToastPositionCenter)
            
        }
    }
    // MARK: - 页面将要显示时替换navigation的titleView
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.calendarView.isHidden = true
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if titleView != nil {
            titleView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 44)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}
// MARK: - 点击顶部titleView按钮的代理方法
extension ReportViewController: ReportHeaderViewDelegate{
    //点击孩子姓名
    func selectStudentButton() {
        appDelegate.window!.makeToast("该功能暂不开放", duration: 1, position: CSToastPositionCenter)
        return
        //如果家长未绑定孩子
        if AccountManager.shared.isOnline() {
            if AccountManager.shared.rawUserInfo!.contentData()["studentClass"].arrayValue.count == 0 {
                appDelegate.window!.makeToast("请先绑定孩子", duration: 1, position: CSToastPositionCenter)
            }else{
                //切换孩子
                let alert = UIAlertController(title: "切换孩子", message: "", preferredStyle: UIAlertControllerStyle.actionSheet)
                for item in AccountManager.shared.rawUserInfo!.contentData()["studentClass"].arrayValue {
                    let action = UIAlertAction(title: item["studentName"].stringValue, style: UIAlertActionStyle.default, handler: { (action) in
                        //选择完孩子切换数据
                    })
                    alert.addAction(action)
                }
                self.present(alert, animated: true, completion: nil)
            }
        }else{
            appDelegate.window!.makeToast("请先登录", duration: 1, position: CSToastPositionCenter)
        }
    }
    //点击日期
    func selectDateButton() {
        
        appDelegate.window!.makeToast("该功能暂不开放", duration: 1, position: CSToastPositionCenter)
        return
        
        let isHidden = calendarView.isHidden
        if isHidden {
            calendarView.isHidden = false
        }else{
            calendarView.isHidden = true
        }
    }
    //点击筛选
    func selectTypeButton() {
        appDelegate.window!.makeToast("该功能暂不开放", duration: 1, position: CSToastPositionCenter)
        return
    }
}
// MARK: - 顶部展示日期
extension ReportViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: getScreenWidth()/7, height: getScreenWidth()/7)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReportDateCollectionViewCell", for: indexPath) as! ReportDateCollectionViewCell
        cell.setupWithDate(week: dateAry.weeks[indexPath.row], date: dateAry.days[indexPath.row])
        return cell
    }
    
    
}
// MARK: - 作业报告列表
extension ReportViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MoralTableViewCell", for: indexPath) as! MoralTableViewCell
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "TaskPresentationTableViewCell", for: indexPath) as! TaskPresentationTableViewCell
            return cell
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 280
    }
    
}