无故事王国
2023-10-11 f7e33a3255d9f87b20e4a06fc32012eaad77cad5
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
//
//  ExperienceCourseVC.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/9/19.
//
 
import UIKit
import JQTools
 
class ExperienceCourseVC: BaseVC {
 
    @IBOutlet weak var label_name: UILabel!
    @IBOutlet weak var label_num: UILabel!
    @IBOutlet weak var label_week: UILabel!
    @IBOutlet weak var label_times: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var cons_collectionHei: NSLayoutConstraint!
    @IBOutlet weak var label_needNum: UILabel!
 
    private var courseId:Int!
    private var experienceCourseModel:ExperienceCourseModel?
    private var cellW:Double = 0
    private var cellH:Double = 0
    private var selectIndexs = Set<Int>()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "体验购课"
        cellW = ((view.width - 28.0) - 11.0 * 3) / 4
        cellH = cellW * 0.439
 
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "Common_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_Common_1_CCell")
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
 
        Services.payCourseInfo(courseId: courseId).subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            if let model = data.data{
                self?.label_name.text = model.name
                self?.label_num.text = "\(model.num)课时"
                self?.label_week.text = model.week
                self?.label_times.text = model.time.joined(separator: "|")
                self?.experienceCourseModel = model
                self?.collectionView.reloadData()
                let h = ceil(Double(model.day.count) / 4) * weakSelf.cellH + floor(Double(model.day.count) / 4) * 11
                self?.cons_collectionHei.constant = h
            }
        }) { error in
 
        }.disposed(by: disposeBag)
    }
 
    init(courseId:Int) {
        super.init(nibName: nil, bundle: nil)
        self.courseId = courseId
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    @IBAction func paymenAction(_ sender: UIButton) {
        let n = (experienceCourseModel?.num ?? 0) * (experienceCourseModel?.time.count ?? 0) * selectIndexs.count
        if let storeId = UserDefaults.standard.value(forKey: "Current_StoreID") as? Int{
            PaymentCourseView.show(storeId: storeId, number: n) {[weak self] s in
                guard let weakSelf = self else { return }
 
                var t = [String]()
                for selectIndex in weakSelf.selectIndexs {
                    t.append(weakSelf.experienceCourseModel!.day[selectIndex])
                }
                Services.payCourse(courseId: weakSelf.courseId, num: n, oldCourseId: s, time: t).subscribe(onNext: {data in
                    alertSuccess(msg: "购买成功")
                }) { error in
 
                }.disposed(by: weakSelf.disposeBag)
            }
        }
    }
}
 
extension ExperienceCourseVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if selectIndexs.contains(indexPath.row){
            selectIndexs.remove(indexPath.row)
        }else{
            selectIndexs.insert(indexPath.row)
        }
        let n = (experienceCourseModel?.num ?? 0) * (experienceCourseModel?.time.count ?? 0) * selectIndexs.count
        label_needNum.text = "\(n)"
        collectionView.reloadData()
    }
}
 
extension ExperienceCourseVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return experienceCourseModel?.day.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_Common_1_CCell", for: indexPath) as! Common_1_CCell
        let isSelected = selectIndexs.contains(indexPath.row)
        cell.label_content.text = experienceCourseModel!.day[indexPath.row]
        cell.label_content.backgroundColor = isSelected ? Def_ThemeColor : .white
        cell.label_content.textColor = isSelected ? .white : UIColor(hexStr: "#898989")
        cell.label_content.borderWidth = isSelected ? 0:1
        return cell
    }
}
 
extension ExperienceCourseVC:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 11
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 11
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSizeMake(cellW, cellH)
    }
}