-- 创建代理提现记录表
CREATE TABLE IF NOT EXISTS agent_withdrawals (
  id INT AUTO_INCREMENT PRIMARY KEY,
  agent_id INT NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  payment_type ENUM('bank', 'wechat', 'alipay', 'unionpay') DEFAULT 'bank' COMMENT '收款方式类型',
  bank_name VARCHAR(100) COMMENT '银行名称',
  account_number VARCHAR(50) COMMENT '账号/银行账号',
  account_holder VARCHAR(100) COMMENT '持有人姓名',
  qr_code_url VARCHAR(255) COMMENT '收款码图片URL',
  status ENUM('pending', 'approved', 'rejected', 'completed') DEFAULT 'pending',
  apply_note TEXT,
  admin_note TEXT,
  processed_by INT NULL,
  processed_at TIMESTAMP NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (agent_id) REFERENCES regional_agents(id) ON DELETE CASCADE,
  FOREIGN KEY (processed_by) REFERENCES users(id) ON DELETE SET NULL,
  -- 兼容旧字段
  bank_account VARCHAR(50) COMMENT '银行账号（兼容旧版本）'
);

-- 为regional_agents表添加提现相关字段
ALTER TABLE regional_agents ADD COLUMN withdrawn_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '已提现金额';
ALTER TABLE regional_agents ADD COLUMN pending_withdrawal DECIMAL(10,2) DEFAULT 0.00 COMMENT '待审核提现金额';
ALTER TABLE regional_agents ADD COLUMN payment_type ENUM('bank', 'wechat', 'alipay', 'unionpay') DEFAULT 'bank' COMMENT '收款方式类型';
ALTER TABLE regional_agents ADD COLUMN bank_name VARCHAR(100) COMMENT '银行名称';
ALTER TABLE regional_agents ADD COLUMN account_number VARCHAR(50) COMMENT '账号/银行账号';
ALTER TABLE regional_agents ADD COLUMN account_holder VARCHAR(100) COMMENT '持有人姓名';
ALTER TABLE regional_agents ADD COLUMN qr_code_url VARCHAR(255) COMMENT '收款码图片URL';

-- 兼容旧字段（可选，用于数据迁移）
ALTER TABLE regional_agents ADD COLUMN bank_account VARCHAR(50) COMMENT '银行账号（兼容旧版本）';