<template>
  <el-dialog
    :append-to-body="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('兑换码前缀')" prop="code_prefix">
        <el-input
          v-model="form.code_prefix"
          :placeholder="__('请输入兑换码前缀')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('生成兑换码数量')" prop="code_num">
        <el-input
          v-model.number="form.code_num"
          :placeholder="__('请输入生成数量')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="saveCode">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doCodeCreate } from '@/api/store/activity/base'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreActivityBaseEdit',
    data() {
      return {
        form: {
          code_prefix: '',
          code_num: '',
        },
        row: {},
        title: '',
        dialogFormVisible: false,
        rules: {
          code_num: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入生成数量'),
            },
          ],
        },
      }
    },
    created() {
      this.title = this.__('创建兑换码')
    },
    methods: {
      __,
      close() {
        this.form = {
          activity_id: '',
          code_prefix: '',
          code_num: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      showTable(activity_id) {
        this.form.activity_id = activity_id
        this.dialogFormVisible = true
      },
      saveCode() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doCodeCreate(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
