import history from '../../utils/history';
import { confirmReceiveDelivery } from '../order-detail/service';
Component({
  props: {
    componentData: {},
    item: {}
  },

  didMount() {},

  data: {
    deadLine: 0,
    deliveryInfoBtn: [{
      text: '我知道了',
      extClass: 'deliver',
      type: 'cancel'
    }],
    deliveryInfoCopyBtn: [{
      text: '我知道了',
      extClass: 'deliver',
      type: 'cancel'
    }, {
      text: '复制单号',
      extClass: 'deliver solid',
      type: 'confirm'
    }],
    confirmReceiveBtn: [{
      text: '取消',
      extClass: 'deliver',
      type: 'cancel'
    }, {
      text: '确认',
      extClass: 'deliver solid',
      type: 'confirm'
    }]
  },
  methods: {
    makePhoneCall() {
      const {
        contactMobile = ''
      } = this.props.item.shop;

      if (!contactMobile) {
        return my.showToast({
          type: 'none',
          content: '暂无联系方式'
        });
      }

      my.makePhoneCall({
        number: contactMobile
      });
    },

    navigateToDetail() {
      history.push({
        title: '订单详情',
        pageType: 'mallOrderDetail',
        query: {
          orderId: this.props.item.orderId
        },
        header: ''
      });
    },

    // 点击底部按钮
    handleBtnClick(e) {
      const tempObj = {
        deliveryInfo: () => {
          const {
            item
          } = this.props;
          my.setClipboard({
            text: item.deliveryInfo.logisticsNo,
            success: () => {
              my.showToast({
                content: '复制成功'
              });
            }
          });
        },
        confirmReceive: async () => {
          await this.onConfirmReceiveClick();
          this.props.onQueryFilter && this.props.onQueryFilter();
        }
      };
      tempObj[e] && tempObj[e]();
    },

    async handleConfirmReceive() {
      await this.onConfirmReceiveClick();
    },

    // 确认收货
    async onConfirmReceiveClick() {
      const {
        item
      } = this.props;
      const {
        goods = [],
        orderId = ''
      } = item;
      my.confirm({
        title: '确认收货',
        content: `当前订单共${goods.length}件商品,请收到实物无误后再确认收货`,
        success: async res => {
          if (res.confirm) {
            try {
              const flag = await confirmReceiveDelivery({
                orderId
              });
              my.showToast({
                content: flag ? '收货成功' : '收货失败',
                type: 'none'
              });
              this.props.onQueryFilter && this.props.onQueryFilter();
            } catch (err) {
              my.showToast({
                content: '收货失败',
                type: 'none'
              });
            }
          }
        }
      });
    }

  }
});