//
|
// 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)
|
}
|
}
|