<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.mobile"
              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">
      <el-table class="mt-4" size="large" :data="pager.lists">
        <el-table-column type="index" width="55"/>
        <el-table-column label="用户id" prop="userId" min-width="120" show-tooltip-when-overflow/>
        <el-table-column label="手机号" prop="userMobile" min-width="120" show-tooltip-when-overflow/>
        <el-table-column label="会员名称" prop="membershipName" min-width="80"/>
        <el-table-column label="会员编号" prop="membershipNo" min-width="100"/>
        <el-table-column label="账户余额(元)" prop="accountAmount" align="right" min-width="100">
          <template #default="scope">
            <span style="font-weight: bolder;color:#F56C6C">{{ formatAmount(scope.row.accountAmount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入会时间" prop="time" min-width="120"/>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="showChargeConsumeRecord(row)">余额变动记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
  </div>
  <ChargeConsumeRecord @init="init()" ref="chargeConsumeRecordRef"></ChargeConsumeRecord>
</template>
<script lang="ts" setup>
import { useElabPaging } from '@/hooks/useElabPaging'
import feedback from '@/utils/feedback'
import {queryMemberUser} from "@/api/membership";
import ChargeConsumeRecord from './chargeConsumeRecord.vue'

const router = useRouter()
const chargeConsumeRecordRef = ref<InstanceType<typeof ChargeConsumeRecord>>()

const queryParams = reactive({
  mobile: ''
})
const { pager, getLists, resetPage, resetParams } = useElabPaging({
  fetchFun: queryMemberUser,
  params: queryParams
})
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
// 示例：添加千分位分隔符并保留两位小数
const formatAmount = (amount: string) => {
  return Number(amount).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
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 useMethodFormatter = (row: any) => {
  return row.useMethod == 0 ? '仅展示': ''
}
const volumeFormatter = (row: any) => {
  if (row.volume == 0) {
    return '大'
  } else if (row.volume == 1) {
    return '中'
  } else if (row.volume == 2) {
    return '小'
  } else if (row.volume == 3) {
    return '关闭'
  } else {
    return '大'
  }
}

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()
  }
}

const showChargeConsumeRecord = async (row: any) => {
  chargeConsumeRecordRef.value?.open(row)
}
getLists()
</script>
