// // ShoppingCarDetailView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/11/1. // import UIKit import RxSwift import RxCocoa class ShoppingCarDetailView: UIView { /// 购物车类型 var shopType : String?{ didSet{ self.viewModel.HomeTimeGetModel.shopType = shopType } } /// 购物车ID var shopcartId : NSNumber?{ didSet{ self.viewModel.getShoppingCarMsgResultMeal.shopcartId = shopcartId self.viewModel.editShoppingCarRequestModel.shopcartId = shopcartId self.viewModel.cleanShopcarModel.id = shopcartId } } /// 购物车时间ID var shopcartTimeId : NSNumber?{ didSet{ self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = shopcartTimeId } } @IBOutlet weak var blackView: UIView! let dateView = Bundle.main.loadNibNamed("HomeDateView", owner: nil, options: nil)?.last as? HomeDateView let editorDeliveryTimeView = Bundle.main.loadNibNamed("EditorDeliveryView", owner: nil, options: nil)?.last as? EditorDeliveryView let editPersonNumView = Bundle.main.loadNibNamed("EditPersonNumView", owner: nil, options: nil)?.last as? EditPersonNumView let adhAV = Bundle.main.loadNibNamed("ADHAlertView", owner: nil, options: nil)?.last as? ADHAlertView @IBOutlet weak var priceLab: UILabel! @IBOutlet weak var tableView: UITableView! var editModel : ShopMealMsgDetailModel? /// 当前选择的时间 var nowTimeModel : HomeTimeItem? let viewModel : ShoppingCarViewModel = ShoppingCarViewModel.init() let diposableBag = DisposeBag() var isRefresh = true lazy var hud : MCHud! = { return MCHud() }() var reFreshBlock : (()->Void)? var checkOrderBlock : (()->Void)? override func awakeFromNib() { super.awakeFromNib() tableView.register(withType: MenuTableViewCell.self) tableView.separatorStyle = UITableViewCell.SeparatorStyle.none self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in self.isRefresh = true self.getShopCarDetail() }); self.tableView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in self?.isRefresh = true self?.getShopCarDetail() }) self.dateView?.deletBtn.isHidden = false self.dateView?.timeBtn.isHidden = true self.dateView?.titleLab.text = LanguagesUtil.createTextBy(Ctext: "已选菜品", Etext: "selected foods") self.addSubview(self.dateView!) self.dateView!.mas_remakeConstraints { make in make?.left.right().mas_equalTo()(self) make?.top.mas_equalTo()(self)?.offset()(200) make?.height.offset()(180) } self.dateView?.selectBlock = {[weak self] model in self?.nowTimeModel = model self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = self?.nowTimeModel!.shopcartTimeId self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = model.shopcartTimeId self?.getShopCarDetail() self?.reloadDateView() } self.dateView?.deletActionBlock = {[weak self] in self?.adhAV?.show() } self.adhAV?.sureActionBlock = {[weak self] in //清空购物车 self?.viewModel.signalCleanShopCar()?.on(success: { dd in NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil) self?.hidden() }, error: { error in self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } self.editorDeliveryTimeView?.selectTimeBlock = {[weak self] model in self?.viewModel.editShoppingCarRequestModel.shopMealId = self?.editModel?.shopMealId self?.viewModel.editShoppingCarRequestModel.mealType = self?.editModel?.mealType self?.viewModel.editShoppingCarRequestModel.shopcartTimeId = self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId self?.viewModel.editShoppingCarRequestModel.deliveryId = model.id self?.viewModel.signalEditShopCar()?.on(success: { dd in self?.getShopCarDetail() self?.getShopCarPrice() }, error: { error in self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } self.editPersonNumView?.inputBlock = {[weak self] num in if num < Int(self?.editModel?.lowestNum ?? "1"){ self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self?.editModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self?.editModel?.lowestNum ?? "1")")) return } self?.viewModel.editShoppingCarRequestModel.shopMealId = self?.editModel?.shopMealId self?.viewModel.editShoppingCarRequestModel.mealType = self?.editModel?.mealType self?.viewModel.editShoppingCarRequestModel.shopcartTimeId = self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId self?.viewModel.editShoppingCarRequestModel.numPeople = num self?.viewModel.signalEditShopCar()?.on(success: { dd in self?.getShopCarDetail() self?.getShopCarPrice() }, error: { error in self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } } //MARK: 获取时间范围 func getTimeRangDetail(){ self.hud.showLoading() self.viewModel.signalGetHomeTime()?.on(success: { dd in for item in self.viewModel.TimeRangDetail!.timeVoList! { let model : HomeTimeItem = item if model.day == self.nowTimeModel?.day{ model.isSelect = true } } self.getShopCarPrice() self.reloadDateView() self.hud.hidden() }, error: { error in self.hud.showFailure("获取时间范围失败") }) } //MARK: 获取日期下购物车详情 func getShopCarDetail(){ if isRefresh{ self.hud.showLoading() NSLog("刷新") }else{ NSLog("计算") } self.viewModel.signalgetShoppingCarMsg()?.on(success: { dd in self.tableView.reloadData() self.hud.hidden() self.tableView.mj_header.endRefreshing() }, error: { error in self.hud.showFailure("获取失败") self.tableView.mj_header.endRefreshing() }) } //MARK: 获取购物车价格详情 func getShopCarPrice(){ self.viewModel.getShopCarPriceRModel.id = self.viewModel.TimeRangDetail?.shopcartId self.viewModel.signalGetShopCarPrice()?.on(success: { dd in self.priceLab.text = "\(self.viewModel.shopCarPriceDetail?.price ?? "")" }, error: { error in self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } //MARK: 获取配送时段 func getDeliveryTime(){ self.viewModel.signalGetDeliveryTime()?.on(success: { dd in }, error: { error in self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } func reloadDateView(){ self.dateView!.configModel(dateModel: self.viewModel.TimeRangDetail!) } @IBAction func gotoJIesuan(_ sender: UIButton) { if self.checkOrderBlock != nil{ self.checkOrderBlock!() } self.hidden() } public func show(){ self.getTimeRangDetail() self.getShopCarDetail() self.getDeliveryTime() kAppDelegateWindow.addSubview(self) self.blackView.alpha = 0 self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) UIView.animate(withDuration: 0.5) { self.frame = CGRect.init(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) self.layoutIfNeeded() } DELAY(0.5) { self.blackView.alpha = 0.3 } } public func hidden(){ self.blackView.alpha = 0 UIView.animate(withDuration: 0.5) { self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) self.layoutIfNeeded() } DELAY(0.5) { self.removeFromSuperview() } } @IBAction func backTouchAction(_ sender: UIButton) { if self.reFreshBlock != nil{ self.reFreshBlock!() } self.hidden() } func add(){ if (self.editModel?.numPeople ?? "0") < Int(self.editModel?.lowestNum ?? "1"){ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.editModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.editModel?.lowestNum ?? "1")") ,delay: 1.5) self.getShopCarDetail() self.getShopCarPrice() }else{ self.viewModel.editShoppingCarRequestModel.shopMealId = self.editModel?.shopMealId self.viewModel.editShoppingCarRequestModel.mealType = self.editModel?.mealType self.viewModel.editShoppingCarRequestModel.shopcartTimeId = self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId self.viewModel.editShoppingCarRequestModel.numPeople = self.editModel?.numPeople self.viewModel.signalEditShopCar()?.on(success: { dd in self.getShopCarDetail() self.getShopCarPrice() }, error: { error in self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } } func deleteMeal(){ self.viewModel.editShoppingCarRequestModel.shopMealId = self.editModel?.shopMealId self.viewModel.editShoppingCarRequestModel.mealType = self.editModel?.mealType self.viewModel.editShoppingCarRequestModel.shopcartTimeId = self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId self.viewModel.editShoppingCarRequestModel.del = "1" self.viewModel.signalEditShopCar()?.on(success: { dd in self.getTimeRangDetail() self.getShopCarDetail() self.getShopCarPrice() }, error: { error in self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } } extension ShoppingCarDetailView : UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.viewModel.ShopCarAllModelList?[section].shopMealMsgVos?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell; let batchModel = self.viewModel.ShopCarAllModelList?[indexPath.section] let model = (batchModel?.shopMealMsgVos?[indexPath.row])! var isfirst = indexPath.row == 0 ? true : false cell.configModel(model: model, isfirstLine: isfirst) //加上左滑事件 cell.addLeftSlideAction() cell.addOrSubtractionBlock = {[weak self] isAdd in self?.editModel = model self?.isRefresh = false self?.add() } cell.selectTimeActionBlock = {[weak self] in self?.editorDeliveryTimeView?.configModel(dataSource: self?.viewModel.deliveryTimeList) self?.editModel = model } cell.inPutNumBlock = {[weak self] in self?.editModel = model self?.editPersonNumView?.showWithNum(model.numPeople!) } cell.deleteBlock = {[weak self] in self?.editModel = model self?.deleteMeal() } return cell } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 0.01 } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 0.01 } // func numberOfSections(in tableView: UITableView) -> Int { return self.viewModel.ShopCarAllModelList?.count ?? 0 } }