//
//  FinishOrderSelectFeeVm.swift
//  VattiCSS
//
//  Created by Jan on 2023/12/28.
//

import UIKit

class FinishOrderSelectFeeVm: NSObject {
    var forms:[TCCellBaseProtocol] = []
    var callback:(()->())?
    var selCallback:((M_FinishOrder.M_FeeItem)->())?
    var itemArr = [M_FinishOrder.M_FeeItem]()
    func makeForms(){
       var newForms:[TCCellBaseProtocol] = []

        newForms += makeItemForms()
        forms = newForms
        callback?()
    }
    
    func makeItemForms()->[TCCellBaseProtocol]{
        var newForms:[TCCellBaseProtocol] = []

        guard itemArr.count > 0  else {
            return []
        }
        do {
            let uiConfigModel = TCFormCellConfigModel.defaultModel()
                .bgColor(.clear)
                .cornerMask([.layerMinXMinYCorner, .layerMaxXMinYCorner])
                .cornerRadius(8)
            let rowCell = TCFormCellModel<TCLineCell>.init(data: (0, 0, UIColor(hexString: "f8f8f8")), config: uiConfigModel, id: nil, frameh: 10)
            newForms.append(rowCell)
        }
        
        do {
            let uiConfigModel = TCFormCellConfigModel.defaultModel()
                .bgColor(.clear)
                .cornerMask([.layerMinXMinYCorner, .layerMaxXMinYCorner])
                .cornerRadius(8)
            let rowCell = TCFormCellModel<TCLineCell>.init(data: (15, 15, UIColor(hexString: "ffffff")), config: uiConfigModel, id: nil, frameh: 5)
            newForms.append(rowCell)
        }
        
        for (index,item) in itemArr.enumerated() {
            // header
            do {
                let name = item.subjectName
                let code = item.price
               
                let uiConfigModel = TCFormCellConfigModel.defaultModel()
                
                let rowCell = TCFormCellModel<Cell_FinishFeeSelect>.init(data: (name,code,"",item.selected), config: uiConfigModel, id: nil, frameh: 60) { a in
                    //选择
    
                    var newItem = item
                    newItem.selected = !item.selected
                    self.itemArr[index] = newItem
                    self.makeForms()
                   
                    self.selCallback?(item)
                }
                newForms.append(rowCell)
            }
        }
        
        do {
            let uiConfigModel = TCFormCellConfigModel.defaultModel()
                .bgColor(.clear)
                .cornerMask([.layerMinXMaxYCorner, .layerMaxXMaxYCorner])
                .cornerRadius(8)
            let rowCell = TCFormCellModel<TCLineCell>.init(data: (15, 15, UIColor(hexString: "ffffff")), config: uiConfigModel, id: nil, frameh: 10)
            newForms.append(rowCell)
        }
        
       return newForms
    }
}
