<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-14 21:53:04
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-15 22:42:41
 * @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-flaw-manage">
      <el-dialog
        v-model="visible"
        :title="dataForm.flawId ? $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="flawName"
          >
            <el-input
              v-model="dataForm.flawName"
              type="text"
              maxlength="10"
              placeholder="缺陷名称"
            />
          </el-form-item>
          <el-form-item
            label="缺陷价格:"
            prop="flawPrice"
          >
            <el-input
              v-model="dataForm.flawPrice"
              type="text"
              maxlength="10"
              placeholder="缺陷价格"
            />
          </el-form-item>

          <!-- 排序 -->
          <el-form-item
            label="排序:"
            prop="flawSeq"
          >
            <el-input
              v-model="dataForm.flawSeq"
              type="text"
              maxlength="10"
              placeholder="排序"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            label="状态:"
            prop="flawStatus"
          >
            <el-radio-group v-model="dataForm.flawStatus">
              <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 { getStickDefectDetail, addStickDefect, updateStickDefect } 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({
  flawId: null,
  flawName: '',
  flawPrice: '',
  flawSeq: '',
  flawStatus: 1
})
const rules = reactive({
  flawName: [{ required: true, message: '请输入缺陷名称', trigger: 'blur' }],
  typeImage: [
    { required: true, message: '请选择缺陷图片', trigger: 'blur' }
  ],
  flawPrice: [
    { required: true, message: '请输入缺陷价格', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '价格必须为数字值',
      trigger: 'blur'
    }
  ],
  flawSeq: [
    { required: true, message: '请输入缺陷排序', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '排序必须为数字值',
      trigger: 'blur'
    }
  ],
  flawStatus: [
    { required: true, message: '请选择缺陷状态', trigger: 'blur' }
  ]
})
const init = (flawId) => {
  console.log('init', flawId)
  visible.value = true
  dataForm.value.flawId = flawId || null
  dataFormRef.value?.clearValidate()
  if (flawId) {
    getClubInfo()
  }
}
const closeDialog = () => {
  visible.value = false
  dataForm.value = {
    flawId: null,
    flawName: '',
    flawPrice: '',
    flawSeq: '',
    flawStatus: 1
  }
}
const getClubInfo = () => {
  getStickDefectDetail({ flawId: dataForm.value.flawId }).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.flawId) {
        addStickDefect(dataForm.value).then(() => {
          ElMessage.success('新增成功')
          emit('refresh-data-list')
          btnLoading.value = false
          closeDialog()
        }).catch((err) => {
          btnLoading.value = false
          console.log(err)
        })
      } else {
        updateStickDefect(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>
