<!--
 * @Author: 快乐橙 1760016317@qq.com
 * @Date: 2025-04-14 21:53:04
 * @LastEditors: 快乐橙 1760016317@qq.com
 * @LastEditTime: 2025-04-15 20:09:31
 * @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-manage">
      <el-dialog
        v-model="visible"
        :title="dataForm.productId ? $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="productName"
          >
            <el-input
              v-model="dataForm.productName"
              type="text"
              maxlength="10"
              placeholder="商品名称"
            />
          </el-form-item>
          <!-- 商品图片 -->
          <el-form-item
            label="商品图片:"
            prop="productImages"
          >
            <img-upload
              v-model="dataForm.productImages"
              @input="onPropChange('productImages')"
            />
            <div
              class=""
              style="width: 100%"
            >
              建议图片尺寸为1920*45 0
            </div>
          </el-form-item>
          <!-- 商品价格 -->
          <el-form-item
            label="商品价格:"
            prop="productPrice"
          >
            <el-input
              v-model="dataForm.productPrice"
              type="text"
              maxlength="10"
              placeholder="商品价格"
            />
          </el-form-item>
          <!-- 品牌选择 -->
          <el-form-item
            label="品牌选择:"
            prop="brandId"
          >
            <el-select
              v-model="dataForm.brandId"
              placeholder="请选择品牌"
              :empty-values="[null, undefined]"
              value-key="brandId"
            >
              <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="typeId"
          >
            <el-select
              v-model="dataForm.typeId"
              placeholder="请选择类型"
            >
              <el-option
                v-for="item in stickTypeList"
                :key="item.typeId"
                :disabled="item.typeStatus == 0"
                :label="item.typeName"
                :value="item.typeId"
              />
            </el-select>
          </el-form-item>
          <!-- 获取系列 -->
          <el-form-item
            label="系列选择:"
            prop="seriesId"
          >
            <el-select
              v-model="dataForm.seriesId"
              placeholder="请选择系列"
            >
              <el-option
                v-for="item in stickSeriesList"
                :key="item.seriesId"
                :disabled="item.seriesStatus == 0"
                :label="item.seriesName"
                :value="item.seriesId"
              />
            </el-select>
          </el-form-item>
          <!-- 排序 -->
          <el-form-item
            label="排序:"
            prop="productSeq"
          >
            <el-input
              v-model="dataForm.productSeq"
              type="text"
              maxlength="10"
              placeholder="排序"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            label="状态:"
            prop="productStatus"
          >
            <el-radio-group v-model="dataForm.productStatus">
              <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 { getStickDetail, addStick, updateStick } from '@/api/product/recycle'
const props = defineProps({
  stickBrandList: {
    type: Array,
    default: () => []
  },
  stickTypeList: {
    type: Array,
    default: () => []
  },
  stickSeriesList: {
    type: Array,
    default: () => []
  }
})
const emit = defineEmits(['refresh-data-list'])
const { proxy } = getCurrentInstance()
const dataFormRef = ref(null)
const route = useRoute()
const router = useRouter()
const visible = ref(false)
const title = ref('')
const type = ref('')
const clubList = ref([])
const attrValue = ref('')
const btnLoading = ref(false)
const dataForm = ref({
  productId: null,
  productName: '',
  productDescription: '',
  productImages: '',
  productPrice: '',
  productSeq: '',
  productStatus: 1,
  typeId: null,
  brandId: null,
  seriesId: null
})
const rules = reactive({
  productName: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
  productPrice: [
    { required: true, message: '请输入商品价格', trigger: 'blur' },
    // 价格必须为数字值 且不能小于0
    {
      pattern: /^\d+$/,
      message: '价格必须为数字值且不能小于0',
      trigger: 'blur'
    }
  ],
  productImages: [
    { required: true, message: '请选择商品图片', trigger: 'blur' }
  ],
  productSeq: [
    { required: true, message: '请输入商品排序', trigger: 'blur' },
    {
      pattern: /^\d+$/,
      message: '排序必须为数字值且不能小于0',
      trigger: 'blur'
    }
  ],
  productStatus: [
    { required: true, message: '请选择商品状态', trigger: 'blur' }
  ],
  typeId: [{ required: true, message: '请选择球杆类型', trigger: 'blur' }],
  brandId: [{ required: true, message: '请选择球杆品牌', trigger: 'blur' }],
  seriesId: [{ required: true, message: '请选择球杆系列', trigger: 'blur' }]
})
const init = (productId) => {
  console.log('init', productId)
  visible.value = true
  dataForm.value.productId = productId || null

  dataFormRef.value?.clearValidate()
  if (productId) {
    getClubInfo()
  }
}
const closeDialog = () => {
  visible.value = false
  dataForm.value = {
    productId: null,
    productName: '',
    productDescription: '',
    productImages: '',
    productPrice: '',
    productSeq: '',
    productStatus: 1,
    typeId: null,
    brandId: null,
    seriesId: null
  }
}
const getClubInfo = () => {
  getStickDetail({ productId: dataForm.value.productId }).then((res) => {
    Object.keys(dataForm.value).forEach((key) => {
      dataForm.value[key] = res[key]
    })
    console.log(props.stickBrandList)
    nextTick(() => {
      dataFormRef.value?.forceUpdate()
    })
    console.log(dataForm.value)
  })
}
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.productId) {
        addStick(dataForm.value).then(() => {
          ElMessage.success('新增成功')
          emit('refresh-data-list')
          btnLoading.value = false
          closeDialog()
        }).catch((err) => {
          btnLoading.value = false
          console.log(err)
        })
      } else {
        updateStick(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>
