// pages/inquiryForm/inquiryForm.js
const app = getApp()
import {nation,job} from "../../../api/common"
import {sendFields} from "../../../api/sickRecord"
import {throttle} from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog:false,
    obj:{
      height:'身高',
      weight:'体重',
      marital_status:'婚姻状态',
      nation_id:'民族',
      job_id:'职业',
      allergy_history:'是否有过敏史？',
      family_history:'是否有家族病史？',
      is_pregnant:'是否处于备孕、妊娠、哺乳期？',
      is_operation:'是否做过手术？',
      drink_wine_status:'是否有饮酒史?',
      smoke_status:'是否有吸烟史？',
      chemical_compound_status:'是否有接触过化学物?',
      diagnosis_hospital:'确诊医院是？',
      is_take_medicine:'服用药品是？'

    },
    showNation:false,
    showJob:false,
    showMarital:false,
    nationname:'',
    order_inquiry_id:'',
    jobname:'',
    hideGuomin:false,
    hideFamilysick:false,
    hidePregnant:false,
    hideOperation:false,
    hideChemical:false,
    hideHospital:false,
    hideDrug:false,
    personInfo:{},
    showBtn:true,
    maritalColumn: [{
      text: '未婚',
      value: 0
    }, {
      text: '已婚 ',
      value: 1
    }, {
      text: '离异 ',
      value: 2
    }],
    nationcolumns:[],
    jobcolumns:[],
    list1:[],
    list2:[],
    startIndex1:1,
    showList:[{
      isHas:false,
      index:1
    },{
      isHas:false,
      index:2
    },{
      isHas:false,
      index:3  
    },{
      isHas:false,
      index:4
    },{
      isHas:false,
      index:5
    },{
      isHas:false,
      index:6
    },{
      isHas:false,
      index:7
    },{
      isHas:false,
      index:8
    },{
      isHas:false,
      index:9
    }]
    

  },
  handleIpt(e){
    let key = e.target.dataset.id;
    let obj="personInfo."+key;
    if(key=='height' || key=="weight"){
     this.setData({
       [obj]:e.detail.value
     })
    }else{
     this.setData({
       [obj]: e.detail.value
     })
    }
   
 },
 confirmDialog:throttle(function(event){
  if(event.detail){
    this.handleSend();
  }
}),
handleSend(){
  const {order_inquiry_id,personInfo,obj}=this.data;
  let dataObj={};
  for (const key in obj) {
    if(personInfo[key] || personInfo[key]==0){
      dataObj[key]=personInfo[key];
    }
  }
 let res=Object.assign({},dataObj,personInfo);
  sendFields({
    order_inquiry_id,
    fields:res,
  }).then(data=>{
    wx.showToast({
      title: '发送成功',
      icon:'none'
    }),
    wx.navigateBack()
    thi.setData({
      showDialog:false
    })
   
    
  })
},
  handleSendFields(){
    const {order_inquiry_id,personInfo,list1,list2,marital_name,jobname,nationname,obj}=this.data;
    console.log(personInfo)
    const fields=[];
  for (let i = 0; i <list1.length; i++) {
   if(list1[i]=='height' && !personInfo.height){
      wx.showToast({
        title: '请填写身高',
        icon:'none'
      })
      return false
   }
   if (list1[i]=='height' && personInfo.height) {
    if (!/^3[5-9](\.\d+)?$|^[4-9][0-9](\.\d+)?$|^1[0-9][0-9](\.\d+)?$|^2[0-4][0-9](\.\d+)?$|250/.test(personInfo.height)) {
      wx.showToast({
        title: `请输入范围内身高！`,
        icon: 'none',
      });
      return false;
    }
  };
    if(list1[i]=='weight' && !personInfo.weight){
      wx.showToast({
        title: '请填写体重',
        icon:'none'
      })
      return false
  }
  
  if (list1[i]=='weight' && personInfo.weight) {
    if (!/^[2-9](\.\d+)?$|^[1-9][0-9](\.\d+)?$|^1[0-9]{1,2}(\.\d+)?$|200/.test(personInfo.weight)) {
      wx.showToast({
        title: `请输入范围内体重！`,
        icon: 'none',
      });
      return false;
    }
  }
  if(list1[i]=='marital_status' && !marital_name){
    wx.showToast({
      title: '请选择婚姻状态',
      icon:'none'
    })
    return false
 }
 if(list1[i]=='nation_id' && !nationname){
  wx.showToast({
    title: '请选择民族',
    icon:'none'
  })
  return false
}
if(list1[i]=='job_id' && !jobname){
  wx.showToast({
    title: '请选择职业',
    icon:'none'
  })
  return false
}
    
}
for (let i = 0; i < list2.length; i++) {
  if( list2[i]=="is_allergy_history"){
 if(!personInfo.is_allergy_history && personInfo.is_allergy_history!=0){
    wx.showToast({
      title: '请选择是否有过敏史',
      icon:'none'
    })
    return false
  }
  if(personInfo.is_allergy_history==1 && !personInfo.allergy_history){
    wx.showToast({
      title: '请填写过敏源',
      icon:'none'
    })
    return false
  }
  
 
  }else if(list2[i]=="is_family_history"){
    if(!personInfo.is_family_history && personInfo.is_family_history!=0){
      wx.showToast({
        title: '请选择是否有家族病史',
        icon:'none'
      })
      return false
    }
    if(personInfo.is_family_history==1 && !personInfo.family_history){
      wx.showToast({
        title: '请填写家族病史',
        icon:'none'
      })
      return false
    }
  
  }else if(list2[i]=="is_pregnant"){
    if(!personInfo.is_pregnant && personInfo.is_pregnant!=0){
      wx.showToast({
        title: '请选择是否处于备孕、妊娠、哺乳期',
        icon:'none'
      })
      return false
    }
    if(personInfo.is_pregnant==1 && !personInfo.is_pregnant){
      wx.showToast({
        title: '请填写请描述您目前所处阶段，如备孕中、哺乳期中、妊娠月份',
        icon:'none'
      })
      return false
    }
  
  }else if(list2[i]=="is_operation"){
    if(!personInfo.is_operation && personInfo.is_operation!=0){
      wx.showToast({
        title: '请选择是否做过手术',
        icon:'none'
      })
      return false
    }
    if(personInfo.is_operation==1 && !personInfo.operation){
      wx.showToast({
        title: '请描述具体手术名称和做手术医院',
        icon:'none'
      })
      return false
    }
    
  }else if(list2[i]=="drink_wine_status"){
    if(!personInfo.drink_wine_status){
      wx.showToast({
        title: '请选择是否有饮酒史',
        icon:'none'
      })
      return false
    }
  }else if(list2[i]=="smoke_status"){
    if(!personInfo.smoke_status){
      wx.showToast({
        title: '请选择是否有吸烟史',
        icon:'none'
      })
      return false
    }
  }else if(list2[i]=="chemical_compound_status"){
    if(!personInfo.chemical_compound_status){
      wx.showToast({
        title: '是否有接触过化学物',
        icon:'none'
      })
      return false
    }
    if(personInfo.chemical_compound_status && personInfo.chemical_compound_status!=1 && !personInfo.chemical_compound_describe){
      wx.showToast({
        title: '请详细填写接触过的相关化学物',
        icon:'none'
      })
      return false
    }
  }else if(list2[i]=="diagnosis_hospital"){
    if(!personInfo.diagnosis_hospital){
      wx.showToast({
        title: '请填写确诊医院名称',
        icon:'none'
      })
      return false
    }
  
  }else if(list2[i]=="is_take_medicine"){
    if(!personInfo.is_take_medicine && personInfo.is_take_medicine!=0){
      wx.showToast({
        title: '请填写是否正在服药',
        icon:'none'
      })
      return false
    }
    if(personInfo.is_take_medicine==1 && !personInfo.drugs_name){
      wx.showToast({
        title: '请填服药名称',
        icon:'none'
      })
      return false
    }
  }
  
}
  this.setData({
    showDialog:true
  })
  },
  openMaritalPicker() {
     wx.hideKeyboard();
      if(this.data.showBtn){
        this.setData({
          showMarital: true
        })
      }  
  },
  closeMaritalPicker() {
    this.setData({
      showMarital: false
    })
  },
  onConfirmMaritalPicker(event) {
    const {
      value
    } = event.detail;
  
    this.setData({
      showMarital: false,
      marital_name:value.text,
     'personInfo.marital_status':value.value
    })
  },
  onChangeRadio(event) {
    let key = event.target.dataset.id;
    let obj= "personInfo."+key;
    this.setData({
       [obj]:Number(event.detail)
    });
    if(key=="chemical_compound_status" && event.detail==1){
      this.setData({
        "personInfo.chemical_compound_describe":''
      })
    }else if(event.detail==0){
       if(key=="is_allergy_history"){
         this.setData({
         "personInfo.allergy_history":'',
         hideGuomin:false
         })
       };
       if(key=="is_pregnant"){
        this.setData({
          "personInfo.pregnant":'',
           hidePregnant:false
          })
       }
       if(key=="is_family_history"){
        this.setData({
          "personInfo.family_history":'',
           hideFamilysick:false
          })
       }
       if(key=="is_take_medicine"){
        this.setData({
          "personInfo.drugs_name":'',
          hideDrug:false
          })
       }
    }
  },
  handelFocus(e) {
    let key = e.target.dataset.id;
    let value = e.target.dataset.value;
    let obj="personInfo."+value;
    if (!e.detail.value) {
      this.setData({
        [key]: false,
        [obj]: e.detail.value
      })
    } else {
      this.setData({
        [key]: true,
        [obj]: e.detail.value
      })
    }
  },
  onConfirmNation(event) {
    const {
      value
    } = event.detail;
    this.setData({
     "nationname": `${value.nation_name}`,
     "personInfo.nation_id": `${value.nation_id}`,
     showNation: false
    })
  },
  showNation() {
    wx.hideKeyboard();
    if(this.data.showBtn){
      this.setData({
        showNation: true
      })
    }  
     

  },
  onCancelNation() {
    this.setData({
      showNation: false
    })
  },
  onConfirmJob(event) {
    const {
      value
    } = event.detail;
    this.setData({
     "jobname": `${value.job_name}`,
     "personInfo.job_id": `${value.job_id}`,
      showJob: false
    })
  },
  showJob() {
    wx.hideKeyboard();
    if(this.data.showBtn){
      this.setData({
        showJob: true
      }) 
    }  
     
  },
  onCancelJob() {
    this.setData({
      showJob: false
    })
  },
  commonMethods(arr1,arr2){
    this.setData({
      list1:arr1,
      list2:arr2
    });
    let count=0;
    arr2.forEach((item)=>{
       if(item=="allergy_history"){
         count++;
        this.setData({
          'showList[0].isHas':true,
          'showList[0].index':1
        })
       }else if(item=="family_history"){
         count++;
        this.setData({
          'showList[1].isHas':true,
          'showList[1].index':count
        })
       }else if(item=="is_pregnant"){
        count++;
        this.setData({
          'showList[2].isHas':true,
          'showList[2].index':count
        })
       }else if(item=="is_operation"){
        count++;
        this.setData({
          'showList[3].isHas':true,
          'showList[3].index':count
        })
       }else if(item=="drink_wine_status"){
        count++;
        this.setData({
          'showList[4].isHas':true,
          'showList[4].index':count
        })
       }else if(item=="smoke_status"){
        count++;
        this.setData({
          'showList[5].isHas':true,
          'showList[5].index':count
        })
       }else if(item=="chemical_compound_status"){
        count++;
        this.setData({
          'showList[6].isHas':true,
          'showList[6].index':count
        })
       }else if(item=="diagnosis_hospital"){
        count++;
        this.setData({
          'showList[7].isHas':true,
          'showList[7].index':count
        })
       }else if(item=="is_take_medicine"){
        count++;
        this.setData({
          'showList[8].isHas':true,
          'showList[8].index':count
        })
       }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getNation();
    this.getJob();
       if(options.order_inquiry_id){
         this.setData({
          order_inquiry_id:options.order_inquiry_id
         })
       }
      if(options.case_filled_fields){
        
        let data=JSON.parse(options.case_filled_fields);
        let arr1=[];
        let arr2=[];
      
        this.setData({
          personInfo:data,
          showBtn:false
        });
        if(data.is_allergy_history==1){
          this.setData({
            hideGuomin:true
          })
        }
        if(data.is_family_history==1){
          this.setData({
            hideFamilysick:true
          })
        }
        if(data.is_operation==1){
          this.setData({
            hideOperation:true
          })
        }
        if(data.is_pregnant==1){
          this.setData({
            hidePregnant:true
          })
        }
        if(data.chemical_compound_status && data.chemical_compound_status!=1){
          this.setData({
            hideChemical:true
          })
        }
        if(data.diagnosis_hospital){
          this.setData({
            hideHospital:true
          })
        }
        if(data.is_take_medicine==1){
          this.setData({
           hideDrug:true
          })
        }
        let {maritalColumn,jobcolumns,nationcolumns}=this.data;
        if(data.marital_status==0 || data.marital_status){
          let select=maritalColumn.filter(item=>{return item.value==data.marital_status});
          this.setData({
            'marital_name':select[0].text
          })
        };
       
       
      
        Object.keys(data).forEach(function(key){
          if(key=="weight" || key=="height" || key=="marital_status" || key=="nation_id" || key=="job_id"){
            arr1.push(key);
         }else{
            arr2.push(key);
         }
          })
          this.commonMethods(arr1,arr2);
         


      }
     if(options.params){
       let data=JSON.parse(options.params);
       let {obj}=this.data;
       let arr1=[];
       let arr2=[];
        data.forEach(item => {
        if(item=="weight" || item=="height" || item=="marital_status" || item=="nation_id" || item=="job_id"){
           arr1.push(item);
        }else{
           arr2.push(item);
        }
       
      })
      this.commonMethods(arr1,arr2);
     
     }
  },

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

  },
  getNation(){
    nation().then(data=>{
        this.setData({
          nationcolumns:data
        })
        let {personInfo}=this.data;
        if(personInfo.nation_id || personInfo.nation_id==0){
          let select=data.filter(item=>{return item.nation_id==personInfo.nation_id});
          this.setData({
            'nationname':select[0].nation_name
          })
        }
    })
  },
 getJob(){
    job().then(data=>{
      this.setData({
        jobcolumns:data
      })
      let {personInfo}=this.data;
      if(personInfo.job_id || personInfo.job_id==0){
         let select=data.filter(item=>{return item.job_id==personInfo.job_id});
        this.setData({
          'jobname':select[0].job_name
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})