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

import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { auditWithdrawal } from '#/api/agent';
import type { AgentApi } from '#/api/agent';

interface ModalData {
  withdrawal: AgentApi.AgentWithdrawalListItem;
  onSuccess?: () => void;
}

const [Modal, modalApi] = useVbenModal({
  onConfirm: () => handleSubmit(),
});

const modalData = computed(() => modalApi.getData<ModalData>());
const withdrawal = computed(() => modalData.value?.withdrawal);

const [Form, formApi] = useVbenForm({
  schema: [
    {
      fieldName: 'status',
      label: '审核结果',
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '通过', value: 2 },
          { label: '拒绝', value: 3 },
        ],
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注信息（可选）',
        rows: 4,
        maxlength: 500,
        showCount: true,
      },
    },
  ],
  showDefaultActions: false,
});

const handleSubmit = async () => {
  if (!withdrawal.value) {
    return;
  }

  // 验证表单
  const { valid } = await formApi.validate();
  if (!valid) {
    message.error('请完善审核信息');
    return;
  }

  // 获取表单值
  const values = await formApi.getValues();
  if (!values) {
    return;
  }

  // 确保 status 字段存在且有效（2=通过，3=拒绝）
  if (values.status === undefined || values.status === null) {
    message.error('请选择审核结果');
    return;
  }

  if (values.status !== 2 && values.status !== 3) {
    message.error('审核结果无效，请选择"通过"或"拒绝"');
    return;
  }

  modalApi.lock(true);
  try {
    await auditWithdrawal({
      withdrawal_id: withdrawal.value.id,
      status: values.status,
      remark: values.remark || '',
    });
    message.success('审核成功');
    modalData.value?.onSuccess?.();
    modalApi.close();
  } catch (error: any) {
    message.error(error?.message || '审核失败');
  } finally {
    modalApi.lock(false);
  }
};
</script>

<template>
  <Modal class="w-[700px]">
    <div class="audit-modal">
      <!-- 提现信息卡片 -->
      <div v-if="withdrawal" class="withdrawal-info">
        <div class="info-header">
          <h3 class="info-title">
            <span class="title-icon">💰</span>
            提现信息
          </h3>
        </div>
        <div class="info-content">
          <div class="info-row">
            <div class="info-item">
              <span class="label">提现单号</span>
              <span class="value highlight">{{ withdrawal.withdraw_no }}</span>
            </div>
            <div class="info-item">
              <span class="label">代理ID</span>
              <span class="value">{{ withdrawal.agent_id }}</span>
            </div>
          </div>
          
          <div class="info-row">
            <div class="info-item">
              <span class="label">提现金额</span>
              <span class="value amount">¥{{ withdrawal.amount.toFixed(2) }}</span>
            </div>
            <div class="info-item">
              <span class="label">实际到账</span>
              <span class="value amount-success">¥{{ withdrawal.actual_amount?.toFixed(2) || '0.00' }}</span>
            </div>
          </div>
          
          <div class="info-row">
            <div class="info-item">
              <span class="label">税费</span>
              <span class="value tax">¥{{ withdrawal.tax_amount?.toFixed(2) || '0.00' }}</span>
            </div>
            <div class="info-item">
              <span class="label">提现方式</span>
              <span class="value">
                <span :class="['method-tag', withdrawal.withdrawal_type === 1 ? 'alipay' : 'bank']">
                  {{ withdrawal.withdrawal_type === 1 ? '支付宝' : '银行卡' }}
                </span>
              </span>
            </div>
          </div>
          
          <div class="info-row">
            <div class="info-item full-width">
              <span class="label">收款账户</span>
              <span class="value">{{ withdrawal.payee_account }}</span>
            </div>
          </div>
          
          <div class="info-row">
            <div class="info-item">
              <span class="label">收款人</span>
              <span class="value">{{ withdrawal.payee_name }}</span>
            </div>
            <div v-if="withdrawal.bank_card_no" class="info-item">
              <span class="label">银行卡号</span>
              <span class="value">{{ withdrawal.bank_card_no }}</span>
            </div>
          </div>
          
          <div v-if="withdrawal.bank_name" class="info-row">
            <div class="info-item full-width">
              <span class="label">开户行</span>
              <span class="value">{{ withdrawal.bank_name }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 审核表单 -->
      <div class="audit-form">
        <Form />
      </div>
    </div>
  </Modal>
</template>

<style lang="less" scoped>
.audit-modal {
  padding: 0;

  .withdrawal-info {
    margin-bottom: 24px;
    background: linear-gradient(135deg, #f5f7fa 0%, #ffffff 100%);
    border-radius: 8px;
    border: 1px solid #e8eaed;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);

    .info-header {
      padding: 16px 20px;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);

      .info-title {
        margin: 0;
        font-size: 18px;
        font-weight: 600;
        color: #ffffff;
        display: flex;
        align-items: center;
        gap: 8px;

        .title-icon {
          font-size: 20px;
        }
      }
    }

    .info-content {
      padding: 20px;
    }

    .info-row {
      display: flex;
      gap: 24px;
      margin-bottom: 16px;

      &:last-child {
        margin-bottom: 0;
      }
    }

    .info-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 6px;

      &.full-width {
        flex: 1 1 100%;
      }

      .label {
        font-size: 13px;
        color: #8c8c8c;
        font-weight: 500;
        letter-spacing: 0.3px;
      }

      .value {
        font-size: 15px;
        color: #262626;
        font-weight: 500;
        word-break: break-all;

        &.highlight {
          color: #1890ff;
          font-weight: 600;
          font-family: 'Monaco', 'Menlo', monospace;
        }

        &.amount {
          color: #fa8c16;
          font-size: 18px;
          font-weight: 700;
        }

        &.amount-success {
          color: #52c41a;
          font-size: 18px;
          font-weight: 700;
        }

        &.tax {
          color: #ff4d4f;
          font-weight: 600;
        }

        .method-tag {
          display: inline-block;
          padding: 4px 12px;
          border-radius: 12px;
          font-size: 13px;
          font-weight: 500;

          &.alipay {
            background: linear-gradient(135deg, #1677ff 0%, #0958d9 100%);
            color: #ffffff;
          }

          &.bank {
            background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
            color: #ffffff;
          }
        }
      }
    }
  }

  .audit-form {
    padding: 0 4px;

    :deep(.vben-form) {
      .ant-form-item-label {
        label {
          font-weight: 500;
          color: #262626;
        }
      }

      .ant-radio-group {
        .ant-radio-button-wrapper {
          padding: 8px 24px;
          height: auto;
          font-weight: 500;
          transition: all 0.3s;

          &:first-child {
            border-radius: 6px 0 0 6px;
          }

          &:last-child {
            border-radius: 0 6px 6px 0;
          }

          &.ant-radio-button-wrapper-checked {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-color: #667eea;
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
          }
        }
      }

      .ant-input {
        border-radius: 6px;
        transition: all 0.3s;

        &:focus,
        &:hover {
          border-color: #667eea;
          box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
        }
      }

      textarea.ant-input {
        resize: vertical;
        min-height: 100px;
        border-radius: 6px;
        padding: 12px;
        font-size: 14px;
        line-height: 1.6;
        transition: all 0.3s;

        &:focus {
          border-color: #667eea;
          box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
        }
      }
    }
  }
}
</style>
