<!--
<result-table :itemData="itemData"></result-table>

import resultTable from '@/components/createApi/result-table.vue';
-->
<template>
  <div>
    <template v-for="(itemValue, itemName, index) in itemData.properties">
      <table cellspacing="0" cellpadding="0" border="0" class="el-table__body" style="width: 100%; table-layout: fixed;" :key="index + itemName + 121">
        <tbody>
          <tr class="el-table__row">
            <td rowspan="1" colspan="1" class="el-table_1_column_1">
              <div class="cell">
                <el-input v-model="itemValue.currentKey" :disabled="itemValue.type == 'array' || itemValue.type == 'object'" @blur="blurKey(itemValue, itemName)"></el-input>
              </div>
            </td>
            <td rowspan="1" colspan="1" class="el-table_1_column_2">
              <div class="cell">
                <el-select v-model="itemValue.type" placeholder="请选择参数类型" @change="changeType(itemValue.type, itemValue, itemValue.currentKey, itemValue.level)">
                  <el-option label="string" value="string"></el-option>
                  <el-option label="number" value="number"></el-option>
                  <el-option label="boolean" value="boolean"></el-option>
                  <el-option label="object" value="object"></el-option>
                  <el-option label="array" value="array"></el-option>
                </el-select>
              </div>
            </td>
            <td rowspan="1" colspan="1" class="el-table_1_column_3">
              <div class="cell">
                <el-input v-model="itemValue.example"></el-input>
              </div>
            </td>
            <td rowspan="1" colspan="1" class="el-table_1_column_3">
              <div class="cell">
                <el-button v-if="itemValue.type == 'array' || itemValue.type == 'object'" type="text" @click="handleAdd(itemValue, itemValue.currentKey, index, itemValue.level)">添加子属性</el-button>
                <el-button type="text" @click="handleDel(itemValue.parentKey, itemValue.currentKey, index, itemValue.level)">删除</el-button>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
      <body-row class="el-table__row" style="padding-left: 10px;" v-if="itemValue.type == 'array' && Object.keys(itemValue.items.properties).length" :itemData="itemValue.items" :key="index + '122' + itemName"> </body-row>
      <body-row class="el-table__row" style="padding-left: 10px;" v-if="itemValue.type == 'object' && Object.keys(itemValue.properties).length" :itemData="itemValue" :key="index + '122' + itemName"> </body-row>
    </template>
  </div>
</template>
<script>
export default {
  name: 'body-row',
  props: {
    itemData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      trData: {},
      keyLength: 1 // 记录生成 obj key的标志
    };
  },
  methods: {
    /**
     * key 输入变化
     */
    blurKey(obj, oldKey) {
      const that = this;
      if (obj.currentKey != oldKey) {
        that.$set(that.itemData.properties, obj.currentKey, obj);
        that.$delete(that.itemData.properties, oldKey);
      }
    },
    /**
     * 更新参数名字段
     */
    updateKey(itemData) {
      const that = this;
      for (let key in itemData) {
        if (key != itemData[key].currentKey) {
          itemData[key].pkey = itemData[itemData[key].currentKey] = itemData[key];
          that.$delete(itemData, key);
        }
        if (itemData[key] && itemData[key].type == 'object' && Object.keys(itemData[key].properties).length) {
          that.updateKey(itemData[key].properties);
        }
        if (itemData[key] && itemData[key].type == 'array' && Object.keys(itemData[key].items.properties).length) {
          that.updateKey(itemData[key].items.properties);
        }
      }
    },
    /**
     * 删除一项
     */
    async handleDel(parentKey, itemKey, index, level) {
      const that = this;
      await that.updateKey(that.itemData.properties);
      if (!parentKey) {
        that.$delete(that.itemData.properties, itemKey);
        return false;
      }
      that.handleDelData(that.itemData, parentKey, itemKey);
    },
    handleDelData(itemData, pkey, itemKey) {
      const that = this;
      that.handleObjData(itemData, pkey, itemKey);
    },
    handleObjData(itemData, pkey, itemKey) {
      const that = this;
      for (let i in itemData) {
        if (itemData[i] == pkey) {
          that.$delete(itemData.properties, itemKey);
        }
      }
    },
    /**
     * 修改 type
     */
    changeType(type, obj, itemKey, level) {
      const that = this;
      if (type == 'string' || type == 'boolean' || type == 'number' || type == 'object') {
        obj.properties = {};
        if (obj.hasOwnProperty('items')) {
          that.$delete(obj, 'items');
        }
      }
      if (type == 'array') {
        that.$set(obj, 'items', {
          parentKey: itemKey, // 新增父级的 key
          currentKey: 'items',
          level: parseInt(level) + 1, //  层级
          type: 'string',
          defaultVal: 'val', //默认值
          description: '描述',
          example: '示例',
          properties: {}
        });
      }
      that.$forceUpdate();
    },
    /**
     * 添加子属性
     * {
     *   properties: {}
     * }
     */
    handleAdd(obj, itemKey, index, level) {
      const that = this;
      let length = that.keyLength++;
      if (obj.type == 'array') {
        that.$set(obj.items.properties, 'field' + length, {
          parentKey: itemKey, // 新增父级的 key
          level: parseInt(level) + 1, //  层级
          type: 'string',
          currentKey: 'field' + length, // 当前的 key
          defaultVal: 'val', // 默认值
          description: '描述',
          example: '示例',
          properties: {}
        });
        that.$forceUpdate();
        return false;
      }

      // 这里是对对象的添加操作
      that.$set(obj.properties, 'field' + length, {
        parentKey: itemKey, // 新增父级的 key
        level: parseInt(level) + 1, //  层级
        type: 'string',
        currentKey: 'field' + length, // 当前的 key
        defaultVal: 'val', // 默认值
        description: '描述',
        example: '示例',
        properties: {}
      });
      that.$forceUpdate();
    }
  },
  watch: {
    itemData: function(newData, oldData) {
      const that = this;
      if (newData) {
        that.trData = JSON.parse(JSON.stringify(newData));
      }
    }
  },
  mounted() {
    const that = this;
    that.trData = JSON.parse(JSON.stringify(that.itemData));
  }
};
</script>
<style lang="less" scoped>
.w-320 {
  width: 320px;
}
.m-t-14 {
  margin-top: 14px;
}
.add-line {
  height: 48px;
  line-height: 48px;
  border-bottom: 1px solid #e4e7ed;
  cursor: pointer;
}
</style>
