<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-14 21:53:04
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-15 21:50:06
 * @FilePath: \tmerclub-platform\src\views\modules\product\recycle\club\add-or-update.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <div class="club-brand-manage">
      <el-dialog
        v-model="visible"
        :title="dataForm.brandId ? $t('table.edit') : $t('table.create')"
        :close-on-click-modal="false"
        width="600px"
        :destroy-on-close="true"
        top="5vh"
        class="el-dialog-bodyt"
        @close="closeDialog"
      >
        <el-form
          ref="dataFormRef"
          :rules="rules"
          :model="dataForm"
          label-position="right"
          label-width="110px"
          style="width: 500px; margin-left: 35px"
          @submit.prevent
        >
          <!-- 品牌名称 -->
          <el-form-item
            label="品牌名称:"
            prop="brandName"
          >
            <el-input
              v-model="dataForm.brandName"
              type="text"
              maxlength="10"
              placeholder="品牌名称"
            />
          </el-form-item>
          <!-- 品牌图片 -->
          <el-form-item
            label="品牌图片:"
            prop="brandLogo"
          >
            <img-upload
              v-model="dataForm.brandLogo"
              @input="onPropChange('brandLogo')"
            />
            <div
              class=""
              style="width: 100%"
            >
              建议图片尺寸为1920*45 0
            </div>
          </el-form-item>
          <!-- 排序 -->
          <el-form-item
            label="排序:"
            prop="brandSeq"
          >
            <el-input
              v-model="dataForm.brandSeq"
              type="text"
              maxlength="10"
              placeholder="排序"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            label="状态:"
            prop="brandStatus"
          >
            <el-radio-group v-model="dataForm.brandStatus">
              <el-radio :label="1">
                启用
              </el-radio>
              <el-radio :label="0">
                禁用
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="visible = false">
              {{ $t("table.cancel") }}
            </el-button>
            <el-button
              type="primary"
              :loading="btnLoading"
              @click="onSubmit()"
            >
              {{ $t("table.confirm") }}
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { getStickBrandDetail, addStickBrand, updateStickBrand } from '@/api/product/recycle'
const emit = defineEmits(['refresh-data-list'])
const dataFormRef = ref(null)
const visible = ref(false)
const btnLoading = ref(false)
const dataForm = ref({
  brandId: null,
  brandName: '',
  brandLogo: '',
  brandSeq: '',
  brandStatus: 1
})
const rules = reactive({
  brandName: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }],
  brandLogo: [
    { required: true, message: '请选择品牌图片', trigger: 'blur' }
  ],
  brandSeq: [
    { required: true, message: '请输入品牌排序', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '排序必须为数字值且不能小于0',
      trigger: 'blur'
    }
  ],
  brandStatus: [
    { required: true, message: '请选择品牌状态', trigger: 'blur' }
  ]
})
const init = (brandId) => {
  console.log('init', brandId)
  visible.value = true
  dataForm.value.brandId = brandId || null
  dataFormRef.value?.clearValidate()
  if (brandId) {
    getClubInfo()
  }
}
const closeDialog = () => {
  visible.value = false
  dataForm.value = {
    brandId: null,
    brandName: '',
    brandLogo: '',
    brandSeq: '',
    brandStatus: 1
  }
}
const getClubInfo = () => {
  getStickBrandDetail({ brandId: dataForm.value.brandId }).then((res) => {
    Object.keys(dataForm.value).forEach((key) => {
      dataForm.value[key] = res[key]
    })
  })
}
const onPropChange = (prop) => {
  dataFormRef.value?.validateField(prop)
}

const onSubmit = () => {
  console.log(dataForm.value)

  dataFormRef.value?.validate((valid) => {
    if (valid) {
      btnLoading.value = true
      if (!dataForm.value.brandId) {
        addStickBrand(dataForm.value).then(() => {
          ElMessage.success('新增成功')
          emit('refresh-data-list')
          btnLoading.value = false
          closeDialog()
        }).catch((err) => {
          btnLoading.value = false
          console.log(err)
        })
      } else {
        updateStickBrand(dataForm.value).then(() => {
          ElMessage.success('修改成功')
          emit('refresh-data-list')
          btnLoading.value = false
          closeDialog()
        }).catch((err) => {
          btnLoading.value = false
          console.log(err)
        })
      }
      visible.value = false
    } else {
      console.log('error submit!!')
      return false
    }
  })
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped></style>
