<template>
  <div class="mod-index-img">
    <el-dialog
      :title="!dataForm.brandId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      @close="closeDialogHandle"
    >
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="100px">
        <el-form-item label="品牌名称" prop="brandName">
          <el-input
            type="text"
            placeholder="请输入品牌名称"
            v-model="dataForm.brandName"
            maxlength="30"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="品牌首字母" prop="firstChar">
          <el-input
            type="text"
            placeholder="请输入品牌首字母"
            v-model="dataForm.firstChar"
            maxlength="1"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="轮播图片" prop="brandPic">
          <img-upload v-model="dataForm.brandPic"></img-upload>
        </el-form-item>
        <el-form-item label="描述" prop="brief">
          <el-input
            type="textarea"
            placeholder="请输入备注内容"
            v-model="dataForm.brief"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="seq">
          <el-input v-model="dataForm.seq" type="number"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import ImgUpload from '@/components/img-upload'
export default {
  data () {
    return {
      dataForm: {
        brandId: 0,
        brandName: '',
        brandPic: '',
        brief: '',
        seq: 0,
        status: 1,
        firstChar: ''
      },
      brandSelectVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataRule: {
        brandName: [
          { required: true, message: '品牌名称不能为空', trigger: 'blur' }
        ],
        firstChar: [
          { required: true, message: '品牌首字母不能为空', trigger: 'blur' }
        ],
        seq: [
          { required: true, message: '排序值不能为空', trigger: 'blur' }
        ]
      },
      visible: false
    }
  },
  components: {
    ImgUpload
  },
  methods: {
    // 获取分类数据
    init (id) {
      this.visible = true
      this.dataForm.brandId = id || 0
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.brandId) {
        // 获取产品数据
        this.$http({
          url: this.$http.adornUrl(`/platform/brand/info/${this.dataForm.brandId}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          this.dataForm = data
        })
      } else {
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.brandPic = ''
          this.relation = null
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let param = this.dataForm
          this.$http({
            url: this.$http.adornUrl(`/platform/brand`),
            method: param.brandId ? 'put' : 'post',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
              }
            })
          })
        }
      })
    },
    // 关闭对话框回调
    closeDialogHandle () {
      this.$refs['dataForm'].resetFields()
      this.dataForm.relation = null
    }
  }
}
</script>
<style lang="scss">
</style>
