<template>
  <div class="app-container" style="width: 800px; margin: 100px auto 0 50px">
    <el-form ref="tagsForm" :model="form" label-width="130px">
      <el-form-item label="标签名:" prop="keyword" :rules="rules.keyword">
        <el-input v-model="form.keyword" placeholder="请填写标签名称" maxlength="20" />
      </el-form-item>
      <el-form-item label="父标签:" prop="parentLabel" :rules="rules.parentLabel">
        <el-radio v-model="form.parentLabel" :label="0">否</el-radio>
        <el-radio v-model="form.parentLabel" :label="1">是</el-radio>
      </el-form-item>
      <el-form-item v-if="form.parentLabel === 1" label="子标签" prop="subtag" :rules="rules.subtag">
        <template v-if="form.subtag && form.subtag.length === 0">
          <el-button type="primary" autofocussize="mini" size="mini" icon="el-icon-plus" @click="handleDialogVisible">
            选择子标签
          </el-button>
          <el-checkbox-group v-show="false" v-model="form.subtag" />
        </template>
        <template v-else>
          <el-table :data="form.subtag" border width="1000">
            <el-table-column prop="keyword" label="子标签名称" align="center" />
            <el-table-column label="添加时间" prop="addTime" align="center">
              <template slot-scope="{ row }">
                {{ row.addTime | parseTime }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="handleDeleteTags(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-form-item>
    </el-form>
    <div class="el-dialog__footer" style="text-align: center">
      <el-button type="primary" @click="onSubmit('tagsForm')"> 保存 </el-button>
      <el-button plain @click="backToList"> 返回 </el-button>
    </div>
    <!-- 子标签弹框 -->
    <tags-dialog v-if="tagsDialogVisible" ref="tagsDialog" @confirm="handleAddTagsConfirm" @cancel="handleCancel" />
  </div>
</template>

<script>
import { editKeyword, saveLabelByState } from '@/api/library/keyword'
import tagsDialog from './components/tags-dialog'

export default {
  name: 'TagForm',
  components: { tagsDialog },
  data() {
    const defaultForm = () => {
      return {
        id: '',
        keyword: '',
        subtag: [],
        dynamicStatus: 0,
        parentLabel: 0 // 父标签 0 否 1 是
      }
    }
    return {
      form: defaultForm(),
      editType: 'add',
      tagsDialogVisible: false,
      rules: {
        keyword: [{ required: true, message: '请填写标签名称', trigger: 'blur' }],
        subtag: [{ required: true, type: 'array', message: '请添加子标签', trigger: ['change'] }],
        parentLabel: [{ required: true, message: '请选择是否为父标签', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.editType = this.$route.query.type || 'edit'
    if (this.editType === 'edit') {
      console.log('edit')
      this.editKeyword(this.$route.query.id)
    }
  },
  methods: {
    async editKeyword(id) {
      // 获取菜单详情
      try {
        const res = await editKeyword({ id: id })
        this.form = { ...this.form, ...res.data }
      } catch (error) {
        console.log('error', error)
      }
    },
    // 显示选择商品弹窗
    handleDialogVisible() {
      this.tagsDialogVisible = true
    },
    handleGoodDialogVisible(index) {
      // 显示选择商品弹窗
      this.addIndex = index
      this.dialogGoodVisible = true
    },
    handleDeleteTags(index) {
      // 清除子标签
      this.form.subtag.splice(index, 1)
    },
    handleAddTagsConfirm(data) {
      // 确认选择子标签
      this.form.subtag = [...this.form.subtag, ...data]
      this.handleCancel()
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否提交数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            console.log('form', this.form)
            this.saveLabelByState(this.form)
          })
        } else {
          return false
        }
      })
    },
    async saveLabelByState(params) {
      // 保存
      await saveLabelByState(params)
      this.$message.success('保存成功')
      setTimeout(() => {
        this.backToList()
      }, 1000)
    },
    // 取消选择商品
    handleCancel() {
      this.tagsDialogVisible = false
      this.$refs.tagsDialog.visible = false
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({ path: '/library/tag/list' })
        })
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  float: left;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 30px;
  color: #999999;
}
.el-form-item__label {
  text-align: right !important;
}
.el-upload__tip {
  line-height: 20px;
  color: red;
  text-align: left;
  position: absolute;
  right: -50%;
  bottom: 0;
}
.span_tip {
  font-size: 12px;
  color: red;
  margin-left: 5px;
}
.filter-item-temp {
  width: 100px;
}
</style>
