<script lang="ts" setup>
import type { AgentApi } from '#/api/agent';

import { onMounted, reactive, ref } from 'vue';

import { Page } from '@vben/common-ui';

import { Button, Card, Col, Form, InputNumber, Row, Space, message } from 'ant-design-vue';

import { getAgentConfig, updateAgentConfig } from '#/api/agent';

const loading = ref(false);
const config = ref<AgentApi.AgentConfig | null>(null);

// 使用 reactive 管理表单数据（价格配置已移除，改为产品配置表管理）
const formData = reactive<AgentApi.AgentConfig>({
  level_bonus: {
    normal: 0,
    gold: 0,
    diamond: 0,
  },
  upgrade_fee: {
    normal_to_gold: 0,
    normal_to_diamond: 0,
    gold_to_diamond: 0,
  },
  upgrade_rebate: {
    normal_to_gold_rebate: 0,
    to_diamond_rebate: 0,
  },
  direct_parent_rebate: {
    diamond: 0,
    gold: 0,
    normal: 0,
  },
  max_gold_rebate_amount: 0,
  commission_freeze: {
    ratio: 0,
    threshold: 0,
    days: 0,
  },
  tax_rate: 0,
  tax_exemption_amount: 0,
  gold_max_uplift_amount: 0,
  diamond_max_uplift_amount: 0,
});

// 加载配置
async function loadConfig() {
  loading.value = true;
  try {
    const res = await getAgentConfig();
    config.value = res;
    Object.assign(formData, res);
  } catch (error) {
    console.error('加载配置失败:', error);
  } finally {
    loading.value = false;
  }
}

// 保存配置
async function handleSave() {
  try {
    const params: AgentApi.UpdateAgentConfigParams = {
      level_bonus: {
        normal: formData.level_bonus.normal,
        gold: formData.level_bonus.gold,
        diamond: formData.level_bonus.diamond,
      },
      upgrade_fee: {
        normal_to_gold: formData.upgrade_fee.normal_to_gold,
        normal_to_diamond: formData.upgrade_fee.normal_to_diamond,
      },
      upgrade_rebate: {
        normal_to_gold_rebate: formData.upgrade_rebate.normal_to_gold_rebate,
        to_diamond_rebate: formData.upgrade_rebate.to_diamond_rebate,
      },
      direct_parent_rebate: {
        diamond: formData.direct_parent_rebate.diamond,
        gold: formData.direct_parent_rebate.gold,
        normal: formData.direct_parent_rebate.normal,
      },
      max_gold_rebate_amount: formData.max_gold_rebate_amount,
      commission_freeze: {
        ratio: formData.commission_freeze.ratio,
        threshold: formData.commission_freeze.threshold,
        days: formData.commission_freeze.days,
      },
      tax_rate: formData.tax_rate,
      tax_exemption_amount: formData.tax_exemption_amount,
      gold_max_uplift_amount: formData.gold_max_uplift_amount,
      diamond_max_uplift_amount: formData.diamond_max_uplift_amount,
    };
    await updateAgentConfig(params);
    message.success('配置保存成功');
    loadConfig();
  } catch (error) {
    console.error('保存配置失败:', error);
  }
}

// 重置配置
function handleReset() {
  if (config.value) {
    Object.assign(formData, config.value);
  }
}

onMounted(() => {
  loadConfig();
});
</script>

<template>
  <Page auto-content-height>
    <Card title="系统配置" :loading="loading">
      <Form layout="vertical">
        <Card title="等级奖金" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['level_bonus', 'normal']" label="普通代理奖金">
              <InputNumber v-model:value="formData.level_bonus.normal" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['level_bonus', 'gold']" label="黄金代理奖金">
              <InputNumber v-model:value="formData.level_bonus.gold" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['level_bonus', 'diamond']" label="钻石代理奖金">
              <InputNumber v-model:value="formData.level_bonus.diamond" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="等级最高价上调金额" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item name="gold_max_uplift_amount" label="黄金代理最高价上调金额">
              <InputNumber v-model:value="formData.gold_max_uplift_amount" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item name="diamond_max_uplift_amount" label="钻石代理最高价上调金额">
              <InputNumber v-model:value="formData.diamond_max_uplift_amount" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="升级费用" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['upgrade_fee', 'normal_to_gold']" label="普通→黄金">
              <InputNumber v-model:value="formData.upgrade_fee.normal_to_gold" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['upgrade_fee', 'normal_to_diamond']" label="普通→钻石">
              <InputNumber v-model:value="formData.upgrade_fee.normal_to_diamond" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="升级返佣" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item :name="['upgrade_rebate', 'normal_to_gold_rebate']" label="普通→黄金返佣">
              <InputNumber v-model:value="formData.upgrade_rebate.normal_to_gold_rebate" :min="0" :precision="2"
                :step="0.01" style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item :name="['upgrade_rebate', 'to_diamond_rebate']" label="→钻石返佣">
              <InputNumber v-model:value="formData.upgrade_rebate.to_diamond_rebate" :min="0" :precision="2"
                :step="0.01" style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="直接上级返佣配置" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['direct_parent_rebate', 'diamond']" label="直接上级是钻石的返佣金额">
              <InputNumber v-model:value="formData.direct_parent_rebate.diamond" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['direct_parent_rebate', 'gold']" label="直接上级是黄金的返佣金额">
              <InputNumber v-model:value="formData.direct_parent_rebate.gold" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['direct_parent_rebate', 'normal']" label="直接上级是普通的返佣金额">
              <InputNumber v-model:value="formData.direct_parent_rebate.normal" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="返佣限额配置" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item name="max_gold_rebate_amount" label="黄金代理最大返佣金额">
              <InputNumber v-model:value="formData.max_gold_rebate_amount" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="佣金冻结配置" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['commission_freeze', 'ratio']" label="佣金冻结比例（例如：0.1表示10%）">
              <InputNumber v-model:value="formData.commission_freeze.ratio" :min="0" :max="1" :precision="4"
                :step="0.0001" style="width: 100%" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['commission_freeze', 'threshold']" label="佣金冻结阈值">
              <InputNumber v-model:value="formData.commission_freeze.threshold" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <Form.Item :name="['commission_freeze', 'days']" label="佣金冻结解冻天数">
              <InputNumber v-model:value="formData.commission_freeze.days" :min="0" :precision="0" :step="1"
                style="width: 100%" addon-after="天" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Card title="税费配置" size="small" class="mb-4">
          <Row :gutter="[16, 16]">
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item name="tax_rate" label="税率（例如：0.06表示6%）">
              <InputNumber v-model:value="formData.tax_rate" :min="0" :max="1" :precision="4" :step="0.0001"
                style="width: 100%" />
            </Form.Item>
            </Col>
            <Col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <Form.Item name="tax_exemption_amount" label="免税额度">
              <InputNumber v-model:value="formData.tax_exemption_amount" :min="0" :precision="2" :step="0.01"
                style="width: 100%" addon-after="元" />
            </Form.Item>
            </Col>
          </Row>
        </Card>

        <Space>
          <Button type="primary" @click="handleSave">保存</Button>
          <Button @click="handleReset">重置</Button>
        </Space>
      </Form>
    </Card>
  </Page>
</template>
