<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="140px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('店铺等级名称')" prop="store_grade_name">
        <el-input
          v-model="form.store_grade_name"
          clearable
          :placeholder="__('请输入店铺等级名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('收费标准')" prop="store_grade_fee">
        <el-input
          v-model="form.store_grade_fee"
          oninput="value=value.replace(/[^0-9.]/g,'')"
          :placeholder="__('请输入收费标准（单位：元/年）')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('申请说明')" prop="store_grade_desc">
        <el-input
          v-model="form.store_grade_desc"
          clearable
          :placeholder="__('请输入申请说明')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('可发布商品数')"
        prop="store_grade_product_limit"
      >
        <el-input
          v-model.number="form.store_grade_product_limit"
          oninput="value=value.replace(/[^\d]/g,'')"
          :placeholder="__('请输入可发布商品数')"
          :style="{ width: '100%' }"
          type="number"
        />
      </el-form-item>
      <el-form-item :label="__('可上传图片数')" prop="store_grade_album_limit">
        <el-input
          v-model.number="form.store_grade_album_limit"
          oninput="value=value.replace(/[^\d]/g,'')"
          :placeholder="__('请输入可上传图片数')"
          :style="{ width: '100%' }"
          type="number"
        />
      </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/store/grade'

  export default {
    name: 'BaseStoreGradeEdit',
    data() {
      return {
        form: {},
        rules: {
          store_grade_name: [
            {
              required: true,
              message: this.__('请输入店铺等级名称'),
              trigger: 'blur',
            },
          ],
          store_grade_fee: [
            {
              required: true,
              message: this.__('请输入收费标准（单位：元/年）'),
              trigger: 'blur',
            },
          ],
          store_grade_desc: [
            {
              required: true,
              message: this.__('请输入申请说明'),
              trigger: 'blur',
            },
          ],
          store_grade_product_limit: [
            {
              required: true,
              message: this.__('请输入可发布商品数'),
              trigger: 'blur',
            },
          ],
          store_grade_album_limit: [
            {
              required: true,
              message: this.__('请输入可上传图片数'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {}
        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>
