import { enroll } from '../../api/other';
const app = getApp();
Page({
  data: {
    formData: {
      phone: '',
      captcha: '',
      nickname: '',
      gender: '',        // '女' 或 '男'
      race_number: '',   // 身份证号
      competition_no: '', // 比赛ID
      competition_image: '' // 上传的图片路径
    },
    genders: ['女', '男'],
    showPicker: false,
    pickerValue: '',
    fileList: [],
    checked: false,
    showRegistrationSuccess: false,
    canSubmit: false, // 按钮是否可点击
    showAgreementModal:false,
    agreementContent:''
  },

  onShowPicker() {
    this.setData({ showPicker: true });
  },
  onCancel() {
    this.setData({ showPicker: false });
  },
  onPickerChange(event) {
    const { value } = event.detail;
    this.setData({ pickerValue: value });
  },
  onConfirm() {
    this.setData({
      formData: {
        ...this.data.formData,
        gender: this.data.pickerValue
      },
      showPicker: false
    }, () => this.checkFormValid());
  },

  // 上传文件
  afterRead(event) {
    const { file } = event.detail;
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // 替换成真实接口
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success: (res) => {
        const { fileList } = this.data;
        const path = res.data; // 假设返回路径
        fileList.push({ ...file, url: path });
        this.setData({
          fileList,
          formData: { ...this.data.formData, competition_image: path }
        });
      }
    });
  },

  // 输入框数据绑定
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail;
    this.setData({
      formData: { ...this.data.formData, [field]: value }
    }, () => this.checkFormValid());
  },

  // 勾选协议
  onCheckbox(event) {
    this.setData({ checked: event.detail }, () => this.checkFormValid());
  },

  // 校验表单是否可提交
  checkFormValid() {
    const { phone, captcha, nickname, gender, race_number } = this.data.formData;
    const regPhone = /^1[3-9]\d{9}$/;
    const valid = regPhone.test(phone) && captcha && nickname && gender && race_number && this.data.checked;
    this.setData({ canSubmit: valid });
  },

  // 提交报名
  async onSubmit() {
    if (!this.data.canSubmit) return;
    const payload = {
      ...this.data.formData,
      gender: this.data.formData.gender === '女' ? 0 : 1
    };

    const res = await enroll(payload);
    if (res.code === 200) {
      wx.showToast({ title: res.data, icon: 'none', duration: 2000 });
      this.setData({ showRegistrationSuccess: true });
    } else {
      wx.showToast({ title: res.message || '报名失败', icon: 'none', duration: 2000 });
    }
  },
  // 点击协议文字显示弹窗
  showAgreement(e) {
    const type = e.currentTarget.dataset.type;
    const dataInfo = app.globalData.programConfig.marathon_event;
    this.setData({
      showAgreementModal: true,
      agreementContent: type == 'rules'?dataInfo.rules : type == 'liability'?dataInfo.disclaimer : dataInfo.privacy_policy 
    });
  },
  closeAgreementModal() {
    this.setData({ showAgreementModal: false });
  },

  onReceive() {
    this.setData({ showRegistrationSuccess: false });
    wx.navigateBack({ delta: 1 });
  }
});