// // IHCountryPicker.swift // Inhealth // // Created by weclouds on 2020/3/13. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import IBAnimatable import PKHUD class IHCountryPicker: AnimatableModalViewController { var isClickBuild:Bool = false //判断是否已经选择了楼房 这个很关键 var buildArr:[(hotelName: String, hotelId:String, buildList:[DropDownData], isExpand:Bool)]? = [(String,String,[DropDownData],Bool)]() var cityList: [DropDownData]?{ didSet{ if let list = self.cityList { if list.count > 0 { self.tableView.reloadSections([0], with: .automatic) self.buildArr?.removeAll() //重新请求 //获取第一个城市 let firstCity = list.first getHotelList(firstCity?.id, city: firstCity) Intermediate.cityId = firstCity!.id! Intermediate.cityName = firstCity!.name! } } } } var historyCity:DropDownData?//保存搜索过的城市 var historyHotel:DropDownData? //保存搜索过的酒店 var completion:(()->Void)? //用来存储这五个值 var selectedBuid:DropDownData? var selectedHotel:(hotelName: String, hotelId:String, buildList:[DropDownData], isExpand:Bool)? var cityId:String?{ didSet{ //如果没有则用provinceId province也没有就用countryId if let cityId = self.cityId { getHotelList(cityId, city: nil) } } } @IBOutlet weak var contentView: UIView! lazy var tableView: UITableView = { let tableView = UITableView(frame: CGRect.zero, style: .plain) tableView.delegate = self tableView.dataSource = self tableView.backgroundColor = .white tableView.separatorStyle = .none tableView.register(UINib(nibName: "IHTPickerAreaCell", bundle: nil), forCellReuseIdentifier: "areaCell") tableView.register(UINib(nibName: "IHPickFloorCell", bundle: nil), forCellReuseIdentifier: "floorCell") return tableView }() override func viewDidLoad() { super.viewDidLoad() //buildArr?.removeAll() self.contentView.addSubview(tableView) } @IBAction func close(_ sender: Any) { self.dismiss(animated: true, completion: nil) } @IBAction func save(_ sender: Any) { if self.selectedBuid == nil || isClickBuild == false { //Please selected building g_showHUD("请选择区域") return } if self.selectedHotel == nil { //Please selecte hotel g_showHUD("请选择酒店/学校") return } log.debug("-- 选择了 - \(self.selectedBuid?.id) -buildname-- \(self.selectedBuid?.name)") // let hotel = IHHistoryHotelModel(id: selectedHotel!.hotelId, name: selectedHotel!.hotelName) let hotel = IHHistoryHotelModel(id: selectedHotel!.hotelId, name: selectedHotel!.hotelName, countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: Intermediate.provinceId, provinceName: Intermediate.provinceName, cityId: Intermediate.cityId, cityName: Intermediate.cityName, buildId: Intermediate.buildId, buildName: Intermediate.buildName) IHHistroyPlist.share.addHotel(hotel) Intermediate.hotelId = selectedHotel!.hotelId Intermediate.hotelName = selectedHotel!.hotelName Intermediate.buildId = selectedBuid!.id! Intermediate.buildName = selectedBuid!.name! let info = StorageInfo(countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: Intermediate.provinceId, provinceName: Intermediate.provinceName, cityId: Intermediate.cityId, cityName: Intermediate.cityName, hotelId: Intermediate.hotelId, hotelName: Intermediate.hotelName, buildId: Intermediate.buildId, buildName: Intermediate.buildName) IHHotelStorage.shareInstance.storageHotelInfo(info) NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil) NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHTabBarControllerUpdateBadgeNumber), object: Intermediate.hotelId) self.dismiss(animated: true, completion: nil) if let block = self.completion { NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHHotelPickerViewGetHistory), object: nil) block() isClickBuild = false } } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() tableView.frame = self.contentView.bounds } } extension IHCountryPicker:UITableViewDelegate,UITableViewDataSource{ func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return 1 }else{ return self.buildArr?.count ?? 0 } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.section == 0 { return culAreaCellHeight(cityList?.count ?? 0) }else{ let build = self.buildArr![indexPath.row] if build.isExpand == true{ return culAreaCellHeight(build.buildList.count) } return 40 } } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if section == 0 { return 0 }else{ return 40 } } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if section == 1 { let header = UIView() header.backgroundColor = .white let label = UILabel(frame: CGRect(x: 20, y: 0, width: KSCREENWIDTH - 40, height: 40)) label.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 14) label.textColor = UIColor(hexString: "#333333") label.text = "列表" header.addSubview(label) return header }else{ return UIView() } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "areaCell") as! IHTPickerAreaCell cell.selectionStyle = .none cell.cityList = self.cityList cell.cityCallback = { [unowned self] (city) in self.buildArr?.removeAll() self.getHotelList(city.id,city: city) } return cell }else{ let build = self.buildArr![indexPath.row] if build.isExpand == false { var cell = tableView.dequeueReusableCell(withIdentifier: "cell") if cell == nil { cell = UITableViewCell(style: .default, reuseIdentifier: "cell") } cell?.selectionStyle = .none cell?.textLabel?.text = build.hotelName cell?.textLabel?.font = UIFont(name: PingFangSC_Regular, size: 13) cell?.textLabel?.textColor = UIColor(hexString: "#333333") return cell! }else{ let cell = tableView.dequeueReusableCell(withIdentifier: "floorCell") as! IHPickFloorCell cell.selectionStyle = .none let build = self.buildArr?[indexPath.row] cell.buildList = build?.buildList cell.completion = {[unowned self] (build) in // log.debug("self.buildArr - \(self.buildArr)") if self.buildArr?.count == 0 { g_showHUD("重试") //出现数组为空的情况 表示没请求完 }else{ self.selectedBuid = build self.selectedHotel = self.buildArr![indexPath.row - 1]//前一组 } } return cell } } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if indexPath.section == 1 { let build = self.buildArr![indexPath.row] //只有关闭的才可以点击 if build.isExpand == false { //点击展开 log.debug("点击展开") self.isClickBuild = true self.selectedHotel = build if build.buildList.count > 1 { self.selectedBuid = build.buildList[1] }else{ self.selectedBuid = build.buildList.first } var newBuild = build //这里要注意是不是最后一组,最后一组 注意越界问题 //判断下一行是否是可以展开的,如果是可以站看的,就可以添加,否则 删除 if indexPath.row < self.buildArr!.count - 1 {//判断是不是最后一组 let nextBuild = self.buildArr![indexPath.row + 1] if nextBuild.isExpand == false{ newBuild.isExpand = true self.buildArr?.insert(newBuild, at: indexPath.row + 1)//插入下一行 removeOtherExpand(indexPath.row + 1) self.tableView.reloadSections([1], with: .fade) }else{ self.buildArr?.remove(at: indexPath.row + 1) self.tableView.reloadSections([1], with: .fade) } }else{ let nextbuild = self.buildArr![indexPath.row]//处理最后一组 if nextbuild.isExpand == false { newBuild.isExpand = true self.buildArr?.insert(newBuild, at: indexPath.row + 1)//插入下一行 removeOtherExpand(indexPath.row + 1) self.tableView.reloadSections([1], with: .fade) } } } } } } extension IHCountryPicker{ //删除其他展开数组 func removeOtherExpand(_ index:Int) { var expandArr :[Int] = [Int]()//搜集展开的数组 ///获取么展开情况 for i in 0..<(self.buildArr?.count)! { let build = self.buildArr![i] if build.isExpand == true{ expandArr.append(i)//添加数组里边 } } //删除其他内容 if expandArr.count > 1 { for j in expandArr { if j != index { self.buildArr?.remove(at: j) } } } } //计算区域的行高 func culAreaCellHeight(_ cityCount: Int) ->CGFloat { if cityCount == 0 { return 0 }else{ //计算行数 var row : Int = cityCount / 4 if row > 5 { //大于5行 只取5行 row = 5 } let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row return CGFloat(contentViewHight) } } //计算楼层的行高 func culFloorCellHeight(_ cityCount: Int) ->CGFloat { if cityCount == 0 { return 0 }else{ //计算行数 let row : Int = cityCount / 4 let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row return CGFloat(contentViewHight) } } //获取酒店 //id 酒店id或者省份id //city 有city则保存起来起来 func getHotelList(_ id:String?,city:DropDownData?) { // self.historyCity = city HUD.show(.progress) HUD.hide(afterDelay: 5) IHAddressService.share.getHotelWithBuildlist(id, requestSuccess: { (hotelList) in HUD.hide() self.buildArr?.removeAll() if hotelList.count > 0{ for hotel in hotelList{ if let buildList = hotel.buildList { // let allBuild = DropDownData() // allBuild.id = "" // allBuild.name = "All" var newlist = [DropDownData]() //newlist.append(allBuild) if buildList.count > 0{ for build in buildList { newlist.append(build) } } self.buildArr?.append((hotel.name!,hotel.id!,newlist,false)) self.tableView.reloadSections([1], with: .automatic) // log.debug("检查楼房数组 -- \(self.buildArr)") } } } }) { } // IHAddressService.share.getHotelList(id, requestSuccess: { (hotelList) in // self.buildArr?.removeAll() // if hotelList.count > 0{ // let queue = DispatchQueue(label: "com.welcome.Inhealth.IhCountryPiker") // for hotel in hotelList{ // queue.async { // self.getBuildList(hotel.id!) { (buildlist) in // HUD.hide() // self.buildArr?.append((hotel.name!,hotel.id!,buildlist,false)) // self.tableView.reloadSections([1], with: .automatic) // } // } // } // } // // }) { } } //获取楼房 func getBuildList(_ hotelId :String,completion: @escaping (([DropDownData])->Void)) { IHAreaService.share.getBuildNavData(hotelId: hotelId, requestSuccess: { (buildList) in let allBuild = DropDownData() allBuild.id = "" allBuild.name = "All" var newlist = [DropDownData]() newlist.append(allBuild) if buildList.count == 0{ completion(newlist) }else{ for build in buildList { newlist.append(build) } completion(newlist) } }) { } } }