<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="projectName">
            <van-icon name="location-o" />
            &nbsp;{{ item.orderName }}
          </p>
          <p>
            <van-icon name="contact-o" />
            &nbsp;{{ item.clientName }}-{{ item.clientSubName}}
          </p>
          <p>
            <van-tag type="primary" size="medium" class="van-tag">{{ item.acreage ? item.acreage : '-' }} 平</van-tag>
            <van-tag type="success" size="medium" class="van-tag">{{ item.price ? item.price : '-' }} 元/平</van-tag>
            <van-tag color="#969799" size="medium" class="van-tag">{{ item.amount ? item.amount / 10000 : '-' }} 万元</van-tag>
          </p>
        </div>
      </van-list>
    </van-pull-refresh>
    <van-back-top right="10vw" bottom="10vh" />
  </div>
</template>

<script>
import { myOrder } from '@/api/personal';

export default {
  data() {
    return {
      loading: false,
      listData: [],
      finished: false,
      listQuery: {
        // 当前页数
        current: 1,
        // 查询关键字
        keyword: null,
      },
    };
  },
  created() {
    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,
        location: item.location,
        acreage: item.acreage,
        price: item.price,
        amount: item.amount / 10000,
        clientName: item.clientName,
        clientSubName: item.clientSubName,
        agent: item.agent,
        contactName: item.contactName,
        contactTel: item.contactTel,
        bailorA: item.bailorA,
        bailorB: item.bailorB,
        realAmount: item.realAmount,
        shouldAmount: item.shouldAmount,
        invoiceAmount: item.invoiceAmount,
        created: item.created
      };
      this.$router.push({
        path: `/index/personal/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>