<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="会员名称">
          <el-input
              class="w-[280px]"
              v-model="queryParams.name"
              placeholder="精准匹配"
              clearable
              @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never" v-loading="pager.loading">
      <div>
        <el-button v-perms="['article:cate:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus"/>
          </template>
          新增会员配置
        </el-button>
      </div>
      <el-table class="mt-4" size="large" :data="pager.lists">
        <el-table-column type="index" width="55"/>
        <el-table-column label="会员名称" prop="name" min-width="120" show-tooltip-when-overflow/>
        <el-table-column label="会员等级" prop="level" min-width="80"/>
        <el-table-column label="首充金额(元)" prop="chargeAmount" min-width="100" :formatter="chargeAmountFormatter"/>
        <el-table-column label="赠送金额(元)" prop="giftAmount" min-width="100" :formatter="giftAmountFormatter"/>
        <el-table-column label="入园次数" prop="enterCount" min-width="80"/>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['article:cate:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
<!--            <el-button-->
<!--                v-perms="['article:cate:del']"-->
<!--                type="danger"-->
<!--                link-->
<!--                @click="handleDelete(row.id)"-->
<!--            >-->
<!--              删除-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>
<script lang="ts" setup>
import { useElabPaging } from '@/hooks/useElabPaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {queryMember} from "@/api/membership";

const queryParams = reactive({
  name: ''
})
const { pager, getLists, resetPage, resetParams } = useElabPaging({
  fetchFun: queryMember,
  params: queryParams
})
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const chargeAmountFormatter = (row: any) => {
  return row.chargeAmount / 100
}
const giftAmountFormatter = (row: any) => {
  return row.giftAmount / 100
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  // await articleCateDelete({id})
  await delPrinter({id})
  feedback.msgSuccess('删除成功')
  getLists()
}

const changeStatus = async (id: number, status: any) => {
  try {
    await editPrinter({id, status})
    feedback.msgSuccess('修改成功')
    getLists()
  } catch (error) {
    getLists()
  }
}
getLists()
</script>
