<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="__('等级名称')"
        label-width="110px"
        prop="user_level_name"
      >
        <el-input
          v-model="form.user_level_name"
          clearable
          :placeholder="__('请输入等级名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('经验值')"
        label-width="110px"
        prop="user_level_exp"
      >
        <el-input
          v-model="form.user_level_exp"
          clearable
          :placeholder="__('请输入经验值')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('发展粉丝')"
        label-width="110px"
        prop="user_level_fans_user"
      >
        <el-input
          v-model="form.user_level_fans_user"
          clearable
          :placeholder="__('请输入发展粉丝')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        v-if="false"
        :label="__('推广店铺')"
        label-width="110px"
        prop="user_level_fans_store"
      >
        <el-input
          v-model="form.user_level_fans_store"
          clearable
          :placeholder="__('请输入推广店铺')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('累计消费')"
        label-width="110px"
        prop="user_level_spend"
      >
        <el-input
          v-model="form.user_level_spend"
          clearable
          :placeholder="__('请输入累计消费')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        v-if="false"
        :label="__('购买产品升级')"
        label-width="110px"
        prop="user_level_product_id"
      >
        <el-input
          v-model="form.user_level_product_id"
          clearable
          :placeholder="__('购买产品升级，此处填写产品SPU编号购买产品升级')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        label="LOGO"
        label-width="110px"
        prop="user_level_logo"
        required
      >
        <upload
          height="100px"
          :image="form.user_level_logo"
          width="100px"
          @upImage="
            (image) => {
              form.user_level_logo = image
            }
          "
        />
      </el-form-item>
      <el-form-item
        :label="__('折扣率')"
        label-width="110px"
        prop="user_level_rate"
      >
        <el-input
          v-model="form.user_level_rate"
          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/user/level'
  import { URL } from '@/config'
  import upload from '@/components/upload'

  export default {
    name: 'BaseUserLevelEdit',
    components: { upload },
    data() {
      return {
        action: URL.action,
        form: {
          user_level_logo: '',
        },
        rules: {
          user_level_name: [
            {
              required: true,
              message: this.__('请输入等级名称'),
              trigger: 'blur',
            },
          ],
          user_level_exp: [
            {
              required: true,
              message: this.__('请输入经验值'),
              trigger: 'blur',
            },
          ],
          user_level_fans_user: [
            {
              required: true,
              message: this.__('请输入发展粉丝'),
              trigger: 'blur',
            },
          ],
          user_level_fans_store: [
            {
              required: true,
              message: this.__('请输入推广店铺'),
              trigger: 'blur',
            },
          ],
          user_level_spend: [
            {
              required: true,
              message: this.__('请输入累计消费'),
              trigger: 'blur',
            },
          ],
          user_level_product_id: [
            {
              required: true,
              message: this.__('购买产品升级，此处填写产品SPU编号购买产品升级'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            user_level_id: row.user_level_id,
            user_level_name: row.user_level_name,
            user_level_exp: row.user_level_exp,
            user_level_fans_user: row.user_level_fans_user,
            user_level_fans_store: row.user_level_fans_store,
            user_level_spend: row.user_level_spend,
            user_level_product_id: row.user_level_product_id,
            user_level_logo: row.user_level_logo,
            user_level_rate: row.user_level_rate,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          user_level_logo: '',
        }
        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>
