<!--
 * @Author: WangJiaCheng
 * @Date: 2021-05-11 11:18:32
 * @LastEditors: WangJiaCheng
 * @LastEditTime: 2021-07-29 16:04:02
 * @Description: 页面配置
-->
<template>
  <div class="page-edit">
    <el-form
      ref="pageForm"
      :rules="rules"
      :model="formData"
      label-width="120px"
    >
      <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 btnTags"
          :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-item>
        <el-button type="primary" @click="next">下一步</el-button>
        <el-button type="primary" @click="previewCode">预览入口组件代码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  name: 'PageConfig',
  data() {
    return {
      formData: {},
      rules: {
        tabName: [
          { required: true, message: '请输入Tab名称' }
        ],
        headerDesc: [
          { required: true, message: '请输入Header描述' }
        ]
      },
      btnTags: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    ...mapMutations('page', ['setPageConfig', 'setActiveTab']),
    handleClose(tag) {
      this.btnTags.splice(this.btnTags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const { inputValue } = this
      if (inputValue) {
        this.btnTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    next() {
      this.$refs.pageForm.validate(valid => {
        if (valid) {
          this.setPageConfig({ ...this.formData, btn: this.btnTags })
          this.setActiveTab('form')
        }
      })
    },
    previewCode() {
      this.$refs.pageForm.validate(valid => {
        if (valid) {
          this.$emit('preview', this.formData)
        }
      })
    }
  }
}
</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>
