/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-15 17:20:23
 * @LastEditors: zhangzhuobin 1093998108@qq.com
 * @LastEditTime: 2022-06-10 10:11:58
 */
import {
  fhGetPatientInfo
} from '../../../api/remote/index'
import {
  fhPostNurseOrder,
  fhGetOrderList
} from '../../../api/nursing/index'
import {
  Tips
} from '../../../utils/util'
const {
  Loading,
  hLoading
} = getApp()
const app = getApp()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    // 表单
    form: {
      sex:1,
      selfCare:1,
    },
    sex:1,
    selfCare:1,
    show: false,
    isShow: false,
    //患者的联系电话
    number: null,
    address: null,
    // 加载中
    loading: false,
    results: [],
    submitIn: true,
    //性别数据
    items: [{
      value: '1',
      name: '男'
    },
    {
      value: '2',
      name: '女'
    }
    ],
    lists: [{
      value: '1',
      name: '自理'
    },
    {
      value: '2',
      name: '半自理'
    },
    {
      value: '3',
      name: '不能自理'
    }
    ],
    array: ['本人', '配偶', '父母', '子女', '亲戚', '朋友', '其他'],
    objectArray: [{
      id: 0,
      name: '本人'
    },
    {
      id: 1,
      name: '配偶'
    },
    {
      id: 2,
      name: '父母'
    },
    {
      id: 3,
      name: '子女'
    },
    {
      id: 4,
      name: '亲戚'
    },
    {
      id: 5,
      name: '朋友'
    },
    {
      id: 6,
      name: '其他'
    }
    ],
    index: null,
    value: 0
  },
  // 页面载入时
  onLoad(e) {
    this.init(e)
  },
  // 页面初始化
  init(e) {
    this.fhGetPatientInfo()
    const {
      name
    } = wx.$getGlobalData('patientInfo') || {}
    const {
      phone
    } = wx.$getGlobalData('patientInfo') || {}
    const {
      sickareaId
    } = wx.$getGlobalData('hospitalInfo') || {}
    const hospitalAddress = wx.$getGlobalData('hospitalAddress') || {}
    const memberId = app.globalData.baseInfo.memberId
    const historySick = hospitalAddress.addressDetail.slice(0, 3) !== hospitalAddress.sickroomName ? (hospitalAddress.sickroomName + hospitalAddress.addressDetail) : hospitalAddress.addressDetail
    console.log(hospitalAddress)
    this.setData({
      form: {
        ...this.data.form,
        memberId,
        name,
        phone,
        sickAreaId: sickareaId,
        ...hospitalAddress,
        historySick
      },
      address: hospitalAddress
    })
    console.log(this.data.form);
  },
  async fhGetPatientInfo() {
    const form = []
    const memberId = app.globalData.baseInfo.memberId
    try {
      const {
        results
      } = await fhGetPatientInfo(form, memberId)
      this.setData({
        results
      })
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  async fhPostNurseOrder(dataset) {
    Loading()
    let {
      form
    } = this.data
    this.setData({
      nurseOrderDto: {
        ...form
      }
    })
    let {
      nurseOrderDto
    } = this.data
    const memberId = app.globalData.baseInfo.memberId
    try {
      const results = await fhPostNurseOrder(nurseOrderDto, memberId)
      const orderId = results.results.orderId
      const code = results.code
      this.setData({
        orderId,
        code
      })
      Dialog.alert({
        title: '提示',
        message: '您的预约申请已成功提交，我们会在24小时内与您取得联系，请保持电话畅通。'
      })
        .then(() => {
          const {
            orderId
          } = this.data
          wx.navigateTo({
            url: dataset.path + '?query=' + orderId + '&type=' + dataset.type
          })
        })
        .catch(() => { })

    } catch (e) {
      console.log('debug axios', e)
      Dialog.alert({
        title: '提示',
        message: '该病区不在服务范围！'
      })
    }
    hLoading()
  },
  // 页面显示时
  onShow() {
    const {
      isFirstOnShow
    } = this.data
    if (isFirstOnShow) {
      // 首次执行时
      this.setData({
        isFirstOnShow: false
      })
      return
    }
  },

  // 用户输入
  wardInput({
    currentTarget: {
      dataset
    },
    detail
  }) {
    const {
      value
    } = detail
    const {
      key
    } = dataset
    this.setData({
      [`form.${key}`]: value
    })
    console.log(form);
    const {
      form
    } = this.data
    const {
      sex,
      selfCare,
      relationship,
      age
    } = form
    this.setData({
      isShow: sex && selfCare && relationship && age
    })
  },
  // 校验是否可以提交
  check({
    currentTarget: {
      dataset
    }
  }) {
    return new Promise((resolve, reject) => {
      const {
        form
      } = this.data
      console.log(form)
      const {
        sex,
        selfCare,
        age
      } = form
      if (!sex) {
        Tips('请先选择性别')
        reject('请先选择性别')
      } else if (!selfCare) {
        Tips('请先选择自理能力')
        reject('请先选择自理能力')
      } else if (!age) {
        Tips('请先填写患者年龄')
        reject('请先填写患者年龄')
      } else {
        this.fhPostNurseOrder(dataset)
      }

      resolve()
    })
  },
  bindPickerChange({
    currentTarget: {
      dataset
    },
    detail
  }, e) {
    console.log(e)
    let {
      value
    } = detail
    const {
      key
    } = dataset
    console.log('picker发送选择改变，携带值为', value)
    this.setData({
      [`form.${key}`]: value,
      index: value
    })
    const {
      form
    } = this.data
    const {
      sex,
      selfCare,
      relationship,
      age
    } = form
    this.setData({
      isShow: sex && selfCare && relationship && age
    })
  },
  changeSex({
    currentTarget: {
      dataset
    }
  }) {
    const {
      key,value
    } = dataset
    console.log(key,value);
    this.setData({
      [`form.${key}`]: value,
      sex:value
    })
    const {
      form
    } = this.data
    const {
      sex,
      selfCare,
      relationship,
      age
    } = form
    
    this.setData({
      isShow: sex && selfCare && relationship && age
    })
  },
  radioChange({
    currentTarget: {
      dataset
    }
  }) {
    const {
      value,key
    } = dataset
    this.setData({
      [`form.${key}`]: value,
      selfCare:value
    })
    const {
      form
    } = this.data
    const {
      sex,
      selfCare,
      relationship,
      age
    } = form
    this.setData({
      isShow: sex && selfCare && relationship && age
    })
  }
}

Page(pageOptions)