<template>
  <div class="el-classreg_show_reging_mobile_1">
    <div class="classreg_show_reging_mobile_1">
   <div class="class-reging-info">
      <span class="sp1">{{infos.czName}}</span>
      <ul class="ul1">
         <li>培训时间：
           <span v-if="infos.czStartDate">{{infos.czStartDate | $toYMD}}-</span>
           <span>{{infos.czEndDate | $toYMD}}</span>
         </li>
         <li>培训地点：<span>{{infos.czAdress}}</span></li>
         <li>培训费用：
          <span v-if="Number(infos.czTrainingFee)" class="ul1-red">￥{{infos.czTrainingFee}}元</span>
           <span v-else class="ul1-red">免费</span>
         </li>
      </ul>
      <span class="sp2"><i>*</i>提示：请核对班级信息及报名费用，并确认无误。</span>
   </div>
   <div class="class-reging-form">
         <span class="c-sp1">


               <fyer-upload-imgs
                        multiple
                        compress
                        :limit="limit"
                        :before-read="beforeRead"
                        :before-remove="beforeRemove"
                        @exceed="exceed"
                        @readFile="onReadFile"
                        v-model="files"
                    >
                    </fyer-upload-imgs>


            <span class="c-spp2">上传个人头像</span>
         </span>
         <ul class="c-ul1">
            <li>
               <input type="text" v-model="userInfos.custName" placeholder="姓名" class="inp1">
            </li>
            <li>
               <input type="text"  v-model="userInfos.idcarNo" placeholder="身份证号" class="inp1">
            </li>
            <li>
               <input type="text" v-model="userInfos.phone" placeholder="手机号码" class="inp2">
               <input type="button"  @click="getCode($event)" value="获取验证码" class="btn1">
            </li>
            <li>
               <input type="text" v-model="code" placeholder="输入手机验证码" class="inp1">
            </li>
         </ul>
         <span class="c-sp2"><i>*</i>提示：请核对班级信息及报名费用，并确认无误。</span>
         <span class="twn-btns">
            <a href="javascript:void(0)"  @click="goPay" class="a1">确认并报名</a>
            <a href="javascript:void(0)" @click="cancal" class="a2">取消报名</a>
         </span>
   </div>
</div>


  </div>
</template>

<script>
   import formMixin from "../../../src/mixins/formMixin";
  import lenye_base from "lenye_base";
  import $ from "fy-kit";
   import msg from '../../message';
   import { fyerUploadImgs } from "fyer-upload-imgs";
export default {
  name: 'ElClassregShowRegingMobile1',
  mixins: [formMixin],
  model: {},
  data(){
    return {
      czId:'',
      userData:null,
      userInfos:{},
      type:'',
      id:'',
      actId:'',
      siteId:'',
      custId:'',
      infos:{},
      time:60,
      timer:null,
     ele:null,
     code:'',
     files: [

      ],
      limit:1,
      uploadurl:'',
      maxSize: 1024 * 10, // 10 KB




    }
   },
   components: {
    fyerUploadImgs
  },
  created() {
    this.czId = $.util.urlParams("id");
     this.userData = $.util.getCookie('userData');
     this.custId = lenye_base.user_data.getuserData().custId;
     this.siteId = lenye_base.site_data.getsiteData().siteId;
     this.type = $.util.urlParams("type");
     this.actId = $.util.urlParams("id");
     this.id =  $.util.urlParams("id");
     this.uploadurl = lenye_base.base_data.getapiUrl() + "/file-service/upload/pic";
     console.log( this.uploadurl,123)
  },
   mounted() {
    //this.init();
   },
    filters:{
    $toYMD(val){
      if(val){
        return $.date.moment(val).format("YYYY/MM/DD");
      }
    }
  },
   computed: {},
   methods: {
    exceed() {
      msg.warning(`只能上传${this.limit}张图片`);
    },
    closePreview() {
      this.isPreview = false;
    },
    beforeRead(files) {
      if(!this.userData){
        msg.warning("请登录后操作！");
        return false;
      }
      var res = $.img.imgType(files,['jpg','png']);
      if(res){
        return res;
      }else{
        msg.warning("只能上传jpg和png格式的图片");
      }

    },
    beforeRemove(index, file) {
      this.infos.wksPhoto = ""
      return true;
    },
    onReadFile(file){
      this.uploadFile(file[0])
    },

    uploadFile(userfile){
      $.img.upload(userfile,this.uploadurl).then(res=>{
          if(res.success=='1'){
             msg.success("头像上传成功！");
             this.userInfos.photo = res.data.fileUrl;

          }else{
             msg.warning(res.message);
          }
      })
    },

      orderDetail(){
        $.util.ajax(
          lenye_base.base_data.getapiUrl() +
            "/schoolclass-service/common/clzClazz/queryById.do",
          lenye_base.base_data.getformatting({
            czId: this.czId,

          })
        ).then(res=>{
          if(res.success=='1'){
            this.infos = res.data;
          }else{
            msg.warning(res.message);
          }
        })
     },
     init(){
        this.detail();
        this.orderDetail();
         if(this.type=='4'){  //活动
          this.activeInfo();
        }else if(this.type=='0'){  //class
          this.classInfo();
        }else if(this.type=='1'){ //course
          this.courseInfo();
        }else if(this.type=='5'){ //li
          this.liveInfo();
        }
     },
     cancal(){
       window.history.go(-1)
     },
      //active signup
     subSignAct(){
       $.util
          .ajax(
            lenye_base.base_data.getapiUrl() +
            '/activity-service/api2/actActivityMemer/signUp.do',
            lenye_base.base_data.getformatting({
              actId:this.actId
            })
          ).then(res=>{
            if(res.success=='1'){
              if(res.data.status=='0'){
                msg.success('活动报名成功！')
                setTimeout(()=>{
                  window.history.back()
                },2000)
              }else{
                msg.warning('该活动已报名,请勿重复添加！');
              }
            }else{
              msg.warning(res.message);
            }
          })
     },
     //class gg
     async getSpcId(){
       let {data} = await $.util
          .ajax(
            lenye_base.base_data.getapiUrl() +
            '/schoolclass-service/common/clzSpecification/queryPage.do',
            lenye_base.base_data.getformatting({
              czId:this.id
            })
          )
        return data[0].czSpcId
     },
     //course gg
      async getSpcIdCourse(){
      let {data} = await $.util
          .ajax(
            lenye_base.base_data.getapiUrl() +
            '/course-service/common/crsSpecification/queryPage.do',
            lenye_base.base_data.getformatting({
              csId:this.id
            })
          )
        return data[0].csSpcId
     },
     //class signup
      async subSign(objType) {
        var spcId =  ''
        switch(Number(objType)){
          case 0:
            spcId =  await this.getSpcId();
            break;
          case 1:
            spcId =  await this.getSpcIdCourse();
            break;
          case 5:
            spcId =  this.id;
            break;
        }


       let res =  await  $.util
          .ajax(
            lenye_base.base_data.getapiUrl() +
            '/orderform-service/api2/ordShoppingCart/signup.do',
            lenye_base.base_data.getformatting({
              objId:this.id,
              objType,
              shoppingNum:1,
              spcId,
              custId:this.custId,
              siteId:this.siteId

            }))
            if (res.success == '1') {
                var status = res.data.status;
                if(status=='1'){  //待支付
                  this.orderId = res.data.orderId;
                  //todo ...
                  window.location.href="/classreging?orderId="+res.data.orderId+'&id='+res.data.orderId+'&type=0';
                }else if(status=='2'){
                  msg.warning("已有待付订单！");
                  setTimeout(()=>{
                    //todo ...
                    window.location.href="/personcenter";//待付订单列表
                  },1000);
                }else if(status=='3'){
                  msg.success("报名成功！");
                }else{
                  msg.warning('报名失败！');
                }
            } else {
              msg.warning(res.message);
            }

      },
      async checkCode(DYN_CODE){
        //common/dynCodeCheck.do
       let res =   await  $.util
            .ajax(
              lenye_base.base_data.getapiUrl() +
              "/base-service/common/dynCodeCheck.do",
              lenye_base.base_data.getformatting({
                DYN_CODE
              })
            )
        if(res.success=='1'){
          return true;
        }else{
           // msg.warning(res.message);
          return false;
        }
      },
     getCode(ev) {


        if ($.validation.isPhone(this.userInfos.phone)) {
          var ele = $._.default(ev.target);
          this.ele = ele;
          $.login.sendBefore(ele);
          $.util
            .ajax(
              lenye_base.base_data.getapiUrl() +
              lenye_base.api_url.base.dynCodeSend,
              lenye_base.base_data.getformatting({
                stType: 2,
                mobile: this.userInfos.phone
              })
            )
            .then(res => {
               $.login.sendAfter(ele);
            });
        }else{
          msg.warning("请输入正确手机号！");
        }
      },
    async goPay(){
        if($.validation.isZh(this.userInfos.custName)){
          msg.warning('请输入中文姓名！');
          return false;
        }else if ($.validation.isPhone(this.userInfos.phone)) {
          msg.warning('请输入正确格式手机号！');
          return false;
        }else if($.validation.isCard(this.userInfos.idcarNo)){
          msg.warning('请输入正确格式身份证号！');
          return false;
        } else if (!this.code) {
          msg.warning('验证码不能为空！');
          return false;
        }
        let isBy = await this.checkCode(this.code);
        if(!isBy){
          msg.warning("验证码错误！");
          return  false;
        }

        $.util
        .ajax(
          lenye_base.base_data.getapiUrl() +
            "/base-service/api/cust/userSelf/save.do",
          lenye_base.base_data.getformatting(this.userInfos)
        )
        .then(res => {
          if (res.success == "1") {
            if(this.type=='4'){
              this.subSignAct();
            }else {
              this.subSign(this.type);
            }

          }else{
            msg.warning(res.message);
          }
        });
     },
     detail(){
       $.util
        .ajax(
          lenye_base.base_data.getapiUrl() +
            "/base-service/api/cust/userSelf/detail.do",
          lenye_base.base_data.getformatting({
          })
        )
        .then(res => {
          if (res.success == "1") {
            this.userInfos = res.data;
          }else{
            msg.warning(res.message);
          }
        });
     }
   },
};
</script>
