//
//  FinishOrderItemVm.swift
//  VattiCSS
//
//  Created by Jan on 2024/1/18.
//

import UIKit

class FinishOrderItemVm: NSObject {
    var forms:[TCCellBaseProtocol] = []
    var callback:(()->())?
    var selCallback:((M_ProductItem)->())?
    var itemArr = [M_ProductItem]()
    func makeForms(){
       var newForms:[TCCellBaseProtocol] = []

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

        guard itemArr.count > 0  else {
            return []
        }
    
        for (index,item) in itemArr.enumerated() {
            // header
            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 name = item.itemName
                let code = item.itemCode
               
                let uiConfigModel = TCFormCellConfigModel.defaultModel()
                
                let rowCell = TCFormCellModel<Cell_FinishOrderItemSelect>.init(data: (name,code,item.pic,item.selected), config: uiConfigModel, id: nil, frameh: 80) { a in
    
                    for (cl_index,cl_item) in self.itemArr.enumerated() {
                        var new_clean_item = cl_item
                        new_clean_item.selected = false
                        self.itemArr[cl_index] = new_clean_item
                    }
    
                    //选中的
                    var newItem = item
                    newItem.selected = true
                    self.itemArr[index] = newItem
                    self.makeForms()
                   
                    self.selCallback?(item)
                }
                newForms.append(rowCell)
            }
        }
        
   
        
       return newForms
    }
}
