//
//  YHInformationPerfectListVC.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/25.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

/*
 *信息完善列表
 */
class YHInformationPerfectListVC: YHBaseViewController {
//    private let serviceCenterMainReqVM : YHServiceCenterMainViewModel = YHServiceCenterMainViewModel()

    
    private var arrData : [YHInformationPerfectModel] = []
    private var myTargetIndex : Int32 = 1 //for test hjl
    private var hasSubmitFlag : Bool = false  {
        didSet {
            submitBtn.isHidden = hasSubmitFlag
            submitHoldView.isHidden = !hasSubmitFlag
            
            if hasSubmitFlag == true {
                headerTitleDesLable.text = "您已提交信息表"
            } else {
                headerTitleDesLable.text = "您好，DavidHuang先生"
            }
        }
    }
    
    private lazy var headerTitleDesLable : UILabel = {
        let titleLable = UILabel(text: "您好,黄先生")
        titleLable.font = UIFont.PFSC_M(ofSize: 21)
        return titleLable
    }()
    
    
    private lazy var topBkg : UIView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "service_center_head_bkg")
        imagV.contentMode = .scaleAspectFill
        return imagV
        
    }()
    
    var homeTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.register(YHInformationPerfectCell.self,forCellReuseIdentifier: YHInformationPerfectCell.cellReuseIdentifier)
        return tableView
    }()
    
    var myHeadView : UIView = {
        let view = UIView()
        return view
    }()
    
    
    var myBottomView : UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    
    var submitHoldView : UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    var submitBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
        
        
        
        YHInformationFillTipsAlertView.showCheckView { agree in
            if agree == true {
                printLog("点击了 确认 按钮")
            } else {
                printLog("点击了 取消 按钮")
            }
        }
    }
}

extension YHInformationPerfectListVC {
    func loadData() {
//        self.serviceCenterMainReqVM.getContactList { success, error in
//            if success == true {
//
//            }
//            self.homeTableView.reloadData()
//        }
    }
    
    @objc func goScoreVC() {
        //我的评分
        navigationController?.pushViewController(YHScoreDetailViewController())
        
    }
    
    @objc func goInfoListVC() {
        //个人信息表
        navigationController?.pushViewController(YHPersonInformMainViewController())
    }
    
    @objc func clickSubmitBtn() {
        YHSubmitAllInfoCheckView.showCheckView { agree in
            if agree == true {
                printLog("点击了 确认 按钮")
                
                YHSubmintAllInfoSuccessTipView.showCheckView { agree in
                    self.hasSubmitFlag = true
                    if agree == true {
                        self.goScoreVC()
                    } else {
                        
                    }
                }
            } else {
                printLog("点击了 取消 按钮")
            }
        }
    }
    
    func setupUI() {
        gk_navTitle = "信息完善"
        gk_navBarAlpha = 1.0
        gk_navLineHidden = true
        
        
        let arr = [1,2,3,4,5,6]
        let target = myTargetIndex //for test hjl 待填写的项目
        for item in arr {
            let model = YHInformationPerfectModel()
            model.type = item
            arrData.append(model)
            
            if item < target {
                model.isFinished = true
            } else {
                model.isFinished = false
            }
            
            
            if item == target  {
                model.isDoing = true
            }
            
            
            if item == target + 1 {
                model.isNextDoingCell = true
            } else {
                model.isNextDoingCell = false
            }
        }
        
        
        view.addSubview(topBkg)
        topBkg.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(216)
        }
        
        
        view.addSubview(myBottomView)
        myBottomView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(84)
        }
        
        
        submitBtn = UIButton(type: .custom)
        submitBtn.layer.opacity = 0.4
        submitBtn.setTitle("提交信息", for: .normal)
        submitBtn.setTitleColor(UIColor.white, for: .normal)

        submitBtn.setBackgroundImage(UIImage.from(color: UIColor(hex: 0x4D9FF8, alpha: 1.0)), for: .normal)
        submitBtn.layer.cornerRadius = 8
        submitBtn.clipsToBounds = true
        myBottomView.addSubview(submitBtn)
        
        submitBtn.addTarget(self, action: #selector(clickSubmitBtn), for: .touchUpInside)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(48)
        }
        
        submitBtn.isHidden = hasSubmitFlag
        submitHoldView.isHidden = !hasSubmitFlag
        
        myBottomView.addSubview(submitHoldView)
        submitHoldView.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(48)
        }
        
        
        let scoreButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0xffffff)
            button.titleLabel?.font = kBoldFont(size: 14)
            button.contentHorizontalAlignment = .center
            button.setTitle("我的评分", for: .normal)
            button.setTitleColor( UIColor(hex:0x3c86f8), for: .normal)
            button.layer.cornerRadius = 8
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor(hex: 0x3c86f8).cgColor
            button.addTarget(self, action: #selector(goScoreVC), for: .touchUpInside)
            return button
        }()
        submitHoldView.addSubview(scoreButton)
        scoreButton.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.right.equalTo(submitHoldView.snp.centerX).offset(-6)
        }
        
        let infoButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0x3c86f8)
            button.titleLabel?.font = kBoldFont(size: 14)
            button.contentHorizontalAlignment = .center
            button.setTitle("个人信息表", for: .normal)
            button.setTitleColor( UIColor(hex:0xffffff), for: .normal)
            button.layer.cornerRadius = 8
            button.addTarget(self, action: #selector(goInfoListVC), for: .touchUpInside)
            return button
        }()
        submitHoldView.addSubview(infoButton)
        infoButton.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(submitHoldView.snp.centerX).offset(6)
        }
        
        homeTableView.delegate = self
        homeTableView.dataSource = self
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalTo(myBottomView.snp.top)
        }
        
        
        
        
        
        
        myHeadView.addSubview(headerTitleDesLable)
        headerTitleDesLable.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.height.equalTo(26)
        }
        
        let desLable = UILabel(text: "此表由主申请人填写，我们将会根据您的个人信息制作资料清单，请按顺序填写，提交后1-2个工作日内联系您哦~")
        desLable.font = UIFont.PFSC_R(ofSize: 12)
        desLable.numberOfLines = 0
        desLable.lineBreakMode = .byWordWrapping
        
        myHeadView.addSubview(desLable)
        desLable.snp.makeConstraints { make in
            make.top.equalTo(headerTitleDesLable.snp.bottom).offset(10)
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.bottom.equalToSuperview()
        }
        
        myHeadView.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height: 96)
        homeTableView.tableHeaderView = myHeadView
        homeTableView.reloadData()
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHInformationPerfectListVC : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YHInformationPerfectCell.cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let newCell = tableView.dequeueReusableCell(withClass: YHInformationPerfectCell.self)
        newCell.selectionStyle = .none
        newCell.dataModel = arrData[indexPath.row]
        return newCell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        printLog("点击了 \(indexPath.section)section  --- \(indexPath.row)row")
        let model = arrData[indexPath.row]
        if (indexPath.row + 1 > myTargetIndex) {
            let message = "请先完成" + model.name + "填写"
            YHHUD.flash(message: message)
        } else {
            navigationController?.pushViewController(YHMainApplicantInformationViewController())
        }
    }
}
