<template>
  <div>
    <BackBar title="我的订单-资产" lefttext="返回" />
    <van-search v-model="listQuery.keyWord" placeholder="请输入搜索关键词" @search="onSearch()" />
    <van-pull-refresh v-model="loading" @refresh="onRefresh()">
      <van-list v-model:loading="loading" :finished="finished" finished-text="还没有订单" @load="getMyOrder()">
        <div class="card" v-for="item in listData" :key="item.id" @click="toDetail(item)">
          <h3>
            <van-icon name="link-o" />
            &nbsp;{{ item.orderId }}
          </h3>
          <p class="productionNo" v-if="item.statementNo || item.reportNo">
            <van-icon name="coupon-o" />
            &nbsp;
            {{ item.statementNo }}
            <!-- 产品号都不为空才显示中间的分割空格, 以保持页面间距统一 -->
            <span v-if="item.statementNo && item.reportNo">&nbsp;&nbsp;</span>
            {{ item.reportNo }}
          </p>
          <p class="projectName">
            <van-icon name="location-o" />
            &nbsp;{{ item.reportName || item.statementName || item.name }}
          </p>
          <p>
            <van-tag type="primary" size="medium" class="van-tag">{{ item.currentNodeName }}</van-tag>
            <van-tag type="success" size="medium" class="van-tag">{{ item.clientManagerName }}</van-tag>
            <van-tag color="#969799" size="medium" class="van-tag">{{ item.created }}</van-tag>
          </p>
        </div>
      </van-list>
    </van-pull-refresh>
    <van-back-top right="10vw" bottom="10vh" />
  </div>
</template>

<script>
import { mapStores } from 'pinia';
import { useUserStore } from '@/stores/useUserStore';
import { myOrder } from '@/api/assets';

export default {
  data() {
    return {
      loading: false,
      listData: [],
      finished: false,
      listQuery: {
        // 当前页数
        current: 1,
        // 查询关键字
        keyWord: null,
        clientManagerId: null,
      },
    };
  },
  computed: {
    ...mapStores(useUserStore),
  },
  created() {
    this.listQuery.clientManagerId = this.userStore.userInfo.id;
    this.getMyOrder();
  },
  methods: {
    // 列表刷新
    onRefresh() {
      this.listQuery.current = 1;
      this.finished = false;
      this.listData = [];
      this.getMyOrder();
    },
    // 列表关键字搜索
    onSearch() {
      this.listQuery.current = 1;
      this.finished = false;
      this.listData = [];
      this.getMyOrder();
    },
    // 获取我的订单
    getMyOrder() {
      this.loading = true;
      myOrder(this.listQuery).then((res) => {
        if (res.code == 200) {
          if (res.data.records) {
            this.listData = this.listData.concat(res.data.records);
          }
          // 判断是否还有下一页
          if (res.data.pages > this.listQuery.current) {
            // 服务端返回的总页数大于当前页数, 将当前页数+1
            this.listQuery.current++;
          } else {
            // 反之停止加载
            this.finished = true;
          }
        }
        this.loading = false;
      });
    },
    toDetail(item) {
      const detailInfo = {
        orderId: item.orderId,
        name: item.name,
        statementNo: item.statementNo,
        reportNo: item.reportNo,
        assetsBusinessGener: item.assetsBusinessGener,
        customerName: item.customerName,
        customerSubName: item.customerSubName,
        bailor: item.bailor,
        bailorContactTel: item.bailorContactTel,
        currentNodeName: item.currentNodeName,
        handlerName: item.handlerName,
        principalName: item.principalName,
        clientManagerName: item.clientManagerName,
        estimatedValue: item.estimatedValue,
        productionShouldAmount: item.productionShouldAmount,
        productionRealAmount: item.productionRealAmount,
        invoiceRealAmount: item.invoiceRealAmount,
        reportDelivery: item.reportDelivery ? '已送达' : '未送达',
      };
      this.$router.push({
        path: `/index/assets/detail`,
        query: {
          item: JSON.stringify(detailInfo),
        },
      });
    },
  },
};
</script>

<style scoped>
.card {
  background-color: white;
  border-radius: 5px;
  padding: 16px;
  margin: 10px;
}

.productionNo {
  width: 100%;
  font-size: 14px;
}

.projectName {
  width: 100%;
}

.van-tag {
  margin: 2px;
}
</style>