import { API } from './../../../utils/network/api'
import Toast from 'tdesign-miniprogram/toast/index';
let api = new API()
const app = getApp()
Page({

  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '价格设置', //导航栏 中间的标题
    },
    inquiry_type: 1,
    inquiry_mode: 1,
    info:{
      is_open: false,
      inquiry_price: 0,
      work_num_day: 0
    },
    myprice: "",
    config:{
      "max_work_num_day": 30,
      "min_inquiry_price": 0,
      "max_inquiry_price": 0,
      "default_inquiry_price": 0,
      "times_number": 10,
      "duration": 1440,
      "system_inquiry_price": [
        "0",
        "5",
        "10"
      ]
    }
  },
  onShow(){
    let params = {};
    params.inquiry_type = this.data.inquiry_type;//接诊类型（1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药）
    params.inquiry_mode = this.data.inquiry_mode;//接诊方式（1:图文 2:视频 3:语音 4:电话 5:会员）
    //获取医生我的账户数据
    api.getDoctorInquiryConfig(params).then(response => {
      console.log(response);
      this.setData({
        info: response.data.info,
        config: response.data.config,
        note: response.data.info.is_open == 1? this.data.open_note : this.data.close_note
      })
    }).catch(errors => {console.error(errors);})
  },
  onStepperChange(event) {
    console.log(event.detail);
    wx.vibrateShort({
      "type": "heavy"
    })
    this.setData({ 
      "info.work_num_day": event.detail 
    });
  },
  onPriceChange(e){
    console.log(e.detail);
    let price = e.detail.value;
    this.setData({
      "info.inquiry_price": price
    })

  },
  myToast(message){
    Toast({
      context: this,
      selector: '#t-toast',
      message: message,
      theme: 'error',
      direction: 'column',
    });
  },
  checkPrice(price){
    let value = Number(price);
    let min = Number(this.data.config.min_inquiry_price);
    let max = Number(this.data.config.max_inquiry_price);
    if(value < min){
      value = min;
      let message = '价格不能低于' + min + '元';
      this.myToast(message);
      return false;
    }else if(value >= min && value <=max ){
      return true;
    }else{
      value = max;
      let message = '价格不能高于' + max + '元';
      this.myToast(message);
      return false;
    }
  },
  async updateConfig(){
    let check = this.checkPrice(this.data.info.inquiry_price);
    console.log("checkcheckcheck:", check);
    if(!check){
      return;
    }
    //修改医生问诊配置
    let params = {};
    params.inquiry_type = this.data.inquiry_type;//接诊类型（1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药）
    params.inquiry_mode = this.data.inquiry_mode;//接诊方式（1:图文 2:视频 3:语音 4:电话 5:会员）
    params.inquiry_price = this.data.info.inquiry_price;
    params.work_num_day = this.data.info.work_num_day;
    const result = await api.putDoctorInquiryConfig(params).then(response => {
     console.log(response);
     this.myToast("修改成功");
     return true;
    }).then(res => {
      setTimeout(() => {
        wx.navigateBack();
        
      }, 1000);
    }).catch(errors => {
      console.error(errors);
      return false;
    })

    console.log("updateConfig result result: ", result);
    return result;
  }
})