<script setup lang="ts">
import { reactive, ref } from "vue";
import { getCustomUserList } from "@/api/custom";

const emit = defineEmits(["refresh"]);

const drawerVisible = ref(false);
const loading = ref(false);

const state = reactive({
  userInfo: null as any
});

const open = async (row: any) => {
  drawerVisible.value = true;
  state.userInfo = row;
};

const handleClose = () => {
  drawerVisible.value = false;
  state.userInfo = null;
};

const formatMoney = (value: number) => {
  if (value === null || value === undefined) return "¥0.00";
  return `¥${(value / 100).toFixed(2)}`;
};
</script>

<template>
  <el-drawer
    v-model="drawerVisible"
    title="用户详情"
    size="600px"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <template v-if="state.userInfo">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户ID">{{ state.userInfo.userId || state.userInfo.id }}</el-descriptions-item>
        <el-descriptions-item label="昵称">{{ state.userInfo.nickname || "-" }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ state.userInfo.mobilePhone || "-" }}</el-descriptions-item>
        <el-descriptions-item label="归属站点">{{ state.userInfo.stationName || "-" }}</el-descriptions-item>
        <el-descriptions-item label="站点ID">{{ state.userInfo.stationId || "-" }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="state.userInfo.status === 1 ? 'success' : 'info'" size="small">
            {{ state.userInfo.status === 1 ? "启用" : "禁用" }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="总余额">{{ formatMoney(state.userInfo.balance) }}</el-descriptions-item>
        <el-descriptions-item label="充值余额">{{ formatMoney(state.userInfo.rechargeBalance) }}</el-descriptions-item>
        <el-descriptions-item label="赠金余额">{{ formatMoney(state.userInfo.grantsBalance) }}</el-descriptions-item>
        <el-descriptions-item label="冻结余额">{{ formatMoney(state.userInfo.frozenAmount) }}</el-descriptions-item>
        <el-descriptions-item label="充值次数">{{ state.userInfo.rechargeTimes || 0 }}</el-descriptions-item>
        <el-descriptions-item label="充值金额">{{ formatMoney(state.userInfo.rechargeAmount) }}</el-descriptions-item>
        <el-descriptions-item label="退款次数">{{ state.userInfo.refundTimes || 0 }}</el-descriptions-item>
        <el-descriptions-item label="退款金额">{{ formatMoney(state.userInfo.refundAmount) }}</el-descriptions-item>
        <el-descriptions-item label="洗车次数">{{ state.userInfo.washTimes || 0 }}</el-descriptions-item>
        <el-descriptions-item label="消费总额">{{ formatMoney(state.userInfo.amount) }}</el-descriptions-item>
        <el-descriptions-item label="应付总额">{{ formatMoney(state.userInfo.amountReceivable) }}</el-descriptions-item>
        <el-descriptions-item label="实付总额">{{ formatMoney(state.userInfo.amountReceived) }}</el-descriptions-item>
        <el-descriptions-item label="优惠总额">{{ formatMoney(state.userInfo.discountAmount) }}</el-descriptions-item>
        <el-descriptions-item label="退款扣除优惠">{{ formatMoney(state.userInfo.refundDiscountAmount) }}</el-descriptions-item>
        <el-descriptions-item label="注册时间">{{ state.userInfo.registerTime || "-" }}</el-descriptions-item>
      </el-descriptions>
    </template>
  </el-drawer>
</template>
