<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-14 21:53:04
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-15 21:32:05
 * @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-type-manage">
      <el-dialog
        v-model="visible"
        :title="dataForm.typeId ? $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="typeName"
          >
            <el-input
              v-model="dataForm.typeName"
              type="text"
              maxlength="10"
              placeholder="类型名称"
            />
          </el-form-item>
          <!-- 类型图片 -->
          <el-form-item
            label="类型图片:"
            prop="typeImage"
          >
            <img-upload
              v-model="dataForm.typeImage"
              @input="onPropChange('typeImage')"
            />
            <div
              class=""
              style="width: 100%"
            >
              建议图片尺寸为1920*45 0
            </div>
          </el-form-item>
          <!-- 排序 -->
          <el-form-item
            label="排序:"
            prop="typeSeq"
          >
            <el-input
              v-model="dataForm.typeSeq"
              type="text"
              maxlength="10"
              placeholder="排序"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            label="状态:"
            prop="typeStatus"
          >
            <el-radio-group v-model="dataForm.typeStatus">
              <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 { getStickTypeDetail, addStickType, updateStickType } 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({
  typeId: null,
  typeName: '',
  typeImage: '',
  typeSeq: '',
  typeStatus: 1
})
const rules = reactive({
  typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
  typeImage: [
    { required: true, message: '请选择类型图片', trigger: 'blur' }
  ],
  typeSeq: [
    { required: true, message: '请输入类型排序', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '排序必须为数字值且不能小于0',
      trigger: 'blur'
    }
  ],
  typeStatus: [
    { required: true, message: '请选择类型状态', trigger: 'blur' }
  ]
})
const init = (typeId) => {
  console.log('init', typeId)
  visible.value = true
  dataForm.value.typeId = typeId || null
  dataFormRef.value?.clearValidate()
  if (typeId) {
    getClubInfo()
  }
}
const closeDialog = () => {
  visible.value = false
  dataForm.value = {
    typeId: null,
    typeName: '',
    typeImage: '',
    typeSeq: '',
    typeStatus: 1
  }
}
const getClubInfo = () => {
  getStickTypeDetail({ typeId: dataForm.value.typeId }).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.typeId) {
        addStickType(dataForm.value).then(() => {
          ElMessage.success('新增成功')
          emit('refresh-data-list')
          btnLoading.value = false
          closeDialog()
        }).catch((err) => {
          btnLoading.value = false
          console.log(err)
        })
      } else {
        updateStickType(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>
