// pages/selectPatient/selectPatient.js
const app = getApp()
import {family,addfamily} from "../../../api/familyDoc"
import {throttle} from "../../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctor_id:'',
    chat_id:'',
    inquiry_type:'',
    inquiry_mode:'',
    show:false,
    sex:null,
    height:970,
    currentFamilyId:'',
    showPicker:false,
    realName:'',
    idCard:'',//'',
    phoneNumber:'',
    relation:"",
    relationId:"",
    family:[],
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    columns:[
      {text:"本人",value:1},
      {text:"父母",value:2},
      {text:"爱人",value:3},
      {text:"子女",value:4},
      {text:"亲戚",value:5},
       {text:"其他 ",value:6}
    ]
  },
  addFamily(){
    let {realName,idCard,phoneNumber,relationId}=this.data;
    addfamily({
      card_name:realName,
      type:1,
      id_number:idCard,
      mobile:phoneNumber,
      is_default:0,
      relation:relationId    
    }).then((data)=>{

      this.getFamily();
      this.setData({
        currentFamilyId:data.family_id,
        realName:'',
        idCard:'',
        phoneNumber:'',
        relationId:'',
        relation:'',
        show:false

      })
      // let {currentFamilyId,doctor_id,chat_id,inquiry_type,sex}=this.data;
      // app.method.navigateTo({
      //   url: '../writeSick/writeSick?doctor_id='+doctor_id+"&family_id="+currentFamilyId+"&chat_id="+chat_id+"&sex="+sex+"&inquiry_type="+inquiry_type
      // })
    })
  },
  selectPatient(e){
    let currentFamilyId=e.currentTarget.dataset.familyid;
    let sex=e.currentTarget.dataset.sex;
     this.setData({
       currentFamilyId:currentFamilyId,
        sex:sex
     })
  },
  getFamily(){
    family().then((data)=>{
      this.setData({
        family:this.transforpage(data)
      })
    })
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
  },
  addPatient(){
    this.setData({
      show:true,
      currentFamilyId:"",
      sex:""
    })
  },
  transforpage(arr) {
    const pages = []
    let newArr=arr.concat([{
      type:"add"
    }])
    newArr.forEach((item, index) => {
      const page = Math.floor(index / 2)
      if (!pages[page]) {
        pages[page] = []
      }
      pages[page].push(item)
    });
    return pages
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  onConfirm(event) {
    const { value} = event.detail;
    this.setData({
      relationId: `${value.value}`,
      relation:`${value.text}`,
      showPicker:false
    });
  },

  openPicker(){
    this.setData({
      showPicker:true
    })
  },
  closePicker(){
    this.setData({
      showPicker:false
    })
  },
  
  inputChange(e){
    this.setData({
      [e.target.dataset.id]: e.detail.value
  })
  },
  //获取身份证号信息
  getIdInfo(IdCard){
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if(reg.test(IdCard)){
		let sex
        if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
			sex = '1'
		} else {
			sex = '2'
		}
		var ageDate = new Date()
		var month = ageDate.getMonth() + 1
		var day = ageDate.getDate()
		var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
		if (IdCard.substring(10, 12) < month || (IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day)) {
			age++
		}
		if (age <= 0) {
			age = 1
		}
		return { sex, age}
	}else{
        return false;
    }
},
handleThrottleNext:throttle(function(){
  this.goNext()
}),
  goNext(){
    let url='';
    let {doctor_id,currentFamilyId,chat_id,inquiry_type,sex,inquiry_mode}=this.data;
    if(this.data.doctor_id){
      url='/patient/pages/writeSick/writeSick?doctor_id='+doctor_id+"&family_id="+currentFamilyId+"&chat_id="+chat_id+"&inquiry_type="+inquiry_type+"&sex="+sex+"&inquiry_mode="+inquiry_mode
    }else{
      url='/patient/pages/writeSick/writeSick?family_id='+currentFamilyId+"&inquiry_type="+inquiry_type+"&sex="+sex+"&inquiry_mode="+inquiry_mode
    }
    app.method.navigateTo({
      url: url
    })
  },
  handleThrottleSick:throttle(function(){
    this.goSick()
  }),
  goSick(){
    if (!/^([\u4e00-\u9fa5\·]{2,10})$/.test(this.data.realName)) {
      wx.showToast({
        title: `姓名要求在2-10个汉字`,
        icon: 'none',
      });
      return false;
    };
    if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this.data.idCard)){
      wx.showToast({
        title: `请输入有效的身份证号`,
        icon: 'none',
      });
      return false;
    };
    if(this.data.phoneNumber){
      if(!/^1[3456789]\d{9}$/.test(this.data.phoneNumber)){
        wx.showToast({
          title: `请输入有效的手机号码！`,
          icon: 'none',
        });
        return false;    
      }
    };
    // if(!this.data.relationId){
    //   wx.showToast({
    //     title: `请选择与患者的关系`,
    //     icon: 'none',
    //   });
    //   return false;    
    // };
    let idInfo=this.getIdInfo(this.data.idCard);
     if(idInfo.age<6){
      wx.showToast({
        title: `六岁以下儿童不支持线上问诊`,
        icon: 'none',
      });
      return false;  
     }
   
     this.setData({
      sex:idInfo.sex
     })
    this.addFamily()
  
  },
  onfocus(event){
    this.setData({
      height:970+event.height+120
    })
  },
  onblur(event){
    this.setData({
      height:970
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.doctor_id){
      this.setData({
        doctor_id:options.doctor_id,
        chat_id:options.chat_id,
        
      })
    }
    if(options.inquiry_type){
      this.setData({
        inquiry_type:options.inquiry_type,
        inquiry_mode:options.inquiry_mode
      })
    }
    this.getFamily();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    })
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  
})