<!--
 * @Author: WangJiaCheng
 * @Date: 2021-05-11 11:18:32
 * @LastEditors: WangJiaCheng
 * @LastEditTime: 2021-05-11 17:18:41
 * @Description: 页面配置
-->
<template>
  <div class="page-edit">
    <el-form
      :model="formData"
      label-width="100px"
    >
      <el-form-item label="页面Tab名称" prop="tabName">
        <el-input v-model="formData.tabName" />
      </el-form-item>
      <el-form-item label="Header 描述" prop="headerDesc">
        <el-input v-model="formData.headerDesc" />
      </el-form-item>
      <el-form-item label="Header 按钮">
        <el-tag
          v-for="tag in dynamicTags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">
          添加按钮
        </el-button>
      </el-form-item>
      <el-form-item label="路由" prop="route">
        <el-input v-model="formData.route" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'PageConfig',
  data() {
    return {
      formData: {},
      dynamicTags: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const { inputValue } = this
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style lang="less">
  .page-edit {
    .el-tag + .el-tag {
      margin-left: 10px;
    }
    .button-new-tag {
      margin-left: 10px;
      height: 32px;
      line-height: 30px;
      padding-top: 0;
      padding-bottom: 0;
    }
    .input-new-tag {
      width: 90px;
      margin-left: 10px;
      vertical-align: bottom;
    }
  }
</style>
