<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import {
  amountTakingListsApi,
  financialRecordListsApi,
} from "@/api/merchant.js";
import { ElMessage } from "element-plus";
import mitt from "@/utils/mitt.js";
import moment from "moment";

const where = ref({
  page_no: 1,
  page_size: 15,
  loadend: false,
  loading: false,
  create_time: "",
  start_time: "",
});
const changeDate = () => {
  where.value.page_no = 1;
  where.value.loadend = false;
  where.value.loading = false;
  goods_list.value = [];
  getOrderList();
};

const listType = ref(1);
const goods_list = ref([]);
const getOrderList = () => {
  console.log("触底", !where.value.loadend, !where.value.loading);
  if (!where.value.loadend && !where.value.loading) {
    console.log("加载");
    where.value.loading = true;
    if (listType.value == 1)
      amountTakingListsApi({
        page_no: where.value.page_no,
        page_size: where.value.page_size,
        create_time: where.value.create_time,
      }).then((res) => {
        goods_list.value = [...goods_list.value, ...res.data.lists];
        if (res.data.lists.length < where.value.page_size)
          where.value.loadend = true;
        where.value.loading = false;
        where.value.page_no++;
      });
    if (listType.value == 2) {
      if(where.value.start_time[0] >= where.value.start_time[1]) {
        where.value.loading = false;
        return ElMessage.error('开始时间不能大于等于结束时间');
      }
      let start_time = where.value.start_time[0]
        ? moment(where.value.start_time[0]).format("YYYY-MM-DD")
        : "";
      let end_time = where.value.start_time[1]
        ? moment(where.value.start_time[1]).format("YYYY-MM-DD")
        : "";
      financialRecordListsApi({
        page_no: where.value.page_no,
        page_size: where.value.page_size,
        start_time: start_time,
        end_time: end_time,
      }).then((res) => {
        goods_list.value = [...goods_list.value, ...res.data.lists];
        if (res.data.lists.length < where.value.page_size)
          where.value.loadend = true;
        where.value.loading = false;
        where.value.page_no++;
      });
    }
  }
};
getOrderList();

const changeListType = () => {
  where.value.page_no = 1;
  where.value.loadend = false;
  where.value.loading = false;
  goods_list.value = [];
  getOrderList();
};

onMounted(() => {
  mitt.on("get-taking-lists", (res) => {
    where.value.page_no = 1;
    where.value.loadend = false;
    where.value.loading = false;
    goods_list.value = [];
    getOrderList();
  });
});

onUnmounted(() => {
  mitt.off("get-taking-lists");
});
</script>

<template>
  <div class="my-order">
    <div class="date">
      <div>
        <el-date-picker
          v-if="listType == 1"
          v-model="where.create_time"
          type="month"
          placeholder="请选择月份"
          @change="changeDate"
          :clearable="false"
        />
        <el-date-picker
          v-if="listType == 2"
          v-model="where.start_time"
          type="daterange"
          placeholder="请选择日期"
          @change="changeDate"
          format="YYYY-MM-DD"
          :clearable="false"
        />
        <el-button type="primary" style="margin-left: 1rem">重置</el-button>
      </div>
      <el-radio-group v-model="listType" @change="changeListType">
        <el-radio-button label="提现记录" :value="1" />
        <el-radio-button label="余额明细" :value="2" />
      </el-radio-group>
    </div>
    <div class="detail">
      <div
        class="table"
        v-loading="where.loading"
        v-infinite-scroll="getOrderList"
        ::infinite-scroll-delay="500"
        :infinite-scroll-distance="300"
        style="overflow: auto"
        :infinite-scroll-immediate="false"
        v-if="listType == 1"
      >
        <el-table :data="goods_list">
          <el-table-column
            prop="merchant_bank_info.is_own"
            label="账户类型"
            width="100"
          >
            <template #default="{ row }">
              <div>
                {{ row.merchant_bank_info.is_own ? "对公账户" : "个人账户" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="银行卡">
            <template #default="{ row }">
              <div>
                {{ row.merchant_bank_info.bank_info.name }} ({{
                  row.merchant_bank_info.bank_code.slice(-4)
                }})
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="amount" label="提现金额" width="150" />
          <el-table-column label="备注/凭证" width="200">
            <template #default="{ row }">
              <div v-if="row.arrival_proof">
                <el-image
                  style="width: 3rem; height: 3rem; margin-right: 0.2rem"
                  v-for="(item, index) in JSON.parse(row.arrival_proof)"
                  :key="index"
                  :src="item"
                  :preview-src-list="JSON.parse(row.arrival_proof)"
                  :initial-index="index"
                ></el-image>
              </div>
              <div v-else>{{ row.fail_msg }}</div>
            </template>
          </el-table-column>
          <el-table-column label="提现状态" width="100">
            <template #default="{ row }">
              <el-tag v-if="row.arrival_proof" type="success">已到账</el-tag>
              <el-tag v-else-if="row.is_check == 1" type="primary"
                >待转账</el-tag
              >
              <el-tag v-else-if="row.is_check == 0" type="info">待审核</el-tag>
              <el-tag v-else-if="row.is_check == 2" type="danger"
                >提现失败</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="提现时间" width="200" />
        </el-table>
      </div>

      <div
        class="table"
        v-loading="where.loading"
        v-infinite-scroll="getOrderList"
        ::infinite-scroll-delay="500"
        :infinite-scroll-distance="300"
        style="overflow: auto"
        :infinite-scroll-immediate="false"
        v-if="listType == 2"
      >
        <el-table :data="goods_list">
          <el-table-column prop="number_sn" label="订单号" width="250">
          </el-table-column>
          <el-table-column prop="number" label="变动金额" width="180" />
          <el-table-column prop="financial_type" label="变动类型" width="180" />
          <el-table-column prop="create_time" label="时间" />
        </el-table>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.my-order {
  border-radius: 1.2rem;
  height: 100%;
  flex: 1;
  background-color: #fff;
  position: relative;
  overflow: hidden;

  .date {
    padding: 1rem 1rem 0 1rem;
    display: flex;
    justify-content: space-between;
  }

  .detail {
    height: calc(100vh - 100px - 4rem);
    box-sizing: border-box;
    position: relative;

    .table {
      padding: 1rem;
      padding-bottom: 6rem;
      overflow-y: auto;
      height: 100%;

      .table-title {
        font-weight: bold;
      }
      .table-info {
        display: flex;
        flex-wrap: wrap;
        color: #777;
        font-size: 0.9rem;
        padding-bottom: 1rem;
        margin-bottom: 1rem;
        border-bottom: 1px solid #eee;

        &:last-child {
          border-bottom: none;
        }

        .info-item {
          width: 33%;
          display: flex;
          padding-top: 1rem;
          .info-item-title {
            flex-shrink: 0;
            padding-right: 1rem;
          }
        }
      }
    }
  }
}
</style>
