<script lang="ts" setup>
import { computed, ref } from "vue";
import Taro from "@tarojs/taro";
import * as dayjs from "dayjs";
import { getIntegralDetail, getBeanDetail, getGiftRecord } from "@/api/user";

const tabValue = ref(1);

const tabsList = ref([
  {
    title: "兑换明细",
    value: 1,
  },
  {
    title: "赠送明细",
    value: 3,
  },
  {
    title: "豆子明细",
    value: 2,
  },
  {
    title: "活动赠送",
    value: 4,
  },
  {
    title: "聚合积分",
    value: 5,
  },
]);

const computerType = computed(() => {
  return (val: number) => {
    switch (val) {
      case 5:
        return "注册赠送";
      case 6:
        return "签到赠送";
      case 7:
        return "系统赠送";
    }
  };
});

const tabChange = (index: number) => {
  tabValue.value = index;
  data.value = [];
  getList();
  page.value.PageNum = 1;
};

interface DataType {
  goods_name: string;
  add_time: string;
  number: number;
  order_number: number;
  type: number;
  oid: string;
  residue: number;
}

const data = ref<DataType[]>([]);

const userInfo = ref<any>({});

Taro.useLoad(() => {
  getList();
  userInfo.value = Taro.getStorageSync("userInfo");
});

const page = ref({
  PageNum: 1,
  PageSize: 10,
  ItemCount: 0,
});

const pageChange = (e: number) => {
  page.value.PageNum = e;
  getList();
};

const getList = async () => {
  try {
    let res: any;
    const newData = {
      PageNum: page.value.PageNum,
      PageSize: page.value.PageSize,
      Type: tabValue.value,
    };
    if (tabValue.value === 1 || tabValue.value === 3 || tabValue.value === 5) {
      res = await getBeanDetail(newData);
    } else if (tabValue.value === 2) {
      // delete data.value.Type;
      Reflect.deleteProperty(data.value, "Type");
      res = await getIntegralDetail(newData);
    } else if (tabValue.value === 4) {
      res = await getGiftRecord(newData);
    }
    data.value = res.data.data || [];
    page.value.ItemCount = res.data.count;
  } catch (error) {
    Taro.showToast({
      title: error.msg,
      icon: "none",
    });
  }
};
</script>

<template>
  <view>
    <view class="card">
      <view class="info">
        <view class="left">
          <view>
            <view class="num">豆子: {{ userInfo.data?.pulse }}</view>
          </view>
          <view>
            <view class="num">积分: {{ userInfo.data?.integral }}</view>
          </view>
        </view>
      </view>
    </view>
    <scroll-view scroll-x>
      <view class="tabs-box">
        <view
          v-for="item in tabsList"
          :key="item.value"
          @click="tabChange(item.value)"
        >
          <view class="text">{{ item.title }}</view>
          <view
            class="line"
            :class="{ lineColor: item.value === tabValue }"
          ></view>
        </view>
      </view>
    </scroll-view>

    <view v-if="data.length > 0">
      <view v-if="tabValue === 1">
        <view class="card-list" v-for="(item, index) in data" :key="index">
          <view class="left">
            <view>订单号: {{ item.oid }}</view>
            <text class="jf">{{
              dayjs(item.add_time).format("YYYY/MM/DD mm:ss")
            }}</text>
          </view>
          <view class="right">
            <view v-if="tabValue === 1" style="color: red"
              >-
              <text>{{ item.number }}</text>
            </view>
            <text class="jf">积分余额: {{ item.residue || 0 }}</text>
          </view>
        </view>
      </view>
      <view v-if="tabValue === 3 || tabValue === 5">
        <view class="card-list" v-for="(item, index) in data" :key="index">
          <view class="left">
            <view>订单：{{ item.oid }}</view>
            <view>时间：{{ item.add_time.slice(0, 10) }}</view>
          </view>
          <view class="right">
            <view v-if="tabValue === 5" style="color: red"
              >-
              <text>{{ item.number }}积分</text>
            </view>
            <view v-if="tabValue === 3" style="color: green"
              >+
              <text>{{ item.number }}</text>
              积分
            </view>
          </view>
        </view>
      </view>
      <view v-else-if="tabValue === 2">
        <view class="card-list" v-for="(item, index) in data" :key="index">
          <view class="left">
            <view class="text">订单：{{ item.oid }}</view>
            <view>时间：{{ item.add_time.slice(0, 10) }}</view>
          </view>
          <view class="right">
            <view style="color: green"
              >+
              <text>{{ item.number }}</text>
              豆子
            </view>
          </view>
        </view>
      </view>
      <view v-else-if="tabValue === 4">
        <view class="card-list" v-for="(item, index) in data" :key="index">
          <view class="left">
            <view class="text">类型：{{ computerType(item.type) }}</view>
            <view>时间：{{ item.add_time.slice(0, 10) }}</view>
          </view>
          <view class="right">
            <view style="color: green"
              >+
              <text>{{ item.number }}</text>
              豆子
            </view>
          </view>
        </view>
      </view>
      <nut-pagination
        v-model="page.PageNum"
        :total-items="page.ItemCount"
        :items-per-page="page.PageSize"
        @change="pageChange"
      />
    </view>
    <nut-empty v-else description="暂无明细"></nut-empty>
  </view>
</template>

<style lang="scss">
.nut-pagination {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.card {
  width: 90%;
  border-radius: 20px;
  background-color: #282828;
  margin: 10px auto;
  color: #d0a568;
  padding: 20px;

  .info {
    .left {
      height: 200px;
      text-align: left;
      margin-left: 50px;
      display: flex;
      flex-direction: column;
      justify-content: center;

      .num {
        font-weight: bold;
        margin-top: 10px;
      }
    }
  }
}

.tabs-box {
  display: flex;
  flex-direction: row;
  // justify-content: space-evenly;
  align-items: center;
  height: auto;
  background-color: #fff;
  padding: 0 10px;
  text-align: center;
  overflow-x: auto;

  &::-webkit-scrollbar {
    width: 0;
    height: 0;
  }

  view {
    width: 200px;
  }

  .text {
    margin: 10px 0;
    align-items: center;
    font-size: large;
  }

  .line {
    margin: 0 auto;
    width: 100px;
    height: 5px;
    border-radius: 30px;
    transition: all 0.3s ease-in-out;
  }

  .lineColor {
    background-color: #ff0000;
  }
}

.card-list {
  margin: 10px 20px;
  background-color: #fff;
  display: flex;
  border-radius: 10px;
  padding: 20px;
  justify-content: space-between;
  align-items: center;

  .jf {
    color: #8a8a8a;
  }

  .left {
    .text {
      text-align: left;
      font-weight: bolder;
      // 超出一行隐藏
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
      word-break: break-word;
      color: #484848;
      font-size: large;
    }
  }

  .right {
    flex: 1;
    text-align: right;
  }
}
</style>
