无故事王国
2023-10-19 eb795a6523edbc0fe3a3b1a3d3bea1063041efd5
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
//  StudentChooseView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/12.
//
 
import UIKit
import JQTools
import QMUIKit
import RxSwift
import RxCocoa
import HandyJSON
 
class StudentViewModel:RefreshModel<CourseDetailStudentModel>{
    override func api() -> (Observable<BaseResponse<[CourseDetailStudentModel]>>)? {
        return Services.queryStudentList()
    }
}
 
class ActivityViewModel:RefreshModel<ActivityDetailPartModel>{
    override func api() -> (Observable<BaseResponse<[ActivityDetailPartModel]>>)? {
        return Services.queryParticipantList()
    }
    
}
 
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!
    @IBOutlet weak var cons_tableHei: NSLayoutConstraint!
    @IBOutlet weak var cons_tableLeading: NSLayoutConstraint!
    @IBOutlet weak var cons_tableTrailing: NSLayoutConstraint!
    
    private var clickClouse:(([Any])->Void)!
    private var needAddClouse:(()->Void)!
    private var itemType:ItemType!
    private var selectStudents = [Any]()
    private var disposeBag = DisposeBag()
 
 
    private lazy var stuViewModel:StudentViewModel = {
        return StudentViewModel()
    }()
 
    private lazy var actViewModel:ActivityViewModel = {
        return ActivityViewModel()
    }()
 
    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()
        setRx()
    }
    
    static func show(itemType:ItemType,defaultStu:[Any]? = nil,clickClouse:@escaping ([Any])->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")
            studentChooseView.btn_add.setTitle("添加运动营成员", for: .normal)
        }else if itemType == .activity{
            studentChooseView.tableView.register(UINib(nibName: "StudentInfo_2_TCell", bundle: nil), forCellReuseIdentifier: "_StudentInfo_2_TCell")
            studentChooseView.btn_add.setTitle("添加人员", for: .normal)
        }
 
        studentChooseView.frame = sceneDelegate?.window?.frame ?? .zero
        studentChooseView.itemType = itemType
        studentChooseView.clickClouse = clickClouse
        studentChooseView.needAddClouse = needAddClouse
        sceneDelegate?.window?.addSubview(studentChooseView)
        studentChooseView.cons_bottom.constant = 0
 
        if itemType == .course{
            studentChooseView.stuViewModel.configure(studentChooseView.tableView,needMore: false)
            studentChooseView.stuViewModel.beginRefresh()
        }else{
            studentChooseView.actViewModel.configure(studentChooseView.tableView,needMore: false)
            studentChooseView.actViewModel.beginRefresh()
            studentChooseView.cons_tableLeading.constant = 0
            studentChooseView.cons_tableTrailing.constant = 0
        }
 
        UIView.animate(withDuration: 0.4) {
            studentChooseView.alpha = 1
            studentChooseView.layoutIfNeeded()
            studentChooseView.tableView.reloadData()
        }
    }
 
    private func setRx(){
        stuViewModel.dataSource.subscribe(onNext: {[weak self] data in
 
            let hei = min(360,Double(data.count) * 90.0)
            self?.cons_tableHei.constant = hei
            UIView.animate(withDuration: 0.4) {
                self?.layoutIfNeeded()
            }
        }).disposed(by: disposeBag)
 
        actViewModel.dataSource.subscribe(onNext: {[weak self] data in
            let hei = min(360,Double(data.count) * 90.0)
            self?.cons_tableHei.constant = hei
            UIView.animate(withDuration: 0.4) {
                self?.layoutIfNeeded()
            }
        }).disposed(by: disposeBag)
    }
 
    @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) {
 
        if itemType == .course{
            let item = stuViewModel.dataSource.value[indexPath.row]
           if self.selectStudents.contains(where: {($0 as! CourseDetailStudentModel).id == item.id}){
               if self.selectStudents.count == 1{
                   alert(msg: "至少选择一位运动营成员");return
               }
               if let index = self.selectStudents.firstIndex(where: {($0 as! CourseDetailStudentModel).id == item.id}){
                   self.selectStudents.remove(at: index)
               }
           }else{
               self.selectStudents.append(item)
           }
        }else{
            let item = actViewModel.dataSource.value[indexPath.row]
           if self.selectStudents.contains(where: {($0 as! ActivityDetailPartModel).id == item.id}){
               if self.selectStudents.count == 1{
                   alert(msg: "至少选择一位参赛运动营成员");return
               }
               if let index = self.selectStudents.firstIndex(where: {($0 as! ActivityDetailPartModel).id == item.id}){
                   self.selectStudents.remove(at: index)
               }
           }else{
               self.selectStudents.append(item)
           }
        }
        tableView.reloadData()
    }
}
 
extension StudentChooseView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if itemType == .course{
            return stuViewModel.dataSource.value.count
        }else{
            return actViewModel.dataSource.value.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if itemType == .course{
            let item = stuViewModel.dataSource.value[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfoTCell") as! StudentInfoTCell
            cell.cons_handleCenterY.constant = -7
            if self.selectStudents.contains(where: {($0 as! CourseDetailStudentModel).id == item.id}){
                cell.btn_handle.setImage(UIImage(named: "btn_choose_s"), for: .normal)
                cell.view_container.borderColor = UIColor(hexStr: "#FD7902").withAlphaComponent(0.28)
            }else{
                cell.btn_handle.setImage(nil, for: .normal)
                cell.view_container.borderColor = UIColor(hexStr: "#ADADAD").withAlphaComponent(0.28)
            }
            cell.studentModel = item
            return cell
        }else if itemType == .activity{
            let item = actViewModel.dataSource.value[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfo_2_TCell") as! StudentInfo_2_TCell
            cell.btn_delete.isHidden = true
            cell.btn_edit.isHidden = true
            cell.activityDetailPartModel = item
            if self.selectStudents.contains(where: {($0 as! ActivityDetailPartModel).id == item.id}){
                cell.img_radio.image = UIImage(named: "btn_choose_s")
                cell.view_container.borderColor = UIColor(hexStr: "#FD7902").withAlphaComponent(0.28)
            }else{
                cell.img_radio.image = UIImage(named: "")
                cell.view_container.borderColor = UIColor(hexStr: "#ADADAD").withAlphaComponent(0.28)
            }
            return cell
        }
        
        
        return UITableViewCell()
    }
}