<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="文案" prop="text">
        <el-input v-model="form.text" autocomplete="off" :rows="2" type="textarea" />
      </el-form-item>
      <el-form-item label="跳转地址" prop="url">
        <el-input v-model="form.url" autocomplete="off" />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-select v-model="form.tag" placeholder="请选择标签" style="width: 100%">
          <el-option label="新品" value="new" />
          <el-option label="热门" value="hot" />
          <el-option label="爆款" value="hot2" />
          <el-option label="精选" value="featured" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%">
          <el-option label="默认" value="" />
          <el-option label="页面" value="path" />
          <el-option label="tab页" value="switchTab" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnabled">
        <el-switch v-model="form.isEnabled" active-text="启用" :active-value="true" inactive-text="禁用" :inactive-value="false" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, doAdd } from '@/api/spring/index/tips'

  export default {
    name: 'AppManagementEdit',
    data() {
      return {
        id: '',
        form: {
          text: '',
          url: '',
          tag: '',
          type: '',
          isEnabled: true,
        },
        rules: {
          text: [{ required: true, trigger: 'blur', message: '请输入文案' }],
          url: [{ required: true, trigger: 'blur', message: '请输入跳转地址' }],
          isEnabled: [{ required: true, trigger: 'blur', message: '请选择是否启用' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.form = {
            text: '',
            url: '',
            tag: '',
            type: '',
            isEnabled: true,
          }
        } else {
          this.title = '编辑'
          this.form = {
            text: row.text,
            url: row.url,
            tag: row.tag,
            type: row.type,
            isEnabled: row.isEnabled,
          }
          this.id = row.id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.id = ''
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.id) {
              const { msg } = await doEdit(this.id, this.form)
              this.$baseMessage(msg, 'success')
            } else {
              const { msg } = await doAdd(this.form)
              this.$baseMessage(msg, 'success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
