// pages/new_teacher/new_teacher.js
import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber,formatEncrypt} from '../../utils/util.js'
import { CreateTeacher, CreateTeacherV1 } from '../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, saveUserInfo, getWxloginCode, checkStatus } = getApp()
const { baseImgUrl, thumbnail } = globalData
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    input:125533222255,
    subList:[
      {
        text:'语文',
        status:"active"
      },
      {
        text: '数学',
        status: "active"
      },
      {
        text: '英语',
        status: "active"
      },
    ],
    idList:[
      {
        text: '作业标注',
        status: "active",
        num: '4'
      },
      {
        text: '现场管理',
        status: "active",
        num: '2'
      },
      {
        text: '学科答疑',
        status: "active",
        num: '3'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  },
   /**
   * 加密
   */
  code:function(res){
    var input_rsa = res;
    var encrypt_rsa = new RSA.RSAKey();
    encrypt_rsa = RSA.KEYUTIL.getKey(publicKey);
    var encStr = encrypt_rsa.encrypt(input_rsa)
    encStr = RSA.hex2b64(encStr);
    return encStr
  },
  /**
   * 选择学科
   */
  selectSubject:function(e){
    const { sidx } = e.currentTarget.dataset
    const { subList}=this.data
    var temp={}
    subList[sidx]['status'] = subList[sidx].status == '' ? 'active' : '';
    this.setData({ subList})
  }, 
   /**
   * 选择身份
   */
  selectTeacher: function(e) {
    const { sidx } = e.currentTarget.dataset
    const { idList } = this.data
    var temp = {}
    idList[sidx]['status'] = idList[sidx].status == '' ? 'active' : '';
    this.setData({ idList })
  },
 
  /**
   * input
   */
  inputCtl: function (e) {
    const mobileExp = /^1[0-9]{10}$/;
    const { temp = {}, type } = e.currentTarget.dataset
    const { value } = e.detail
    temp[type] = value;
    const { name, mobile } = temp;
    // console.log(name)
    if (type == 'mobile') {
      if (value != '') {
        if (!mobileExp.test(value)) {
          wx.showToast({
            title: '手机号码位数不对',
            icon: 'none',
            duration: 2000
          })
          return this.setData({ mobile })
        } else {
          return this.setData({ mobile })
        }
      }
    } else if (type == 'name') {
      // if (value != '') {
      return this.setData({ name })
      // }
    } 
  },
  /**
   * 取消
   */
  cancel:function(){
    wx.navigateBack({})
  },
  /**
  * 添加老师
  */
  sureCreateTeacher: function (cb) {
    const continuousFn = { fn: this.sureDeleteClass, param: { ...arguments } }
    const { name, mobile, subList, idList} = this.data;
    var that=this
    var subjectList = [];
    for (var i in subList){
      if (subList[i].status =='active'){
        console.log(subList[i].text)
        subjectList.push(subList[i].text)
      }
    }
    var teacherTypeList = []
    for (var i in idList) {
      if (idList[i].status == 'active') {
        teacherTypeList.push(idList[i].num)
      }
    }
    if (!name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (!mobile) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (subjectList.length==0) {
      wx.showToast({
        title: '至少选择一个学科',
        icon: 'none'
      })
      return
    }
    if (teacherTypeList.length==0) {
      wx.showToast({
        title: '至少选择一种身份',
        icon: 'none'
      })
      return
    }
    CreateTeacherV1({ data: { name:formatEncrypt(name), mobile:formatEncrypt(mobile), subjectList, teacherTypeList }, continuousFn }).then(res => {
      wx.showToast({
        title: '创建成功',
        icon: 'none'
      }) 
      that.cancel()
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
})