// // PosViewController.swift // xingchuangke // // Created by 李晓飞 on 2020/9/6. // Copyright © 2020 Virgil. All rights reserved. // import UIKit class PosViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource, PosHeaderViewDelegate { var amount = 0 var bindAmount = 0 var activated = 0 var unBindAmount = 0 var machineTypeId: String! var flodArr: NSMutableArray = NSMutableArray() var selectSNArr: NSMutableArray = NSMutableArray() var stateArr = ["", "0", "1", "2"] var curStatus: String = "0" var userId = CommonValue.getUserId() var titleName = "" @IBOutlet weak var imgBackground: UIImageView! @IBOutlet weak var topView: UIView! @IBOutlet weak var totalBtn: UIButton! @IBOutlet weak var unbindBtn: UIButton! @IBOutlet weak var bindBtn: UIButton! @IBOutlet weak var activeBtn: UIButton! @IBOutlet weak var searchView: UIView! @IBOutlet weak var searchBtn: UIButton! @IBOutlet weak var searchTextField: UITextField! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var transformBtn: UIButton! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) appDelegate.setNavigationBarHidden(isHidden: true) } override func viewDidLoad() { super.viewDidLoad() navInit() flodStateInit() unbindBtn.backgroundColor = UIColor.lightGray tableView.delegate = self tableView.dataSource = self tableView.tableFooterView = UIView.init() tableView.separatorStyle = .none tableView.register(UINib(nibName: "PosTableViewCell", bundle: nil), forCellReuseIdentifier: "PosTableViewCell") tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in self!.currentPage = 1 self!.loadData() }) tableView.mj_header?.lastUpdatedTimeKey = "ShowTableViewCell" tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in self!.currentPage += 1 self!.loadData() }) tableView.mj_header?.beginRefreshing() } // 自定义导航栏 func navInit() { let viewNav = (CommonViewUntils.getViewForXIB(xibName: "NavView") as! NavView) viewNav.initView(title: titleName) {[weak self] (index, _) in if index == 0 { self!.handleBack() } } self.view.addSubview(viewNav) viewNav.marginTop(top: 0) searchView.marginTop(top: viewNav.bottom()) imgBackground.translatesAutoresizingMaskIntoConstraints = false let h: NSLayoutConstraint = NSLayoutConstraint(item: imgBackground as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: searchView.bottom()) imgBackground.addConstraint(h) } func flodStateInit() { for _ in 0...1000 { flodArr.add("0") } } func btnTitleInit() { let activStr = " 激活 \(dataSize?["status2"] ?? "0")台 " let activAttStr = CommonViewUntils.getAttributedString(str: activStr, left: 3, right: 2, color: UIColor.red, otherColor: UIColor.black) activeBtn.setAttributedTitle(activAttStr, for: .normal) let bindStr = " 绑定 \(dataSize?["status1"] ?? "0")台 " let bindAttStr = CommonViewUntils.getAttributedString(str: bindStr, left: 3, right: 2, color: UIColor.red, otherColor: UIColor.black) bindBtn.setAttributedTitle(bindAttStr, for: .normal) let unbindStr = " 未绑定 \(dataSize?["status0"] ?? "0")台 " let unbindAttStr = CommonViewUntils.getAttributedString(str: unbindStr, left: 4, right: 2, color: UIColor.red, otherColor: UIColor.black) unbindBtn.setAttributedTitle(unbindAttStr, for: .normal) let totalStr = " 共 \(dataSize?["total"] ?? "0")台 " let totalAttStr = CommonViewUntils.getAttributedString(str: totalStr, left: 2, right: 2, color: UIColor.red, otherColor: UIColor.black) totalBtn.setAttributedTitle(totalAttStr, for: .normal) } // MARK: - action @IBAction func checkBtnClick(_ sender: Any) { let btn = (sender as! UIButton) for view in btn.superview!.subviews as [UIView] { if let button = view as? UIButton { if button.tag == btn.tag { button.backgroundColor = UIColor.lightGray } else { button.backgroundColor = UIColor.white } } } let index: NSInteger = btn.tag - 1011 curStatus = stateArr[index] currentPage = 1 if curStatus == "0" { self.transformBtn.isHidden = false } else { self.transformBtn.isHidden = true } loadData() } @IBAction func transformBtnClick(_ sender: Any) { let mids = selectSNArr.componentsJoined(by: ",") let vc = SplitDeviceViewController() vc.mid = mids toViewController(viewController: vc) } @IBAction func searchBtnClick(_ sender: Any) { self.view.endEditing(true) loadData() } // MARK: - request func loadData() { let url = RequestURL.machineList let params = NSMutableDictionary() params.setValue(userId, forKey: "userId") params.setValue(machineTypeId, forKey: "machineTypeId") params.setValue(searchTextField.text!, forKey: "snCode") params.setValue(curStatus, forKey: "status") loadDataList111(url: url, params: params, tableView: self.tableView, tag: 1001) } func loadDataList111(url: String, params: NSMutableDictionary, tableView: UITableView, tag: Int) { isAlertError = true if self.currentPage == 1 { self.arrData.removeAllObjects() } params.setValue(self.currentPage, forKey: "pageNumber") params.setValue(self.pageSize, forKey: "pageSize") MBProgressHUD.showAdded(to: self.view, animated: true) let http = AFHTTPSessionManager() http.requestSerializer.setValue(userId, forHTTPHeaderField: "userId") http.requestSerializer.setValue("1", forHTTPHeaderField: "phoneType") http.requestSerializer.setValue(CommonUntils.getSystemVersion(), forHTTPHeaderField: "apkVersion") http.get(url, parameters: params, progress: nil, success: {[weak self] (_, json1) -> Void in let json = json1 as! NSDictionary print(json) let succeed = json.object(forKey: "succeed") as! String if succeed == "000" { if self!.currentPage == 1 { self!.arrData.removeAllObjects() } let dataInfo = json["dataInfo"] as? NSDictionary if dataInfo != nil { self!.dicDataList.setDic(dic: dataInfo!) self!.dicData.setDictionary(dataInfo as! [AnyHashable: Any]) let array = dataInfo!["dataList"] as? NSArray if array != nil { for dic in array! { let dicNew = NSMutableDictionary(dictionary: dic as! NSDictionary) self!.arrData.add(dicNew) } }else{ let array1 = dataInfo!["agents"] as? NSArray for dic in array1! { let dicNew = NSMutableDictionary(dictionary: dic as! NSDictionary) self!.arrData.add(dicNew) } } } else { let array = json["dataInfo"] as? NSArray if array != nil { for dic in array! { let dicNew = NSMutableDictionary(dictionary: dic as! NSDictionary) self!.arrData.add(dicNew) } } } let dataS = json["dataSize"] as? NSDictionary if dataS != nil { self?.dataSize = dataS self?.btnTitleInit() } self!.returnData(tag: tag) } else { self!.returnError(tag: tag, type: succeed) if self!.isAlertError { SVProgressHUD.showError(withStatus: json.object(forKey: "sucInfo") as? String) } } if self!.arrData.count > 0 { self!.hiddenNoDataViewForTableView(tableView: tableView) } else { self!.showNoDataViewForTableView(tableView: tableView) } if tableView.mj_header != nil { tableView.mj_header?.endRefreshing() } if tableView.mj_footer != nil { tableView.mj_footer?.endRefreshing() } MBProgressHUD.hide(for: self?.view ?? UIView.init(), animated: true) }, failure: { (_, err) -> Void in MBProgressHUD.hide(for: self.view, animated: true) if tableView.mj_header != nil { tableView.mj_header?.endRefreshing() } if tableView.mj_footer != nil { tableView.mj_footer?.endRefreshing() } CommonUntils.alertError(error: err as NSError) self.returnError(tag: tag, type: "9999") }) } override func returnData(tag: Int) { if tag == 1001 { tableView.reloadData() } } override func returnError(tag: Int, type: String) { } // MARK: - delagete func selectBtnClicked(selected: Bool, snIdStr: NSString) { if selected { selectSNArr.add(snIdStr) } else { selectSNArr.remove(snIdStr) } let btnTit = (self.selectSNArr.count == 0) ? "我要分拨" : "我要分拨(已选\(self.selectSNArr.count)台)" transformBtn.setTitle(btnTit, for: .normal) } func numberOfSections(in tableView: UITableView) -> Int { return arrData.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if flodArr[section] as! String == "0" { return 0 } return 1 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 44 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView: PosHeaderView = CommonViewUntils.getViewForXIB(xibName: "PosHeaderView") as! PosHeaderView headerView.delegate = self headerView.flodBlock = { if self.flodArr[section] as! String == "0" { self.flodArr.replaceObject(at: section, with: "1") } else { self.flodArr.replaceObject(at: section, with: "0") } self.tableView.reloadSections([section], with: .none) } if curStatus == "0" { headerView.selectBtn.isHidden = false } else { headerView.selectBtn.isHidden = true } let state = getIntValue(current: section, key: "status") if state == 1 { headerView.statusLabel.text = "已绑定" headerView.statusLabel.textColor = UIColor.red } else if state == 0 { headerView.statusLabel.text = "未绑定" headerView.statusLabel.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x0cab1f, alpha: 1.0) } else { headerView.statusLabel.text = "已激活" headerView.statusLabel.textColor = UIColor.red } headerView.snLabel.text = "SN: \(getString(current: section, key: "sn_code"))" headerView.snId = getString(current: section, key: "id") return headerView } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PosTableViewCell", for: indexPath) as! PosTableViewCell cell.selectionStyle = .none if "\(getString(current: indexPath.section, key: "superior_user_name"))".length() == 0 { cell.nameLabel.text = "平台分拨" } else { cell.nameLabel.text = "\(getString(current: indexPath.section, key: "superior_user_name"))" } cell.dateLabel.text = "\(getString(current: indexPath.section, key: "allocation_time"))" cell.shopNameLabel.text = "\(getString(current: indexPath.section, key: "bind_merchant_name"))" cell.endTimeLabel.text = "\(getString(current: indexPath.section, key: "endtime"))" if cell.shopNameLabel.text == "未绑定商户" { cell.bindDateLabel.text = "未绑定" } else { cell.bindDateLabel.text = "\(getString(current: indexPath.section, key: "bind_time"))" } return cell } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ }