import {
const __staticUrl = require('../../../config/env.js').static_url
  pageTitle,
  tabsList
} from './constant';
import {
  doNavigateWithUser
} from '../../../utils/helper';
import {
  postUrl,
} from '../../../utils/util';
import { apiArr } from '../../../api/user';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: null,
    checked: 0,
    allList: [{
        number: "B123456",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '白色乳胶记忆枕',
        amount: 45,
        price: 60,
        order_id: 48,
        state: '待取货',
        states: 'c'
      },
      {
        number: "A123456",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '黑色涤纶裤子',
        amount: 95,
        price: 120,
        state: '待接单',
        states: 'b'
      },
      {
        number: "C9744123",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '紫色抱枕',
        amount: 600,
        price: 600,
        state: '配送中',
        states: 'd'
      },
      {
        number: "B123456",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '白色乳胶记忆枕',
        amount: 45,
        price: 60,
        state: '退款中',
        states: 'e'
      },
      {
        number: "A123456",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '黑色涤纶裤子',
        amount: 95,
        price: 120,
        state: '已退款',
        states: 'f'
      },
      {
        number: "C9744123",
        icon: (__staticUrl + '/User/_assets/Classroom.png'),
        title: '紫色抱枕',
        amount: 600,
        price: 600,
        state: '待接单',
        states: 'b'
      }
    ],
    list: [],
    type: '',
    size: 10,
    number: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('1111', options);
    // 基于父页面传递参数设置页面名称
    wx.setNavigationBarTitle({
      title: `${pageTitle[options.type]}订单`
    });
    this.setData({
      type: options.type,
      tabs: tabsList[options.type],
      title: pageTitle[options.type],
    });
    // this.init(options.type);
  },

  // 页面初始化 请求list 数据
  init(type, pageNum) {
    const { list, checked } = this.data;
    wx.showLoading({
      title: '加载中',
    })
    let url;
    if (type === 'visit') {
      url = apiArr.getOrdersAllList
    } else if ( type === 'community') {
      url = '周边商超'
    } else if (type === 'ambient') {
      url = '社区商城'
    } else {
      wx.hideLoading();
      console.log('货不对板');
      return;
    }
    let params = {}
    if (checked !== 0) {
      params = {
        page_size: Number(10),
        page_num: pageNum || 1,
        status: checked
      }
    } else {
      params = {
        page_size: Number(10),
        page_num: pageNum || 1,
      }
    }
    postUrl(url, params, res => {
      console.log('上门服务', res);
      wx.hideLoading();
      this.setData({
        list: list.concat(res.rows || []),
      })
      if (!res.rows) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none',
        });
      }
    })
  },


  onChange(event) {
    const { name } = event.detail;
    const { type } = this.data;
    this.setData({
      list: [],
      number: 1,
      checked: name
    });
    this.init(type);
  },

  // 取消订单
  handleOrdeClick(e) {
    const { type } = this.data;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    postUrl(apiArr.revoke, {
      order_id: Number(e.detail)
    },
     res => {
       wx.hideLoading();
      if (res.error) {
        wx.showToast({
          title: '取消订单失败',
          icon: 'none',
        })
        return;
      }
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
      this.init(type, 1);
    })
  },

  // 订单详情
  handleDetailsClick(e) {
    console.log('详情事件触发', e);
    doNavigateWithUser({
      url: `/packages/user/transactionDetail/index?orderId=${e.detail.id}&ordersn=${e.detail.ordersn}`
    });
  },

  // 去付款
  handlePaymentClick(e) {
    console.log('去付款事件触发',e)
    const {name, tel, region, addr, order_id} = e.detail;
    // 获取当前点击付款项的地址参数
    const addAress = {
      real_name: name,
      mobile: tel,
      optionAddr: `${region}${addr}`,
    }
    console.log('addAressaddAressaddAress', addAress);
    wx.setStorageSync('newAddress', addAress);
    doNavigateWithUser({
      url: `/packages/user/goPay/index?orderId=${order_id}`
    });

  },


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

},

/**
 * 生命周期函数--监听页面显示
 */
onShow(options) {
  const { type } = this.data;
  this.setData({
    list: [],
    number: 1,
  })
  this.init(type);
},

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

},

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

},

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

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom() {
  const { type, size, number } = this.data;
  this.setData({
    number: number + 1,
  });
 this.init(type, number + 1);
},

/**
 * 用户点击右上角分享
 */
onShareAppMessage() {

}
})