<template>
  <div
    v-for="(param, index) in dynamicInput.params"
    :key="index"
    style="display: flex; margin-top: 0.25vh"
  >
    <Select
      v-model:value="param.dataType"
      style="width: 140px"
      :options="selectOptions"
      @change="emitChange"
    />
    <a-input
      placeholder="请输入Data key"
      v-model:value="param.key"
      style="width: 38%; margin-bottom: 5px; margin-left: 1vh"
      @change="emitChange"
    />
    <a-input
      placeholder="请输入OID(不能重复)"
      v-model:value="param.oid"
      style="width: 38%; margin: 0 0 5px 8px"
      @change="emitChange"
    />
    <MinusCircleOutlined
      v-if="dynamicInput.params.length > min"
      class="dynamic-delete-button"
      @click="remove(param)"
      style="width: 50px"
    />
  </div>
  <div>
    <a-button type="text" style="width: 28%; margin-top: 0.25vh" @click="add">
      <PlusCircleOutlined />
      Add mapping
    </a-button>
  </div>
</template>
<script lang="ts">
  import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';
  import { defineComponent, reactive, UnwrapRef, watchEffect, ref } from 'vue';
  import { propTypes } from '/@/utils/propTypes';
  import { SelectTypes } from 'ant-design-vue/es/select';
  import { Select } from 'ant-design-vue';

  interface Params {
    dataType: string;
    key: string;
    oid: string;
  }

  export default defineComponent({
    name: 'JAddInput',
    components: {
      MinusCircleOutlined,
      PlusCircleOutlined,
      Select,
    },
    //--------------不继承Antd Design Vue Input的所有属性 否则控制台报大片警告--------------
    inheritAttrs: false,
    props: {
      value: propTypes.array.def([]),
      //自定义删除按钮多少才会显示
      min: propTypes.integer.def(0),
    },
    emits: ['change', 'update:value', 'dynamicReduceHeight', 'dynamicAddHeight'],
    setup(props, { emit }) {
      const selectOptions = ref<SelectTypes['options']>([
        {
          label: '字符串',
          value: 'STRING',
        },
        {
          label: '数字',
          value: 'LONG',
        },
        {
          label: '布尔值',
          value: 'BOOLEAN',
        },
        {
          label: '双精度小数',
          value: 'DOUBLE',
        },
        {
          label: 'JSON',
          value: 'JSON',
        },
      ]);

      //input动态数据
      const dynamicInput: UnwrapRef<{ params: Params[] }> = reactive({ params: [] });
      //删除Input
      const remove = (item: Params) => {
        let index = dynamicInput.params.indexOf(item);
        if (index !== -1) {
          dynamicInput.params.splice(index, 1);
        }
        emitChange();
        emit('dynamicReduceHeight');
      };

      //新增Input
      const add = () => {
        dynamicInput.params.push({
          dataType: 'STRING',
          key: '',
          oid: '',
        });
        emitChange();
        emit('dynamicAddHeight');
      };

      //监听传入数据value
      watchEffect(() => {
        initVal();
      });

      /**
       * 初始化数值
       */
      function initVal() {
        dynamicInput.params = [];
        if (props.value) {
          let jsonObj = props.value;
          jsonObj.forEach((item: Params) => {
            dynamicInput.params.push({
              dataType: item.dataType,
              key: item.key,
              oid: item.oid,
            });
          });
        }
      }
      /**
       * 数值改变
       */
      function emitChange() {
        let obj: any = [];
        if (dynamicInput.params.length > 0) {
          dynamicInput.params.forEach((item: Params) => {
            obj.push({
              dataType: item.dataType,
              key: item.key,
              oid: item.oid,
            });
          });
        }
        emit('change', obj);
        emit('update:value', obj);
      }

      return {
        dynamicInput,
        emitChange,
        remove,
        add,
        selectOptions,
      };
    },
  });
</script>
<style scoped>
  .dynamic-delete-button {
    cursor: pointer;
    position: relative;
    top: 4px;
    font-size: 24px;
    color: #999;
    transition: all 0.3s;
  }

  .dynamic-delete-button:hover {
    color: #777;
  }

  .dynamic-delete-button[disabled] {
    cursor: not-allowed;
    opacity: 0.5;
  }
</style>
