<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('奖品名称')" prop="awards_name">
        <el-input
          v-model="form.awards_name"
          :placeholder="__('请输入奖品名称')"
        />
      </el-form-item>
      <el-form-item :label="__('图片')" prop="awards_image">
        <upload
          height="100px"
          :image="form.awards_image"
          width="100px"
          @upImage="(image) => (form.awards_image = image)"
        />
      </el-form-item>
      <el-form-item :label="__('奖品数量')" prop="awards_quantity">
        <el-input
          v-model="form.awards_quantity"
          :placeholder="__('奖品数量')"
        />
      </el-form-item>
      <el-form-item :label="__('奖品权重')" prop="awards_probability">
        <el-input
          v-model="form.awards_probability"
          :placeholder="__('奖品权重')"
        />
      </el-form-item>

      <el-form-item :label="__('是否开启')" prop="awards_enable">
        <el-radio v-model="form.awards_enable" label="1">
          {{ __('是') }}
        </el-radio>
        <el-radio v-model="form.awards_enable" label="0">
          {{ __('否') }}
        </el-radio>
      </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 { translateTitle as __ } from '@/utils/i18n'
  import Upload from '@/components/upload'
  import { saveLotteryItem } from '@/api/store/activity/base'
  import { URL } from '@/config'

  export default {
    name: 'StoreActivityBaseEdit',
    components: { Upload },
    data() {
      return {
        form: {
          awards_enable: '0',
          awards_image: '',
          index: -1,
          awards_type: 1,
          awards_title: '',
        },
        rules: {
          awards_name: [
            {
              required: true,
              message: '请输入奖品名称',
              trigger: 'blur',
            },
          ],
          awards_probability: [
            {
              required: true,
              message: '请输入奖品权重',
              trigger: 'blur',
            },
          ],
          awards_quantity: [
            {
              required: true,
              message: '请输入奖品数量',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        action: URL.action,
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row, activity_id) {
        if (row == null && activity_id != null) {
          this.form.activity_id = activity_id
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')

          this.form = Object.assign({}, row)
          this.form.activity_id = activity_id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await saveLotteryItem(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
