//
//  YHOtherInfoFillViewController.swift
//  galaxy
//
//  Created by davidhuangA on 2024/2/6.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import GKNavigationBarSwift
/*
 其他信息
 */

class YHOtherInfoFillViewController: YHBaseViewController {
    var orderId : Int?
    private let otherInfoVM : YHOtherInfoFillViewModel = YHOtherInfoFillViewModel()
    var homeTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style:.grouped)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(YHOtherInfoFillCell.self,forCellReuseIdentifier: YHOtherInfoFillCell.cellReuseIdentifier)
        return tableView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.0
        initView()
        
        //2.0
        loadData()
    }
}

// MARK: - private methods
extension YHOtherInfoFillViewController {
    private func initView() {
        gk_backStyle = .black
        gk_navTitle = "其他信息填写"
        gk_navBackgroundColor = .white
        gk_navBarAlpha = 1
        
        let bottomView = YHBasicInfoFillBottomView()
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(YHBasicInfoFillBottomView.viewH)
        }
        
        bottomView.block = { tag in
            if tag == 0 {
                //点击了保存按钮
//                let arr = self.basicInfoVM.arrBasicInfoSessionDataForEdit
//                printLog(arr)
                
                printLog("点击了保存按钮")
                self.saveData()
            } else {
                //点击了提交按钮
                printLog("点击了提交按钮")
                self.submitData()
            }
        }
        
        
        homeTableView.delegate = self
        homeTableView.dataSource = self
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalTo(bottomView.snp.top)
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
        }
    }
    
    private func loadData() {
        guard let orderId = orderId else { return}
        let param = ["order_id":orderId]
        otherInfoVM.getOtherInfo(params: param) { success, error in
            if success == true {
                self.homeTableView.reloadData()
            } else {
                YHHUD.flash(message: error?.errorMsg ?? "发生错误,请重试")
            }
            self.homeTableView.reloadData()
        }
    }
    
    
    private func saveData() {
        
        let model = otherInfoVM.otherInfoFillData
        
        //保存
        
        if let param = model?.toDictionary() {
            
            printLog(param)
            
            otherInfoVM.saveOtherInfo(params: param) { success, error in
                if success == true {
                    YHHUD.flash(message: "保存成功")
                } else {
                    let msg = error?.errorMsg ?? "保存失败"
                    YHHUD.flash(message: msg)
                }
            }
        }
    }
    
    private func submitData() {
        //提交
        
    }
}

// MARK: - delegates
// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHOtherInfoFillViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 15))
        view.backgroundColor = .clear
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: YHOtherInfoFillCell.self)
        cell.selectionStyle = .none
        cell.block = {
            self.homeTableView.reloadData()
        }
        cell.dataModel = otherInfoVM.otherInfoFillData
        return cell
    }
}

