younger_times
2023-07-14 b932f704f6c04adc7d7b4104a06cbc09c8d49cb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
//  StudentChooseView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/12.
//
 
import UIKit
import JQTools
import QMUIKit
import RxSwift
import RxCocoa
 
class StudentViewModel:RefreshModel<CourseDetailStudentModel>{
    override func api() -> (Observable<BaseResponse<[CourseDetailStudentModel]>>)? {
        return Services.queryStudentList()
    }
}
 
class StudentChooseView: UIView,JQNibView{
 
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var view_container: UIView!
    @IBOutlet weak var cons_bottom: NSLayoutConstraint!
    @IBOutlet weak var btn_add: QMUIButton!
    
    private var clickClouse:(([CourseDetailStudentModel])->Void)!
    private var needAddClouse:(()->Void)!
    private var itemType:ItemType!
    private var selectStudents = [CourseDetailStudentModel]()
    private var viewModel = StudentViewModel()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        btn_add.imagePosition = .right
        btn_add.spacingBetweenImageAndTitle = 5
        cons_bottom.constant = -(JQ_ScreenW * 1.1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        alpha = 0
        layoutIfNeeded()
 
        viewModel.configure(tableView,needMore: false)
        viewModel.beginRefresh()
 
    }
    
    static func show(itemType:ItemType,defaultStu:[CourseDetailStudentModel]? = nil,clickClouse:@escaping ([CourseDetailStudentModel])->Void,needAddClouse:@escaping ()->Void){
        let studentChooseView = StudentChooseView.jq_loadNibView()
        if defaultStu != nil{
            studentChooseView.selectStudents = defaultStu!
        }
        if itemType == .course{
            studentChooseView.tableView.register(UINib(nibName: "StudentInfoTCell", bundle: nil), forCellReuseIdentifier: "_StudentInfoTCell")
        }else if itemType == .activity{
            studentChooseView.tableView.register(UINib(nibName: "StudentInfo_2_TCell", bundle: nil), forCellReuseIdentifier: "_StudentInfo_2_TCell")
        }
        
        studentChooseView.frame = screnDelegate?.window?.frame ?? .zero
        studentChooseView.itemType = itemType
        studentChooseView.clickClouse = clickClouse
        studentChooseView.needAddClouse = needAddClouse
        screnDelegate?.window?.addSubview(studentChooseView)
        
        studentChooseView.cons_bottom.constant = 0
        
        UIView.animate(withDuration: 0.4) {
            studentChooseView.alpha = 1
            studentChooseView.layoutIfNeeded()
            studentChooseView.tableView.reloadData()
        }
    }
    
    @IBAction func closeAction(_ sender: UIButton) {
        closeAction()
    }
 
    @IBAction func addNewStudentAction(_ sender: QMUIButton) {
        needAddClouse!()
        closeAction()
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
            self.view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20)
        }
    }
 
    private func closeAction(){
        self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
    @IBAction func completeAction(_ sender: UIButton) {
        clickClouse!(selectStudents)
        closeAction()
    }
}
 
extension StudentChooseView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
         let item = viewModel.dataSource.value[indexPath.row]
        if self.selectStudents.contains(where: {$0.id == item.id}){
            if self.selectStudents.count == 1{
                alert(msg: "至少选择一位学员");return
            }
            self.selectStudents.remove(at: indexPath.row)
        }else{
            self.selectStudents.append(item)
        }
        tableView.reloadData()
    }
}
 
extension StudentChooseView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource.value.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if itemType == .course{
            let item = viewModel.dataSource.value[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfoTCell") as! StudentInfoTCell
            if selectStudents.contains(where: {$0.id == item.id}){
                cell.btn_handle.setImage(UIImage(named: "btn_choose_s"), for: .normal)
            }else{
                cell.btn_handle.setImage(nil, for: .normal)
            }
            cell.studentModel = item
            return cell
        }else if itemType == .activity{
            let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfo_2_TCell") as! StudentInfo_2_TCell
            return cell
        }
        
        
        return UITableViewCell()
    }
}