<template>
  <doc-alert title="会员手册（功能开启）" url="https://doc.iocoder.cn/member/build/" />

  <ContentWrap>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
<!--      <el-form-item label="hideId" v-show="false">-->
<!--        <el-input v-model="formData.id" />-->
<!--      </el-form-item>-->

      <el-tabs>
        <el-tab-pane label="付费会员配置">
          <el-form-item label="开启付费会员" prop="openPaidMember">
            <el-radio v-model="formData.openPaidMember" :label="true">开启</el-radio>
            <el-radio v-model="formData.openPaidMember" :label="false">关闭</el-radio>
            <el-text class="w-full" size="small" type="info">
              付费会员开关
            </el-text>
          </el-form-item>
          <el-form-item v-if="formData.openPaidMember === true" label="商品付费会员价" prop="paidMemberPrice">
            <el-radio v-model="formData.paidMemberPrice" :label="true">开启</el-radio>
            <el-radio v-model="formData.paidMemberPrice" :label="false">关闭</el-radio>
            <el-text class="w-full" size="small" type="info">
              商品付费会员价是否展示
            </el-text>
          </el-form-item>

        </el-tab-pane>
      </el-tabs>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PadiMemberTypeVoApi from '@/api/member/memberconfig'
import PaidMemberConfigForm from './PaidMemberConfigForm.vue'
import * as ConfigApi from "@/api/member/config";

/** 付费会员配置 列表 */
defineOptions({ name: 'PaidMemberConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  openPaidMember: true,
  paidMemberPrice: true
})

const formRules = reactive({})
const formRef = ref() // 表单 Ref

const onSubmit = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PadiMemberTypeVoApi.PaidMemberConfigVO
    await PadiMemberTypeVoApi.PaidMemberConfigApi.savePaidMemberConfig(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
  } finally {
    formLoading.value = false
  }
}

/** 查询付费会员配置 */
const getPaidMemberConfig = async () =>{
  try {
    const data = await PadiMemberTypeVoApi.PaidMemberConfigApi.getPaidMemberConfig()
    if(data == null){
      return
    }
    formData.value = data
  }finally {
  }
}


// 使用 watch 来观察 openPaidMember 的变化
watch(
  () => formData.value.openPaidMember,
  (newVal) => {
    if (!newVal) {
      formData.value.paidMemberPrice = false; // 当 openPaidMember 变为 false 时，重置 paidMemberPriceStatus
    }
    // 注意：这里不需要处理 newVal 为 true 的情况，因为表单项会根据 v-if 指令的条件渲染
  },
  { immediate: true } // 立即执行一次观察者回调，以确保初始状态正确
);

/** 初始化 **/
onMounted(() => {
   getPaidMemberConfig()
})
</script>
