//
//  PosTransferVC.swift
//  xingchuangke
//
//  Created by 刘惠萍 on 2023/2/4.
//

import UIKit

class PosTransferVC: BaseViewController, UITableViewDataSource, UITableViewDelegate {
    
    
    @IBOutlet weak var lblOpenAmount: UILabel!
    @IBOutlet weak var lblAmount: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var viewTop: UIView!
    @IBOutlet weak var viewMenu1: UIView!
    @IBOutlet weak var viewMenu2: UIView!
    @IBOutlet var searchTextField: UITextField!
    @IBOutlet var searchView: UIView!
    @IBOutlet var searchBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "机具调拨"
        let viewNav = (CommonViewUntils.getViewForXIB(xibName: "NavView") as! NavView)
        viewNav.initView(title: "机具调拨") {[weak self] (index, _) in
            if index == 0 {
                self!.handleBack()
            }
            if index == 1 {
                self?.navigationController?.pushViewController(MachineLogListViewController(), animated: true)
            }
        }
        viewNav.initRightBtn(title: "日志")
        self.view.addSubview(viewNav)
        viewNav.marginTop(top: 0)
        searchView.marginTop(top: viewNav.bottom())

        viewTop.setSizeWidth(width: ScreenWidth)
        CommonViewUntils.setViewAverage(arrView: [viewMenu1, viewMenu2])

//        tableView.register(UINib(nibName: "PosManangerTableViewCell", bundle:nil) , forCellReuseIdentifier: "PosManangerTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView.init()
        tableView.showsVerticalScrollIndicator = false
        tableView.estimatedRowHeight = 100
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self!.currentPage = 1
            self!.loadData()
        })
        tableView.mj_header?.lastUpdatedTimeKey = "ShowTableViewCell"
    }
    // MARK: =============加载数据===============
    func loadData() {
        let url = RequestURL.machineListAllot
        let params = NSMutableDictionary()
        params.setValue(CommonValue.getUserId(), forKey: "userId")
        params.setValue(searchTextField.text!, forKey: "snCode")
        loadDataList(url: url, params: params, tableView: self.tableView, tag: 1001)
    }
    override func returnData(tag: Int) {
        if tag == 1001 {
            if dataSize != nil {
                let strAmount = "\(dataSize!["total"] ?? "0")台"
                lblAmount.attributedText = CommonViewUntils.getAttributedStringForFont(str: strAmount, rangs: [NSRange.init(location: 0, length: strAmount.length() - 1), NSRange.init(location: strAmount.length() - 1, length: 1)], fonts: [UIFont.systemFont(ofSize: 24), UIFont.systemFont(ofSize: 14)])
                let bindNum = getIntValue(key: "sTotal", dic: dataSize!)
                let strOpenAmount = "\(bindNum)台"
                lblOpenAmount.attributedText = CommonViewUntils.getAttributedStringForFont(str: strOpenAmount, rangs: [NSRange.init(location: 0, length: strOpenAmount.length() - 1), NSRange.init(location: strOpenAmount.length() - 1, length: 1)], fonts: [UIFont.systemFont(ofSize: 24), UIFont.systemFont(ofSize: 14)])
            }
//            var amount = 0;
//            var openAmount = 0;
//            for i in 0 ..< self.arrData.count
//            {
//                amount += getIntValue(current: i, key: "total")
//                openAmount += getIntValue(current: i, key: "activated")
//            }
//            let strAmount = "\(amount)台"
//            let strOpenAmount = "\(openAmount)台"
//            lblAmount.attributedText = CommonViewUntils.getAttributedStringForFont(str: strAmount, rangs: [NSRange.init(location: 0, length: strAmount.length() - 1),NSRange.init(location: strAmount.length() - 1, length: 1)], fonts: [UIFont.systemFont(ofSize: 24),UIFont.systemFont(ofSize: 14)])
//
//            lblOpenAmount.attributedText = CommonViewUntils.getAttributedStringForFont(str: strOpenAmount, rangs: [NSRange.init(location: 0, length: strOpenAmount.length() - 1),NSRange.init(location: strOpenAmount.length() - 1, length: 1)], fonts: [UIFont.systemFont(ofSize: 24),UIFont.systemFont(ofSize: 14)])
            tableView.reloadData()
        }
    }
    override func returnError(tag: Int, type: String) {

    }

    // MARK: TABLEVLEW 实现
    // MARK: TABLEVLEW 实现
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    @IBAction func searchBtnClick(_ sender: Any) {
        self.view.endEditing(true)
        loadData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arrData.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "PosManangerTableViewCell", for: indexPath as IndexPath) as! PosManangerTableViewCell
//
//        cell.lblTitle.text = getString(current: indexPath.row, key: "name")
//        //cell.lblRemark.text = getString(current: indexPath.row, key: "brief");
//        cell.img.loadImage(imgUrl: getString(current: indexPath.row, key: "icon"), defaultImage: "");
//        cell.lblCount.text = "\(getIntValue(current: indexPath.row, key: "total"))台"
//        return cell
        var cell = tableView.dequeueReusableCell(withIdentifier: "posManagerCell")
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: "posManagerCell")
            cell?.detailTextLabel?.textColor = UIColor.red
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 14)
            cell?.accessoryType = .disclosureIndicator
        }
        cell?.textLabel!.text = "\(getString(current: indexPath.row, key: "real_name")) / \(getString(current: indexPath.row, key: "rec_code"))"
        cell?.detailTextLabel!.text = "\(getIntValue(current: indexPath.row, key: "profit_switch"))台"
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row >= arrData.count {
            return
        }
        let vc = PosViewController()
        vc.amount = getIntValue(current: indexPath.row, key: "profit_switch")
        vc.titleName = "\(getString(current: indexPath.row, key: "real_name")) / \(getString(current: indexPath.row, key: "rec_code"))"
        vc.userId = "\(getString(current: indexPath.row, key: "id"))"
        toViewController(viewController: vc)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        appDelegate.setNavigationBarHidden(isHidden: true)
        tableView.mj_header?.beginRefreshing()
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: "PosTransferVC", bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}