<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-row :gutter="15">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-col :span="24">
          <el-form-item :label="__('品牌名称')" prop="brand_name">
            <el-input
              v-model="form.brand_name"
              clearable
              :placeholder="__('请输入品牌名称')"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="__('品牌描述')" prop="brand_desc">
            <el-input
              v-model="form.brand_desc"
              :autosize="{ minRows: 1, maxRows: 10 }"
              :placeholder="__('请输入品牌描述')"
              :style="{ width: '100%' }"
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="brand_show_type">
            <el-radio-group v-model="form.brand_show_type" size="medium">
              <el-radio
                v-for="(item, index) in brand_show_typeOptions"
                :key="index"
                :disabled="item.disabled"
                :label="item.value"
              >
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="__('品牌LOGO')" prop="brand_image">
            <Upload
              height="100px"
              :image="form.brand_image"
              width="100px"
              @upImage="
                (image) => {
                  form.brand_image = image
                }
              "
            />
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <template #footer>
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </template>
  </el-dialog>
</template>

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

  export default {
    name: 'ProductSellerBrandEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        uploadDisabled: 0,
        form: {
          brand_name: undefined,
          brand_desc: undefined,
          brand_show_type: 1,
          brand_image: '',
        },
        title: '',
        dialogFormVisible: false,
        rules: {
          brand_name: [
            {
              required: true,
              message: this.__('请输入品牌名称'),
              trigger: 'blur',
            },
          ],
          brand_desc: [
            {
              required: true,
              message: this.__('请输入品牌描述'),
              trigger: 'blur',
            },
          ],
          brand_show_type: [
            {
              required: true,
              message: this.__('不能为空'),
              trigger: 'change',
            },
          ],
        },
        brand_show_typeOptions: [
          {
            label: this.__('图片'),
            value: 1,
          },
          {
            label: this.__('文字'),
            value: 2,
          },
        ],
      }
    },
    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 = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          brand_name: undefined,
          brand_desc: undefined,
          brand_show_type: 1,
          brand_image: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
