<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-08-14 16:52:02
 * @LastEditors: 无尘
 * @LastEditTime: 2019-11-06 11:29:36
 -->
<!--
<back-constant :itemData="itemData" :interfaceType="interfaceType"></back-constant>

import backConstant from '@/components/createApi/back-constant.vue';
-->
<template>
  <div>
    <div class="el-table el-table--fit el-table--enable-row-hover el-table--enable-row-transition" style="width: 100%; table-layout: fixed;">
      <div class="el-table__header-wrapper">
        <table cellspacing="0" cellpadding="0" border="0" class="el-table__header" style="width: 100%; table-layout: fixed;">
          <thead class="has-gutter">
            <tr class="">
              <th colspan="1" rowspan="1" class="el-table_1_column_1 is-leaf">
                <div class="cell">参数名</div>
              </th>
              <th v-if="interfaceType == '0'" colspan="1" rowspan="1" class="el-table_1_column_2 is-leaf">
                <div class="cell">参数位置</div>
              </th>
              <th colspan="1" rowspan="1" class="el-table_1_column_2 is-leaf">
                <div class="cell">参数类型</div>
              </th>
              <th colspan="1" rowspan="1" class="el-table_1_column_4 is-leaf">
                <div class="cell">参数值</div>
              </th>
              <th v-if="interfaceType == 1" colspan="1" rowspan="1" class="el-table_1_column_4 is-leaf">
                <div class="cell">包路径</div>
              </th>
              <th v-if="interfaceType == 1" colspan="1" rowspan="1" class="el-table_1_column_4 is-leaf">
                <div class="cell">排序</div>
              </th>
              <th colspan="1" rowspan="1" class="el-table_1_column_7 is-leaf">
                <div class="cell">操作</div>
              </th>
            </tr>
          </thead>
        </table>
      </div>
      <div class="el-table__body-wrapper is-scrolling-none">
        <div class="table-content">
          <template v-for="(itemValue, itemName, index) in itemData.properties">
            <table v-if="itemValue.constFlag" 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-tooltip">
                      <el-input v-model="itemValue.currentKey" placeholder="请输入参数名" @blur="blurKey(itemValue, itemName)"></el-input>
                    </div>
                  </td>
                  <td v-if="interfaceType == '0'" rowspan="1" colspan="1" class="el-table_1_column_2">
                    <div class="cell">
                      <el-select v-model="itemValue.backendOriginType" placeholder="请选择参数位置">
                        <el-option label="header" value="header"></el-option>
                        <el-option label="query" value="query"></el-option>
                        <el-option label="body" value="body"></el-option>
                        <el-option label="form" value="form"></el-option>
                      </el-select>
                    </div>
                  </td>
                  <td rowspan="1" colspan="1" class="el-table_1_column_2">
                    <div class="cell">
                      <el-select v-model="itemValue.type" placeholder="请选择参数类型">
                        <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="integer" value="integer"></el-option>
                      </el-select>
                    </div>
                  </td>
                  <td rowspan="1" colspan="1" class="el-table_1_column_3">
                    <div class="cell el-tooltip">
                      <el-input v-model="itemValue.defaultVal" placeholder="默认值"></el-input>
                    </div>
                  </td>
                  <td v-if="interfaceType == '1'" rowspan="1" colspan="1" class="el-table_1_column_4">
                    <div class="cell">
                      <el-input class="w-110" v-model="itemValue.objectClass" placeholder="请输入包路径"></el-input>
                    </div>
                  </td>
                  <td v-if="interfaceType == 1" rowspan="1" colspan="1" class="el-table_1_column_3">
                    <div class="cell el-tooltip">
                      <el-input v-model="itemValue.sort" placeholder="请输入排序"></el-input>
                    </div>
                  </td>
                  <td rowspan="1" colspan="1" class="el-table_1_column_3">
                    <div class="cell">
                      <el-button type="text" @click="handleDel(itemValue, itemName, index)">删除</el-button>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </template>
        </div>
      </div>
    </div>
    <div class="add-line text-center" @click="toAddItem"><i class="el-icon-circle-plus-outline color-5584FF"></i><span class="color-5584FF font-14 p-l-4">添加常量参数</span></div>
  </div>
</template>
<script>
export default {
  name: 'back-constant',
  props: {
    itemData: {
      type: Object,
      default() {
        return {};
      }
    },
    interfaceType: {
      type: [String, Number],
      default() {
        return '0';
      }
    }
  },
  data() {
    return {
      trData: {
        type: 'object',
        properties: {
          name: {
            frontOriginType: 'header',
            backendOriginType: 'header', //来源类型 query,body,header
            type: 'string',
            constFlag: true,
            currentKey: 'name',
            isBackendField: 1, //是否后端字段
            defaultVal: 'val', //默认值
            isRootKey: 1, //是否是后端参数字段
            isRootKeyFlag: false, // 是否是后端参数字段 check
            rootKey: '', //后端参数字段
            objectClass: '', //如果是object 并且是dubbo
            description: '描述',
            example: '示例',
            check: true
          }
        }
      },
      keyLength: 1
    };
  },
  methods: {
    /**
     * key 输入变化
     */
    blurKey(obj, oldKey) {
      const that = this;
      obj.rootKey = obj.currentKey;
      if (obj.currentKey != oldKey) {
        that.$set(that.itemData.properties, obj.currentKey, obj);
        that.$delete(that.itemData.properties, oldKey);
      }
    },
    /**
     * 删除
     */
    handleDel(obj, itemKey, index) {
      const that = this;
      that.$delete(that.itemData.properties, itemKey);
    },
    /**
     * 添加
     */
    toAddItem() {
      const that = this;
      let length = that.keyLength++;
      that.$set(that.itemData.properties, 'field' + length, {
        frontOriginType: 'header',
        backendOriginType: 'header', //来源类型 query,body,header
        type: 'string',
        currentKey: 'field' + length,
        constFlag: true,
        isBackendField: 1, //是否后端字段
        defaultVal: '', //默认值
        isRootKey: 1, //是否是后端参数字段
        isRootKeyFlag: false, // 是否是后端参数字段 check
        rootKey: 'field' + length, //后端参数字段
        objectClass: '', //如果是object 并且是dubbo
        description: '描述',
        example: '示例',
        check: true,
        sort: ''
      });
    }
  },
  watch: {
    itemData: function(newData, oldData) {
      const that = this;
      that.trData = JSON.parse(JSON.stringify(newData));
    },
    interfaceType: function(newData, oldData) {
      const that = this;
      that.backType = 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;
}
.el-table th {
  padding: 7px 0;
}
</style>
