// pages/medinceRecord/medinceRecord.js
const app = getApp()
import {
  medinceRecord,
  family
} from "../../../api/familyDoc"
import {throttle} from "../../../utils/util"
import {isReceivepatient} from "../../../api/consultExpert"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    show: false,
    showDialogConsult:false,
    isTriggered:false,
    showdialog: false,
    message: '',
    id:'',
    page: 1,
    pageNumber: 10,
    currentName: "全部就诊人",
    family_id: 0,
    currentFamilyId: '',
    currentIndex: 0,
    list: [],
    sex: "",
    option2: [],
    product: "",
    isLock: false
  },
  isChangePeople(){
    this.setData({
      showdialog: true
    })
  },
  handleSeeDr:throttle(function(event) { 
    let {id,familyid,sex}= event.currentTarget.dataset
    let info = (this.data.list)[id].order_product_item;
     info.forEach(item=>{
      item.product_num=item.amount;
     })
    this.setData({
      currentFamilyId: familyid,
      product:JSON.stringify(info),//JSON.stringify(medinceInfo)
      sex:sex
    })
    this.handleIsReceivepatient(this.isChangePeople)
  }),
  confirmConsult:throttle(function(event) {
    let {id}=this.data;
    if (event.detail) {
      app.method.navigateTo({
        url: '/patient/pages/orderDetail/orderDetail?order_inquiry_id=' + id,
      })
    }
  }),
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
  },
  lower(e) {
    if (!this.data.isLock) {
      this.setData({
        page: ++this.data.page
      });
      this.handleMedinceRecord()
    }
  },
  handleIsReceivepatient(callback) {
    isReceivepatient({
      inquiry_type:4,
      inquiry_mode: 1,
      doctor_id: ''
    }).then(data => {
      if (data.status == 1) {
        callback(); 
      } else if (data.status == 2) {
        this.setData({
          showDialogConsult: true,
          id: data.data.order_inquiry_id
        })
      } else if (data.status == 3) {
        wx.showToast({
          title: data.message,
          icon: "none"
        })
      }
    })
  },
  getfamily() {
    family().then(data => {
      let arr = [];
      data.forEach((item, index) => {
        arr[index] = {};
        let sex = "";
        switch (item.sex) {
          case '0':
            sex = "未知";
            break;
          case '1':
            sex = "男";
            break;
          case '2':
            sex = "女";
            break;
        }
        let age = item.age;
        arr[index].text = item.card_name + "（" + sex + "，" + age + "岁）";
        arr[index].value = item.family_id;
      })

      arr = [{
        text: "全部就诊人",
        value: 0
      }].concat(arr);
      this.setData({
        option2: arr
      })
    })
  },
  handleRefresher(){
    this.onFresh()
  },
  handleMedinceRecord() {
    let {
      family_id,
      page,
      pageNumber
    } = this.data;
    medinceRecord({
      family_id,
      page,
      per_page: pageNumber
    }).then(data => {
      let result = data.data;
      if (result.length == 0) {
        this.setData({
          isLock: true,
          isTriggered:false
        })
        return false
      }
      this.setData({
        isTriggered:false,
        list: this.data.list.concat(result)
      })
    })
  },
  onFresh() {
    this.setData({
      page: 1,
      list: [],
      isLock: false
    })
    this.handleMedinceRecord();
  },
  showPicker() {
    this.setData({
      show: true
    })
  },
  onConfirm(event) {
    const {
      value
    } = event.detail;
    this.setData({
      show: false,
      currentName: `${value.text}`,
      family_id: `${value.value}`,
    })
    this.onFresh()
  },

  onCancel() {
    this.setData({
      show: false
    })
  },
  onConfirmDialog:throttle(function() {
    let {
      product
    } = this.data;
    this.setData({
      showdialog: false
    });
    app.method.navigateTo({
      url: "/patient/pages/medinceList/medinceList?product="+product
    })
  }),
  onCloseDialog:throttle(function() {
    let {
      currentFamilyId,
      product,
      sex
    } = this.data;
    this.setData({
      showdialog: false
    });
    console.log(currentFamilyId);
    app.method.navigateTo({
      url: '/patient/pages/writeSick/writeSick?family_id='+currentFamilyId +"&inquiry_type=4"+"&product="+product+"&sex="+sex
    })
  }),



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

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

  },

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

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

  },

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

  },

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

  },

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

  },

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