<template>
  <view class="invoice-wrap" :class="[`${themeInfo.theme}-theme`]">
    <navBar></navBar>

    <view class="wrapBox">
      <view class="top">
        <view class="arrow">
          <image src="/static/setOrder/selectArrow.png" mode=""></image>
        </view>
        {{ $t("invoice.valid") }}
      </view>
      <view class="content">
        <view class="orderBox">
          <uv-checkbox-group v-model="state.checkList">
            <!-- @click="goInvoiceApply(item)" -->
            <view
              class="orderLi"
              v-for="(item, index) in state.list"
              :key="index"
            >
              <view class="leftCheckBox">
                <uv-checkbox activeColor="black" :name="item" shape="circle">
                </uv-checkbox>
              </view>
              <view class="left">
                <view>{{ $t("invoice.pay") }}: {{ item.paymentTime }}</view>
                <view>{{ $t("invoice.site") }}: {{ item.siteName }}</view>
                <view>{{ $t("invoice.type") }}: {{ item.unitTypeName }}</view>
                <view
                  >{{ $t("invoice.rent") }}:
                  {{ item.termOfLease?.match(/\d{4}-\d{2}-\d{2}/g)[0] }} - {{
                    item.termOfLease?.match(/\d{4}-\d{2}-\d{2}/g)[1]
                  }}</view
                >
              </view>
              <view class="right">
                <view>{{ item.lockerName }}</view>
                <view>{{ currency }}{{ item.money }}</view>
              </view>
            </view>
          </uv-checkbox-group>
        </view>
        <button class="contact" @click="goInvoiceApplyforRecord">
          <uv-icon
            name="file"
            custom-prefix="custom-icon"
            size="16"
            :color="themeInfo.theme === 'default' ? '#045459' : '#0F2232'"
          ></uv-icon>
          <text style="margin-left: 8px">{{ $t("invoice.record") }}</text>
        </button>
      </view>
      <view class="bottom">
        <!-- <text
          >已选 {{ state.checkList.length }} 个订单，共
          {{ checkRowMoney }} 元</text
        > -->
        <text>{{ $t("invoice.order", { number: state.checkList.length, money: checkRowMoney }) }}</text>
        <text>{{ $t("invoice.tip") }}</text>
      </view>
      <view class="goApply">
        <view class="checlBox">
          <uv-checkbox-group @change="selectAll">
            <uv-checkbox
              activeColor="black"
              name="all"
              shape="circle"
              :label="$t('invoice.allSelect')"
            >
            </uv-checkbox>
          </uv-checkbox-group>
        </view>
        <view class="next">
          <button size="large" @click="goInvoiceApply">{{ $t("invoice.nextStep") }}</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
// 主题色配置
import { reactive, computed } from "vue";
import { useMainStore } from "@/store/index.js";
const { themeInfo } = useMainStore();
import navBar from "@/components/navBar.vue";
import { useInvoiceApi } from "@/Apis/invoice";
import { onShow } from "@dcloudio/uni-app";
import { useI18n } from "vue-i18n";
import { currency } from "@/config/index.js";
const { t } = useI18n();

const getApi = useInvoiceApi();
const state = reactive({
  list: [],
  checkList: [],
});
const selectAll = (e) => {
  if (e.length) {
    state.checkList = state.list;
  } else {
    state.checkList = [];
  }
};
const checkRowMoney = computed(() => {
  return state.checkList.reduce((pre, cur) => {
    return pre + Number(cur.money);
  }, 0).toFixed(2);
});
const goInvoiceApply = (row) => {
  if(state.checkList.length == 0){
    uni.showToast({
      title: t("invoice.selectOrder"),
      icon: "none",
    });
    return
  }
  if (checkRowMoney.value <= 0) {
    uni.showToast({
      title: t("invoice.validMoney"),
      icon: "none",
    });
    return;
  }
  const ids = state.checkList.map((item) => item.paymentId);
  uni.navigateTo({
    url: `/pagesb/invoiceApply/index?paymentId=${ids}&num=${checkRowMoney.value}`,
  });
  // 跳转后 清空选择
  state.checkList = [];
};
const getDataList = () => {
  uni.showLoading();
  getApi.GetCanInvoiceList().then((res) => {
    uni.hideLoading();
    if (res.code == 200) {
      state.list = res.data
    } else {
      state.list = [];
    }
  });
};
const goInvoiceApplyforRecord = ()=>{
  uni.navigateTo({
    url: "/pagesb/invoiceApplyforRecord/index",
  });
}
onShow(() => {
  getDataList();
});
</script>

<style lang="scss" scoped>
@import '@/static/style/theme.scss';
.invoice-wrap {
  height: 100vh;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(
    to bottom,
    var(--right-linear),
    var(--left-linear)
  );
  .wrapBox {
    flex: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow: auto;
    .top {
      height: 100rpx;
      line-height: 100rpx;
      font-size: 26rpx;
      font-weight: 600;
      padding: 0 40rpx;
      display: flex;
      align-items: center;
      .arrow {
        width: 20rpx;
        display: flex;
        margin-right: 6rpx;
        image {
          width: 20rpx;
          height: 12rpx;
        }
      }
    }
    .content {
      flex: 1;
      width: 100%;
      padding: 20rpx;
      padding-bottom: 200px;
      overflow-y: auto;
      background-color: #fff;
      position: relative;
      .contact {
        position: fixed;
        bottom: 300rpx;
        right: 0;
        height: 74rpx;
        background-color: var(--main-color);
        border-radius: 45rpx 0rpx 0rpx 45rpx;
        border: 4rpx solid var(--stress-text);
        box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(0, 0, 0, 0.1);
        padding: 0 20rpx 0 30rpx;
        z-index: 9;
        display: flex;
        justify-content: center;
        align-items: center;
        color: var(--stress-text);
        font-size: 28rpx;
        font-weight: 700;
      }
      .orderBox {
        padding: 20rpx;
        .orderLi {
          width: 100%;
          margin-bottom: 30rpx;
          border-radius: 18rpx;
          padding: 20rpx;
          font-size: 24rpx;
          background-color: var(--main-color);
          display: flex;
          .leftCheckBox {
            width: 80rpx;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          .left {
            flex: 1;
          }
          .right {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            width: 150rpx;
            :nth-child(1) {
              border-radius: 99rpx;
              padding: 0 20rpx;
              height: 40rpx;
              line-height: 40rpx;
              background-color: black;
              color: var(--main-color);
              margin-bottom: 10rpx;
            }
            :nth-child(2) {
              font-size: 30rpx;
            }
          }
        }
      }
    }
    .bottom {
      height: 100rpx;
      font-size: 24rpx;
      padding: 0 40rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: var(--stress-color);

      text:first-of-type {
        margin-right: 10px;
      }

      text:last-of-type {
        margin-left: 10px;
      }
    }
    .goApply {
      height: 130rpx;
      display: flex;
      justify-content: space-between;
      padding: 10upx 40upx;
      align-items: center;
      .next {
        button {
          background-color: black;
          color: #fff;
          font-size: 28rpx;
          border-radius: 99rpx;
        }
      }
    }
  }
}
</style>
