<template>
  <a-drawer
    :visible="vdata.visible"
    :title=" vdata.isAdd ? '新增会员' : '修改会员'"
    width="40%"
    @close="onClose"
  >
    <a-form ref="infoFormModel" :model="vdata.saveObject" layout="vertical" :rules="vdata.rules">
      <a-row :gutter="16">
        <a-col :span="12">
          <a-form-item label="会员名称" name="mbrName">
            <a-input v-model:value="vdata.saveObject['mbrName']" placeholder="请输入会员名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" name="state">
            <a-radio-group v-model:value="vdata.saveObject['state']">
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">禁用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="会员手机号" name="mbrTel">
            <a-input v-model:value="vdata.saveObject['mbrTel']" :disabled="!vdata.isAdd" placeholder="请输入会员手机号" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="24">
          <a-form-item label="会员备注" name="remark" class="m-b-50 ">
            <a-textarea v-model:value="vdata.saveObject['remark']" placeholder="请输入会员备注" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" @click="onClose"><close-outlined />取消</a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="onSubmit"><check-outlined />保存</a-button>
    </div>
  </a-drawer>
</template>

<script setup lang="ts">
import { API_URL_MEMBER_LIST, req } from '@/api/manage'
import {ref, reactive} from 'vue'

const props = defineProps({
  callbackFunc: { type: Function, default: () => () => ({}) }
})
  
const vdata : any =  reactive({
  isAdd: true, // 新增 or 修改
  visible: false, // 抽屉开关
  mbrId: '', // 会员ID
  saveObject: {}, // 数据对象
  btnLoading:false,
  rules: {
    mbrName: [{ required: true, message: '请输入会员名称', trigger: 'blur' }],
    mbrTel: [{ required: true, pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }],
    state: [{ required: true, message: '请输选择状态', trigger: 'blur' }],
  }
})

// 表单组件
const infoFormModel = ref()

// 抽屉显示
const show = (mbrId) => {
  vdata.isAdd = !mbrId
  // 数据清空
  vdata.saveObject = { state:1 }
  
  if (infoFormModel.value !== undefined) {
    infoFormModel.value.resetFields()
  }

  if (!vdata.isAdd) { // 修改信息 延迟展示弹层
    vdata.mbrId = mbrId
    // 拉取详情
    req.getById(API_URL_MEMBER_LIST, mbrId).then(res => {
      vdata.saveObject = res
    })

    vdata.visible = true
  } else {

    vdata.visible = true // 展示弹层信息
  }
}
// 表单提交
const onSubmit = () => {

  infoFormModel.value.validate().then(() => {

    vdata.btnLoading = true
    let reqObject = Object.assign({}, vdata.saveObject) // 请求数据

    req.addOrUpdate(vdata.isAdd ? null : vdata.mbrId, API_URL_MEMBER_LIST, reqObject).then(res => {
        vdata.visible = false
        props.callbackFunc() // 刷新列表
    }).finally(() => {
        vdata.btnLoading = false
    })
  })
}

const onClose = () => {
  vdata.visible = false
}

defineExpose({ show })
</script>

