import { API } from './../../../utils/network/api'
import { requestSubscribeMessage } from './../../../utils/requestSubscribeUtil';

const api = new API()
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '去提现', //导航栏 中间的标题
    },
    height: app.globalData.height,
    bank_card_id: "",
    static_host: api.getStaticHost(),
    bank_icon_path: api.getStaticHost()+"/applet/doctor/static/images/yishi/bankcard/js_icon.png",
    bank_card_name: "默认姓名",
    bank_name	: "xx银行",
    bank_card_code_mask: "888 xxxx xxxx 888",
    withdrawal_amount: "xxxx",
    income_tax: "xx",
    contactKeFu: false,
    sub_visible: false,
    skeleton_loading: true,
    order_inquiry_ids: [],
    sub_btn_disabled: false
  },
  onLoad(option){
    let order_inquiry_ids = option.order_inquiry_ids;
    let params = {};
    if(order_inquiry_ids){
      params.order_inquiry_ids = order_inquiry_ids;
      this.setData({
        "navbarData.back_url": "/Pages/yishi/my/index",
        "navbarData.back_url_type": 1,
      })
    }
    this.postDoctorWithdrawalInfo(params);
  },
  onShow(){
    this.setData({
      sub_btn_disabled: false
    })
  },
  go(e){
    let url = e.currentTarget.dataset.url;
    app.go(url);
  },
  contactKeFu(e){
    console.log("concatKeFu");
    let tmplIds = ['JMPmlYGh1HcUSuEfJCVQUIsZv4H5ar3QvXZfrNYNFuc']
    let sub_promise = requestSubscribeMessage(tmplIds)
    sub_promise.then(res => {
      console.log("订阅成功")
      this.setData({
        contactKeFu: true
      })
    }).catch(err => {
      console.log(err)
      let errCode = err.errCode
      if(errCode == 20004){
        this.setData({
          sub_visible: true,
        })
      }
    })
  },
  subcancelDialog(){
    this.setData({
      sub_visible: false
    })
  },
  subconfirmDialog(){
    this.setData({
      sub_visible: false
    })
    wx.openSetting({
      success (res) {
        console.log(res.authSetting)
      }
    })
  },
  postDoctorWithdrawalInfo(params){
    
    //获取提现数据
    api.postDoctorWithdrawalInfo(params).then(response => {
      console.log(response);
      if(response.data.bank.bank_card_id){
        this.setData({
          bank_card_id: response.data.bank.bank_card_id,
          bank_icon_path: response.data.bank.bank_icon_path,
          bank_name	: response.data.bank.bank_name,
          bank_card_code_mask: response.data.bank.bank_card_code_mask,
          withdrawal_amount: response.data.withdrawal_amount,
          order_inquiry_ids: response.data.order_inquiry_ids,
          bank_card_name: response.data.bank.bank_card_name_mask,
          income_tax: response.data.income_tax,
          skeleton_loading: false
        })
      }
    }).catch(errors => {
      console.error(errors);
      
    })
  },
  postDoctorWithdrawal(){
    this.setData({
      sub_btn_disabled: true
    })
    let params = {};
    params.order_inquiry_id = this.data.order_inquiry_ids.join(",");
    params.withdrawal_amount_total = this.data.withdrawal_amount;
    params.bank_card_id = this.data.bank_card_id;
    //获取提现数据
    api.postDoctorWithdrawal(params).then(response => {
      console.log(response);
      this.setData({
        withdrawal_amount: 0,
        income_tax: 0
      })
      wx.showToast({
        title: '提现申请成功',
        icon: "success"
      })
    }).catch(errors => {
      console.error(errors);
      this.setData({
        sub_btn_disabled: false
      })
    })
  }
})