export default {
  methods: {
    fromApiModel(apiModel, clone = true) {
      const model = clone ? this.deepClone(apiModel) : apiModel
      model.dateRange = [model.beginTime, model.endTime]
      model.state = model.state == '1'
      model.importWeight = model.importWeight || []
      model.groupModels = model.groupModels || []
      model.groupModels.forEach(gm => {
        gm.groupIds = gm.groupIds && gm.groupIds.split(',').map(id => id * 1) || []
        gm.roundGroups = gm.roundGroups || []
        gm.roundGroups.forEach(rg => {
          rg.limitPerson = rg.limitPerson == 1
          rg.rankOut = rg.rankOut == 1
        })
      })
      return model
    },
    toApiModel(localModel, clone = true) {
      const round = clone ? this.deepClone(localModel) : localModel
      round.beginTime = round.dateRange?.first()
      round.endTime = round.dateRange?.last()
      round.state = round.state ? '1' : '0'
      delete round.dateRange
      round.groupModels.forEach(gm => {
        gm.groupIds = gm.groupIds.toString()
        gm.roundGroups.forEach(rg => {
          rg.limitPerson = rg.limitPerson ? 1 : 0
          rg.rankOut = rg.rankOut ? 1 : 0
        })
      })
      return round
    }
  }
}
