无故事王国
2023-11-01 b3269976c84d03208f633cd0136a6f78e8dd53f7
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    //
    //  CourseDetailApplyVC.swift
    //  WanPai
    //
    //  Created by 杨锴 on 2023/6/9.
    //
 
import UIKit
import JQTools
import QMUIKit
 
class CourseDetailApplyVC: BaseVC {
 
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var cons_collectHei: NSLayoutConstraint!
    @IBOutlet weak var btn_addStudent: QMUIButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var cons_collHei: NSLayoutConstraint!
    @IBOutlet weak var cons_tableHei: NSLayoutConstraint!
    @IBOutlet weak var btn_coupon: TapBtn!
 
    @IBOutlet weak var view_banner: CommonBannerView!
    @IBOutlet weak var label_title: UILabel!
    @IBOutlet weak var label_listenWeek: UILabel!
    @IBOutlet weak var label_listenTime: UILabel!
    @IBOutlet weak var label_store: UILabel!
    @IBOutlet weak var label_address: UILabel!
 
    @IBOutlet weak var label_price: UILabel!
    @IBOutlet weak var label_originPrice: UILabel!
    @IBOutlet weak var label_vipPrice: UILabel!
    @IBOutlet weak var label_coin: UILabel!
    @IBOutlet weak var btn_hasCoupon: TapBtn!
    @IBOutlet weak var btn_couponInfo: UIButton!
    @IBOutlet weak var label_courseType: UILabel!
 
    @IBOutlet weak var view_vaildTime: UIView!
    @IBOutlet weak var label_vaild: UILabel!
    @IBOutlet weak var studentTableView: UITableView!
    @IBOutlet weak var view_packageList: UIView!
    @IBOutlet weak var btn_handleBtn: UIButton!
    @IBOutlet weak var view_teach: UIView!
 
    private var detailModel:CourseDetailModel?
    private var selectClassIndex:Int = 0
    private var isExtend:Bool = false
    private var CellW:Double!
    private var CellH:Double!
    private var studentModels = [CourseDetailStudentModel]()
    private var couponModels = [CouponInfoModel]()
    private var selectCouponModel:CouponInfoModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "运动营详情"
 
        if let m = detailModel{
            label_title.text = m.name
            label_listenWeek.text = "每" + m.weeks.joined(separator: "、")
            label_listenTime.text = m.times.joined(separator: "|")
            label_store.text = m.storeName
            label_address.text = m.storeAddress
            label_vaild.text = m.time
            label_courseType.text = m.type.strTitle
 
            switch m.type {
                case .vocation:
                    label_vaild.text = m.time
                    view_vaildTime.isHidden = false
                    view_packageList.isHidden = true
                case .experience:
                    label_vaild.text = "购买当天有效"
                    view_vaildTime.isHidden = false
                    view_packageList.isHidden = true
                default:
                    view_vaildTime.isHidden = true
            }
 
            if m.student?.id != 0{
                studentModels.append(m.student!)
            }
 
            cons_collHei.constant = ceil(Double(m.list.count) / 3.0) * CellH + floor(Double(m.list.count) / 3.0) * 21.0
            collectionView.reloadData()
 
            cons_tableHei.constant = CGFloat(studentModels.count * 87)
            tableView.reloadData()
            changePrice(selectClassIndex)
 
            let imgs = m.detailDrawing.components(separatedBy: ",")
            var items = [CommonBannerModel]()
            for (index,img) in imgs.enumerated(){
                items.append(CommonBannerModel(index: index,resource: img,mediaType: .imageUrl))
            }
            view_banner.setItems(items: items)
        }
 
 
        btn_addStudent.isHidden = isExtend
        if isExtend{
            btn_handleBtn.setTitle("续课", for: .normal)
        }else{
            btn_handleBtn.setTitle("立即报名", for: .normal)
        }
 
 
    }
    
    override func setUI() {
 
        btn_hasCoupon.isHidden = true
        CellW = (JQ_ScreenW - 155) / 3.0
        CellH = CellW * 0.439
        
        cons_tableHei.constant = 76
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "Common_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_Common_1_CCell")
        
        studentTableView.dataSource = self
        studentTableView.separatorStyle = .none
        studentTableView.register(UINib(nibName: "StudentInfoTCell", bundle: nil), forCellReuseIdentifier: "_StudentInfoTCell")
        
        btn_addStudent.imagePosition = .right
        btn_addStudent.spacingBetweenImageAndTitle = 3
    }
 
    init(detailModel:CourseDetailModel,isExtend:Bool = false) {
        super.init(nibName: nil, bundle: nil)
        self.detailModel = detailModel
        self.isExtend = isExtend
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override func setRx() {
        NotificationCenter.default.rx.notification(StudentUpdate_Nofi).take(until: self.rx.deallocated).subscribe {[weak self] noti in
            guard let weakSelf = self else { return }
            //重新唤起添加学生
            weakSelf.studentAction(weakSelf.btn_addStudent)
        }.disposed(by: disposeBag)
    }
 
    @IBAction func couponAction(_ sender: TapBtn) {
        CouponChooseView.show(couponModels,defaultModel: selectCouponModel) {[weak self] m in
            guard let weakSelf = self else { return }
            weakSelf.selectCouponModel = m
            guard m != nil else {
                weakSelf.btn_couponInfo.setTitle("去选择", for: .normal);return
            }
            weakSelf.btn_couponInfo.setTitle("-\(m!.favorable.currency())", for: .normal)
            weakSelf.changePrice(weakSelf.selectClassIndex)
        }
    }
    
    @IBAction func studentAction(_ sender: QMUIButton) {
        StudentChooseView.show(itemType: .course, defaultStu: studentModels) { [weak self] studs in
            guard let weakSelf = self else { return }
            weakSelf.studentModels = studs as! [CourseDetailStudentModel]
            weakSelf.tableView.reloadData()
            weakSelf.cons_tableHei.constant = CGFloat((weakSelf.studentModels.count) * 87)
            weakSelf.changePrice(weakSelf.selectClassIndex)
        } needAddClouse: { [weak self] () in
            let vc = AddStudentVC(type: .course)
            self?.push(vc: vc)
        }
    }
    
    @IBAction func paymentAction(_ sender: UIButton) {
        guard detailModel != nil else {return}
        guard studentModels.count != 0 else {alertError(msg: "请选择运动营成员");return}
        StoresInfoView.show(detailModel!) { [weak self] status in
            guard let weakSelf = self else { return }
            if status{
                if let model = weakSelf.detailModel?.list[weakSelf.selectClassIndex]{
 
                    var price:Double = 0
                    //是否是会员
                    if weakSelf.detailModel?.isVip == 1 && model.vipPrice != 0{
                        price = model.vipPrice
                    }else{
                        price = model.paymentPrice
                    }
 
                    //计算人数总价
                    price = price * Double(weakSelf.studentModels.count)
 
                    //减去优惠
                    let discountPrice = price - (weakSelf.selectCouponModel?.favorable ?? 0)
                    let coin = model.playPaiCoin * weakSelf.studentModels.count
 
                    PaymentView.show(money: (ali:discountPrice,wx:discountPrice,coin:coin,course:nil,integral:nil)) { [weak self] payType in
                        guard let weakSelf = self else { return }
 
                        var paymentPrice:Double = 0
                        switch payType {
                            case .aliPay,.wechat:
                                paymentPrice = discountPrice
                            case .coin:
                                paymentPrice = Double(coin)
                            case .courseNum,.integral:break
                        }
 
                        Services.courcePayment(conponId: weakSelf.selectCouponModel?.id, courseConfigId: model.id, id: weakSelf.detailModel!.id, price: paymentPrice, payType: payType, stuId: weakSelf.studentModels.map({$0.id})).subscribe(onNext: { data in
                            if data.code == 200{
                                switch payType {
                                    case .aliPay:
                                        YYPaymentManager.shared.sendPaymentRequest(YYAlipayRequest(orderString: data.data!.orderString)) { [weak self] result in
                                            switch result {
                                                case .success:
                                                    let vc = PaymentResultVC(result: .success, objType: .courseApply,handleVC: self,courseConfigId: model.id)
                                                    vc.isExtend = self?.isExtend ?? false
                                                    self?.push(vc: vc)
                                                case .cancel:
                                                    alert(msg: "已取消")
                                                case .failure(_):
                                                    let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .courseApply)
                                                    self?.push(vc: vc)
                                            }
                                        }
                                    case .wechat:
                                        self?.navigationController?.popViewController(animated: false)
                                        break
                                    case .coin:
//                                        self?.navigationController?.popViewController(animated: false)
                                        let vc = PaymentResultVC(result: .success, objType: .courseApply,handleVC: self,courseConfigId: model.id)
                                        vc.isExtend = self?.isExtend ?? false
                                        self?.push(vc: vc)
                                    case .courseNum,.integral:break
                                }
                            }else{
                                alertError(msg: data.msg)
                            }
                        },onError: { error in
                            if let er = error as? NetworkRequest.NetRequestError{
                                switch er {
                                    case .Other(let code,let string):
                                        let vc = PaymentResultVC(result: .fail(string,code), objType: .courseApply,handleVC: nil)
                                        self?.push(vc: vc)
                                    default:
                                        let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .courseApply,handleVC: nil)
                                        self?.push(vc: vc)
                                }
                            }
 
                        }).disposed(by: weakSelf.disposeBag)
                    }
                }
            }else{
                self?.navigationController?.popToRootViewController(animated: true)
                NotificationCenter.default.post(name: ChooseHomeStore_Noti, object: nil)
            }
        }
    }
 
    private func changePrice(_ index:Int){
        if let subM = detailModel?.list[index]{
            
            var studentCount:Double = 0
            if studentModels.count == 0{
                studentCount = 1
            }else{
                studentCount = Double(studentModels.count)
            }
 
 
            switch subM.payType {
                case .coin:
                    label_price.text = ""
                    label_price.isHidden = true
                    label_originPrice.isHidden = true
                    label_vipPrice.isHidden = true
 
                        //玩湃币
                    label_coin.attributedText = AttributedStringbuilder.build()
                        .add(string: "玩湃币:", withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#3F3F3F"))
                        .add(string: "\(subM.playPaiCoin * studentModels.count)币", withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#F21313")).mutableAttributedString
 
                case .cash:
                    label_coin.isHidden = true;fallthrough
                case .cashCoin:
                    if subM.originalPrice == 0{label_originPrice.isHidden = true}
                    label_originPrice.isHidden = (subM.paymentPrice == subM.originalPrice && subM.paymentPrice != 0 && subM.originalPrice != 0)
 
                        //玩湃币
                    label_coin.attributedText = AttributedStringbuilder.build()
                        .add(string: "玩湃币:", withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#3F3F3F"))
                        .add(string: "\(subM.playPaiCoin * studentModels.count)币", withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#F21313")).mutableAttributedString
 
                        //原价
                    let attribute = AttributedStringbuilder.build().add(string: (subM.originalPrice * studentCount).currency(), withFont: UIFont.systemFont(ofSize: 16, weight: .semibold), withColor: UIColor(hexStr: "#3F3F3F").withAlphaComponent(0.58)).delLine(color: UIColor(hexStr: "#3F3F3F").withAlphaComponent(0.58))
                    label_originPrice.attributedText = attribute.mutableAttributedString
 
                    if UserInfoModel.get()?.isVip == 1{
                            //会员
                        label_vipPrice.isHidden = true
                        if subM.paymentPrice < subM.vipPrice && subM.paymentPrice != 0{
                            label_price.text = (subM.paymentPrice * studentCount - (selectCouponModel?.favorable ?? 0)).currency()
                        }else if subM.vipPrice != 0{
                            label_price.text = (subM.vipPrice * studentCount - (selectCouponModel?.favorable ?? 0)).currency()
                        }else{
                            label_price.text = (subM.paymentPrice * studentCount - (selectCouponModel?.favorable ?? 0)).currency()
                        }
                    }else{
                            //非会员
                        label_vipPrice.isHidden = subM.vipPrice == 0 || subM.vipPrice == subM.paymentPrice
                        let vipAttribute = AttributedStringbuilder.build()
                            .add(string: "会员价:", withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#3F3F3F"))
                            .add(string: (subM.vipPrice * studentCount).currency(), withFont: UIFont.systemFont(ofSize: 14, weight: .semibold), withColor: UIColor(hexStr: "#F21313"))
                        label_vipPrice.attributedText = vipAttribute.mutableAttributedString
 
 
                        if subM.originalPrice != 0 && subM.paymentPrice == 0{
                            label_price.text = (subM.originalPrice * studentCount - (selectCouponModel?.favorable ?? 0)).currency()
                        }else if subM.originalPrice == 0 && subM.paymentPrice != 0{
                            label_price.text = (subM.paymentPrice * studentCount - (selectCouponModel?.favorable ?? 0)).currency()
                        }else{
                            var money = min(subM.originalPrice,subM.paymentPrice)
                            money = money - (selectCouponModel?.favorable ?? 0)
                            label_price.text = (money * studentCount).currency()
                        }
                    }
            }
 
            if detailModel?.type == .experience{
                label_price.text = subM.paymentPrice.currency()
                view_teach.isHidden = true
                label_originPrice.isHidden = true
                label_vipPrice.isHidden = true
            }
        }
 
        queryCouponInfo()
    }
 
    private func queryCouponInfo(){
        if let subM = detailModel?.list[selectClassIndex]{
 
            var price:Double?
            switch subM.payType{
                case .cash,.cashCoin:
                    price = subM.originalPrice == 0 ? subM.vipPrice : subM.originalPrice
                case .coin:
                    price = Double(subM.playPaiCoin)
            }
 
            guard price != nil else {
                LogError("会员优惠价格出现问题:nil");return
            }
 
            Services.queryAvaiableCopons(id: detailModel!.id, price: price!).subscribe(onNext: { [weak self] data in
                self?.btn_hasCoupon.isHidden = (data.data?.count ?? 0) == 0
                self?.couponModels = data.data ?? []
            }).disposed(by: disposeBag)
        }
    }
 
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let w = String.jq_getWidth(text: detailModel?.type.strTitle ?? "", height: 25, font: 14) + 16
        label_courseType.jq_addCorners(corner: [.topLeft,.bottomLeft], radius: 4, width: w, height: 25)
    }
}
 
extension CourseDetailApplyVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectClassIndex = indexPath.row
        collectionView.reloadData()
        changePrice(selectClassIndex)
    }
}
 
extension CourseDetailApplyVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let m = detailModel!.list[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_Common_1_CCell", for: indexPath) as! Common_1_CCell
        cell.isSelected = indexPath.row == selectClassIndex
        cell.courseDetailListModel = m
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return detailModel?.list.count ?? 0
    }
}
 
extension CourseDetailApplyVC:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 21
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 21
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CellW, height: CellH)
    }
}
 
extension CourseDetailApplyVC:UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return studentModels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfoTCell") as! StudentInfoTCell
        cell.indexPath = indexPath
        cell.studentModel = studentModels[indexPath.row]
        cell.deleClouse = { [weak self] index in
            guard let weakSelf = self else { return }
            if weakSelf.studentModels.count > 1{
                tableView.beginUpdates()
                weakSelf.studentModels.remove(at: index)
                tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .right)
                tableView.endUpdates()
                weakSelf.cons_tableHei.constant = CGFloat((weakSelf.studentModels.count) * 87)
                tableView.reloadData()
                weakSelf.changePrice(weakSelf.selectClassIndex)
            }else{
                alert(msg: "至少选择一位运动营成员")
            }
        }
        cell.btn_handle.isHidden = isExtend
        return cell
    }
}