<script lang="ts" setup>
import { computed, h, onMounted, ref } from 'vue';

import { Page } from '@vben/common-ui';
import { Button, message, Modal, Select, Switch, Tooltip } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  batchUnfreezeAgentCommission,
  getAgentCommissionList,
  getAgentList,
  getAgentWallet,
  updateAgentCommissionStatus,
  getSystemConfig,
  updateSystemConfig,
} from '#/api/agent';
import type { AgentApi } from '#/api';

import { useCommissionColumns, useCommissionFormSchema } from './data';

interface Props {
  agentId?: number;
}

const props = defineProps<Props>();

// 用于一键解冻筛选的代理商ID
const unfreezeAgentId = ref<number | undefined>();

// 佣金安全防御模式配置
const commissionSafeMode = ref<boolean>(false);
const safeModeLoading = ref<boolean>(false);

// 代理商列表（完整列表）
const allAgentList = ref<AgentApi.AgentListItem[]>([]);

// 显示在下拉框中的代理商列表（可能是过滤后的）
const agentList = ref<AgentApi.AgentListItem[]>([]);

const queryParams = computed(() => ({
  ...(props.agentId ? { agent_id: props.agentId } : {}),
  ...(unfreezeAgentId.value ? { agent_id: unfreezeAgentId.value } : {}),
}));

// 加载代理商列表
async function loadAgentList() {
  try {
    const result = await getAgentList({ page: 1, pageSize: 10000 });
    allAgentList.value = result.items || [];
    agentList.value = result.items || [];
  } catch (error) {
    console.error('加载代理商列表失败:', error);
    message.error('加载代理商列表失败，请刷新页面重试');
  }
}

// 搜索时动态过滤代理商（支持ID和手机号）
function onAgentSearch(value: string) {
  if (!value || value.trim() === '') {
    // 如果输入为空，显示所有代理商
    agentList.value = allAgentList.value;
    return;
  }

  const searchValue = value.trim();

  // 从完整列表中过滤匹配的代理商
  const filtered = allAgentList.value.filter(agent => {
    // 匹配代理ID
    if (agent.id.toString().includes(searchValue)) {
      return true;
    }
    // 匹配手机号
    if (agent.mobile && agent.mobile.includes(searchValue)) {
      return true;
    }
    // 匹配姓名（如果存在）
    if (agent.real_name && agent.real_name.includes(searchValue)) {
      return true;
    }
    return false;
  });

  agentList.value = filtered;
}

// 获取未找到时的提示文案
function getNotFoundContent() {
  if (unfreezeAgentId.value) {
    return '点击确认使用此ID';
  }
  return '暂无代理商';
}

// 页面加载时获取代理商列表和系统配置
onMounted(async () => {
  loadAgentList();
  await loadSystemConfig();
});

// 加载系统配置
async function loadSystemConfig() {
  try {
    const config = await getSystemConfig();
    commissionSafeMode.value = config.commission_safe_mode;
  } catch (error: any) {
    console.error('加载系统配置失败:', error);
    message.error('加载系统配置失败');
  }
}

// 切换安全防御模式
async function onSafeModeChange(checked: boolean | string | number) {
  const isChecked = Boolean(checked);
  safeModeLoading.value = true;
  try {
    await updateSystemConfig({ commission_safe_mode: isChecked });
    commissionSafeMode.value = isChecked;
    message.success(`佣金安全防御模式已${isChecked ? '开启' : '关闭'}`);
  } catch (error: any) {
    const errorMsg = error?.response?.data?.msg || error?.message || '操作失败，请重试';
    message.error(errorMsg);
    // 恢复原状态
    commissionSafeMode.value = !isChecked;
  } finally {
    safeModeLoading.value = false;
  }
}

// 操作处理函数
function onActionClick({ code, row }: { code: string; row: any }) {
  switch (code) {
    case 'freeze':
      onFreeze(row);
      break;
    case 'unfreeze':
      onUnfreeze(row);
      break;
  }
}

// 冻结佣金
async function onFreeze(row: any) {
  try {
    // 先获取代理商钱包信息，检查余额是否充足
    const hideChecking = message.loading({
      content: '正在检查余额...',
      duration: 0,
      key: 'check_balance',
    });

    const wallet = await getAgentWallet(row.agent_id);
    hideChecking();

    // 检查余额是否充足
    if (wallet.balance < row.amount) {
      Modal.warning({
        title: '余额不足',
        content: `该代理商当前可用余额为 ¥${wallet.balance.toFixed(2)}，不足以冻结佣金 ¥${row.amount.toFixed(2)}。\n缺少金额：¥${(row.amount - wallet.balance).toFixed(2)}`,
        okText: '我知道了',
      });
      return;
    }

    // 余额充足，继续冻结操作
    Modal.confirm({
      title: '确认冻结',
      content: `确定要冻结佣金金额 ¥${row.amount.toFixed(2)} 吗？\n当前可用余额：¥${wallet.balance.toFixed(2)}，冻结后可用余额：¥${(wallet.balance - row.amount).toFixed(2)}`,
      okText: '确认冻结',
      cancelText: '取消',
      onOk: async () => {
        try {
          await updateAgentCommissionStatus(row.id, 1);
          message.success('佣金已冻结');
          onRefresh();
        } catch (error: any) {
          const errorMsg = error?.response?.data?.msg || error?.message || '操作失败，请重试';
          message.error(errorMsg);
        }
      },
    });
  } catch (error: any) {
    const errorMsg = error?.response?.data?.msg || error?.message || '获取钱包信息失败，请重试';
    message.error(errorMsg);
  }
}

// 解冻佣金到用户余额
async function onUnfreeze(row: any) {
  try {
    // 获取代理商钱包信息用于显示
    const hideChecking = message.loading({
      content: '正在获取钱包信息...',
      duration: 0,
      key: 'get_wallet',
    });

    const wallet = await getAgentWallet(row.agent_id);
    hideChecking();

    Modal.confirm({
      title: '确认解冻',
      content: `确定要解冻佣金金额 ¥${row.amount.toFixed(2)} 吗？\n解冻后将转入用户钱包余额。\n当前可用余额：¥${wallet.balance.toFixed(2)}，解冻后可用余额：¥${(wallet.balance + row.amount).toFixed(2)}`,
      okText: '确认解冻',
      cancelText: '取消',
      onOk: async () => {
        try {
          await updateAgentCommissionStatus(row.id, 0);
          message.success('佣金已解冻并转入用户钱包余额');
          onRefresh();
        } catch (error: any) {
          const errorMsg = error?.response?.data?.msg || error?.message || '操作失败，请重试';
          message.error(errorMsg);
        }
      },
    });
  } catch (error: any) {
    const errorMsg = error?.response?.data?.msg || error?.message || '获取钱包信息失败，请重试';
    message.error(errorMsg);
  }
}

// 刷新列表
function onRefresh() {
  gridApi.query();
}

// 选中代理商后刷新表格
function onAgentSelect(value: any) {
  console.log('选中代理商:', value);
  // 如果是清空选择，value 为 undefined
  // 如果有值，转换为数字
  unfreezeAgentId.value = value ? parseInt(String(value), 10) : undefined;
  // 延迟一点让 computed 更新后再刷新
  setTimeout(() => {
    onRefresh();
  }, 100);
}

// 批量解冻佣金
async function onBatchUnfreeze() {
  const targetAgentId = unfreezeAgentId.value || props.agentId;

  const hideChecking = message.loading({
    content: '正在检查数据，请稍候...',
    duration: 0,
    key: 'check_frozen_data',
  });

  try {
    // 1. 查询所有冻结中的佣金记录
    const frozenCommissions = await getAgentCommissionList({
      page: 1,
      pageSize: 10000,
      status: 1,
      ...(targetAgentId ? { agent_id: targetAgentId } : {}),
    });

    hideChecking();

    // 如果没有冻结的佣金，直接返回
    if (!frozenCommissions.items || frozenCommissions.items.length === 0) {
      message.info({
        content: '没有需要解冻的冻结佣金',
        key: 'check_frozen_data',
      });
      return;
    }

    // 2. 统计每个代理商的佣金冻结金额
    const commissionFrozenMap = new Map<number, number>();
    frozenCommissions.items.forEach((item: any) => {
      const currentAmount = commissionFrozenMap.get(item.agent_id) || 0;
      commissionFrozenMap.set(item.agent_id, currentAmount + item.amount);
    });

    // 3. 检查每个代理商的钱包冻结余额是否足够
    let insufficientAgents: string[] = [];
    let totalFrozenAmount = 0;

    for (const [agentId, commissionAmount] of commissionFrozenMap) {
      totalFrozenAmount += commissionAmount;

      // 获取该代理商的钱包信息
      const wallet = await getAgentWallet(agentId);

      if (wallet.frozen_balance < commissionAmount) {
        // 找到该代理商的信息
        const agent = allAgentList.value.find(a => a.id === agentId);
        const agentInfo = agent ? `${agent.real_name || agent.mobile} (ID: ${agentId})` : `ID: ${agentId}`;
        insufficientAgents.push(
          `${agentInfo}：钱包冻结余额 ¥${wallet.frozen_balance.toFixed(2)}，需要解冻 ¥${commissionAmount.toFixed(2)}，缺少 ¥${(commissionAmount - wallet.frozen_balance).toFixed(2)}`
        );
      }
    }

    // 如果有余额不足的代理商，显示错误信息
    if (insufficientAgents.length > 0) {
      Modal.error({
        title: '冻结余额不足，无法解冻',
        width: 600,
        content: h('div', [
          h('p', '以下代理商的钱包冻结余额不足以解冻其冻结的佣金：'),
          h('ul', { style: { 'max-height': '300px', 'overflow-y': 'auto', 'padding-left': '20px' } },
            insufficientAgents.map(msg => h('li', { style: { marginBottom: '8px' } }, msg))
          ),
          h('p', { style: { marginTop: '16px', color: '#999' } }, '请先核实钱包冻结余额数据，或联系技术支持。'),
        ]),
        okText: '我知道了',
      });
      return;
    }

    // 4. 余额检查通过，确认解冻
    const content = targetAgentId
      ? `确定要一键解冻代理商 ID: ${targetAgentId} 所有冻结中的佣金吗？\n\n共 ${frozenCommissions.items.length} 条记录，总金额 ¥${totalFrozenAmount.toFixed(2)}。\n解冻后将全部转入用户钱包余额。`
      : `确定要一键解冻所有冻结中的佣金吗？\n\n共 ${frozenCommissions.items.length} 条记录，总金额 ¥${totalFrozenAmount.toFixed(2)}。\n解冻后将全部转入用户钱包余额。`;

    Modal.confirm({
      title: '批量解冻确认',
      content,
      okText: '确认解冻',
      cancelText: '取消',
      onOk: async () => {
        const hideLoading = message.loading({
          content: '正在批量解冻佣金，请稍候...',
          duration: 0,
          key: 'batch_unfreeze',
        });
        try {
          const result = await batchUnfreezeAgentCommission(targetAgentId);
          message.success({
            content: `批量解冻成功！共解冻 ${result.count} 条记录，总金额 ¥${result.amount.toFixed(2)}`,
            key: 'batch_unfreeze',
          });
          onRefresh();
        } catch (error: any) {
          hideLoading();
          const errorMsg = error?.response?.data?.msg || error?.message || '批量解冻失败，请重试';
          // 如果是版本冲突错误，给出更友好的提示
          if (errorMsg.includes('update db no rows change') || errorMsg.includes('版本冲突') || errorMsg.includes('状态已被其他操作修改')) {
            message.error({
              content: '批量解冻失败：部分佣金或钱包数据已被其他操作修改，请稍后重试。如果问题持续，请联系管理员。',
              key: 'batch_unfreeze',
            });
          } else {
            message.error({
              content: `批量解冻失败：${errorMsg}`,
              key: 'batch_unfreeze',
            });
          }
        }
      },
    });
  } catch (error: any) {
    hideChecking();
    const errorMsg = error?.response?.data?.msg || error?.message || '数据检查失败，请重试';
    message.error({
      content: `检查失败：${errorMsg}`,
      key: 'check_frozen_data',
    });
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useCommissionFormSchema(),
    submitOnChange: true,
  },
  gridOptions: {
    columns: useCommissionColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    rowConfig: {
      keyField: 'id',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page, form, sort }: any, formValues: Record<string, any>) => {
          return await getAgentCommissionList({
            ...queryParams.value,
            ...formValues,
            page: page.currentPage,
            pageSize: page.pageSize,
          });
        },
      },
      props: {
        result: 'items',
        total: 'total',
      },
      autoLoad: true,
    },
  },
});
</script>

<template>
  <Page :auto-content-height="!agentId">
    <Grid :table-title="agentId ? '佣金记录列表' : '所有佣金记录'">
      <template #toolbar-tools>
        <div class="flex items-center">
          <Tooltip placement="top" title="开启后，佣金结算时将先冻结到钱包冻结余额，需要手动解冻才能使用；关闭后，佣金将直接结算到可用余额">
            <div class="flex items-center gap-2 mr-4">
              <span class="text-sm text-gray-600">安全防御机制:</span>
              <Switch
                v-model:checked="commissionSafeMode"
                :loading="safeModeLoading"
                checked-children="开启"
                un-checked-children="关闭"
                @change="onSafeModeChange"
              />
            </div>
          </Tooltip>
          <div class="w-px h-6 bg-gray-300 mx-2"></div>
          <div class="flex items-center gap-2">
            <span class="text-sm text-gray-600">选择代理商:</span>
            <Select
              v-model:value="unfreezeAgentId"
              placeholder="全部代理商 / 输入代理ID或手机号"
              :allow-clear="true"
              :loading="agentList.length === 0"
              style="width: 260px"
              show-search
              :filter-option="false"
              :show-arrow="true"
              :not-found-content="getNotFoundContent()"
              @search="onAgentSearch"
              @change="onAgentSelect"
            >
              <Select.Option
                v-for="agent in agentList"
                :key="agent.id"
                :value="agent.id"
                :label="`${agent.real_name || agent.mobile} (ID: ${agent.id})`"
              >
                {{ agent.real_name || agent.mobile }} (ID: {{ agent.id }})
              </Select.Option>
            </Select>
            <Button type="primary" @click="onBatchUnfreeze">
              <span class="mr-1">⚡</span>
              一键解冻
            </Button>
          </div>
        </div>
      </template>
    </Grid>
  </Page>
</template>
