宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-05-30 ab6ef7166ee51721fec3e3537414f6d6ed08b49d
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
    //
    //  ReasonforTravelVC.swift
    //  OKProject
    //
    //  Created by 无故事王国 on 2023/4/14.
    //  Copyright © 2023 yangwang. All rights reserved.
    //
 
import UIKit
import QMUIKit
import HandyJSON
 
class PassengerInfo:HandyJSON{
    var name = ""
    var phone:String?
    
    required init(){}
 
    convenience init(name:String,phone:String? = nil){
        self.init()
        self.name = name
        self.phone = phone
    }
}
 
class ReasonforTravelVC: YYViewController {
 
    @IBOutlet weak var scrollButtomCons: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewHeiCons: NSLayoutConstraint!
    @IBOutlet weak var collectViewHeiCons: NSLayoutConstraint!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var btn_cost: QMUIButton!
    @IBOutlet weak var btn_companyCost: QMUIButton!
    @IBOutlet weak var textView: QMUITextView!
    @IBOutlet weak var label_limit: UILabel!
    @IBOutlet weak var tf_passenger: UITextField!
    @IBOutlet weak var tf_passengerPhone: UITextField!
    @IBOutlet weak var view_addPassenger: UIView!
    var passengers = [PassengerInfo]()
    var reasonModels = [ReasonModel]()
    var viewModel:TravelViewModel!
    var selectIndex = Set<Int>()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        btn_cost.spacingBetweenImageAndTitle = 7
        btn_companyCost.spacingBetweenImageAndTitle = 7
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "Reason_Content_TCell", bundle: nil), forCellReuseIdentifier: "_Reason_Content_TCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        collectionView.register(UINib(nibName: "Common_SingleText_CCell", bundle: nil), forCellWithReuseIdentifier: "_Common_SingleText_CCell")
        scrollButtomCons.constant = app.window?.safeAreaInsets.bottom ?? 0
        let h = ceil(8 / 4.0) * 30.0 + floor(8 / 4.0) * 10.0
        collectViewHeiCons.constant = h
        
        if viewModel.passengers.value.count == 0{
            passengers.append(PassengerInfo(name: app.userInfo.nickName, phone: app.userInfo.phone))
        }else{
            passengers = viewModel.passengers.value
        }
 
        tableViewHeiCons.constant = 40 * Double(passengers.count)
        
        btn_cost.isSelected = viewModel.officalPaymentType.value == .selfPay
        btn_companyCost.isSelected = viewModel.officalPaymentType.value == .company
        textView.text = self.viewModel.trailContentReason.value
        
        APIManager.shared.provider.rx.request(.getBusinessReason).mapThenValidate([ReasonModel].self).subscribe { result in
            switch result{
            case .success(let m):
                self.reasonModels = m ?? []
                let h = ceil(Double(self.reasonModels.count) / 4.0) * 30.0 + floor(Double(self.reasonModels.count) / 4.0) * 10.0
                self.collectViewHeiCons.constant = h
                
                for (index,v) in self.reasonModels.enumerated(){
                    
                    if let values = self.viewModel.trailReason.value{
                        if  values.contains(v.name){
                            self.selectIndex.insert(index)
                        }
                    }
                }
                self.collectionView.reloadData()
            case .failure(let error):
                alert(text: error.localizedDescription)
            }
        } onError: { error in
            alert(text: error.localizedDescription)
        }.disposed(by: disposeBag)
    }
 
    override func bindRx() {
        super.bindRx()
        textView.rx.text.changed.subscribe(onNext: {text in
            self.label_limit.text = "\(text?.count ?? 0)/50"
 
        }).disposed(by: disposeBag)
        
        textView.rx.text.orEmpty.changed.subscribe(onNext: {text in
            self.viewModel.trailContentReason.accept(text)
        }).disposed(by: disposeBag)
    }
 
 
    @IBAction func paymentTypeAction(_ sender: UIButton) {
        btn_cost.isSelected = btn_cost.tag == sender.tag
        btn_companyCost.isSelected = btn_companyCost.tag == sender.tag
    }
 
    @IBAction func addPassengerAction(_ sender: UIButton) {
        guard !tf_passenger.isEmpty else {
            alert(text: "请输入或选择出行人");return
        }
 
        guard !tf_passengerPhone.isEmpty else {
            alert(text: "请输入或选择出行人的电话号码");return
        }
 
        guard passengers.filter({$0.name == tf_passenger.text && $0.phone == tf_passengerPhone.text}).count == 0 else {
            alert(text: "重复出行人");return
        }
 
        passengers.append(PassengerInfo(name: tf_passenger.text!, phone: tf_passengerPhone.text!))
        tableViewHeiCons.constant = 40 * Double(passengers.count)
        tableView.reloadData()
        view_addPassenger.isHidden = passengers.count >= 4
    }
 
    @IBAction func closeAction(_ sender: Any) {
        self.removeViewAndControllerFromParentViewController()
    }
    
    @IBAction func completeAction(_ sender: UIButton) {
        self.viewModel.trailContentReason.accept(textView.text)
        var cotnent = [String]()
        for v in selectIndex{
            cotnent.append(reasonModels[v].name)
        }
        
        guard cotnent.count != 0 else {alert(text: "请选择出行事由");return}
    
        viewModel.trailReason.accept(cotnent.joined(separator: ","))
        viewModel.officalPaymentType.accept(btn_cost.isSelected ? .selfPay:.company)
        viewModel.passengers.accept(passengers)
        self.removeViewAndControllerFromParentViewController()
    }
}
 
extension ReasonforTravelVC:UITableViewDelegate{
 
}
 
extension ReasonforTravelVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_Reason_Content_TCell") as! Reason_Content_TCell
        cell.index = indexPath
        cell.label_name.text = passengers[indexPath.row].name
        cell.tf_phone.text = passengers[indexPath.row].phone
 
        cell.tf_phone.rx.text.changed.subscribe(onNext: { [weak self] text in
            self?.passengers[indexPath.row].phone = text
        }).disposed(by: disposeBag)
 
        cell.deleteComplete { [weak self] index in
            guard let weakSelf = self else { return }
            weakSelf.passengers.remove(at: index.row)
            weakSelf.tableView.reloadData()
            weakSelf.view_addPassenger.isHidden = weakSelf.passengers.count >= 4
        }
        return cell
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return passengers.count
    }
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
}
 
extension ReasonforTravelVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if selectIndex.contains(indexPath.row){
            selectIndex.remove(indexPath.row)
        }else{
            selectIndex.insert(indexPath.row)
        }
        collectionView.reloadData()
    }
}
 
extension ReasonforTravelVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_Common_SingleText_CCell", for: indexPath) as! Common_SingleText_CCell
        let model = reasonModels[indexPath.row]
        cell.titleL.text = model.name
        cell.titleL.borderWidth = 1
        cell.titleL.cornerRadius = 2
        cell.titleL.backgroundColor = .white
        cell.titleL.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        if selectIndex.contains(indexPath.row){
            cell.titleL.borderColor = UIColor(hexString: "#FF884D")!
            cell.titleL.textColor = UIColor(hexString: "#FF884D")!
        }else{
            cell.titleL.borderColor = UIColor(hexString: "#90A1B1")!
            cell.titleL.textColor = UIColor(hexString: "#90A1B1")!
        }
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return reasonModels.count
    }
}
 
extension ReasonforTravelVC:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSizeMake(70, 30)
    }
}