//
//  M_CreatOrder.swift
//  VattiCSS
//
//  Created by Jan on 2023/8/29.
//

import UIKit

struct M_CreatOrder {

    var username = ""
    var usermobile = ""
    var useraddress = ""
    
    var usertown = ""
    var site_province = ""
    var site_city = ""
    var site_county = ""
    
    var areaid = ""
    var areaname = ""
    
    var ordertypeid = ""
    var ordertypename = ""
    var servicetypeid = ""
    var servicetypename = ""
    
    
    var brandid = ""
    var brandname = ""
    
    var invoiceno = ""
    var buyChannelId = ""
    var buyChannelName = ""
    
    var buyshopid = ""
    var buyshopname = ""
    var buydate = ""
    var ontimelocation = ""
    
    var comfirecode = ""
    var usertype = "0"
    var remark = ""
    var longdis = ""
    var items = [M_Item]()
    
    var resultPicList:[ScanImageModel] = []
    var hasIemi = false
    var guaranteeType = 1
    //make json
    func jsonValue() -> [String:Any] {
        var param:[String:Any] = [:]
        param["userName"] = username
        param["userMobile"] = usermobile
        param["userAddress"] = useraddress
        
        param["areaId"] = areaid
        param["fullName"] = areaname
        
        param["ordertypeid"] = ordertypeid
        param["ordertypename"] = ordertypename
        
        param["serviceTypeId"] = servicetypeid
        param["servicetypename"] = servicetypename
        
        param["brandId"] = brandid
        
        
        param["buyInvoiceNo"] = invoiceno
        
        param["buyshopid"] = buyshopid
        param["buyShopName"] = buyshopname
        
        param["buyChannelId"] = buyChannelId
        param["buyChannelName"] = buyChannelName
        
        param["buyDate"] = buydate
        param["ontimelocation"] = ontimelocation
        
        param["userType"] = usertype
        param["remark"] = remark
        param["longDis"] = longdis
        
        param["itemList"] = items.compactMap({ (item) -> [String:Any] in
            return item.jsonValue(["brandId":brandid])
        })
        
        
        param["guaranteeType"] = guaranteeType
        
        return param
    }
    
    struct M_Item {

        var qty = ""
        
        var brandId = ""
        
        var itemid = ""
        var itemname = ""
        var itemcode = ""
        
        var entId = "0"
        var bigitemcatname = ""
        var bigitemcatid = ""
        
        var smallitemcatname = ""
        var smallitemcatid = ""
        
        var inbarcode = ""
        var outbarcode = ""
        var barCode3 = ""
        
        var barcodetype = 2 //1。 条码1 2.条码2 3.条码3
        
        var hasInCode = false
        var hasOutCode = false
        var hasBarCode3 = false
        
        ///图片
        //购买图片
        var buypicmust = ""
        var buypic = ""
        var buypicname = ""
        var buypicnametemp = ""
        var buypicImage:UIImage?
        
        //条码图片
         var codepicmust = ""
        var codepic = ""
        var codepicname = ""
        var codepicnametemp = ""
        var codepicImage:UIImage?
        
        //现场图片
        var spotpicmust = ""
        var spotpic = ""
        var spotpicname = ""
        var spotpicnametemp = ""
        var spotpicImage:UIImage?
        
        //安装图片
        var installpicmust = ""
        var installpic = ""
        var installpicname = ""
        var installpicnametemp = ""
        var installpicImage:UIImage?
        
        var hasIemi = false
        var iemi = ""
        var picList:[ScanImageModel] = []
        //make json
        func jsonValue(_ joinParam:[String:Any]) -> [String:Any] {
            var param:[String:Any] = [:]
            param["entId"] = entId
            param["qty"] = 1
            param["itemId"] = itemid
            param["itemName"] = itemname
            param["itemCode"] = itemcode
            param["bigCategoryId"] = bigitemcatid
            param["bigCategoryName"] = bigitemcatname
            
            param["smallCategoryId"] = smallitemcatid
            param["smallCategoryName"] = smallitemcatname
           
            param += joinParam
            
            if brandId != ""{
                param["brandId"] = brandId
            }else{
                param["brandId"] = joinParam.stringValue(forKey: "brandId")
            }
          
            if hasInCode {
                param["inBarcode"] = inbarcode
            }
            if hasOutCode {
                param["outBarcode"] = outbarcode
            }
            if hasBarCode3 {
                param["barCode3"] = barCode3
            }
            
            param["barcodetype"] = barcodetype
            if hasIemi {
                param["iemi"] = iemi
            }
            
            if !picList.isEmpty {
                param["picList"] = picList.compactMap({$0.jsonValue()})
            }
            return param
        }
    }
    
    
    struct ScanImageModel {
        var ismust = "" //是否必填:0否1是
        var picname = ""
        var pic = ""
        var picnametemp = ""
        var picImage = ""
        
        var isNo = ""//不需要这组图片
        
        init(_ data:[String:Any] = [:]) {
            picname = data.stringForKey("picName")
            ismust = data.stringForKey("isMust")
            pic = data.stringForKey("picUrl")
        }
        
        func jsonValue() -> [String:Any] {
            var picParam = [String:Any]()
            picParam["isMust"] = ismust
            picParam["picName"] = picname
            picParam["picUrl"] = pic
            return picParam
        }
    }
}


