<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"
        size="medium"
      >
        <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="category_id">
            <el-select
              v-model="form.category_id"
              clearable
              :placeholder="__('请选择')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in categorys"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </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-col :span="24">
          <el-form-item label="是否推荐" prop="brand_recommend">
            <el-radio-group v-model="form.brand_recommend" size="medium">
              <el-radio
                v-for="(item, index) in brand_recommendOptions"
                :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="是否启用" prop="brand_enable">
            <el-radio-group v-model="form.brand_enable" size="medium">
              <el-radio
                v-for="(item, index) in brand_enableOptions"
                :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="申请状态" prop="brand_apply">
            <el-radio-group v-model="form.brand_apply" size="medium">
              <el-radio
                v-for="(item, index) in brand_applys"
                :key="index"
                :disabled="item.disabled"
                :label="item.value"
              >
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </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: 'BrandManagementEdit',
    components: { Upload },
    props: {
      categorys: {
        type: Array,
        required: true,
      },
    },
    data() {
      return {
        action: URL.action,
        uploadDisabled: 0,
        form: {
          brand_show_type: 1,
          brand_image: '',
          brand_recommend: 1,
          brand_enable: 1,
          brand_apply: 1,
        },
        title: '',
        dialogFormVisible: false,
        rules: {
          category_id: [
            {
              required: true,
              message: this.__('请选择所属分类'),
              trigger: 'change',
            },
          ],
          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_recommend: [
            {
              required: true,
              message: this.__('不能为空'),
              trigger: 'change',
            },
          ],
          brand_enable: [
            {
              required: true,
              message: this.__('不能为空'),
              trigger: 'change',
            },
          ],
          brand_apply: [
            {
              required: true,
              message: this.__('不能为空'),
              trigger: 'change',
            },
          ],
        },
        brand_show_typeOptions: [
          {
            label: this.__('图片'),
            value: 1,
          },
          {
            label: this.__('文字'),
            value: 2,
          },
        ],
        brand_recommendOptions: [
          {
            label: this.__('推荐'),
            value: 1,
          },
          {
            label: this.__('不推荐'),
            value: 0,
          },
        ],
        brand_enableOptions: [
          {
            label: this.__('启用'),
            value: 1,
          },
          {
            label: this.__('禁用'),
            value: 0,
          },
        ],
        brand_applys: [
          {
            label: this.__('拒绝'),
            value: 0,
          },
          {
            label: this.__('通过'),
            value: 1,
          },
          {
            label: this.__('申请中'),
            value: 2,
          },
        ],
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          brand_show_type: 1,
          brand_image: '',
          brand_recommend: 1,
          brand_enable: 1,
          brand_apply: 1,
        }
        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>
<style lang="scss"></style>
