//
//  RescueController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/4.
//

import Foundation
import JXCategoryView
import SnapKit
import DDCategoryKit_Private
import DDAutoUIKit_Private
import DDControlsKit_Private
import RxSwift
import RxRelay
import MJRefresh
import DDWebImageKit_Private

// MARK: Action
extension RescueController {
    func addActions() {
        MCOUNT.messageCountRelay
            .subscribe(onNext: {[weak self] count in
                if count > 0 {
                    self?.messageView.countLabel.isHidden = false
                    self?.messageView.countLabel.text = String(count)
                    self?.messageView.countLabel.snp.updateConstraints({ make in
                        make.width.equalTo(20)
                    })
                }else{
                    self?.messageView.countLabel.isHidden = true
                    self?.messageView.countLabel.snp.updateConstraints({ make in
                        make.width.equalTo(0)
                    })
                }
            })
            .disposed(by: disposeBag)
        
        MCOUNT.pendingCountRelay
            .subscribe(onNext: {[weak self] count in
                if count > 0 {
                    self?.pendingView.countLabel.isHidden = false
                    self?.pendingView.countLabel.text = String(count)
                    self?.pendingView.countLabel.snp.updateConstraints({ make in
                        make.width.equalTo(20)
                    })
                }else{
                    self?.pendingView.countLabel.isHidden = true
                    self?.pendingView.countLabel.snp.updateConstraints({ make in
                        make.width.equalTo(0)
                    })
                }
            })
            .disposed(by: disposeBag)
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: Notification_reloadRescusControolerToDoMessageCount), object: nil)
        
        messageTapGes.rx.event
            .subscribe(onNext: {[weak self] _ in
                let vc = MessageCenterController()
                self?.navigationController?.pushViewController(vc, animated: true)
            })
            .disposed(by: disposeBag)
        
        pendingTapGes.rx.event
            .subscribe(onNext: { _ in
                
            })
            .disposed(by: disposeBag)
    }

}

extension RescuePendingOrderController {
    func addActions() {
        pendingOrderView.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.pageNum = 1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })
        
        pendingOrderView.tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in
            self?.pageNum+=1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })

        refreshRelay
            .flatMapLatest { pageNum in
                return Observable.zip(RQ.orderList(parameters: OrderListParameters(pageNum: pageNum, queryType: .TO_ACCEPT_DISPATCH)).asObservable(), Observable.just(pageNum))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] (response, pageNum) in
                if response?.success == true {
                    if pageNum == 1 {
                        if response?.data?.isEmpty == false {
                            self?.resultArr.removeAll()
                            self?.pendingOrderView.tableView.mj_footer?.resetNoMoreData()
                        }
                        self?.pendingOrderView.tableView.mj_header?.endRefreshing()
                    }else{
                        if response?.data?.isEmpty == false {
                            self?.pendingOrderView.tableView.mj_footer?.endRefreshing()
                        }else{
                            self?.pendingOrderView.tableView.mj_footer?.endRefreshingWithNoMoreData()
                        }
                    }
                    
                    if let array = response?.data {
                        self?.resultArr.append(contentsOf: array)
                        self?.pendingOrderView.tableView.reloadData()
                    }
                }else{
                    if pageNum == 1 {
                        self?.pendingOrderView.tableView.mj_header?.endRefreshing()
                    }else{
                        self?.pendingOrderView.tableView.mj_footer?.endRefreshing()
                    }
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        
        pendingOrderView.tableView.mj_header?.beginRefreshing()
        
        refuseRemarkIdRelay
            .flatMapLatest { model in
                return Observable.zip(RQ.refuseDispatch(parameters: RefuseDispatchParameters(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId)).asObservable(), Observable.just(model))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] (response,model) in
                if response?.success == true {
                    self?.entry(model: model,remarkId: response?.data)
                }else{
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        
        acceptOrderRelay
            .flatMapLatest { model in
                return Observable.zip(RQ.acceptDispatch(parameters: AcceptDispatchParameters(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId)).asObservable(), Observable.just(model))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] (response,model) in
                if response?.success == true {
                    self?.resultArr.removeAll { _model in
                        _model.userOrderId == model.userOrderId
                    }
                    self?.pendingOrderView.tableView.reloadData()
                }else{
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
    }
    
    func entry(model:OrderListDataModel,remarkId:String?) {
        let view = RefuseOrderEntryView()
        view.exitButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: { _ in
                ENTRY.dismiss(name: refuseOrderEntry)
            })
            .disposed(by: disposeBag)
        
        view.submitButton.rx.tap
            .flatMapLatest({[weak self] _ in
                return self?.getRefuseParameters(view:view,model: model,remarkId: remarkId) ?? Observable.just(RefuseDispatchParameters(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId))
             })
            .flatMapLatest({ parameters in
                return RQ.refuseDispatch(parameters: parameters)
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                if response?.success == true {
                    self?.resultArr.removeAll { _model in
                        _model.userOrderId == model.userOrderId
                    }
                    self?.pendingOrderView.tableView.reloadData()
                    ENTRY.dismiss(name: refuseOrderEntry)
                }else{
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        ENTRY.showRefuseOrderEntry(view: view,name: refuseOrderEntry)
    }
    
    func getRefuseParameters(view:RefuseOrderEntryView,model:OrderListDataModel,remarkId:String?) -> Observable<RefuseDispatchParameters>{
        return Observable.create { ob in
            var refuseReasonItem : String?
            var refuseReasonDesc : String?
            if view.timeView.selectButton.isSelected == true {
                refuseReasonItem = RefuseDispatchParameters.RefuseReasonItemEnum.time.rawValue
                refuseReasonDesc = view.timeTextFieldView.textFiled.text
            }
            if view.distanceView.selectButton.isSelected == true {
                refuseReasonItem = RefuseDispatchParameters.RefuseReasonItemEnum.disatance.rawValue
                refuseReasonDesc = view.distanceTextFieldView.textFiled.text
            }
            if view.cannotDispatchView.selectButton.isSelected == true {
                refuseReasonItem = RefuseDispatchParameters.RefuseReasonItemEnum.cannotDispatch.rawValue
                refuseReasonDesc = view.cannotDispatchTextViewView.textView.text
            }
           let parameters = RefuseDispatchParameters(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId,remarkId: remarkId,refuseReasonItem: refuseReasonItem,refuseReasonDesc: refuseReasonDesc)
            ob.onNext(parameters)
            ob.onCompleted()
            return Disposables.create()
        }
    }
}

extension RescuePendingDispatchController {
    
    func addActions() {
        pendingDispatchView.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.pageNum = 1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })
        
        pendingDispatchView.tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in
            self?.pageNum+=1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })

        refreshRelay
            .flatMapLatest { pageNum in
                return Observable.zip(RQ.orderList(parameters: OrderListParameters(pageNum: pageNum, queryType: .TO_DISPATCH_VEHICLE)).asObservable(), Observable.just(pageNum))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] (response, pageNum) in
                if response?.success == true {
                    if pageNum == 1 {
                        if response?.data?.isEmpty == false {
                            self?.resultArr.removeAll()
                            self?.pendingDispatchView.tableView.mj_footer?.resetNoMoreData()
                        }
                        self?.pendingDispatchView.tableView.mj_header?.endRefreshing()
                    }else{
                        if response?.data?.isEmpty == false {
                            self?.pendingDispatchView.tableView.mj_footer?.endRefreshing()
                        }else{
                            self?.pendingDispatchView.tableView.mj_footer?.endRefreshingWithNoMoreData()
                        }
                    }
                    
                    if let array = response?.data {
                        self?.resultArr.append(contentsOf: array)
                        self?.pendingDispatchView.tableView.reloadData()
                    }
                }else{
                    if pageNum == 1 {
                        self?.pendingDispatchView.tableView.mj_header?.endRefreshing()
                    }else{
                        self?.pendingDispatchView.tableView.mj_footer?.endRefreshing()
                    }
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        
        pendingDispatchView.tableView.mj_header?.beginRefreshing()
    }
}

extension RescueIsIngController {
    func addActions() {
        pendingIsIngView.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.pageNum = 1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })
        
        pendingIsIngView.tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in
            self?.pageNum+=1
            self?.refreshRelay.accept(self?.pageNum ?? 1)
        })

        refreshRelay
            .flatMapLatest { pageNum in
                return Observable.zip(RQ.orderList(parameters: OrderListParameters(pageNum: pageNum, queryType: .TO_RESCUE)).asObservable(), Observable.just(pageNum))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] (response, pageNum) in
                if response?.success == true {
                    if pageNum == 1 {
                        if response?.data?.isEmpty == false {
                            self?.resultArr.removeAll()
                            self?.pendingIsIngView.tableView.mj_footer?.resetNoMoreData()
                        }
                        self?.pendingIsIngView.tableView.mj_header?.endRefreshing()
                    }else{
                        if response?.data?.isEmpty == false {
                            self?.pendingIsIngView.tableView.mj_footer?.endRefreshing()
                        }else{
                            self?.pendingIsIngView.tableView.mj_footer?.endRefreshingWithNoMoreData()
                        }
                    }
                    
                    if let array = response?.data {
                        self?.resultArr.append(contentsOf: array)
                        self?.pendingIsIngView.tableView.reloadData()
                    }
                }else{
                    if pageNum == 1 {
                        self?.pendingIsIngView.tableView.mj_header?.endRefreshing()
                    }else{
                        self?.pendingIsIngView.tableView.mj_footer?.endRefreshing()
                    }
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        
        pendingIsIngView.tableView.mj_header?.beginRefreshing()
    }
}

// MARK: JXCategoryViewDelegate,JXCategoryListContainerViewDelegate
extension RescueController : JXCategoryViewDelegate,JXCategoryListContainerViewDelegate {
    func number(ofListsInlistContainerView listContainerView: JXCategoryListContainerView!) -> Int {
        return categoryView.titles.count
    }
    
    func listContainerView(_ listContainerView: JXCategoryListContainerView!, initListFor index: Int) -> JXCategoryListContentViewDelegate! {
        if index == 0 {
            return RescuePendingOrderController.init()
        }
        if index == 1 {
            return RescuePendingDispatchController.init()
        }
        return RescueIsIngController.init()
    }
}

// MARK: UITableViewDelegate,UITableViewDataSource
extension RescuePendingOrderController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? RescuePendingOrderCell
        if cell == nil {
            cell = RescuePendingOrderCell(style: .default, reuseIdentifier: "cell")
            cell?.line.backgroundColor = .hex("E9E9EA").alpha(0.6)
            cell?.merchantTitleLabel.text = "商户名称:"
            cell?.serviceTitleLabel.text = "服务地:"
            cell?.acceptButton.setTitle("接单", for: .normal)
            cell?.refuseButton.setTitle("拒单", for: .normal)
        }
//        
        let model = resultArr[indexPath.row]
        cell?.typeLabel.text = model.taskServiceName
        cell?.orderNumLabel.text = model.orderCode
        cell?.merchantLabel.text = model.contractName
        if model.model?.isEmpty == false {
            cell?.carTypeTitleLabel.text = "车型:"
            cell?.carTypeLabel.text = model.model
            cell?.carTypeTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.merchantLabel.snp.bottom).offset(auto(8))
            })
        }else{
            cell?.carTypeTitleLabel.text = nil
            cell?.carTypeLabel.text = nil
            cell?.carTypeTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.merchantLabel.snp.bottom).offset(0)
            })
        }
        cell?.serviceLabel.text = model.vehiclePointAddress
        if model.destinationAddress?.isEmpty == false {
            cell?.destinationTitleLabel.text = "目的地:"
            cell?.destinationLabel.text = model.destinationAddress
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(auto(8))
            })
        }else{
            cell?.destinationTitleLabel.text = nil
            cell?.destinationLabel.text = nil
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(0)
            })
        }
        if model.appointTime?.isEmpty == false {
            cell?.reserveTitleLabel.text = "预约时间:"
            cell?.reserveLabel.text = model.appointTime
            cell?.reserveTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationLabel.snp.bottom).offset(auto(8))
            })
        }else{
            cell?.reserveTitleLabel.text = nil
            cell?.reserveLabel.text = nil
            cell?.reserveTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationLabel.snp.bottom).offset(0)
            })
        }
        
        cell?.refuseButton.rx.tap
            .subscribe(onNext: {[weak self] _ in
                self?.refuseRemarkIdRelay.accept(model)
            })
            .disposed(by: cell!.disposeBag)
        
        cell?.acceptButton.rx.tap
            .subscribe(onNext: {[weak self] _ in
                self?.acceptOrderRelay.accept(model)
            })
            .disposed(by: disposeBag)
        return cell!
    }
}

extension RescuePendingDispatchController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? RescuePendingDispatchCell
        if cell == nil {
            cell = RescuePendingDispatchCell(style: .default, reuseIdentifier: "cell")
            cell?.line.backgroundColor = .hex("E9E9EA").alpha(0.6)
            cell?.merchantTitleLabel.text = "客商:"
            cell?.positionTitleLabel.text = "所处位置:"
            cell?.serviceTitleLabel.text = "事发地:"
            cell?.contactButton.setTitle("联系客户", for: .normal)
            cell?.dispatchButton.setTitle("指派车辆", for: .normal)
        }
        
        let model = resultArr[indexPath.row]
        cell?.typeLabel.text = model.taskServiceName
        cell?.orderNumLabel.text = model.orderCode
        cell?.merchantLabel.text = model.contractName
        if model.model?.isEmpty == false {
            cell?.carTypeTitleLabel.text = "车型:"
            cell?.carTypeLabel.text = model.model
            cell?.carTypeTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.merchantLabel.snp.bottom).offset(auto(8))
            })
        }else{
            cell?.carTypeTitleLabel.text = nil
            cell?.carTypeLabel.text = nil
            cell?.carTypeTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.merchantLabel.snp.bottom).offset(0)
            })
        }
        
        cell?.serviceLabel.text = model.vehiclePointAddress
        
        if model.vehiclePointRemark?.isEmpty == false {
            cell?.destinationNoticeView.isHidden = false
            cell?.serviceNoticeLabel.text = "备注:"+(model.vehiclePointRemark ?? "")
            cell?.serviceNoticeView.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(auto(2.5))
            })
            cell?.serviceNoticeLabel.snp.updateConstraints({ make in
                make.top.equalTo(auto(5))
                make.bottom.equalTo(-auto(5))
            })
        }else{
            cell?.destinationNoticeView.isHidden = true
            cell?.serviceNoticeLabel.text = nil
            cell?.serviceNoticeView.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(0)
            })
            cell?.serviceNoticeLabel.snp.updateConstraints({ make in
                make.top.equalTo(0)
                make.bottom.equalTo(0)
            })
        }
        if model.destinationAddress?.isEmpty == false {
            cell?.destinationTitleLabel.text = "目的地:"
            cell?.destinationLabel.text = model.destinationAddress
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceNoticeView.snp.bottom).offset(auto(2.5))
            })
        }else{
            cell?.destinationTitleLabel.text = nil
            cell?.destinationLabel.text = nil
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceNoticeView.snp.bottom).offset(0)
            })
        }
        if model.destinationRemark?.isEmpty == false {
            cell?.destinationNoticeView.isHidden = false
            cell?.destinationNoticeLabel.text = "备注:"+(model.destinationRemark ?? "")
            cell?.destinationNoticeView.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationLabel.snp.bottom).offset(auto(2.5))
            })
            cell?.destinationNoticeLabel.snp.updateConstraints({ make in
                make.top.equalTo(auto(5))
                make.bottom.equalTo(-auto(5))
            })
        }else{
            cell?.destinationNoticeView.isHidden = true
            cell?.destinationNoticeLabel.text = nil
            cell?.destinationNoticeView.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationLabel.snp.bottom).offset(0)
            })
            cell?.destinationNoticeLabel.snp.updateConstraints({ make in
                make.top.equalTo(0)
                make.bottom.equalTo(0)
            })
        }
        if model.appointTime?.isEmpty == false {
            cell?.reserveTitleLabel.text = "预约时间:"
            cell?.reserveLabel.text = model.appointTime
            cell?.reserveTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationNoticeView.snp.bottom).offset(auto(2.5))
            })
        }else{
            cell?.reserveTitleLabel.text = nil
            cell?.reserveLabel.text = nil
            cell?.reserveTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.destinationNoticeView.snp.bottom).offset(0)
            })
        }
    
        cell?.photoCollectionView.delegate = self
        cell?.photoCollectionView.dataSource = self
        cell?.photoCollectionView.register(RescuePendingDispatchPhotoCell.self, forCellWithReuseIdentifier: "cell")
        
        if model.environmentPic?.isEmpty == false {
            let environmentPics = model.environmentPic?.components(separatedBy: ",")
            cell?.photoTitleLabel.text = "查看照片:"
            cell?.photoView.isHidden = false
            cell?.photoTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.reserveTitleLabel.snp.bottom).offset(auto(8))
            })
            cell?.photoView.snp.updateConstraints({ make in
                make.height.equalTo(auto(50))
            })
            if let environmentPics {
                self.environmentPics = environmentPics
                cell?.photoCollectionView.reloadData()
            }
        }else{
            cell?.photoTitleLabel.text = nil
            cell?.photoView.isHidden = true
            cell?.photoTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.reserveTitleLabel.snp.bottom).offset(0)
            })
            cell?.photoView.snp.updateConstraints({ make in
                make.height.equalTo(0)
            })
        }
        
        cell?.contactButton.rx.tap
            .subscribe(onNext: { _ in
                TOOL.call(phone: model.userPhone)
            })
            .disposed(by: cell!.disposeBag)
        cell?.dispatchButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                let vc = DispatchOrderController.init(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId,serviceAddressLat: model.vehiclePointLatitude,serviceAddressLon: model.vehiclePointLongitude)
                vc.dispatchCompletionHandler = {[weak self] _ in
                    self?.resultArr.removeAll(where: { _model in
                        _model.userOrderId == model.userOrderId
                    })
                    
                    self?.pendingDispatchView.tableView.reloadData()
                }
                self?.navigationController?.pushViewController(vc, animated: true)
            })
            .disposed(by: cell!.disposeBag)
        return cell!
    }
}

extension RescueIsIngController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? RescueIsIngCell
        if cell == nil {
            cell = RescueIsIngCell(style: .default, reuseIdentifier: "cell")
            cell?.line.backgroundColor = .hex("E9E9EA").alpha(0.6)
            cell?.serviceTitleLabel.text = "故障地:"
            cell?.destinationTitleLabel.text = "目的地:"
            cell?.reportButton.setTitle("报备", for: .normal)
            cell?.dispatchButton.setTitle("改派单", for: .normal)
        }
        let model = resultArr[indexPath.row]
        
        cell?.typeLabel.text = model.taskServiceName
        cell?.orderNumLabel.text = model.orderCode
        cell?.orderDateLabel.text = model.createTime

        if model.driverName?.isEmpty == false && model.drivePhone?.isEmpty == false {
            cell?.driverLabel.text = "\(model.driverName!)/\(model.drivePhone!)"
        }else if model.driverName?.isEmpty == false {
            cell?.driverLabel.text = model.driverName!
        }else if model.drivePhone?.isEmpty == false {
            cell?.driverLabel.text = model.drivePhone!
        }else{
            cell?.driverLabel.text = nil
        }

        cell?.serviceLabel.text = model.vehiclePointAddress
        if model.destinationAddress?.isEmpty == false {
            cell?.destinationTitleLabel.text = "目的地:"
            cell?.destinationLabel.text = model.destinationAddress
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(auto(8))
            })
        }else{
            cell?.destinationTitleLabel.text = nil
            cell?.destinationLabel.text = nil
            cell?.destinationTitleLabel.snp.updateConstraints({ make in
                make.top.equalTo(cell!.serviceLabel.snp.bottom).offset(0)
            })
        }
        
        cell?.dispatchButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                let vc = DispatchOrderController.init(userOrderId: model.userOrderId, taskOrderId: model.taskOrderId,serviceAddressLat: model.vehiclePointLatitude,serviceAddressLon: model.vehiclePointLongitude)
                vc.dispatchCompletionHandler = {[weak self] _ in
                    self?.resultArr.removeAll(where: { _model in
                        _model.userOrderId == model.userOrderId
                    })
                    
                    self?.pendingIsIngView.tableView.reloadData()
                }
                self?.navigationController?.pushViewController(vc, animated: true)
            })
            .disposed(by: cell!.disposeBag)
        return cell!
    }
}

// MARK: UICollectionViewDelegate, UICollectionViewDataSource
extension RescuePendingDispatchController : UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return environmentPics.count > 4 ? 4 : environmentPics.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? RescuePendingDispatchPhotoCell
        let imageUrlStr = environmentPics[indexPath.item]
        if imageUrlStr.contains(".mp4?") == true {
            DispatchQueue.global().async {
                let image = DDImage.dd_thumbnailImage(forVideo: URL(string: imageUrlStr)!)
                DispatchQueue.main.async {
                    cell!.imageView.image = image
                }
            }
            let cmTime = TOOL.getDuration(url: URL(string: imageUrlStr)!)
            cell!.dateLabel.text = TOOL.getVideoDateString(duration: Int(cmTime.seconds))
        }else{
            cell!.imageView.sd_setImage(with: URL(string: imageUrlStr))
            cell!.dateLabel.text = nil
        }
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = RescuePhotoController(environmentPics: environmentPics)
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: 救援中
class RescueController : DDViewController {
    private let categoryView = JXCategoryTitleView()
    private lazy var categoryContainerView = JXCategoryListContainerView(type: .scrollView, delegate: self)
    private let messageTapGes = UITapGestureRecognizer()
    private let messageView = MessageView()
    private let pendingTapGes = UITapGestureRecognizer()
    private let pendingView = MessageView()
    private let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(auto(15))]
        dd_navigationItemTitle = "救援工单"
        navigationItem.leftBarButtonItems = nil
        addSubviews()
        addActions()
    }
    
    func addSubviews() {
        categoryView.titles = ["待接单","待派车","救援中"]
        categoryView.titleColor = .hex("737373").alpha(0.55)
        categoryView.titleSelectedColor = .hex("3678FF")
        categoryView.backgroundColor = .hex("F4F5F7")
        categoryView.delegate = self
        view.addSubview(categoryView)
        categoryView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(CGRectGetMaxY(navigationController?.navigationBar.frame ?? CGRectZero) + CGRectGetHeight(UIApplication.shared.dd_statusBarFrame))
            make.height.equalTo(auto(44))
        }
        // 指示器
        let indicator = JXCategoryIndicatorLineView()
        indicator.indicatorColor = .hex("3678FF")
        indicator.indicatorWidth = JXCategoryViewAutomaticDimension
        indicator.verticalMargin = 7
        categoryView.indicators = [indicator]
        
        // 内容
        view.addSubview(categoryContainerView!)
        categoryView.listContainer = categoryContainerView
        categoryContainerView?.snp.makeConstraints({ make in
            make.top.equalTo(categoryView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-(view.safeAreaInsets.bottom+CGRectGetHeight(tabBarController?.tabBar.frame ?? CGRectZero)))
        })
        
        messageView.titleLabel.text = "消息"
        messageView.addGestureRecognizer(messageTapGes)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: messageView)
        messageView.snp.makeConstraints { make in
            make.height.equalTo(40)
            make.width.greaterThanOrEqualTo(40)
        }

        pendingView.titleLabel.text = "待办"
        pendingView.addGestureRecognizer(pendingTapGes)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: pendingView)
        pendingView.snp.makeConstraints { make in
            make.height.equalTo(40)
            make.width.greaterThanOrEqualTo(40)
        }
    }
}

// MARK: 待派单
class RescuePendingOrderController : DDViewController {
    private let disposeBag = DisposeBag()
    private let pendingOrderView = RescuePendingOrderView()
    private var resultArr : [OrderListDataModel] = []
    private var refreshRelay = ReplayRelay<Int>.create(bufferSize: 1)
    private var pageNum : Int = 1
    private var refuseRemarkIdRelay = ReplayRelay<OrderListDataModel>.create(bufferSize: 1)
    private var refuseSubmitRelay = ReplayRelay<Any?>.create(bufferSize: 1)
    private var acceptOrderRelay = ReplayRelay<OrderListDataModel>.create(bufferSize: 1)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        addSubviews()
        addActions()
    }
    
    func addSubviews() {
        view.addSubview(pendingOrderView)
        pendingOrderView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        pendingOrderView.tableView.delegate = self
        pendingOrderView.tableView.dataSource = self
    }
}

// MARK: 待派车
class RescuePendingDispatchController : DDViewController {
    private let disposeBag = DisposeBag()
    private let pendingDispatchView = RescuePendingDispatchView()
    private var resultArr : [OrderListDataModel] = []
    private var environmentPics : [String] = []
    private var refreshRelay = ReplayRelay<Int>.create(bufferSize: 1)
    private var pageNum : Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        addSubviews()
        addActions()
    }
    
    func addSubviews() {
        view.addSubview(pendingDispatchView)
        pendingDispatchView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        pendingDispatchView.tableView.delegate = self
        pendingDispatchView.tableView.dataSource = self
    }
}

// MARK: 救援中
class RescueIsIngController : DDViewController {
    private let disposeBag = DisposeBag()
    private let pendingIsIngView = RescueIsIngView()
    private var resultArr : [OrderListDataModel] = []
    private var refreshRelay = ReplayRelay<Int>.create(bufferSize: 1)
    private var pageNum : Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        addSubviews()
        addActions()
    }
    
    func addSubviews() {
        view.addSubview(pendingIsIngView)
        pendingIsIngView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        pendingIsIngView.tableView.delegate = self
        pendingIsIngView.tableView.dataSource = self
    }
}

// MARK: Cell
class RescuePendingOrderCell : DDTableViewCell {
    public let radiusView : DDView
    public let typeLabel : DDLabel
    public let orderNumLabel : DDLabel
    public let line : DDView
    public let merchantTitleLabel : DDLabel
    public let merchantLabel : DDLabel
    public let carTypeTitleLabel : DDLabel
    public let carTypeLabel : DDLabel
    public let serviceTitleLabel : DDLabel
    public let serviceLabel : DDLabel
    public let destinationTitleLabel : DDLabel
    public let destinationLabel : DDLabel
    public let reserveTitleLabel : DDLabel
    public let reserveLabel : DDLabel
    public let refuseButton : DDButton
    public let acceptButton : DDButton
    public var acceptLayer : CAGradientLayer = {
        var layer = CAGradientLayer.init()
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = CGPoint(x: 1, y: 1)
        layer.locations = [0.0,1.0]
        layer.colors = [UIColor.hex("FF5A2C").cgColor,UIColor.hex("FE9D4D").cgColor]
        layer.cornerRadius = auto(4)
        layer.masksToBounds = true
        return layer
    }()
    public var disposeBag : DisposeBag
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        radiusView = DDView.init()
        typeLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("000000").alpha(0.85))
        orderNumLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(11)), textColor: .hex("000000").alpha(0.55))
        line = DDView.init()
        merchantTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        merchantLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        carTypeTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        carTypeLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        serviceTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        serviceLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        destinationTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        destinationLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        reserveTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        reserveLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        refuseButton = DDButton.dd_initCustom()
        refuseButton.titleLabel?.font = .mediumFont(auto(13))
        refuseButton.setTitleColor(.hex("000000").alpha(0.7), for: .normal)
        acceptButton = DDButton.dd_initCustom()
        acceptButton.titleLabel?.font = .mediumFont(auto(13))
        acceptButton.setTitleColor(.hex("FFFFFF"), for: .normal)
        disposeBag = DisposeBag()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = .hex("F4F5F7")
        radiusView.layer.cornerRadius = auto(6)
        radiusView.layer.shadowColor = UIColor.black(alpha: 0.1).cgColor
        radiusView.layer.shadowOpacity = 1.0
        radiusView.layer.shadowRadius = 3
        radiusView.layer.shadowOffset = CGSize(width: 0, height: 3)
        radiusView.backgroundColor = .white
        contentView.addSubview(radiusView)
        radiusView.addSubview(typeLabel)
        radiusView.addSubview(orderNumLabel)
        radiusView.addSubview(line)
        radiusView.addSubview(merchantTitleLabel)
        radiusView.addSubview(merchantLabel)
        carTypeLabel.numberOfLines = 0
        radiusView.addSubview(carTypeLabel)
        radiusView.addSubview(carTypeTitleLabel)
        radiusView.addSubview(serviceTitleLabel)
        serviceLabel.numberOfLines = 0
        radiusView.addSubview(serviceLabel)
        radiusView.addSubview(destinationTitleLabel)
        destinationLabel.numberOfLines = 0
        radiusView.addSubview(destinationLabel)
        radiusView.addSubview(reserveTitleLabel)
        radiusView.addSubview(reserveLabel)
        refuseButton.layer.borderColor = UIColor.hex("DDDDDD").cgColor
        refuseButton.layer.borderWidth = 1
        refuseButton.layer.cornerRadius = auto(4)
        radiusView.addSubview(refuseButton)
        acceptButton.layer.cornerRadius = auto(4)
        acceptButton.layer.masksToBounds = true
        acceptButton.backgroundColor = .hex("FF5A2C")
        acceptButton.layer.insertSublayer(acceptLayer, at: 0)
        radiusView.addSubview(acceptButton)
        
        radiusView.snp.makeConstraints { make in
            make.left.equalTo(auto(12))
            make.right.equalTo(-auto(12))
            make.top.equalTo(0)
            make.bottom.equalTo(-auto(12))
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(18))
            make.top.equalTo(auto(13))
        }
        
        orderNumLabel.snp.makeConstraints { make in
            make.centerY.equalTo(typeLabel)
            make.left.equalTo(typeLabel.snp.right).offset(auto(20))
        }
        
        line.snp.makeConstraints { make in
            make.top.equalTo(typeLabel.snp.bottom).offset(auto(7))
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        merchantTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(auto(80))
        }
        
        merchantLabel.snp.makeConstraints { make in
            make.centerY.equalTo(merchantTitleLabel)
            make.left.equalTo(merchantTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        carTypeTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(merchantLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        carTypeLabel.snp.makeConstraints { make in
            make.top.equalTo(carTypeTitleLabel)
            make.left.equalTo(carTypeTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        serviceTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(carTypeLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceTitleLabel)
            make.left.equalTo(serviceTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        destinationTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        destinationLabel.snp.makeConstraints { make in
            make.top.equalTo(destinationTitleLabel)
            make.left.equalTo(destinationTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        reserveTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(destinationLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        reserveLabel.snp.makeConstraints { make in
            make.centerY.equalTo(reserveTitleLabel)
            make.left.equalTo(reserveTitleLabel.snp.right)
        }
        
        acceptButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(10))
            make.top.equalTo(reserveTitleLabel.snp.bottom).offset(auto(10))
            make.width.equalTo(auto(80))
            make.height.equalTo(auto(30))
            make.bottom.equalToSuperview().offset(-auto(10))
        }

        refuseButton.snp.makeConstraints { make in
            make.right.equalTo(acceptButton.snp.left).offset(-auto(10))
            make.top.equalTo(acceptButton)
            make.width.height.equalTo(acceptButton)
        }

    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        acceptLayer.frame = CGRectMake(0, 0, auto(80), auto(30))
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = DisposeBag()
    }
}

class RescuePendingDispatchCell : DDTableViewCell {
    public let radiusView : DDView
    public let typeLabel : DDLabel
    public let orderNumLabel : DDLabel
    public let line : DDView
    public let merchantTitleLabel : DDLabel
    public let merchantLabel : DDLabel
    public let carTypeTitleLabel : DDLabel
    public let carTypeLabel : DDLabel
    public let positionTitleLabel : DDLabel
    public let positionLabel : DDLabel
    public let serviceTitleLabel : DDLabel
    public let serviceLabel : DDLabel
    public let serviceNoticeView : DDView
    public let serviceNoticeLabel : DDLabel
    public let destinationTitleLabel : DDLabel
    public let destinationLabel : DDLabel
    public let destinationNoticeView : DDView
    public let destinationNoticeLabel : DDLabel
    public let reserveTitleLabel : DDLabel
    public let reserveLabel : DDLabel
    public let photoTitleLabel : DDLabel
    public let photoView : DDView
    public let photoCollectionView : DDCollectionView
    public let contactButton : DDButton
    public let dispatchButton : DDButton
    public var dispatchLayer : CAGradientLayer = {
        var layer = CAGradientLayer.init()
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = CGPoint(x: 1, y: 1)
        layer.locations = [0.0,1.0]
        layer.colors = [UIColor.hex("FF5A2C").cgColor,UIColor.hex("FE9D4D").cgColor]
        layer.cornerRadius = auto(4)
        layer.masksToBounds = true
        return layer
    }()
    public var disposeBag : DisposeBag

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        radiusView = DDView.init()
        typeLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("000000").alpha(0.85))
        orderNumLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(11)), textColor: .hex("000000").alpha(0.55))
        line = DDView.init()
        merchantTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        merchantLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        carTypeTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        carTypeLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        positionTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        positionLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        serviceTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        serviceLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        serviceNoticeView = DDView.init()
        serviceNoticeLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(11)), textColor: .hex("FF8F37"))
        destinationTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        destinationLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        destinationNoticeView = DDView.init()
        destinationNoticeLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(11)), textColor: .hex("FF8F37"))
        reserveTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        reserveLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        photoTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        photoView = DDView.init()
        let flowLayout = UICollectionViewFlowLayout.init()
        flowLayout.itemSize = CGSize(width: auto(60), height: auto(45))
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumInteritemSpacing = auto(5)
        photoCollectionView = DDCollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        contactButton = DDButton.dd_initCustom()
        contactButton.titleLabel?.font = .mediumFont(auto(13))
        contactButton.setTitleColor(.hex("000000").alpha(0.7), for: .normal)
        dispatchButton = DDButton.dd_initCustom()
        dispatchButton.setTitleColor(.hex("FFFFFF"), for: .normal)
        dispatchButton.titleLabel?.font = .mediumFont(auto(13))
        disposeBag = DisposeBag()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = .hex("F4F5F7")
        radiusView.layer.cornerRadius = auto(6)
        radiusView.layer.shadowColor = UIColor.black(alpha: 0.1).cgColor
        radiusView.layer.shadowOpacity = 1.0
        radiusView.layer.shadowRadius = 3
        radiusView.layer.shadowOffset = CGSize(width: 0, height: 3)
        radiusView.backgroundColor = .white
        contentView.addSubview(radiusView)
        radiusView.addSubview(typeLabel)
        radiusView.addSubview(orderNumLabel)
        radiusView.addSubview(line)
        radiusView.addSubview(merchantTitleLabel)
        radiusView.addSubview(merchantLabel)
        radiusView.addSubview(positionTitleLabel)
        radiusView.addSubview(positionLabel)
        carTypeLabel.numberOfLines = 0
        radiusView.addSubview(carTypeLabel)
        radiusView.addSubview(carTypeTitleLabel)
        radiusView.addSubview(serviceTitleLabel)
        serviceLabel.numberOfLines = 0
        radiusView.addSubview(serviceLabel)
        serviceNoticeView.backgroundColor = .hex("FFF7EE")
        radiusView.addSubview(serviceNoticeView)
        serviceNoticeLabel.numberOfLines = 0
        serviceNoticeView.addSubview(serviceNoticeLabel)
        radiusView.addSubview(destinationTitleLabel)
        destinationLabel.numberOfLines = 0
        radiusView.addSubview(destinationLabel)
        destinationNoticeView.backgroundColor = .hex("FFF7EE")
        radiusView.addSubview(destinationNoticeView)
        destinationNoticeLabel.numberOfLines = 0
        destinationNoticeView.addSubview(destinationNoticeLabel)
        radiusView.addSubview(reserveTitleLabel)
        radiusView.addSubview(reserveLabel)
        radiusView.addSubview(photoTitleLabel)
        radiusView.addSubview(photoView)
        photoCollectionView.backgroundColor = .white
        photoView.addSubview(photoCollectionView)
        contactButton.layer.borderColor = UIColor.hex("DDDDDD").cgColor
        contactButton.layer.borderWidth = 1
        contactButton.layer.cornerRadius = auto(4)
        radiusView.addSubview(contactButton)
        dispatchButton.layer.cornerRadius = auto(4)
        dispatchButton.layer.masksToBounds = true
        dispatchButton.backgroundColor = .hex("FF5A2C")
        dispatchButton.layer.insertSublayer(dispatchLayer, at: 0)
        radiusView.addSubview(dispatchButton)
        
        radiusView.snp.makeConstraints { make in
            make.left.equalTo(auto(12))
            make.right.equalTo(-auto(12))
            make.top.equalTo(0)
            make.bottom.equalTo(-auto(12))
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(18))
            make.top.equalTo(auto(13))
        }
        
        orderNumLabel.snp.makeConstraints { make in
            make.centerY.equalTo(typeLabel)
            make.left.equalTo(typeLabel.snp.right).offset(auto(20))
        }
        
        line.snp.makeConstraints { make in
            make.top.equalTo(typeLabel.snp.bottom).offset(auto(7))
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        merchantTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(auto(70))
        }
        
        merchantLabel.snp.makeConstraints { make in
            make.centerY.equalTo(merchantTitleLabel)
            make.left.equalTo(merchantTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        carTypeTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(merchantLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        carTypeLabel.snp.makeConstraints { make in
            make.top.equalTo(carTypeTitleLabel)
            make.left.equalTo(carTypeTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        serviceTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(carTypeLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceTitleLabel)
            make.left.equalTo(serviceTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        serviceNoticeView.snp.makeConstraints { make in
            make.top.equalTo(serviceLabel.snp.bottom).offset(auto(2.5))
            make.left.equalTo(auto(10))
            make.right.equalTo(-auto(10))
        }
        
        serviceNoticeLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(auto(5))
            make.bottom.right.equalToSuperview().offset(-auto(5))
        }
        
        destinationTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceNoticeView.snp.bottom).offset(auto(2.5))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        destinationLabel.snp.makeConstraints { make in
            make.top.equalTo(destinationTitleLabel)
            make.left.equalTo(destinationTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        destinationNoticeView.snp.makeConstraints { make in
            make.top.equalTo(destinationLabel.snp.bottom).offset(auto(2.5))
            make.left.equalTo(auto(10))
            make.right.equalTo(-auto(10))
        }
        
        destinationNoticeLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(auto(5))
            make.bottom.right.equalToSuperview().offset(-auto(5))
        }
        
        reserveTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(destinationNoticeView.snp.bottom).offset(auto(2.5))
            make.left.equalTo(typeLabel)
            make.width.equalTo(merchantTitleLabel)
        }
        
        reserveLabel.snp.makeConstraints { make in
            make.centerY.equalTo(reserveTitleLabel)
            make.left.equalTo(reserveTitleLabel.snp.right)
        }
        
        photoTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(typeLabel)
            make.top.equalTo(reserveTitleLabel.snp.bottom).offset(auto(8))
            make.width.equalTo(merchantTitleLabel)
        }
        
        photoView.snp.makeConstraints { make in
            make.left.equalTo(photoTitleLabel.snp.right)
            make.top.equalTo(photoTitleLabel)
            make.right.equalToSuperview().offset(-auto(5))
            make.height.equalTo(auto(50))
        }
        
        photoCollectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        dispatchButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(10))
            make.top.equalTo(photoView.snp.bottom).offset(auto(10))
            make.width.equalTo(auto(80))
            make.height.equalTo(auto(30))
            make.bottom.equalToSuperview().offset(-auto(10))
        }

        contactButton.snp.makeConstraints { make in
            make.right.equalTo(dispatchButton.snp.left).offset(-auto(10))
            make.top.equalTo(dispatchButton)
            make.width.height.equalTo(dispatchButton)
        }

    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        dispatchLayer.frame = CGRectMake(0, 0, auto(80), auto(30))
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = DisposeBag()
    }
}

class RescuePendingDispatchPhotoCell : DDCollectionViewCell {
    public let imageView : DDImageView
    public let dateLabel : DDLabel

    override init(frame: CGRect) {
        imageView = DDImageView.init()
        dateLabel = DDLabel.dd_init(withText: "", font: .regularFont(12), textColor: .white)
        super.init(frame: frame)
        contentView.backgroundColor = .hex("F4F5F7")
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        addSubview(imageView)
        addSubview(dateLabel)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        dateLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(2))
            make.bottom.equalToSuperview().offset(-auto(2))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class RescueIsIngCell : DDTableViewCell {
    public let radiusView : DDView
    public let typeLabel : DDLabel
    public let orderNumLabel : DDLabel
    public let orderStatusLabel : DDLabel
    public let line : DDView
    public let orderDateLabel : DDLabel
    public let driverLabel : DDLabel
    public let serviceTitleLabel : DDLabel
    public let serviceLabel : DDLabel
    public let destinationTitleLabel : DDLabel
    public let destinationLabel : DDLabel
    public let dispatchButton : DDButton
    public let reportButton : DDButton
    public var disposeBag : DisposeBag

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        radiusView = DDView.init()
        typeLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("000000").alpha(0.85))
        orderNumLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(11)), textColor: .hex("000000").alpha(0.55))
        orderStatusLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("E69B0B"))
        line = DDView.init()
        orderDateLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("252525"))
        driverLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("252525"))
        serviceTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        serviceLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        destinationTitleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("000000").alpha(0.5))
        destinationLabel = DDLabel.dd_init(withText: "", font: .regularFont(auto(12)), textColor: .hex("000000"))
        dispatchButton = DDButton.dd_initCustom()
        dispatchButton.titleLabel?.font = .mediumFont(13)
        dispatchButton.setTitleColor(.hex("000000").alpha(0.7), for: .normal)
        reportButton = DDButton.dd_initCustom()
        reportButton.titleLabel?.font = .mediumFont(13)
        reportButton.setTitleColor(.hex("FFFFFF"), for: .normal)
        disposeBag = DisposeBag()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = .hex("F4F5F7")
        radiusView.layer.cornerRadius = auto(6)
        radiusView.layer.shadowColor = UIColor.black(alpha: 0.1).cgColor
        radiusView.layer.shadowOpacity = 1.0
        radiusView.layer.shadowRadius = 3
        radiusView.layer.shadowOffset = CGSize(width: 0, height: 3)
        radiusView.backgroundColor = .white
        contentView.addSubview(radiusView)
        radiusView.addSubview(typeLabel)
        radiusView.addSubview(orderNumLabel)
        radiusView.addSubview(orderStatusLabel)
        radiusView.addSubview(line)
        radiusView.addSubview(orderDateLabel)
        radiusView.addSubview(driverLabel)
        radiusView.addSubview(serviceTitleLabel)
        serviceLabel.numberOfLines = 0
        radiusView.addSubview(serviceLabel)
        radiusView.addSubview(destinationTitleLabel)
        destinationLabel.numberOfLines = 0
        radiusView.addSubview(destinationLabel)
        dispatchButton.layer.borderColor = UIColor.hex("DDDDDD").cgColor
        dispatchButton.layer.borderWidth = 1
        dispatchButton.layer.cornerRadius = auto(4)
        radiusView.addSubview(dispatchButton)
        reportButton.layer.cornerRadius = auto(4)
        reportButton.layer.masksToBounds = true
        reportButton.backgroundColor = .hex("2A5094")
        radiusView.addSubview(reportButton)
        
        radiusView.snp.makeConstraints { make in
            make.left.equalTo(auto(12))
            make.right.equalTo(-auto(12))
            make.top.equalTo(0)
            make.bottom.equalTo(-auto(12))
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(18))
            make.top.equalTo(auto(13))
        }
        
        orderNumLabel.snp.makeConstraints { make in
            make.centerY.equalTo(typeLabel)
            make.left.equalTo(typeLabel.snp.right).offset(auto(20))
        }
        
        orderStatusLabel.snp.makeConstraints { make in
            make.right.equalTo(-auto(20))
            make.centerY.equalTo(orderNumLabel)
        }
        
        line.snp.makeConstraints { make in
            make.top.equalTo(typeLabel.snp.bottom).offset(auto(7))
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        orderDateLabel.snp.makeConstraints { make in
            make.left.equalTo(typeLabel)
            make.top.equalTo(line.snp.bottom).offset(auto(5))
        }
        
        driverLabel.snp.makeConstraints { make in
            make.left.equalTo(orderDateLabel.snp.right).offset(auto(10))
            make.centerY.equalTo(orderDateLabel)
        }

        serviceTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(driverLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(auto(60))
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceTitleLabel)
            make.left.equalTo(serviceTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }
        
        destinationTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(serviceLabel.snp.bottom).offset(auto(8))
            make.left.equalTo(typeLabel)
            make.width.equalTo(serviceTitleLabel)
        }
        
        destinationLabel.snp.makeConstraints { make in
            make.top.equalTo(destinationTitleLabel)
            make.left.equalTo(destinationTitleLabel.snp.right)
            make.right.equalTo(-auto(20))
        }

        reportButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(10))
            make.top.equalTo(destinationLabel.snp.bottom).offset(auto(10))
            make.width.equalTo(auto(80))
            make.height.equalTo(auto(30))
            make.bottom.equalToSuperview().offset(-auto(10))
        }

        dispatchButton.snp.makeConstraints { make in
            make.right.equalTo(reportButton.snp.left).offset(-auto(10))
            make.top.equalTo(reportButton)
            make.width.height.equalTo(reportButton)
        }

    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = DisposeBag()
    }
}
// MARK: View
class RescuePendingOrderView : DDView {
    public let tableView = DDTableView.init(frame: CGRectZero)
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.separatorStyle = .none
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class RescuePendingDispatchView : DDView {
    public let tableView = DDTableView.init(frame: CGRectZero)
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.separatorStyle = .none
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class RescueIsIngView : DDView {
    public let tableView = DDTableView.init(frame: CGRectZero)
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.separatorStyle = .none
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: JXCategoryListContentViewDelegate
extension RescuePendingOrderController : JXCategoryListContentViewDelegate {
    func listView() -> UIView! {
        return view
    }
}

extension RescuePendingDispatchController : JXCategoryListContentViewDelegate {
    func listView() -> UIView! {
        return view
    }
}

extension RescueIsIngController : JXCategoryListContentViewDelegate {
    func listView() -> UIView! {
        return view
    }
}
