<!-- 订单详情 -->
<template>
  <s-layout :title="$t('order.invoice.title')" class="invoice-wrap" navbar="inner">
    <view
      class="invoice-heard ss-flex-col ss-row-right ss-col-center"
      :style="[
        {
          marginTop: '-' + Number(statusBarHeight + 88) + 'rpx',
          paddingTop: Number(statusBarHeight + 88) + 'rpx',
        },
      ]"
    >
      <view class="ss-flex ss-m-t-32 ss-m-b-32">
        <text
          class="sicon-warning-line"
          v-if="state.data.status === 'waiting' || state.data.status === 'unpaid'"
        ></text>
        <text class="sicon-check-line" v-if="state.data.status === 'finish'"></text>
        <view class="invoice-heard-title">{{ state.data.status_text }}</view>
      </view>
      <view class="ss-flex ss-m-b-52">
        <view class="ss-m-r-20 invoice-heard-desc">{{$t('order.invoice.expectedInvoiceMoney')}}：</view>
        <view class="invoice-heard-price">￥{{ state.data.amount }}</view>
      </view>
    </view>
    <view class="invoice-content ss-flex-col ss-col-center">
      <view class="ss-m-t-50 ss-m-b-42 invoice-content-title">{{$t('order.invoice.invoiceNormal')}}</view>
      <view class="ss-flex ss-m-b-64">
        <view v-for="(item, index) in state.info" :key="item.title">
          <view class="log-icon ss-flex">
            <text class="sicon-circlecheck" v-if="statusNum >= index"></text>
            <text class="sicon-unchecked" v-else></text>
            <view
              v-if="state.info.length - 1 != index"
              class="line"
              :class="statusNum >= index ? 'activity-color' : ''"
            ></view>
          </view>
          <view class="log-title">{{ item.title }}</view>
        </view>
      </view>
      <view class="invoice-content-list ss-flex ss-row-between ss-col-top">
        <view class="">
          <view class="ss-flex">
            <view class="list-title">{{$t('order.invoice.invoiceType')}}</view>
            <view class="list-desc">{{ state.data.type_text }}</view>
          </view>
          <view class="ss-flex">
            <view class="list-title">{{$t('order.invoice.invoice')}}</view>
            <view class="list-desc">{{ state.data.name }}</view>
          </view>
          <view class="ss-flex" v-if="state.data.type === 'company'">
            <view class="list-title">{{$t('order.invoice.taxId')}}</view>
            <view class="list-desc">{{ state.data.tax_no }}</view>
          </view>
          <view class="ss-flex" v-if="state.data.status === 'finish'">
            <view class="list-title">{{$t('order.invoice.actualMoney')}}</view>
            <view class="list-desc">￥{{ state.data.invoice_amount }}</view>
          </view>
          <view class="ss-flex" v-if="state.data.status === 'finish'">
            <view class="list-title">{{$t('order.invoice.invoiceTime')}}</view>
            <view class="list-desc">{{ state.data.finish_time }}</view>
          </view>
          <view class="ss-flex">
            <view class="list-title">{{$t('order.invoice.applyTime')}}</view>
            <view class="list-desc">{{ state.data.createtime }}</view>
          </view>
        </view>
        <view
          class="invoice-content-img ss-flex-col ss-col-center"
          v-if="state.data.status === 'finish'"
        >
          <su-image
            class="invoice-img"
            isPreview
            :previewList="state.jointImage"
            :current="0"
            :src="sheep.$url.static('/assets/addons/shopro/uniapp/order/invoice_thumb.png')"
            :height="110"
            mode="scaleToFill"
            v-if="state.jointImage[0].substr(-4) != '.pdf'"
          ></su-image>
          <!-- TODO: 发票为多个pdf时 -->
          <view v-if="state.jointImage[0].substr(-4) == '.pdf'" @tap="onInvoice">
            <image
              :src="sheep.$url.static('/assets/addons/shopro/uniapp/order/invoice_thumb.png')"
              class="invoice-img"
            ></image>
          </view>
          <view class="invoice-img-num">{{$t('order.invoice.total')}}{{ state.numImage }}{{$t('order.invoice.unit')}}</view>
          <view class="invoice-img-title">{{$t('order.invoice.previewInvoice')}}</view>
        </view>
      </view>
    </view>
    <view class="invoice-order ss-m-t-20">
      <view class="goods-box" v-for="item in state.data.order_items" :key="item.id">
        <s-goods-item
          :img="item.goods_image"
          :title="item.goods_title"
          :skuText="item.goods_sku_text"
          :price="item.goods_price"
          :num="item.goods_num"
        />
      </view>
      <view class="invoice-order-list">
        <view class="ss-flex">
          <view class="list-title">{{$t('order.invoice.orderStatus')}}</view>
          <view class="list-desc">{{ state.data.order?.status_text }}</view>
        </view>
        <view class="ss-flex">
          <view class="list-title">{{$t('order.invoice.orderNo')}}</view>
          <view class="list-desc">{{ state.data.order?.order_sn }}</view>
        </view>
        <view class="ss-flex">
          <view class="list-title">{{$t('order.invoice.orderTime')}}</view>
          <view class="list-desc">{{ state.data.order?.createtime }}</view>
        </view>
      </view>
    </view>
  </s-layout>
</template>

<script setup>
  import sheep from '@/sheep';
  import { onLoad } from '@dcloudio/uni-app';
  import { computed, reactive } from 'vue';

  const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
  const headerBg = sheep.$url.css('/assets/addons/shopro/uniapp/order/invoice_bg.png');
  const state = reactive({
    info: [
      {
        title: $t('order.invoice.submitOrder'),
      },
      {
        title: $t('order.invoice.waitInvoice'),
      },
      {
        title: $t('order.invoice.finishInvoice'),
      },
    ],
    data: {},
    jointImage: [],
    numImage: 0,
  });
  const statusNum = computed(() => {
    if (state.data.status === 'finish') {
      return 2;
    } else if (state.data.status === 'waiting') {
      return 1;
    } else {
      return 0;
    }
  });
  function onInvoice() {
    // #ifdef H5
    window.open(state.jointImage);
    // #endif
    // #ifdef MP || APP-PLUS
    uni.downloadFile({
      url: state.jointImage[0],
      success: function (res) {
        var filePath = res.tempFilePath;
        uni.openDocument({
          filePath: filePath,
          showMenu: true,
          success: function (res) {
            console.log($t('order.invoice.openDocSuc'));
          },
        });
      },
    });
    // #endif
  }
  async function getInvoiceDetail(id) {
    const { data } = await sheep.$api.order.invoice(id);
    state.data = data;
    state.data.download_urls?.forEach((i, index) => {
      state.numImage = index + 1;
      if (i.substr(-4) != '.pdf') {
        state.jointImage.push(sheep.$url.static(i));
      } else {
        state.jointImage.push(sheep.$url.static(i));
      }
    });
  }
  onLoad((options) => {
    getInvoiceDetail(options.invoiceId);
  });
</script>

<style lang="scss" scoped>
  .invoice-heard {
    width: 100%;
    box-sizing: border-box;
    background: v-bind(headerBg) no-repeat,
      linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
    background-size: 750rpx 100%;
    .sicon-warning-line {
      color: #fff;
      font-size: 34rpx;
    }
    .sicon-check-line {
      color: #fff;
      font-size: 34rpx;
    }
    .invoice-heard-title {
      font-size: 34rpx;
      font-weight: 500;
      color: #ffffff;
      margin-left: 8rpx;
      line-height: normal;
    }
    .invoice-heard-desc {
      font-size: 24rpx;
      font-weight: 500;
      color: #ffffff;
    }
    .invoice-heard-price {
      font-size: 28rpx;
      font-family: OPPOSANS;
      font-weight: 500;
      color: #ffffff;
    }
  }
  .invoice-content {
    width: 100%;
    position: relative;
    z-index: 3;
    background: #ffffff;
    border-radius: 20rpx;
    margin-top: -16rpx;
    .invoice-content-title {
      font-size: 30rpx;
      font-weight: 500;
      color: #333333;
    }
    .log-icon {
      .sicon-unchecked {
        color: #c2bec2;
        font-size: 44rpx;
      }
      .sicon-circlecheck {
        color: #e60a00;
        font-size: 44rpx;
      }
      .line {
        width: 158rpx;
        height: 6rpx;
        background: #f2f2f2;
        border: 2rpx solid #ffffff;
      }
      .activity-color {
        background: #e60a00;
      }
    }
    .log-title {
      font-size: 26rpx;
      font-weight: 500;
      color: #333333;
      margin-left: -26rpx;
      margin-top: 30rpx;
    }
    .invoice-content-list {
      width: 100%;
      padding: 0 46rpx 0 30rpx;
      box-sizing: border-box;
    }
    .list-title {
      font-size: 26rpx;
      font-weight: 500;
      color: #999999;
      margin-right: 44rpx;
      margin-bottom: 36rpx;
    }
    .list-desc {
      font-size: 26rpx;
      font-weight: 500;
      color: #333333;
      margin-bottom: 36rpx;
    }
    .invoice-img {
      width: 200rpx;
      height: 110rpx;
    }
    .invoice-img-num {
      width: 216rpx;
      height: 40rpx;
      background: rgba(#000000, 0.45);
      font-size: 24rpx;
      font-weight: 500;
      color: #ffffff;
      text-align: center;
      margin-top: -30rpx;
      z-index: 1;
    }
    .invoice-img-title {
      font-size: 24rpx;
      font-weight: 500;
      color: #999999;
    }
  }
  .invoice-order {
    width: 100%;
    padding-top: 30rpx;
    box-sizing: border-box;
    background: #fff;
    border-radius: 20rpx;
  }
  .goods-box {
    border-bottom: 2rpx solid #dfdfdf;
  }
  .invoice-order-list {
    padding: 40rpx 24rpx 0 24rpx;
    .list-title {
      font-size: 26rpx;
      font-weight: 500;
      color: #999999;
      margin-right: 44rpx;
      margin-bottom: 36rpx;
    }
    .list-desc {
      font-size: 26rpx;
      font-weight: 500;
      color: #333333;
      margin-bottom: 36rpx;
    }
  }
</style>
