宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  MinePublishSaleDetailVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/5/7.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import WebKit
import QMUIKit
import AVKit
import Lantern
 
class MinePublishSaleDetailVC: YYViewController {
 
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var contanerBannerView: UIView!
    @IBOutlet weak var titleL: UILabel!
    @IBOutlet weak var priceL: UILabel!
    @IBOutlet weak var infoL: UILabel!
    @IBOutlet weak var moreBtn: QMUIButton!
    @IBOutlet weak var authBtn: QMUIButton!
    @IBOutlet weak var firmL: UILabel! //所属厂商
    @IBOutlet weak var mileageL: UILabel! //表里程
    @IBOutlet weak var displacementL: UILabel! //排量
    @IBOutlet weak var transmissionCaseL: UILabel! //变速箱
    @IBOutlet weak var locationOfLicensePlateL: UILabel! //牌照地
    @IBOutlet weak var transferTimesL: UILabel! //过户次数
    @IBOutlet weak var levelL: UILabel! //级别
    @IBOutlet weak var licensingTimeL: UILabel! //上牌时间
    @IBOutlet weak var contactsNameL: UILabel! //联系人
    @IBOutlet weak var addressL: UILabel! //联系地址
    @IBOutlet weak var callBtn: UIButton!
    @IBOutlet weak var imgCollectionView: UICollectionView!
    @IBOutlet weak var imgHeiCons: NSLayoutConstraint!
    @IBOutlet weak var descL: UILabel!
 
    @IBOutlet weak var webView: WKWebView!
    @IBOutlet weak var webViewHeiCons: NSLayoutConstraint!
    @IBOutlet weak var opt1Btn: UIButton!
    @IBOutlet weak var opt2Btn: UIButton!
    @IBOutlet weak var rejectView: UIView!
    @IBOutlet weak var rejectL: UILabel! //拒绝原因
    @IBOutlet weak var rejectHeiCons: NSLayoutConstraint!
    @IBOutlet weak var carTypeL: UILabel! //车辆类型
    @IBOutlet weak var bannerNum: UILabel!
 
    private var videoImg:UIImage?
 
    let cellW:Double = (SCREEN_WIDTH - 27)
    let cellH:Double = (SCREEN_WIDTH - 27) * 0.497
 
    var bannerView:BannerView!
    var carSellModel:SellCarReqModel?
 
    private let agreementViewModel = AgreementViewModel()
 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        yy_nav_back_img = UIImage.init(named: "icon_back_white")!
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font: UIFont.init(name: Medium, size: 18) ?? UIFont.systemFont(ofSize: 18)]
        let image = UIImage(named: "LD_Rectangle")
        self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
        self.navigationController?.navigationBar.isTranslucent = true
    }
 
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.color(light: UIColor.color(hexString: "#000000"), dark: UIColor.color(hexString: "#FFFFFF")),NSAttributedString.Key.font: UIFont.init(name: Medium, size: 18) ?? UIFont.systemFont(ofSize: 18)]
        self.navigationController?.navigationBar.barTintColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#191919"))
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.isTranslucent = false
    }
 
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "车辆详情"
        scrollView.contentInset = UIEdgeInsets(top: -navH, left: 0, bottom: 0, right: 0)
        setReject(text: "")
        imgCollectionView.delegate = self
        imgCollectionView.dataSource = self
        imgCollectionView.register(UINib(nibName: "Common_FullImg_CCell", bundle: nil), forCellWithReuseIdentifier: "_Common_FullImg_CCell")
 
        if let m = carSellModel{
 
            switch m.userType {
                case .business:
                    carTypeL.text = "企业车辆"
//                    carTypeL.backgroundColor = UIColor(hexString: "#FF8A3D")?.withAlphaComponent(0.79)
                case .user:
                    carTypeL.text = "个人车辆"
//                    carTypeL.backgroundColor = UIColor(hexString: "#00BF30")?.withAlphaComponent(0.79)
                default:break
            }
 
            switch m.status {
                case .reject:setReject(text: m.authRemark!)
                default:setReject(text:"")
            }
 
            titleL.text = m.title
//            if m.transferPrice! > 10000{
                priceL.text = String(format: "%.2lf万", Double(m.transferPrice!) / 10000.0)
//            }else{
//                priceL.text = String(format: "%.2lf",m.transferPrice!)
//            }
 
            var temp = [String]()
 
//            if m.newCarPrice! > 10000{
                temp.append(String(format: "%.2lf万", Double(m.newCarPrice!) / 10000.0))
//            }else{
//                temp.append(String(format: "%.2lf",m.newCarPrice!))
//            }
 
            if m.includingTransferFee == 1{
                temp.append("包含过户费")
            }else{
                temp.append("不包含过户费")
            }
            infoL.text = String(format: "新车含税价格:%@", temp.joined(separator: "/"))
            firmL.text = m.brandName
            mileageL.text = String(format: "%@万公里", m.mileage?.ld_formatFloat ?? "0")
            displacementL.text = m.displacement!.contains("L") ? m.displacement! : "\(m.displacement!)L"
            transmissionCaseL.text = m.transmissionCase
            locationOfLicensePlateL.text = m.locationOfLicensePlate
            transferTimesL.text = "\(m.transferTimes!.string())次"
            levelL.text = m.level
            licensingTimeL.text = m.licensingTime
            contactsNameL.text = m.contactsName
            authBtn.isHidden = m.userType != .business
            addressL.text = String(format: "联系地址:%@%@%@", m.provinceName!,m.cityName!,m.addres!)
            bannerView.setImages(images: m.imgUrl!.components(separatedBy: ","), type: .URL) { index in
 
            }
 
            if !m.videoUrl!.isEmpty{
                let asset = AVURLAsset(url: URL(string: m.videoUrl!)!)
                let gen = AVAssetImageGenerator(asset: asset)
                gen.appliesPreferredTrackTransform = true
                let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 1)
                var actualTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale: 0)
                do {
                    let image = try gen.copyCGImage(at: time, actualTime: &actualTime)
                    self.videoImg = UIImage(cgImage: image)
                    calHei()
                } catch  {
                    print("错误")
                }
            }
 
            let attribute = AttributedStringbuilder.build()
            self.descL.attributedText = attribute.add(string: m.describe!, withFont: UIFont.systemFont(ofSize: 14, weight: .medium), withColor: UIColor(hexString: "#696969")!.withAlphaComponent(0.8), lineSpace: 3).mutableAttributedString
 
            calHei()
 
            switch m.status {
                case .review:
                    opt1Btn.isEnabled = false
                    opt1Btn.setTitle("审核中", for: .normal)
                    opt1Btn.backgroundColor = UIColor(hexString: "#DCDCDC")
                    opt1Btn.setTitleColor(UIColor(hexString: "#393939"), for: .normal)
                    opt2Btn.isHidden = true
                case .onShelf:
                    opt1Btn.setTitle("下架", for: .normal)
                    opt1Btn.backgroundColor = UIColor(hexString: "#00BF30")
                    opt1Btn.setTitleColor(.white, for: .normal)
                    opt2Btn.isHidden = true
                case .waitOnShelf:
                    opt1Btn.setTitle("编辑", for: .normal)
                    opt1Btn.backgroundColor = UIColor(hexString: "#DCDCDC")
                    opt1Btn.setTitleColor(UIColor(hexString: "#393939"), for: .normal)
                    opt2Btn.setTitle("上架", for: .normal)
                    opt2Btn.backgroundColor = UIColor(hexString: "#00BF30")
                    opt2Btn.setTitleColor(.white, for: .normal)
                case .reject:
                    //73793 【我发布的售卖车辆】拒绝状态的详情页面,底部应只有一个编辑按钮 ,目前显示了2个编辑按钮
                    setReject(text: m.authRemark ?? "")
                    opt1Btn.setTitle("编辑", for: .normal)
                    opt1Btn.backgroundColor = UIColor(hexString: "#DCDCDC")
                    opt1Btn.setTitleColor(UIColor(hexString: "#393939"), for: .normal)
                    opt2Btn.isHidden = true
                case .takeDown:
                    opt1Btn.setTitle("编辑", for: .normal)
                    opt1Btn.backgroundColor = UIColor(hexString: "#DCDCDC")
                    opt1Btn.setTitleColor(UIColor(hexString: "#393939"), for: .normal)
                    opt2Btn.setTitle("上架", for: .normal)
                    opt2Btn.backgroundColor = UIColor(hexString: "#00BF30")
                    opt2Btn.setTitleColor(.white, for: .normal)
                default:
                    opt1Btn.isHidden = true
                    opt2Btn.isHidden = true
            }
        }
 
        agreementViewModel.type.accept(.question)
        agreementViewModel.agreement()
 
    }
 
    override func setupViews() {
        super.setupViews()
 
        moreBtn.imagePosition = .right
        moreBtn.spacingBetweenImageAndTitle = 7
        authBtn.imagePosition = .left
        authBtn.spacingBetweenImageAndTitle = 4
 
        bannerView = BannerView()
        contanerBannerView.maskToBounds = true
        contanerBannerView.addSubview(bannerView)
        contanerBannerView.sendSubviewToBack(bannerView)
        bannerView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenWidth * 0.8)
 
        bannerView.changeAtIndex { [weak self] current, totalNum in
            if current <= -1{
                self?.bannerNum.text = "\(totalNum)/\(totalNum)"
            }else{
                self?.bannerNum.text = "\(current + 1)/\(totalNum)"
            }
        }
 
    }
 
    override func defineLayouts() {
        super.defineLayouts()
 
    }
 
    override func bindRx() {
        agreementViewModel.requestSubject.subscribe(onNext: {state in
            switch state {
                case .success(let model):
                    guard let data: SwitchCityModel = model as? SwitchCityModel else {return}
                    self.webView.loadHTMLString(data.content.ld_warpHtml, baseURL: nil)
                default:break
            }
 
        }).disposed(by: disposeBag)
 
        self.webView.scrollView.rx.observe(CGSize.self, "contentSize").map { (size) -> CGFloat? in
            if let size = size{
                return size.height
            }
            return nil
        }.subscribe(onNext: { [unowned self](height) in
            if let height = height{
                self.webViewHeiCons.constant = height
            }
        }).disposed(by: disposeBag)
    }
 
    //设置拒绝
    private func setReject(text:String){
        rejectView.isHidden = text.isEmpty
        rejectL.text = text
        if text.isEmpty{
            rejectHeiCons.constant = 7
        }else{
            let h = text.height(12, wight: ScreenWidth - 28)
            rejectHeiCons.constant = h + 20
        }
    }
 
    private func calHei(){
        let count = carSellModel?.describeImgUrl?.components(separatedBy: ",").filter({!$0.isEmpty}).count ?? 0
        imgHeiCons.constant = Double(count) * cellH + 8 * Double(count)
        imgCollectionView.reloadData()
    }
 
    @IBAction func moreInfoAction(_ sender: QMUIButton) {
        let vc = MinePublishSaleDetailMoreVC()
        vc.sellCarReqModel = carSellModel
        yy_push(vc: vc)
    }
 
    @IBAction func opt1Action(_ sender: UIButton) {
        guard let m = carSellModel else { return  }
 
        if sender.titleLabel?.text == "编辑"{
            let vc = PublishSaleCarVC(carSellModel!, isEdit: true,saveEdit: false)
            yy_push(vc: vc)
        }else if sender.titleLabel?.text == "上架"{
            alert(popup: .double, title: nil, text: "是否对该商品进行上架?", submitTitle: "确定", cancelTitle: "取消") {[weak self] () in
                guard let weakSelf = self else { return }
                weakSelf.show()
                APIManager.shared.provider.rx.request(.putOnTheShelf(id:  m.id!.int!, type: 2)).map(YYModel<Nothing>.self).subscribe(onSuccess: {data in
                    //74083 【我发布的-售卖车辆 + 出租车辆】 下架的车辆,点击上架后,页面底部应显示为 下架按钮,不应是 审核中 按钮
                    weakSelf.hide()
                    weakSelf.opt1Btn.setTitle("下架", for: .normal)
                    weakSelf.opt1Btn.backgroundColor = UIColor(hexString: "#00BF30")
                    weakSelf.opt1Btn.setTitleColor(.white, for: .normal)
                    weakSelf.opt2Btn.isHidden = true
                }) { error in
                    weakSelf.hide()
                }.disposed(by: weakSelf.disposeBag)
            } cancelClick: {
 
            }
        }else if sender.titleLabel?.text == "下架"{
            alert(popup: .double, title: nil, text: "是否对该商品进行下架?", submitTitle: "确定", cancelTitle: "取消") {[weak self] () in
                guard let weakSelf = self else { return }
                weakSelf.show()
                APIManager.shared.provider.rx.request(.offTheShelf(id:  m.id!.int!, type: 2)).map(YYModel<Nothing>.self).subscribe(onSuccess: {data in
                    weakSelf.hide()
                    weakSelf.opt1Btn.setTitle("编辑", for: .normal)
                    weakSelf.opt1Btn.backgroundColor = UIColor(hexString: "#DCDCDC")
                    weakSelf.opt1Btn.setTitleColor(UIColor(hexString: "#393939"), for: .normal)
                    weakSelf.opt2Btn.setTitle("上架", for: .normal)
                    weakSelf.opt2Btn.backgroundColor = UIColor(hexString: "#00BF30")
                    weakSelf.opt2Btn.setTitleColor(.white, for: .normal)
                    weakSelf.opt1Btn.isHidden = false
                    weakSelf.opt2Btn.isHidden = false
                }) { error in
                    weakSelf.hide()
                }.disposed(by: weakSelf.disposeBag)
            } cancelClick: {
 
            }
        }
    }
 
    @IBAction func callAction(_ sender: UIButton) {
        if let phone = carSellModel?.contactsPhone{
            UIApplication.shared.open(URL(string: "telprompt://\(phone)")!, options: [:], completionHandler: nil)
        }
    }
 
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
 
}
 
extension MinePublishSaleDetailVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 
        let count = carSellModel?.describeImgUrl?.components(separatedBy: ",").filter({!$0.isEmpty}).count ?? 0
        let lantern = Lantern()
        lantern.numberOfItems = {return count}
 
        lantern.cellClassAtIndex = { _ in LanternImageCell.self}
 
        lantern.transitionAnimator = LanternZoomAnimator(previousView: { index -> UIView? in
            let cell = collectionView.cellForItem(at: IndexPath(item: index, section: indexPath.section)) as! Common_FullImg_CCell
            return cell.img
        })
 
        // UIPageIndicator样式的页码指示器
        lantern.pageIndicator = LanternDefaultPageIndicator()
 
        lantern.pageIndex = indexPath.item
 
        lantern.reloadCellAtIndex = { context in
            let lanternCell = context.cell as? LanternImageCell
            let cell = collectionView.cellForItem(at: IndexPath(item:context.index, section: indexPath.section)) as! Common_FullImg_CCell
            lanternCell?.imageView.image = cell.img.image
        }
        //不要使用push
        lantern.show()
    }
}
 
extension MinePublishSaleDetailVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = carSellModel?.describeImgUrl?.components(separatedBy: ",").filter({!$0.isEmpty}).count ?? 0
        return count
    }
 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let imgUrl = carSellModel!.describeImgUrl!.components(separatedBy: ",").filter({!$0.isEmpty})
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_Common_FullImg_CCell", for: indexPath) as! Common_FullImg_CCell
        cell.closeBtn.isHidden = true
        cell.img.load(url: imgUrl[indexPath.row])
        return cell
    }
}
 
extension MinePublishSaleDetailVC :UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 8
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: cellW, height: cellH)
    }
}