younger_times
2023-07-17 a0071c0c7726d3e6742bc7c4c30536302766626a
WanPai/Root/Other/View/StudentChooseView.swift
@@ -10,11 +10,19 @@
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{
@@ -24,12 +32,20 @@
    @IBOutlet weak var cons_bottom: NSLayoutConstraint!
    @IBOutlet weak var btn_add: QMUIButton!
    
    private var clickClouse:(([CourseDetailStudentModel])->Void)!
    private var clickClouse:(([Any])->Void)!
    private var needAddClouse:(()->Void)!
    private var itemType:ItemType!
    private var selectStudents = [CourseDetailStudentModel]()
    private var viewModel = StudentViewModel()
    private var selectStudents = [Any]()
    private lazy var stuViewModel:StudentViewModel = {
        return StudentViewModel()
    }()
    private lazy var actViewModel:ActivityViewModel = {
        return ActivityViewModel()
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        btn_add.imagePosition = .right
@@ -40,13 +56,9 @@
        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){
    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!
@@ -62,9 +74,16 @@
        studentChooseView.clickClouse = clickClouse
        studentChooseView.needAddClouse = needAddClouse
        screnDelegate?.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()
        }
        UIView.animate(withDuration: 0.4) {
            studentChooseView.alpha = 1
            studentChooseView.layoutIfNeeded()
@@ -108,14 +127,26 @@
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)
        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
               }
               self.selectStudents.remove(at: indexPath.row)
           }else{
               self.selectStudents.append(item)
           }
        }else{
            self.selectStudents.append(item)
            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
               }
               self.selectStudents.remove(at: indexPath.row)
           }else{
               self.selectStudents.append(item)
           }
        }
        tableView.reloadData()
    }
@@ -123,15 +154,19 @@
extension StudentChooseView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource.value.count
        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 = viewModel.dataSource.value[indexPath.row]
            let item = stuViewModel.dataSource.value[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfoTCell") as! StudentInfoTCell
            if selectStudents.contains(where: {$0.id == item.id}){
            if self.selectStudents.contains(where: {($0 as! CourseDetailStudentModel).id == item.id}){
                cell.btn_handle.setImage(UIImage(named: "btn_choose_s"), for: .normal)
            }else{
                cell.btn_handle.setImage(nil, for: .normal)
@@ -139,7 +174,16 @@
            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")
            }else{
                cell.img_radio.image = UIImage(named: "")
            }
            return cell
        }