<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-14 21:53:04
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-15 22:41:30
 * @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-series-manage">
      <el-dialog
        v-model="visible"
        :title="dataForm.seriesId ? $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="seriesName"
          >
            <el-input
              v-model="dataForm.seriesName"
              type="text"
              maxlength="10"
              placeholder="系列称"
            />
          </el-form-item>
          <!-- 系列选择 -->
          <el-form-item
            label="系列选择:"
            prop="seriesId"
          >
            <el-select
              v-model="dataForm.brandId"
              placeholder="请选择系列"
              :empty-values="[null, undefined]"
              value-key="seriesId"
            >
              <el-option
                v-for="item in stickBrandList"
                :key="item.brandId"
                :disabled="item.brandStatus == 0"
                :label="item.brandName"
                :value="item.brandId"
              />
            </el-select>
          </el-form-item>
          <!-- 排序 -->
          <el-form-item
            label="排序:"
            prop="seriesSeq"
          >
            <el-input
              v-model="dataForm.seriesSeq"
              type="text"
              maxlength="10"
              placeholder="排序"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            label="状态:"
            prop="seriesStatus"
          >
            <el-radio-group v-model="dataForm.seriesStatus">
              <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 {
  getStickSeriesDetail,
  addStickSeries,
  updateStickSeries
} from '@/api/product/recycle'
const emit = defineEmits(['refresh-data-list'])
const dataFormRef = ref(null)
const visible = ref(false)
const btnLoading = ref(false)
const props = defineProps({
  stickBrandList: {
    type: Array,
    default: () => []
  }
})
const dataForm = ref({
  seriesId: null,
  seriesName: '',
  brandId: '',
  seriesSeq: '',
  seriesStatus: 1
})
const rules = reactive({
  seriesName: [{ required: true, message: '请输入系列称', trigger: 'blur' }],
  seriesSeq: [
    { required: true, message: '请输入系列排序', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '排序必须为数字值且不能小于0',
      trigger: 'blur'
    }
  ],
  seriesStatus: [
    { required: true, message: '请选择系列状态', trigger: 'blur' }
  ]
})
const init = (seriesId) => {
  console.log('init', seriesId)
  visible.value = true
  dataForm.value.seriesId = seriesId || null
  dataFormRef.value?.clearValidate()
  if (seriesId) {
    getClubInfo()
  }
}
const closeDialog = () => {
  visible.value = false
  dataForm.value = {
    seriesId: null,
    seriesName: '',
    brandId: '',
    seriesSeq: '',
    seriesStatus: 1
  }
}
const getClubInfo = () => {
  getStickSeriesDetail({ seriesId: dataForm.value.seriesId }).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.seriesId) {
        addStickSeries(dataForm.value)
          .then(() => {
            ElMessage.success('新增成功')
            emit('refresh-data-list')
            btnLoading.value = false
            closeDialog()
          })
          .catch((err) => {
            btnLoading.value = false
            console.log(err)
          })
      } else {
        updateStickSeries(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>
