<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="store_category_parent_id">
        <el-input
          v-model="form.store_category_parent_id"
          :placeholder="__('请输入分类父名称')"
          readonly
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('店铺名称')" prop="store_category_name">
        <el-input
          v-model="form.store_category_name"
          clearable
          :placeholder="__('请输入店铺名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('分类图片')" prop="category_image" required>
        <el-upload
          ref="category_image"
          :action="action"
          :before-upload="BeforeUpload"
          class="avatar-uploader"
          :on-success="(value) => handleAvatarSuccess('category_image', value)"
          :show-file-list="false"
        >
          <img
            v-if="form.category_image"
            class="avatar"
            :src="form.category_image"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item :label="__('保证金数额')" prop="store_category_deposit">
        <el-input
          v-model.number="form.store_category_deposit"
          clearable
          :placeholder="__('请输入保证金数额')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="store_category_order">
        <el-input
          v-model.number="form.store_category_order"
          clearable
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="category_is_enable" required>
        <el-switch
          v-model="form.category_is_enable"
          :active-text="__('显示')"
          :active-value="1"
          :inactive-text="__('不显示')"
          :inactive-value="0"
        />
      </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/category'
  import { URL } from '@/config'

  export default {
    name: 'BaseStoreCategoryEdit',
    data() {
      return {
        action: URL.action,
        form: {
          category_image: '',
          category_is_enable: true,
        },
        rules: {
          store_category_parent_id: [
            {
              required: true,
              message: this.__('请输入分类父名称'),
              trigger: 'blur',
            },
          ],
          store_category_name: [
            {
              required: true,
              message: this.__('请输入店铺名称'),
              trigger: 'blur',
            },
          ],
          store_category_deposit: [
            {
              required: true,
              message: this.__('请输入保证金数额'),
              trigger: 'blur',
            },
          ],
          store_category_order: [
            {
              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, store_category_parent_id) {
        if (store_category_parent_id === 0 || store_category_parent_id) {
          this.title = this.__('添加')
          this.form.store_category_parent_id = store_category_parent_id
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = 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>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
