//
//  YHMainViewController.swift
//  galaxy
//
//  Created by EDY on 2023/12/20.
//

import UIKit
import SnapKit

struct PersonalModuleItem {
    var icon: String
    var title: String
}

class YHMyViewController: YHBaseViewController, ConstraintRelatableTarget {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style:.plain)
        tableView.tableHeaderView = headerView
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.register(YHPersonalCenterCell.self, forCellReuseIdentifier: YHPersonalCenterCell.cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var items: [PersonalModuleItem] = {
        return [PersonalModuleItem(icon: "mine_manager", title: "我的管家"),
                PersonalModuleItem(icon: "mine_manager", title: "我的订单"),
                PersonalModuleItem(icon: "mine_manager", title: "我的合同"),
                PersonalModuleItem(icon: "mine_manager", title: "我的进度"),
                PersonalModuleItem(icon: "mine_manager", title: "我的积分"),
                PersonalModuleItem(icon: "mine_manager", title: "我的设置"),
                PersonalModuleItem(icon: "mine_manager", title: "关于我们"),]
    }()
    
    lazy var headerView: UIView = {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 110))
        
        var iconImgView = UIImageView(image: UIImage(named:"mine_head_default"))
        view.addSubview(iconImgView)
        
        var nameLabel = UILabel()
        nameLabel.text = "智慧银河"
        nameLabel.textColor = UIColor(hexString:"#222222")
        nameLabel.textAlignment = NSTextAlignment.left
        nameLabel.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(nameLabel)
        
        var mobileLabel = UILabel()
        mobileLabel.text = "133****0000"
        mobileLabel.textColor = UIColor(hexString:"#666666")
        mobileLabel.textAlignment = NSTextAlignment.left
        mobileLabel.font = UIFont.systemFont(ofSize: 13)
        view.addSubview(mobileLabel)
        
        iconImgView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.centerY.equalTo(view)
            make.width.height.equalTo(55)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.right.equalTo(view).offset(-16)
            make.bottom.equalTo(view.snp.centerY)
        }
        
        mobileLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.right.equalTo(view).offset(-16)
            make.top.equalTo(view.snp.centerY)
        }
        
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    func initView() {
        
        self.title = "个人中心"
        view.backgroundColor = .white
        
        let bgImgView = UIImageView(image: UIImage(named: "mine_header"))
        view.addSubview(bgImgView)
        bgImgView.snp.makeConstraints { make in
            make.left.right.top.equalTo(self.view)
            make.height.equalTo( 435.0/750.0 * KScreenHeight)
        }
        
        view.addSubview(tableView);
        tableView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(view)
        }
    }
}

extension YHMyViewController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YHPersonalCenterCell.cellReuseIdentifier, for: indexPath) as! YHPersonalCenterCell
        let item : PersonalModuleItem = items[indexPath.row];
        cell.updateItem(item: item)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 52
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = YHLoginViewController()
        self.navigationController?.pushViewController(vc)
    }
}
