// pages/payInfo/payInfo.js
const app = getApp()
import {
  payPrescription
} from "../../../api/prescription"
import {createProduct} from "../../../api/medinceOrder"
import {throttle} from "../../../utils/util"
import {client_type} from "../../../utils/isPc"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_prescription_id: '',
    address_id:'',
    product_ids:[],
    btnLock:false,
    prevData:'',
    fromType:'',
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    order: {

    }
  },
  goAddress(event) {
    let address_id = event.currentTarget.dataset.id;
    app.method.navigateTo({
      url: '/patient/pages/address/address?address_id=' + address_id + "&order_prescription_id=" + this.data.order_prescription_id
    })
  },
  goAddAddress:throttle(function() {
    app.method.navigateTo({
      url: '/patient/pages/address/address?address_id=add&order_prescription_id='+ this.data.order_prescription_id
    })
  }),
  handleThrottle:throttle(function(){
    this.handleCreateProduct()
  }),
    //创建药品支付订单
    handleCreateProduct(){
      let {order_prescription_id,address_id,product_ids,fromType}=this.data;
    
      if(!address_id){
        wx.showToast({
          title:'请选择收货地址',
          icon:'none'
        })
        return false
      }
      this.setData({
        btnLock:true
      })
      createProduct({
        order_prescription_id,
        address_id,
        product_ids:product_ids,
        client_type:client_type
      }).then(data=>{
        let {inquiry_no,order_product_id}=data;
        this.setData({
          btnLock:false
        });
        this.setData({
          fromType:encodeURIComponent('patient/pages/medinceOrder/medinceOrder')
        })
        // if(fromType){
        //   this.setData({
        //     fromType:encodeURIComponent('pages/medinceOrder/medinceOrder')
        //   })
        // }
        app.method.navigateTo({
          url: '/patient/pages/payOrder/payOrder?inquiry_no='+ inquiry_no +"&order_product_id="+order_product_id+"&order_type=2&order_prescription_id="+order_prescription_id+"&fromType="+this.data.fromType
        })
      })
    },
  handlepayPrescription(id, address) {
    payPrescription(id).then(data => {
      this.setData({
        order: data
      });
      let product_ids=[];
      let product=data.order_prescription_product;
      product.forEach((item)=>{
        product_ids.push(item.product_id)
      })
      this.setData({
        address_id:data.user_ship_address?data.user_ship_address.address_id:'',
        product_ids:product_ids
      });
      if(!data.user_ship_address){
        this.setData({
          prevData:'',
          'order.user_ship_address':null,
          address_id:''
        })
      }else{
        if (address) {
          let addressInfo = JSON.parse(address);
          this.setData({
            'order.user_ship_address.province': addressInfo.province,
            'order.user_ship_address.city': addressInfo.city,
            'order.user_ship_address.county': addressInfo.country,
            'order.user_ship_address.address': addressInfo.address,
            'order.user_ship_address.consignee_name': addressInfo.name,
            'order.user_ship_address.consignee_tel': addressInfo.phone,
            'order.user_ship_address.address_id': addressInfo.address_id,
            address_id:addressInfo.address_id
          })
        }
      }
      
    })
  },
  goPayOrder:throttle(function() {
    let order_prescription_id = this.data.order_prescription_id;
    app.method.navigateTo({
      url: '/patient/pages/payOrder/payOrder?order_prescription_id='+order_prescription_id + "&order_type=2"
    })
  }),
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.order_prescription_id;
    if(options.fromType){
      this.setData({
        fromType:options.fromType
      })
    };
    let address = options.address;
    this.setData({
      order_prescription_id: id
    })
    this.handlepayPrescription(id, address);
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let {prevData,order_prescription_id}=this.data;
    this.setData({
      img_host:app.hostConfig().imghost
    })
   if(prevData){
    this.handlepayPrescription(order_prescription_id,prevData);
   }
  },

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

  },

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

  },

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

  },

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

  },

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