import { fhGetNurseOrders } from "../../../api/hospital/index";
const baseUrl = require("../../../api/index.js").config;
const order = require("../../../api/order.js");
const { Loading, hLoading } = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentlist: [],
    waimaiOrderItemList: [],
    waitPayOrderItems: [],
    currentLo: null,
    currentLa: null,
    markers: null,
    scale: 12,
    currentflag: 1,
    historylist: [],
    pagesize: 10,
    pagenum: 1,
    statusToCNMap: {},
    typeToCNMap: {},
    refundlist: [],
    anflag: false,
    hasMore: true,
    scrollTop: 0,
    // 预订单
    // appointStatus:0,
    presentmark: true,
    historymark: true,
    refundmark: true,
    ruleflag: false,
    // new (这个属性为0时是周预定，1是外卖，2是护工预约)
    orderType: 0,
    payStatus: "",
    orderStatus: "all",
    // 菜单
    menu: [
      {
        label: "全部订单",
        type: "all",
      },
      {
        label: "待支付",
        type: "pay",
      },
      {
        label: "待评价",
        type: "preComment",
      },
      {
        label: "已退款",
        type: "refunds",
      },
      {
        label: "已取消",
        type: "cancelled",
      },
    ],
    // 查询条件
    query: {
      pageSize: 10,
      pageNumber: 1,
      orderType: 8
    },
    // 加载中
    loading: true,
    // 护工订单
    nurseOrder: [],
    // 是否下拉加载
    refresherTriggered: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.tempNavInfo = options;

    const memberId = wx.getStorageSync("memberId");
    this.memberId = memberId;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { from = "all", type = "shitang" } =
      this.tempNavInfo || wx.$getGlobalData("tempNavInfo") || {};

    const anflag = wx.getStorageSync("anflag");
    this.setData({
      anflag,
    });

    wx.setStorageSync("cart", "");

    if (!anflag) {
      type === 'shitang' || 'patient' ? this.getOrderList(from) : this.getNurseOrders(from);
      this.setData({
        orderStatus: from,
        orderType: type === 'shitang' ? 0 : (type === 'patient' ? 1 : 2),
      });
    } else {
      setTimeout(() => {
        this.setData({
          loading: false,
        });
      }, 1000 * 1.5);
    }
    this.tempNavInfo = null;
  },
  // 页面隐藏
  onHide() {
    wx.$setGlobalData("tempNavInfo", {
      from: this.data.orderStatus,
      type: this.data.orderType === 0 ? 'shitang' : (this.data.orderType === 1 ? 'patient' : 'nurse'),
    });
  },
  // 页面卸载
  onUnload() {
    wx.$setGlobalData("tempNavInfo", {
      from: this.data.orderStatus,
      type: this.data.orderType === 0 ? 'shitang' : (this.data.orderType === 1 ? 'patient' : 'nurse'),
    });
  },
  // 打电话
  ringUp(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    });
  },
  // 催单
  takeReminder(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var memberId = wx.getStorageSync("memberId");
    order.getReminder(
      baseUrl.waimaiUrl + "/client/" + memberId + "/order/" + id + "/reminder",
      {},
      {},
      "加载中",
      function (res) {
        if (res.code == 200) {
          wx.showToast({
            title: "催单成功",
            icon: "none",
          });
        }
      },
      function (err) {
        wx.showToast({
          title: "失败",
        });
      }
    );
  },
  // 下拉刷新
  refresherrefresh() {
    this.setData(
      {
        refresherTriggered: true,
      },
      () => {
        this.switchOrderStatus({
          currentTarget: {
            dataset: {
              type: this.data.orderStatus,
            },
          },
        });
      }
    );
  },
  // 触底加载
  reachBottom() {},
  // 授权
  takeAnflag() {
    wx.navigateTo({
      url: "/pages/authorization/authorization",
    });
  },
  // 切换类型
  orderTypeClick({ currentTarget: { dataset } }) {
    const { type } = dataset;
    const { orderType } = this.data;
    const queryOrderType = type === 0 ? 8 : (type === 1 ? 1 : null)
    this.setData(
      {
        orderType: type,
        ['query.orderType']: queryOrderType
      },
      () => {
        if (type !== orderType) {
          this.switchOrderStatus({
            currentTarget: { dataset: { type: "all" } },
          });
        }
      }
    );
  },
  // 获取订单列表
  switchOrderStatus({ currentTarget: { dataset } }) {
    const { orderType } = this.data;
    const { type } = dataset;
    this.setData({
      orderStatus: type,
    });
    orderType === 0 || orderType === 1 ? this.getOrderList(type) : this.getNurseOrders(type);
  },
  // 获取膳食订单列表
  getOrderList(type) {
    order.takeCurrent(
      `${baseUrl.waimaiUrl}/client/${this.memberId}/order/${type}`,
      this.data.query,
      {},
      "加载中",
      (res) => {
        const { results } = res;
        this.setData({
          currentlist: results,
          loading: false,
          refresherTriggered: false,
        });
      },
      (err) => {
        this.setData({
          loading: false,
        });
      },
      this.data.loading
    );
  },
  // 获取护工订单列表
  async getNurseOrders(type) {
    Loading();
    try {
      const { results } = await fhGetNurseOrders(type);
      this.setData({
        nurseOrder: results,
        loading: false,
        refresherTriggered: false,
      });
    } catch (e) {
      console.log("debug axios", e);
    }
    hLoading();
  },
});
