// pages/winter-training/index.js
const app = getApp()
Page({

    
    /**
     * 页面的初始数据
     */
    data: {
       show:false,
        userName:"",
        typeWordName:"",
        companyName:"",
        signUrl:"",
        mainId:"",
        cultivateType:"",
        cardId:"",
        isShow:false,
        szhprojectId:"",
        smzprojectId:"",
        loadShow:false,
        data:{},
        phoneNumber:"",
        cardNumber:"",
        firstWorkType:""
    },
// 展示图片
showImg(){
  this.setData({
    show:true
  })
  },
  // 关闭遮罩层
  closeShow(){
    this.setData({
      show:false
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let {userName,typeWordName,companyName,signUrl,mainId,cultivateType,cardId,szhprojectId,smzprojectId} = options
        this.setData({
            szhprojectId,
            smzprojectId,
            userName,
            typeWordName,
            companyName,
            signUrl,
            mainId,
            cultivateType,
            cardId
        })
        wx.setStorageSync('companyName', companyName)
        wx.setStorageSync('smzprojectId', smzprojectId)
        wx.setStorageSync('szhprojectId', szhprojectId)
        wx.setStorageSync('answerName', userName)
        wx.setStorageSync('cultivateType', cultivateType)
        this.checkLearn();
        this.getUserInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /**
     * 跳转学习页面
     */
    affirmLearn() {
      if(this.data.cultivateType == 4) {
        console.log(this.data)
          //获取projectId 和用户的phonenumber或idcard
            wx.redirectTo({
              url: '../newAddPage2/safetyBriefingLearning/index?projectId='+this.data.szhprojectId+"&firstWorkType="+this.data.firstWorkType+"&secondWorkType="+this.data.typeWord
              ,complete:function(res){
                console.log(res)
              },fail: err => {
                console.log(err)
              }
            });
      } else {
          this.submit()
      }
    },

    /**
     * 校验学习
     */
    checkLearn(){
      let that = this
      wx.request({
        url: app.globalData.szhUrl+"/business/saftEducationOpenIdMap/listPersonSign",
        method:"GET",
        data:{
          main_id:this.data.mainId,
          project_id:this.data.szhprojectId,
          cultivate_type:this.data.cultivateType,
          resident_id:this.data.cardId
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' //修改此处即可
        },
        success(res){
              that.setData({
                isShow:res.data.data && res.data.code == "200" && res.data.data.length > 0
              })
              
        }
      })
    },



    /**
     * 确认学习
     */
    submit(){
      var that = this;
      that.setData({
        loadShow:true
      })
      console.log(mainId)
        let {userName,mainId,cultivateType,cardId} = this.data
        wx.request({
          url: app.globalData.szhUrl+"/api/wei/gainWeiSanInfo",
          method:"POST",
          data:{
            dept_id:131,
            project_id:this.data.szhprojectId,
            smz_project_id:this.data.smzprojectId,
            main_id:mainId,
            cultivate_type:cultivateType,
            user_name:userName,
            resident_id:cardId,
            result_str:"已通过",
            type:1
          },
           header: {
            'content-type': 'application/x-www-form-urlencoded' //修改此处即可
          },
          success(res){
            that.setData({
              loadShow:false
            })
              if(res.data.code == 200){
                app.toast("学习成功");
                that.setData({
                  isShow:true
                })

              }
          }
        })
    },
    /**
     * 取消学习
     */
    cancelLearn(){
        //在需要退出小程序的地方调用添加下面代码即可(js文件中)
        wx.exitMiniProgram({success: (res) => {

        },fail:(res)=>{
          console.log(res)
        }})
    },
    /**
     * 获取用户信息
     */
    getUserInfo(){
      var that = this
      let openId = wx.getStorageSync('openId') != null || wx.getStorageSync('openId') ?wx.getStorageSync('openId'):"123"
       //获取用户在实名制用户信息
      wx.request({
          url: app.globalData.szhUrl + "/business/saftEducationOpenIdMap/wxList",
          method:"post",
          data:{
              openId:openId
          },
          header: {
              'content-type': 'application/x-www-form-urlencoded' //修改此处即可
          },
          success(signTemp){
              let temp =  signTemp.data.rows.length <= 0 ? "":signTemp.data.rows[0]
              console.log(temp)
              let {projectId,phoneNumber,typeWord,specialFlag,cardId,typeWordName,userName,companyName} = temp
              console.log(temp);
              that.setData({
                  phoneNumber:phoneNumber,
                  smzprojectId:projectId,
                  typeWord:typeWord,
                  firstWorkType:specialFlag==0?9:8,
                  cardId:cardId
              })
              wx.setStorageSync('companyName', companyName)
              wx.setStorageSync('answerName', userName)
              wx.setStorageSync('cardId', cardId)
              wx.setStorageSync('typeWordName', typeWordName)
              
          }
      })

      return;
  }
  
})