<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
        <el-form-item label="会员名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入会员名称" :maxlength="10" clearable/>
        </el-form-item>
        <el-form-item label="会员等级" prop="level">
          <el-input v-model="formData.level" type="number" placeholder="请输入会员等级" clearable/>
        </el-form-item>
        <el-form-item label="首充金额" prop="chargeAmount">
            <el-input v-model="formData.chargeAmount" type="number" placeholder="请输入首充金额" clearable />
        </el-form-item>
        <el-form-item label="赠送金额" prop="giftAmount">
            <el-input v-model="formData.giftAmount" type="number" placeholder="请输入赠送金额" clearable />
        </el-form-item>
        <el-form-item label="入园次数" prop="enterCount">
            <el-input v-model="formData.enterCount" type="number" placeholder="请输入入园次数" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" type="textarea" clearable />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type {FormInstance} from 'element-plus'
// import {articleCateEdit} from '@/api/article'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import { saveMember} from '@/api/membership'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑' : '新增'
})
const formData = reactive({
  id: '',
  name: '',
  level: 1,
  chargeAmount: '',
  giftAmount: '',
  enterCount: '',
  remark: ''
})

const formRules = {
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: ['blur']
    }
  ],
  level: [
    {
      required: true,
      message: '请输入会员等级',
      trigger: ['blur']
    }
  ],
  chargeAmount: [
    {
      required: true,
      message: '请输入首充金额',
      trigger: ['blur']
    }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  await saveMember(formData)
  feedback.msgSuccess('操作成功')
  popupRef.value?.close()
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  // const data = await getPrinterDetail({
  //   id: row.id
  // })
  setFormData(row)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
