<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="mobile-web-app-capable" content="yes">
  <meta name="msapplication-tap-highlight" content="no">
  <meta name="fragment" content="!">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, viewport-fit=cover">
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Expires" content="0" />
  <meta http-equiv="Cache" content="no-cache">
  <meta name="description" content="">

  <link rel="icon" href="../favicon.ico">
  <title>乐团启蒙调查</title>
  <!-- 引入样式文件 -->
  <link rel="stylesheet" href="./js/vant-index.css" />
  <link rel="stylesheet" href="./css/initiation.css">
  <script src="./js/flexible.js"></script>
</head>

<body>
  <style>
    .m-toast {
      position: fixed;
      top: 50%;
      left: 50%;
      display: -webkit-box;
      display: -webkit-flex;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -webkit-flex-direction: column;
      flex-direction: column;
      -webkit-box-align: center;
      -webkit-align-items: center;
      align-items: center;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
      justify-content: center;
      box-sizing: content-box;
      width: 88px;
      max-width: 70%;
      min-height: 88px;
      padding: 16px;
      color: #fff;
      font-size: 14px;
      line-height: 20px;
      white-space: pre-wrap;
      text-align: center;
      word-wrap: break-word;
      background-color: rgba(50, 50, 51, 0.88);
      border-radius: 4px;
      -webkit-transform: translate3d(-50%, -50%, 0);
      transform: translate3d(-50%, -50%, 0);
      z-index: 2002
    }

    .m-loading {
      position: relative;
      color: #fff;
      font-size: 0;
      vertical-align: middle;
      padding: 4px
    }

    .m-loading__spinner {
      position: relative;
      display: inline-block;
      width: 30px;
      max-width: 100%;
      height: 30px;
      max-height: 100%;
      vertical-align: middle;
      -webkit-animation: m-rotate .8s linear infinite;
      animation: m-rotate .8s linear infinite
    }

    .m-loading__spinner--circular {
      -webkit-animation-duration: 2s;
      animation-duration: 2s
    }

    .m-loading__circular {
      display: block;
      width: 100%;
      height: 100%
    }

    .m-loading__circular circle {
      -webkit-animation: m-circular 1.5s ease-in-out infinite;
      animation: m-circular 1.5s ease-in-out infinite;
      stroke: currentColor;
      stroke-width: 3;
      stroke-linecap: round
    }

    .m-loading__text {
      display: inline-block;
      margin-left: 8px;
      color: #969799;
      font-size: 14px;
      vertical-align: middle
    }

    .m-loading--vertical {
      display: -webkit-box;
      display: -webkit-flex;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -webkit-flex-direction: column;
      flex-direction: column;
      -webkit-box-align: center;
      -webkit-align-items: center;
      align-items: center
    }

    .m-loading--vertical .m-loading__text {
      margin: 8px 0 0
    }

    @-webkit-keyframes m-circular {
      0% {
        stroke-dasharray: 1, 200;
        stroke-dashoffset: 0
      }

      50% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -40
      }

      100% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -120
      }
    }

    @keyframes m-circular {
      0% {
        stroke-dasharray: 1, 200;
        stroke-dashoffset: 0
      }

      50% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -40
      }

      100% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -120
      }
    }

    .m-toast__text {
      margin-top: 8px
    }

    .van-field--error .van-field__control,
    .van-field--error .van-field__control::placeholder {
      color: #c8c9cc;
      -webkit-text-fill-color: currentColor;
    }
  </style>
  <div id="m_loading" class="m-toast m-toast--middle m-toast--loading" style="z-index: 2001;">
    <div class="m-loading m-loading--circular m-toast__loading"><span
        class="m-loading__spinner m-loading__spinner--circular"><svg viewBox="25 25 50 50" class="m-loading__circular">
          <circle cx="50" cy="50" r="20" fill="none"></circle>
        </svg></span></div>
    <div class="m-toast__text">加载中...</div>
  </div>
  <div id="app">
    <!-- <div class="banner">
      <img src="./images/initiation/banner.png" alt="">
    </div> -->
    <div class="title" v-clock v-html="(orchestraName || '') + '启蒙登记表'">
    </div>
    <van-form validate-first scroll-to-error @submit="onSubmit" ref='form' class="form">
      <div class="top-tips">科学的教育与关爱,足以改变世界。我们希望,学员的未来会因您和我们的共同努力而更加光辉灿烂!</div>
      <van-cell-group inset class="cell-group">
        <van-field label="学员姓名" :rules="[{ validator, message }]" name="username" v-model="stu.username"
          placeholder="请填写学员真实姓名" autocomplete="off"></van-field>
        <!-- <van-field label="性别" name="sex" :rules="[{ required: true, message: '请选择性别' }]">
          <template #input>
            <van-radio-group v-model="stu.sex" checked-color="#FF8057" direction="horizontal">
              <van-tag size="large" type="primary" :color="!(stu.sex === 1) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.sex === 1) ? '#AAA' : '#FFF'" class="radioSection" round>
                <van-radio class="radioItem" :name="1"></van-radio>男生
              </van-tag>
              <van-tag size="large" type="primary" :color="!(stu.sex === 0) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.sex === 0) ? '#AAA' : '#FFF'" class="radioSection" round>
                <van-radio class="radioItem" :name="0"></van-radio>女生
              </van-tag>
            </van-radio-group>
          </template>
        </van-field> -->
        <van-field label="在读年级" v-model="stu.currentGrade" readonly name="currentGrade"
          :rules="[{ required: true, message: '请选择在读年级', trigger: 'onChange' }]" @click="() => showPicker = true"
          placeholder="请选择在读年级">
          <template #right-icon>
            <van-icon name="arrow" :color="'#323233'" size="16"></van-icon>
          </template>
        </van-field>
        <van-field v-show="false" v-model="stu.currentGradeNum" name="currentGradeNum" readonly></van-field>
        <van-field label="所在班级" v-model="stu.currentClass" readonly name="currentClass"
          :rules="[{ required: true, message: '请选择所在班级', trigger: 'onChange' }]" @click="() => classPicker = true"
          placeholder="请选择所在班级">
          <template #right-icon>
            <van-icon name="arrow" :color="'#323233'" size="16"></van-icon>
          </template>
        </van-field>

        <van-field label="您是否有意向让孩子加入学校乐团?" name="musicalInstrumentsLearning"
          :rules="[{ required: true, message: '请选择您的意向' }]">
          <template #input>
            <van-radio-group v-model="stu.joinOrchestra" checked-color="#FF8057" direction="horizontal">
              <van-tag size="large" type="primary" :color="!(stu.joinOrchestra === 1) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.joinOrchestra === 1) ? '#AAA' : '#FFF'" class="radioSection" round>
                <van-radio class="radioItem" :name="1"></van-radio>有意向
              </van-tag>
              <van-tag size="large" type="primary" :color="!(stu.joinOrchestra === 0) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.joinOrchestra === 0) ? '#AAA' : '#FFF'" class="radioSection"
                @click="() => stu.learningSubjectName = ''" round>
                <van-radio class="radioItem" :name="0"></van-radio>无意向
              </van-tag>
            </van-radio-group>
          </template>
        </van-field>
        <van-field label="孩子是否有学习过乐器" v-if="stu.joinOrchestra==1" name="learningSubjectName"
          v-model="stu.learningSubjectName" placeholder="(非必填)请输入所学乐器"></van-field>
        <van-field label="您是否有意向参与线上家长会了解相关情况" name="joinParentMeeting" v-if="stu.joinOrchestra==1"
          :rules="[{ required: true, message: '请选择您是否有意向参与线上家长会了解相关情况' }]">
          <template #input>
            <van-radio-group v-model="stu.joinParentMeeting" checked-color="#FF8057" direction="horizontal">
              <van-tag size="large" type="primary" :color="!(stu.joinParentMeeting === 1) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.joinParentMeeting === 1) ? '#AAA' : '#FFF'" class="radioSection" round>
                <van-radio class="radioItem" :name="1"></van-radio>有意向
              </van-tag>
              <van-tag size="large" type="primary" :color="!(stu.joinParentMeeting === 0) ? '#EAEAEA' : '#FF8057'"
                :text-color="!(stu.joinParentMeeting === 0) ? '#AAA' : '#FFF'" class="radioSection" round>
                <van-radio class="radioItem" :name="0"></van-radio>无意向
              </van-tag>
            </van-radio-group>
          </template>
        </van-field>
        <!-- <van-field :border="true" label="您对校管乐团的组建有什么建议" type="textarea" rows="3" name="personalSuggestion"
          v-model="stu.personalSuggestion" placeholder="(非必填)请填写您的建议内容">
        </van-field> -->
        <van-field type="tel" v-if="stu.joinOrchestra==1" maxlength="11" minlength="11" label="联系电话(直接监护人)"
          v-model="stu.phone" name="phone" :rules="[{ pattern, message: '输入监护人手机号码有误' }]" placeholder="请输入监护人手机号码">
        </van-field>
      </van-cell-group>

      <van-button size="large" block round class="btn-submit" color="#FF8057" :loading="btnLoading"
        native-type="submit">提交</van-button>
    </van-form>

    <van-popup v-model:show="showPicker" position="bottom" round>
      <van-picker show-toolbar :columns="currentGrade" @cancel="showPicker = false" @confirm="onConfirm" />
    </van-popup>

    <van-popup v-model:show="classPicker" position="bottom" round>
      <van-picker show-toolbar :columns="classList" @cancel="classPicker = false" @confirm="onConfirmClass" />
    </van-popup>

    <van-popup v-model:show="submitStatus" round
      style="width: 100%;background-color: transparent;transform: translateY(-60%);" :close-on-click-overlay="false">
      <div class="submit-container">
        <p class="submit-title">提交成功</p>
        <p class="submit-tips">您已完成乐团启蒙调查</p>
        <div @click="onLinkUrl" class="submit-btn"></div>
      </div>
    </van-popup>

    <van-popup v-model:show="showPopup" round style="width: 88%" :close-on-click-overlay="false" class="wxPopupDialog">
      <div class="popupContainer">
        <p class="title1">温馨提示</p>
        <p class="popupTips">请使用微信打开</p>
      </div>
    </van-popup>
  </div>

  <!-- 引入 Vue 和 Vant 的 JS 文件 -->
  <script src="./js/vue.global.min.js"></script>
  <script src="./js/vant.min.js"></script>
  <script src="./js/axios.js"></script>
  <script type="text/javascript" src="./js/utils.js"></script>
  <script>
    // 乐团交付,乐团停止或关闭,有新的交付团;则不允许报名
    var classList = []
    for (var i = 1; i <= 40; i++) {
      classList.push({ text: i + '班', value: i })
    }
    var app = Vue.createApp({
      data() {
        return {
          orchestraId: getQueryVariable('orchestraId'),
          orchestraName: null,
          schoolId: null,
          showPicker: false,
          classPicker: false,
          submitStatus: false,
          pattern: /^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\d{8}$/,
          nameReg: /^[\u4E00-\u9FA5]+$/,
          currentGrade: [
            { text: '一年级', value: 1 },
            { text: '二年级', value: 2 },
            { text: '三年级', value: 3 },
            { text: '四年级', value: 4 },
            { text: '五年级', value: 5 },
            { text: '六年级', value: 6 },
            { text: '七年级', value: 7 },
            { text: '八年级', value: 8 },
            { text: '九年级', value: 9 }
          ], // 年级数组列表
          classList: classList,
          columns: [],
          pickerType: null, // 下拉类型
          stu: {
            username: null, // 姓名
            // sex: 1, // 性别
            phone: null, // 电话
            currentGrade: '', // 年级
            currentGradeNum: null, // 年级编号
            currentClass: '', // 班级
            currentClassNum: null, // 年级编号
            learningSubjectName: null,
            joinOrchestra: null, //
            personalSuggestion: null, // 个人建议
            joinParentMeeting: null
          },
          btnLoading: false,
          checkPhone: true,
          showPopup: false,
          code: '',
          canSignUp: '',
        }
      },
      async mounted() {
        if (document.querySelector('#m_loading')) {
          document.querySelector('#m_loading').remove()
        }

        // 重置数据
        this.stu = {
          username: null, // 姓名
          // sex: 1, // 性别
          phone: null, // 电话
          currentGrade: '', // 年级
          currentGradeNum: null, // 年级编号
          currentClass: '', // 班级
          currentClassNum: null, // 年级编号
          learningSubjectName: null,
          joinOrchestra: null, //
          personalSuggestion: null, // 个人建议
          joinParentMeeting: null
        }

        // 判断是否是微信,只能微信中打开
        if (!browser().weixin) {
          this.showPopup = true
          return
        } else {
          //授权
          const code = getUrlCode()
          console.log(code)
          if (!code) {
            this.getAppIdAndCode(window.location.href)
            return
          } else {
            this.code = code
          }
        }

        if (!this.orchestraId) {
          vant.showToast('信息获取失败,请联系老师')
        }
        try {
          this.setLoading(true)
          var schoolDetail = await axios.post('/api-student/open/school/detail', {
            orchestraId: this.orchestraId
          })
          if (schoolDetail.data.code === 200 && schoolDetail.data.data) {
            var schoolSystem = schoolDetail.data.data.schoolSystem || 'sixYearSystem'
            this.schoolId = schoolDetail.data.data.id
            // if (schoolSystem === 'sixYearSystem') {
            //   this.currentGrade.push({ text: '六年级', value: 6 }, { text: '初一', value: 7 }, { text: '初二', value: 8 }, { text: '初三', value: 9 })
            // } else {
            //   this.currentGrade.push({ text: '初一', value: 6 }, { text: '初二', value: 7 }, { text: '初三', value: 8 }, { text: '初四', value: 9 })
            // }
          }


          var orchestraDetail = await axios.get('/api-student/open/orchestra/detail/' + this.orchestraId)
          if (orchestraDetail.data.code === 200 && orchestraDetail.data.data) {
            this.orchestraName = orchestraDetail.data.data.name || ''
            this.canSignUp = orchestraDetail.data.data.status
            if (orchestraDetail.data.data.status !== 'INITIATION_SURVEY') {
              vant.showToast('乐团启蒙阶段已结束')
              return
            }
          }
        } catch {
          vant.showDialog({
            message: '信息获取失败,请联系老师',
            theme: 'round-button',
            confirmButtonColor: '#FF8057'
          })
        }
        this.setLoading(false)
      },
      methods: {
        async getAppIdAndCode(url) {
          try {
            var res = await axios.get('/api-school/open/paramConfig/wechatAppId')
            // 判断是否有微信appId
            if (res.data.code === 200 && res.data.data) {
              vant.closeToast()
              if (/online.lexiaoya/.test(location.href)) {
                this.goAuth(res.data.data, url)
              } else {
                this.goTestAuth(res.data.data, url)
              }
            }
          } catch {
            //
          }
        },
        goTestAuth(wxAppId, urlString) {
          const replaceUrl =
            `https://online.lexiaoya.cn/getWxCode?appid=${wxAppId || 'wx8654c671631cfade'}&state=STATE&redirect_uri=` +
            encodeURIComponent(urlString || window.location.href)
          window.location.replace(replaceUrl)
        },
        goAuth(wxAppId, urlString) {
          // 用户授权
          console.log(urlString || window.location.href, 'urlString || window.location.href')
          const urlNow = encodeURIComponent(urlString || window.location.href)
          console.log(urlNow, 'urlNow')
          const scope = 'snsapi_base' //snsapi_userinfo   //静默授权 用户无感知
          const appid = wxAppId || 'wx8654c671631cfade'
          const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
          window.location.replace(url)
        },
        onLinkUrl() {
          this.submitStatus = false
          window.location.href = window.location.origin + '/orchestra-student/#/download'
        },
        // async checkchangePhone(val) {
        //   try {
        //     await this.$refs.form.validate('phone')
        //     this.checkPhone = false
        //     //  查询老数据
        //     let tempValues = {}
        //     this.setLoading(true)
        //     tempValues.orchestraId = this.orchestraId
        //     tempValues.phone = this.stu.phone

        //     var user = await axios.get('/api-student/open/orchestraInitiationSurvey/detailByPhone', {
        //       params: {
        //         ...tempValues
        //       }
        //     })
        //     console.log(user.data.code)
        //     if (user.data.code === 200 && user.data.data) {
        //       var detail = user.data.data
        //       console.log(detail, 'detail')

        //       var grade = this.currentGrade.find(item => item.value == detail.currentGrade)
        //       var cls = this.classList.find(item => item.value == detail.currentClass)
        //       this.stu = {
        //         username: detail.username, // 姓名
        //         sex: detail.sex ? 1 : 0, // 性别
        //         phone: detail.phone, // 电话
        //         currentGrade: grade.text, // 年级
        //         currentGradeNum: detail.currentGrade, // 年级编号
        //         currentClass: cls.text, // 班级
        //         currentClassNum: detail.currentClass, // 年级编号
        //         learningSubjectName: detail.learningSubjectName,
        //         joinOrchestra: detail.joinOrchestra ? 1 : 0, // 是否有学习经验
        //         personalSuggestion: detail.personalSuggestion, // 个人建议
        //         joinParentMeeting: detail.joinParentMeeting ? 1 : 0
        //       }
        //     }
        //   } catch { }
        //   this.setLoading(false)
        // },
        validator(val) {
          // 校验函数返回 true 表示校验通过,false 表示不通过
          return this.nameReg.test(val) && (val.length >= 2 && val.length <= 15);
        },
        message(value) {
          if (!value) {
            return '请填写学员真实姓名'
          } else if (!this.nameReg.test(value)) {
            return '学员姓名必须为中文'
          } else if (value.length < 2 || value.length > 15) {
            return '学员姓名必须为2~15个字'
          }
        },
        async onSubmit() {
          if (this.canSignUp !== 'INITIATION_SURVEY') {
            vant.showToast('乐团启蒙阶段已结束')
            return
          }
          this.btnLoading = true
          try {
            var stu = this.stu
            // 无意向
            if (stu.joinOrchestra == 0) {
              stu.learningSubjectName = null
              stu.joinParentMeeting = null
              stu.phone = null
            }
            var res = await axios.post('/api-student/open/orchestraInitiationSurvey/save', {
              orchestraId: this.orchestraId,
              schoolId: this.schoolId,
              ...stu,
              currentClass: stu.currentClassNum,
              currentGrade: stu.currentGradeNum,
              code: this.code,
            })
            console.log(res)
            if (res.data.code === 200) {
              this.submitStatus = true
              // window.location.href =
              //   'https://mp.weixin.qq.com/s?__biz=MzkxMDMwOTI5Nw==&mid=2247485261&idx=1&sn=70c79a832a609bf9fae01c9e90fb4f69&chksm=c12c2593f65bac85d26362bca470f6abc2bfc087d9f4dcf87c00094420bdf5a3acb1b870199b#rd'
            } else {
              vant.showToast(res.data.message)
            }
          } catch {
            vant.showToast('保存失败,请重试')
          }
          this.btnLoading = false
        },
        setLoading(status) {
          if (status) {
            vant.showLoadingToast({
              duration: 0, // 持续展示 toast
              message: '加载中...',
              forbidClick: true,
              loadingType: 'spinner',
            })
          } else {
            vant.closeToast()
          }
        },
        onConfirm(options) {
          var stu = this.stu
          stu.currentGrade = options.selectedOptions[0].text
          stu.currentGradeNum = options.selectedOptions[0].value
          this.showPicker = false
        },
        onConfirmClass(options) {
          var stu = this.stu
          stu.currentClass = options.selectedOptions[0].text
          stu.currentClassNum = options.selectedOptions[0].value
          this.classPicker = false
        }
      }
    })
    app.use(vant)
    app.mount('#app')
  </script>
</body>

</html>