/// 地区所有数据
let cityInfo = viewModel.allRegionSub.value
/// 已订阅的地区数据
let areaIds: [Int?] = viewModel.subscribeRegionSub.value.map{$0.id}
//通过id查询选择的省和直辖市
var chooseCities: [Children] = []
areaIds.forEach { id in
for item in cityInfo {
// 省和直辖市
if id == item.id {
let tmp = item.copyobj()
tmp.children = []
chooseCities.append(tmp)
break
}else if let chi = item.children{
/// 查询对应的市
if let sitem = chi.first(where: { id == $0.id })?.copyobj(){
// 查询省是否已加入
if let idx = chooseCities.firstIndex(where: { $0.id == sitem.pid }) {
chooseCities[idx].children?.append(sitem)
}else{
// 把省加入
let price = item.copyobj()
price.children = [sitem]
chooseCities.append(price)
}
break
}
}
}
}
/// 省 默认都不选中
chooseCities.forEach({
$0.isSelect = false
$0.children?.forEach({ $0.isSelect = false })
})
var provinceModel = chooseCities.first
/// 插入全部
for item in chooseCities {
let haschiled = (item.children?.count ?? 0) > 0
let obj = item.children?.first ?? item
let tmp = Children()
tmp.id = haschiled ? obj.pid : obj.id
tmp.pid = obj.pid
tmp.realName = item.name
// tmp.isSelect = true
let all = Children.init(all: tmp)
// if let children = item.children{
// //子集中是否有未选中的城市
// if children.map({$0.isSelect}).contains(false){
//
// }else{
// //全部被选中了,这个时候应该取消地区的选中状态,设置全部为选中
// item.children?.forEach{$0.isSelect = false}
// all.isSelect = true
// }
// }
if item.children != nil{
item.children?.insert(all, at: 0)
}else{
item.children = [all]
}
}
// 设置默认选中
if let tmp = self.selAreas {
if tmp.0 > 0 {
provinceModel = chooseCities.first(where: { $0.id == tmp.0 })
chooseCities.forEach { item in
item.children?.forEach({
let state = tmp.1.contains($0.id)
$0.isSelect = state
})
}
}
}
// 没有选择,默认选择全部
else{
chooseCities.forEach { item in
item.children?.first?.isSelect = true
}
}
provinceModel?.isSelect = true
/// 获取选中的数据
var selareass: [Children] = []
chooseCities.forEach { item in
selareass.append(contentsOf: item.children?.filter({ $0.isSelect }) ?? [])
}
ZBDropmenuManager.shared.showRegionDropMenu(with: chooseCities,provinceModel: provinceModel, type: .area, originY: originY, selectIDs: selareass) {[weak self] idx,areas,areasTotalCount in
guard let weakSelf = self else {return}
if let areas = areas{
if areas.count == 1{
weakSelf.areaName.accept(areas.first?.realName ?? "订阅地区")
}else if areas.count == 0{
weakSelf.areaName.accept("订阅地区")
/// 如果没有地区数据
weakSelf.isNoneSelectRegion = true
}else{
weakSelf.areaName.accept("订阅地区(\(areasTotalCount))")
}
}else{
weakSelf.areaName.accept("订阅地区")
/// 如果没有地区数据
weakSelf.isNoneSelectRegion = true
}
if let area = areas?.first{
weakSelf.selAreas = (area.pid ?? 0 , areas!.map({ $0.id }))
}else{
weakSelf.selAreas = (0 , [])
}
let areaString: String = areas?.map({ "\($0.id ?? 0)" }).joined(separator: ",") ?? "0"
weakSelf.areaIdSelect.onNext(areaString)
viewModel.loadData(isFresh: true, isShowLoad: true)
} cancel: {
sender.isSelected = false
}