import { fhRefundNurseOrders } from "../../../api/hospital/index";
const baseUrl = require("../../../api/index.js").config;
const order = require("../../../api/order.js");
const { Tips } = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderId: "",
    phone: "",
    foodlist: [],
    storename: "",
    totalPrice: "",
    showreason: false,
    systemReason: "其他",
    reasonlist: [
      { name: "物流配送时间过长", reasonflag: false },
      { name: "商家通知我卖完了", reasonflag: false },
      { name: "商家沟通态度差", reasonflag: false },
      { name: "骑手提前点确认送达", reasonflag: false },
      { name: "骑手沟通态度差", reasonflag: false },
      { name: "餐品撒漏/包装破损", reasonflag: false },
      { name: "商家错送漏送", reasonflag: false },
      { name: "口感不佳/个人感受不好", reasonflag: false },
      { name: "餐品内有异物", reasonflag: false },
      { name: "食用后引起身体不适", reasonflag: false },
      { name: "商品变质/有异味", reasonflag: false },
      { name: "其他", reasonflag: false },
    ],
    info: {},
    orderType: "",
    // 退款loading
    refundLoading: false,
    // 退款类型选择
    radio: '1',
    // 手动输入的退款金额
    refundNumber: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;

    const { type, orderId, storePhone, storeName, totalPrice } = e;

    const options = wx.getStorageSync("refundData") || {};
    const { refundPrice } = options;
    const waitToApplyForRefundPrice = totalPrice - (refundPrice || 0);

    that.setData(
      {
        orderId: orderId,
        phone: storePhone,
        foodlist: options.foodItemVoList,
        storename: storeName,
        totalPrice: totalPrice,
        info: { ...options, waitToApplyForRefundPrice },
        orderType: type,
      },
      () => {
        wx.removeStorageSync("refundData");
      }
    );
  },
  ringUp: function (e) {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phone,
    });
  },
  refer: function (e) {
    if (this.data.refundLoading) return;

    var that = this;
    var word = e.detail.value.word;
    this.tempReason = word;

    if (!word) {
      Tips("请输入申请退款原因");
      return;
    }

    this.setData({
      refundLoading: true,
    });

    var memberId = wx.getStorageSync("memberId");
    this.data.orderType === "shitang" || this.data.orderType === "patient"
      ? order.takeRefund(
        baseUrl.waimaiUrl +
        "/client/" +
        memberId +
        "/order/" +
        that.data.orderId +
        "/refund",
        {},
        { refundReason: word, systemReason: that.data.systemReason },
        "加载中",
        function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: "提交成功",
              icon: "none",
              duration: 1000,
              mask: true,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  wx.reLaunch({
                    url: "/order/list/index",
                  });
                }, 1300); //延迟时间
              },
            });
          }
        },
        function (err) {
          wx.showToast({
            title: "失败",
          });
        },
        true
      )
      : this.refundNurseOrders();
  },
  takeReason: function () {
    var that = this;
    that.setData({
      showreason: true,
    });
  },
  goReason: function (e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    for (var i = 0; i < that.data.reasonlist.length; i++) {
      that.data.reasonlist[i].reasonflag = false;
    }
    that.data.reasonlist[index].reasonflag = true;
    that.setData({
      reasonlist: that.data.reasonlist,
      systemReason: that.data.reasonlist[index].name,
      showreason: false,
    });
  },
  hideReason: function (e) {
    var that = this;
    that.setData({
      showreason: false,
    });
  },
  // 选择退款类型
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    console.log('我是event', event)
  },
  // 手动输入的金额变化
  onInputChange({ detail }) {
    console.log('detail', detail)
    const { value } = detail
    this.setData({
      refundNumber: value
    })
  },
  // 申请退款
  async refundNurseOrders() {
    try {
      await fhRefundNurseOrders(this.data.orderId, {
        refundReason: this.tempReason,
        systemReason: this.data.systemReason || "其他",
      });
      Tips("申请退款成功");
      setTimeout(function () {
        wx.$routerTo({
          url: "/order/list/index",
          type: "reLaunch",
        });
      }, 1000); //延迟时间
    } catch (e) {
      console.log("debug axios", e);
    }
  },
});
