<template>
  <div class="add-key-value">
    <div class="add-key-value-box">
      <div class="add-key-value-key">key</div>
      <div class="add-key-value-value">value</div>
    </div>
    <div class="add-container">
      <div v-for="(item, index) in value" :key="index" class="add-key-value-item">
        <div class="add-key-value-input">
          <el-input
            v-model="value[index].key"
            placeholder="请输入key"
          />
        </div>
        <div class="add-key-value-input">
          <el-input
            v-model="value[index].value"
            placeholder="请输入value"
          />
        </div>
        <div class="add-key-value-action">
          <el-button
            type="default"
            circle
            size="mini"
            icon="el-icon-minus"
            @click="onDeleteInput(index)"
          />
          <el-button
            v-if="value.length === index + 1"
            type="primary"
            circle
            size="mini"
            icon="el-icon-plus"
            @click="onAddInput"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'AddKeyValue',
  props: {
    value: {
      type: Array,
      default: () => []
    }
  },
  computed: {
  },
  created() {
    this.value.length === 0 && this.$emit('input', [{ key: '', value: '' }])
  },
  methods: {
    onAddInput() {
      const _list = this.value
      _list.push({ key: '', value: '' })
      this.$emit('input', _list)
    },
    onDeleteInput(index) {
      if (this.value.length === 1) return
      const _list = this.value
      _list.splice(index, 1)
      this.$emit('input', _list)
    }
  }
}
</script>
<style lang="scss" scoped>
.add-key-value {
  background-color: #fff;
  padding: 0 10px 10px 10px;
  .add-key-value-box {
    width: calc(100% - 94px);
    display: flex;
    & > div {
      width: 50%;
      height: 36px;
      padding-left: 4px;
      font-weight: bold;
    }
  }
  .add-container {
    .add-key-value-item {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      justify-content: space-between;
      .add-key-value-input {
        flex: 1;
        margin-right: 10px;
      }
      .add-key-value-action {
        width: 94px;
      }
      .el-input {
        margin-right: 10px;
      }
    }
  }
}
</style>
