// // SetMealDetailVC.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/26. // import UIKit //import MJRefresh import RxSwift import RxCocoa import RxAlamofire import Alamofire import HandyJSON class SetMealDetailVC: ADHBaseVC { /// 菜品/套餐id var mealId : NSNumber?{ didSet{ self.viewModel.getSMDetailRequestModel.mealId = mealId } } /// /// 菜品类型,1为单品,2为套餐 var mealType : String?{ didSet{ self.viewModel.getSMDetailRequestModel.mealType = mealType } } /// 供餐类型(早餐、中餐、晚餐) var supplyTypeId : String?{ didSet{ self.viewModel.getSMDetailRequestModel.supplyTypeId = supplyTypeId self.viewModel.addShoppingRequestModel.supplyTypeId = supplyTypeId } } /// 菜品分类 var runTypeId : String?{ didSet{ self.viewModel.getSMDetailRequestModel.runTypeId = runTypeId self.viewModel.addShoppingRequestModel.runTypeId = runTypeId } } /// 时间 var shopcartTime : String?{ didSet{ self.viewModel.addShoppingRequestModel.shopcartTime = shopcartTime } } /// 时间id var shopcartTimeId : NSNumber?{ didSet{ self.viewModel.addShoppingRequestModel.shopcartTimeId = shopcartTimeId self.viewModel.getTimeRequestModel.id = shopcartTimeId } } /// 购物车id var shopcartId : NSNumber?{ didSet{ self.viewModel.addShoppingRequestModel.shopcartId = shopcartId } } @IBOutlet weak var addShopBtn: UIButton! @IBOutlet weak var priceLab: UILabel! @IBOutlet weak var attentionLab: UILabel! /// 展示更多 var showMore = false @IBOutlet weak var bottonView: UIView! //HeadView let mealHeadView = Bundle.main.loadNibNamed("MealHeadView", owner: nil, options: nil)?.last as? MealHeadView //固定套餐列表 let mealCollectionView = Bundle.main.loadNibNamed("MealCollectionView", owner: nil, options: nil)?.last as? MealCollectionView //非固定套餐列表 let changeMealCollectionView = Bundle.main.loadNibNamed("ChangeMealCollectionView", owner: nil, options: nil)?.last as? ChangeMealCollectionView //配送时间选择 let deliveryTimeSetView = Bundle.main.loadNibNamed("DeliveryTimeSetView", owner: nil, options: nil)?.last as? DeliveryTimeSetView //需知 let needKonwView = Bundle.main.loadNibNamed("NeedKonwView", owner: nil, options: nil)?.last as? NeedKonwView //替换菜品 let replaceFoodView = Bundle.main.loadNibNamed("ReplaceFoodView", owner: nil, options: nil)?.last as? ReplaceFoodView let viewModel : SetMealViewModel = SetMealViewModel.init() let diposableBag = DisposeBag() /// 当前更换的Model var willReplaceModel : MealFoodMsgModel? //子视图列表 var viewList : [Any]? override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) } override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white // Do any additional setup after loading the view. self.navBar = MCNavBarView.init(title:"", subTitle: nil,style: nil) self.navBar?.insertNavRightBtn(nil, image: "分享图标", tap: {[unowned self] in }, semantic: nil, at: 0) self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal) self.addShopBtn.isUserInteractionEnabled = false //MARK: 获取详情 self.hud.showLoading() self.viewModel.signalGetSetMealDetail()?.on(success: { dd in self.mealHeadView?.configModel(model: self.viewModel.SMDetailModel!) self.mealCollectionView!.configModel(model: self.viewModel.SMDetailModel!) self.changeMealCollectionView!.configModel(model: self.viewModel.SMDetailModel!) self.viewModel.addShoppingRequestModel.activityId = self.viewModel.SMDetailModel?.activityId self.viewModel.addShoppingRequestModel.discountId = self.viewModel.SMDetailModel?.discountId self.viewModel.addShoppingRequestModel.isGeneral = self.viewModel.SMDetailModel?.isGeneral self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType self.viewModel.addShoppingRequestModel.isFixed = self.viewModel.SMDetailModel?.isFixed self.viewModel.addShoppingRequestModel.mealId = self.viewModel.SMDetailModel?.mealId // self.viewModel.addShoppingRequestModel.runTypeId = self.viewModel.SMDetailModel?.runTypeId // self.viewModel.addShoppingRequestModel.supplyTypeId = self.viewModel.SMDetailModel?.supplyTypeId self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType self.setPriceShow() if self.chenkSaveData(){ } self.hud.hidden() self.getDeliveryTime() }, error: { error in self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } func getDeliveryTime(){ //MARK: 获取配送时段 self.viewModel.signalGetDeliveryTime()?.on(success: { dd in self.deliveryTimeSetView?.configModel(dataSource: self.viewModel.deliveryTimeList!) self.createUI() }, error: { error in self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } //MARK: createUI func createUI(){ self.view.addSubview(self.scrollView) self.scrollView.backgroundColor = UIColor.clear self.scrollView.mas_remakeConstraints { make in make?.left.right().mas_equalTo()(self.view) make?.top.mas_equalTo()(self.view)?.offset()(kNavAndStatuHeight) make?.bottom.mas_equalTo()(self.bottonView.mas_top) make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight - 76 - kNavAndStatuHeight) make?.width.offset()(kSCREEN_WIDTH) } if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{ self.viewList = [self.mealHeadView!,self.changeMealCollectionView!,self.deliveryTimeSetView!] }else{ self.viewList = [self.mealHeadView!,self.mealCollectionView!,self.deliveryTimeSetView!] } self.scrollView.addSubview(self.mealHeadView!) if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{ self.scrollView.addSubview(self.changeMealCollectionView!) }else{ self.scrollView.addSubview(self.mealCollectionView!) } self.scrollView.addSubview(self.deliveryTimeSetView!) self.resetConstraint() //查看评论 self.mealHeadView?.evaluateActionBlock = {[weak self] in let vc = MealEvaluateListVC() vc.mealId = self?.mealId self?.navigationController?.pushViewController(vc, animated: true) } self.mealCollectionView?.showMoreActionBlock = {[weak self] isShow in self?.showMore = isShow self?.resetConstraint() } self.mealCollectionView?.needKnowBlock = {[weak self] in self?.needKonwView?.showWithString(detail: self?.viewModel.SMDetailModel?.introduce) } self.mealCollectionView?.changeFoodBlock = {[weak self] in self?.setPriceShow() if (self?.chenkSaveData() ?? false){ } } //MARK: 获取配送时段 self.changeMealCollectionView?.changeFoodBlock = {[weak self] model , row in self?.viewModel.getReplaceFoodRequestModel.poolId = model.replacePoolId self?.viewModel.signalGetReplaceFood()?.on(success: { dd in self?.replaceFoodView?.showWithModel(models: self?.viewModel.replaceFoodModels, row: row, foodName: model.name ?? "") }, error: { error in self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } self.deliveryTimeSetView?.selectTimeBlock = {[weak self] model in self?.viewModel.addShoppingRequestModel.deliveryStart = model.startTime self?.viewModel.addShoppingRequestModel.deliveryEnd = model.endTime self?.viewModel.addShoppingRequestModel.deliveryId = model.id if (self?.chenkSaveData() ?? false){ } } self.deliveryTimeSetView?.inputNumBlock = {[weak self] numStr in self?.viewModel.addShoppingRequestModel.numPeople = numStr self?.setPriceShow() if (self?.chenkSaveData() ?? false){ } } //替换菜品的替换ID self.replaceFoodView?.selectBlock = {[weak self] model , row in let oldModel : MealFoodMsgModel = (self?.viewModel.SMDetailModel?.mealFoodMsgs?[row])! oldModel.link = model.link oldModel.name = model.name oldModel.replaceId = model.id self?.changeMealCollectionView?.collectView.reloadData() } } func setPriceShow(){ if (self.viewModel.SMDetailModel?.isFixed ?? "0") === 1{ self.priceLab.text = self.viewModel.SMDetailModel?.price ** self.viewModel.addShoppingRequestModel.numPeople }else{ var foods : [NSNumber]? = [] for item in self.viewModel.SMDetailModel!.mealFoodMsgs! { let model : MealFoodMsgModel = item if model.selected{ foods?.append(model.id!) } } if let _ = self.viewModel.SMDetailModel?.mealPrices{ for item in self.viewModel.SMDetailModel!.mealPrices!{ let model : MealPricesModel = item if model.quantity === (foods?.count ?? 0){ self.priceLab.text = model.amount ** self.viewModel.addShoppingRequestModel.numPeople break }else{ self.priceLab.text = "" } } } } } func chenkSaveData() ->(Bool){ if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{ self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请先设置用餐人数", Etext: "Please set the number of diners first") self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal) self.addShopBtn.isUserInteractionEnabled = false return false } if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){ self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.viewModel.SMDetailModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.viewModel.SMDetailModel?.lowestNum ?? "1")") self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal) self.addShopBtn.isUserInteractionEnabled = false return false } if self.viewModel.addShoppingRequestModel.deliveryId.isNil{ self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请选择配送时间", Etext: "Please select a delivery time") self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal) self.addShopBtn.isUserInteractionEnabled = false return false } if ((self.viewModel.SMDetailModel?.isFixed ?? "") == "0"){ //非固定套餐 var fixedFoods : [NSNumber]? = [] for item in self.viewModel.SMDetailModel!.mealFoodMsgs! { let model : MealFoodMsgModel = item if model.selected{ fixedFoods?.append(model.id!) } } var isPeopleNUmOK = false if let _ = self.viewModel.SMDetailModel?.mealPrices{ for item in self.viewModel.SMDetailModel!.mealPrices! { let priceModel : MealPricesModel = item if priceModel.quantity === fixedFoods?.count{ isPeopleNUmOK = true } } }else{ isPeopleNUmOK = false } if isPeopleNUmOK{ self.addShopBtn.setBackColor(kThemeColor, forState: .normal) self.addShopBtn.isUserInteractionEnabled = true }else{ self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal) self.addShopBtn.isUserInteractionEnabled = false } return isPeopleNUmOK } self.addShopBtn.setBackColor(kThemeColor, forState: .normal) self.addShopBtn.isUserInteractionEnabled = true return true } //MARK: 重设约束 func resetConstraint(){ var contentSizeH : CGFloat = 0 for i in 0.. 0 { lin2 = 1 } if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{ make?.height.offset()(56 + 170 * CGFloat(lin1 + lin2) + 25) }else{ //self.viewModel.deliveryTimeList?.count date数量 2两列 取整数取余数 if (self.viewModel.SMDetailModel?.mealFoodMsgs?.count ?? 0) > 9{ if showMore{ make?.height.offset()(93 + 29 + 138 * CGFloat(lin1 + lin2) - 12) }else{ make?.height.offset()(93 + 29 + 138 * 3 - 12) } }else{ make?.height.offset()(93 + 29 + 138 * CGFloat(lin1 + lin2) - 12) } } } if i == 2{ if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{ make?.top.mas_equalTo()(self.changeMealCollectionView?.mas_bottom) }else{ make?.top.mas_equalTo()(self.mealCollectionView?.mas_bottom) } make?.width.offset()(kSCREEN_WIDTH) let lin1 = (self.viewModel.deliveryTimeList?.count ?? 0)/3 var lin2 = (self.viewModel.deliveryTimeList?.count ?? 0)%3 if lin2 > 0 { lin2 = 1 } make?.height.offset()(154 + 44 * CGFloat(lin1 + lin2) - 12) make?.bottom.mas_equalTo()(self.scrollView) } } contentSizeH = contentSizeH + itme.frame.size.height } scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH) } //MARK: scrollView private var scrollView : UIScrollView = { let scrollView = UIScrollView.init(frame: CGRect.zero) scrollView.alwaysBounceVertical = true scrollView.isScrollEnabled = true scrollView.showsVerticalScrollIndicator = false scrollView.backgroundColor = kTBackgroundColor scrollView.contentInsetAdjustmentBehavior = .never return scrollView }() func getReplaceFoods(){ // //MARK: 获取配送时段 // self.viewModel.getReplaceFoodRequestModel.poolId = self.willReplaceModel?.replacePoolId // self.viewModel.signalGetReplaceFood()?.on(success: { dd in // self.replaceFoodView?.showWithModel(models: self.viewModel.replaceFoodModels!) // }, error: { error in // self.hud.showFailure("网络错误!") // }) } @IBAction func addShoppingAction(_ sender: UIButton) { if self.viewModel.addShoppingRequestModel.deliveryStart.isEmptyStr{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time")) return } if self.viewModel.addShoppingRequestModel.deliveryEnd.isEmptyStr{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time")) return } if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请输入就餐人数", Etext: "Please enter the number of diners")) return } if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.viewModel.SMDetailModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.viewModel.SMDetailModel?.lowestNum ?? "1")")) return } if ((self.viewModel.SMDetailModel?.isFixed ?? "") == "1"){ //固定套餐 self.viewModel.addShoppingRequestModel.mealFoodMsgs = self.viewModel.SMDetailModel?.mealFoodMsgs }else{ //非固定套餐 var fixedFoods : [NSNumber]? = [] for item in self.viewModel.SMDetailModel!.mealFoodMsgs! { let model : MealFoodMsgModel = item if model.selected{ fixedFoods?.append(model.id!) } } self.viewModel.addShoppingRequestModel.fixedFoods = fixedFoods var isPeopleNUmOK = false if let _ = self.viewModel.SMDetailModel?.mealPrices{ for item in self.viewModel.SMDetailModel!.mealPrices! { let priceModel : MealPricesModel = item if priceModel.quantity === fixedFoods?.count{ isPeopleNUmOK = true } } }else{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "固定套餐规则为空", Etext: "固定套餐规则为空")) return } if isPeopleNUmOK{ }else{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "固定套餐规则为空", Etext: "固定套餐规则为空")) return } } // self.viewModel.addShoppingRequestModel.mealFoodMsgs = //MARK: 加入购物车 self.viewModel.signalAddShopping()?.on(success: { dd in self.hud.showSuccess() NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil) self.navigationController?.popViewController(animated: true) }, error: { error in self.hud.showFailure("\(error.msg ?? "")") }) } }