无故事王国
2023-10-17 77041c81c325c0bc88c94dc28d732f656cc4c885
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
//
//  ActivityDetailApplyVC.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/13.
//
 
import UIKit
import QMUIKit
import SDWebImage
import JQTools
 
class ActivityDetailApplyVC: BaseVC {
 
 
    @IBOutlet weak var view_banner: UIView!
    @IBOutlet weak var label_storeNames: UILabel!
    @IBOutlet weak var label_title: UILabel!
    @IBOutlet weak var label_joinCondition: UILabel!
    @IBOutlet weak var label_address: UILabel!
    @IBOutlet weak var label_duetime: UILabel!
    @IBOutlet weak var label_starttime: UILabel!
    @IBOutlet weak var label_endtime: UILabel!
    @IBOutlet weak var label_ageScope: UILabel!
    @IBOutlet weak var label_city: UILabel!
    @IBOutlet weak var label_money: UILabel!
    @IBOutlet weak var label_coin: UILabel!
    @IBOutlet weak var label_courseNum: UILabel!
    @IBOutlet weak var label_introducion: UILabel!
    @IBOutlet weak var btn_enroll: UIButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var btn_addStudent: QMUIButton!
    @IBOutlet weak var cons_tableHei: NSLayoutConstraint!
    @IBOutlet weak var label_matchStore: UILabel!
    @IBOutlet weak var btn_moreMatchStore: UIButton!
 
    private lazy var bannerView:CommonBannerView = {
        let v = CommonBannerView()
        return v
    }()
 
    var activityDetailModel:ActivityDetailModel!
    private var students = [ActivityDetailPartModel]()
 
    required init(model:ActivityDetailModel) {
        super.init(nibName: nil, bundle: nil)
        self.activityDetailModel = model
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "活动详情"
 
        label_storeNames.text = activityDetailModel.storeInfos.map({$0.name}).joined(separator: ",")
        btn_moreMatchStore.isHidden = (label_storeNames.text?.count ?? 0) < 50
 
        label_title.text = activityDetailModel.name
        label_joinCondition.text = activityDetailModel.registerCondition.strTitle
        label_address.text = activityDetailModel.address
        label_duetime.text = activityDetailModel.registerEndTime
        label_starttime.text = activityDetailModel.startTime
        label_endtime.text = activityDetailModel.endTime
        label_ageScope.text = activityDetailModel.age + "岁"
        label_introducion.text = activityDetailModel.introduction
        label_city.text = activityDetailModel.province + "|" + activityDetailModel.city
        label_money.text = String(format: "%@/人", activityDetailModel.cashPrice.currency())
        label_coin.text = String(format: "%ld币/人", activityDetailModel.playPaiCoin)
        label_courseNum.text = String(format: "%ld课时/人", activityDetailModel.classPrice)
 
        students.append(contentsOf: activityDetailModel.participant)
        cons_tableHei.constant = Double(students.count) * 100
        tableView.reloadData()
 
        bannerView.setImages(images: activityDetailModel.imgs.components(separatedBy: ","), type: .URL) { index in
 
        }
    }
    
    override func setUI() {
        btn_addStudent.imagePosition = .right
        btn_addStudent.spacingBetweenImageAndTitle = 4
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "StudentInfo_2_TCell", bundle: nil), forCellReuseIdentifier: "_StudentInfo_2_TCell")
 
        view_banner.addSubview(bannerView)
        bannerView.frame = CGRect(x: 0, y: 0, width: JQ_ScreenW, height: JQ_ScreenW * 0.56)
 
        let moreBtnAttributeding = AttributedStringbuilder.build().add(string: "查看更多", withFont: UIFont.systemFont(ofSize: 12, weight: .medium), withColor: Def_ThemeColor).underLine(color: Def_ThemeColor).mutableAttributedString
        btn_moreMatchStore.setAttributedTitle(moreBtnAttributeding, for: .normal)
    }
 
    @IBAction func matchStoreAction(_ sender: UIButton) {
        let text = activityDetailModel.storeInfos.map({$0.name}).joined(separator: ",")
        AgreentView.show(type: .matchStore, content: text)
    }
 
    @IBAction func addStudentAction(_ sender: QMUIButton) {
        StudentChooseView.show(itemType: .activity, defaultStu: students) { [weak self] studs in
            self?.students = studs as! [ActivityDetailPartModel]
            self?.cons_tableHei.constant = Double(self?.students.count ?? 0) * 100
            self?.tableView.reloadData()
        } needAddClouse: {[weak self] () in
            let vc = ActivityStudentListVC()
            self?.push(vc: vc)
        }
    }
    
    @IBAction func applyAction(_ sender: UIButton) {
 
        guard students.count != 0 else {
            alertError(msg: "请至少选择一位参赛人员");return
        }
 
        StoresInfoView.show(activityDetailModel) { [weak self] status in
            guard let weakSelf = self else { return }
 
            let price = weakSelf.activityDetailModel.cashPrice * Double(weakSelf.students.count)
            let coin = weakSelf.activityDetailModel.playPaiCoin * weakSelf.students.count
            let course = weakSelf.activityDetailModel.classPrice * weakSelf.students.count
 
            if status{
                PaymentView.show(money: (ali:price,wx:price,coin:coin,course:course,integral:nil)) { [weak self] type in
                    guard let weakSelf = self else { return }
                    if type == .courseNum{
                        PaymentCourseView.show(id: weakSelf.activityDetailModel.id,number:course) { id in
                            weakSelf.payment(type: type,paymentId: id)
                        }
                    }else{
                        weakSelf.payment(type: type)
                    }
                }
            }
        }
    }
 
    private func payment(type:PayType,paymentId:Int? = nil){
        let ids = students.map({"\($0.id)"}).joined(separator: ",")
        Services.paymentCompetition(id: activityDetailModel.id, userIds: ids, payType: type,coursePaymentId: paymentId).subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            if let m = data.data{
                switch type {
                    case .aliPay:
                        let req = YYAlipayRequest(orderString: m.orderString)
                        YYPaymentManager.shared.sendPaymentRequest(req) { result in
                            switch result {
                                case .success:
                                    let vc = PaymentResultVC(result: .success, objType: .activityApply)
                                    vc.modalPresentationStyle = .fullScreen
                                    weakSelf.present(vc, animated: true)
                                    weakSelf.btn_enroll.setTitle("已报名", for: .normal)
                                    weakSelf.btn_enroll.backgroundColor = UIColor.gray.withAlphaComponent(0.5)
                                case .cancel:
                                    alert(msg: "已取消")
                                case .failure(let error):
                                    let vc = PaymentResultVC(result: .fail(error.localizedDescription), objType: .activityApply)
                                    vc.modalPresentationStyle = .fullScreen
                                    weakSelf.present(vc, animated: true)
                            }
                        }
                    case .wechat:break
                    case .coin:
                        let vc = PaymentResultVC(result: .success, objType: .activityApply)
                        vc.modalPresentationStyle = .fullScreen
                        weakSelf.present(vc, animated: true)
                        weakSelf.btn_enroll.setTitle("已报名", for: .normal)
                        weakSelf.btn_enroll.backgroundColor = UIColor.gray.withAlphaComponent(0.5)
                    default:break
                }
            }
        }).disposed(by: disposeBag)
    }
}
 
extension ActivityDetailApplyVC:UITableViewDelegate{
    
}
 
extension ActivityDetailApplyVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let student = students[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfo_2_TCell") as! StudentInfo_2_TCell
        cell.indexPath = indexPath
        cell.activityDetailPartModel = student
        cell.btn_edit.isHidden = true
        cell.img_radio.isHidden = true
        cell.delClouse = {[weak self] index in
            tableView.beginUpdates()
            self?.students.remove(at: index.row)
            tableView.deleteItemsAtIndexPaths([index], animationStyle: .automatic)
            tableView.endUpdates()
            tableView.reloadData()
            self?.cons_tableHei.constant = Double(self?.students.count ?? 0) * 100
        }
 
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return students.count
    }
}