<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('额度名称')" prop="recharge_level_name">
        <el-input
          v-model="form.recharge_level_name"
          clearable
          :placeholder="__('请输入额度名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('充值图片')" prop="recharge_level_img" required>
        <upload
          height="100px"
          :image="form.recharge_level_img"
          width="100px"
          @upImage="
            (image) => {
              form.recharge_level_img = image
            }
          "
        />
      </el-form-item>
      <el-form-item :label="__('充值额度')" prop="recharge_level_value">
        <el-input
          v-model="form.recharge_level_value"
          clearable
          :placeholder="__('请输入充值额度')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('收益总额')" prop="recharge_level_gift">
        <el-input
          v-model="form.recharge_level_gift"
          clearable
          :placeholder="__('请输入收益总额')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="recharge_level_repeat" required>
        <el-switch
          v-model="form.recharge_level_repeat"
          :active-text="__('不限制')"
          :inactive-text="__('购买一次')"
        />
      </el-form-item>
      <el-form-item :label="__('有效时长')" prop="recharge_level_duration">
        <el-input
          v-model="form.recharge_level_duration"
          clearable
          :placeholder="__('有效时长，按照年为单位')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('描述')" prop="recharge_level_description">
        <el-input
          v-model="form.recharge_level_description"
          clearable
          :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="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEdit } from '@/api/base/recharge/level'
  import Upload from '@/components/upload'
  import { URL } from '@/config'

  export default {
    name: 'BaseRechargeLevelEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        form: {
          recharge_level_img: '',
        },
        rules: {
          recharge_level_name: [
            {
              required: true,
              message: this.__('请输入额度名称'),
              trigger: 'blur',
            },
          ],
          recharge_level_value: [
            {
              required: true,
              message: this.__('请输入充值额度'),
              trigger: 'blur',
            },
          ],
          recharge_level_gift: [
            {
              required: true,
              message: this.__('请输入收益总额'),
              trigger: 'blur',
            },
          ],
          recharge_level_duration: [
            {
              required: true,
              message: this.__('有效时长，按照年为单位'),
              trigger: 'blur',
            },
          ],
          recharge_level_description: [
            {
              required: true,
              message: this.__('请输入描述'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      handleAvatarSuccess(name, res) {
        this.form[name] = res.data
      },
      BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            recharge_level_description: row.recharge_level_description,
            recharge_level_id: row.recharge_level_id,
            recharge_level_value: row.recharge_level_value,
            recharge_level_gift: row.recharge_level_gift,
            recharge_level_name: row.recharge_level_name,
            recharge_level_repeat: row.recharge_level_repeat,
            recharge_level_rate: row.recharge_level_rate,
            recharge_level_validity: row.recharge_level_validity,
            recharge_level_img: row.recharge_level_img,
            recharge_level_duration: row.recharge_level_duration,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = { recharge_level_img: '' }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
