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

// Taro.setStorageSync("token", "ok_OL66Ej4_lkQKNA0AwxnGwGyXo");

const tabValue = ref(1);

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

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;
}

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) {
      res = await getBeanDetail(newData);
    } else if (tabValue.value === 2) {
      // delete data.value.Type;
      Reflect.deleteProperty(data.value, "Type");
      res = await getIntegralDetail(newData);
    } else {
      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="greeting">Hello! YuanHuakk</view>-->
      <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>
        <!-- <image class="img" src="https://picdm.sunbangyan.cn/2023/08/15/ste192.png"/> -->
      </view>
    </view>
    <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>
    <view v-if="data.length > 0">
      <view v-if="tabValue === 1 || tabValue === 3">
        <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 === 1" style="color: green"
              >-
              <text>{{ item.number }}积分</text>
            </view>
            <view v-if="tabValue === 3" style="color: red"
              >+
              <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: red"
              >+
              <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: red"
              >+
              <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%;
  height: 300px;
  border-radius: 20px;
  background-color: #282828;
  box-shadow: 0 0 10px rgba(40, 40, 40, 0.1);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  margin: 10px auto;
  color: #d0a568;
  padding: 20px;

  .greeting {
    font-size: 35px;
    font-weight: bold;
  }

  .info {
    display: flex;
    justify-content: space-between;

    .left {
      height: 300px;
      width: 50%;
      text-align: left;
      margin-left: 50px;
      display: flex;
      flex-direction: column;
      justify-content: center;

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

    .img {
      width: 380px;
      height: 270px;
    }
  }
}

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

  .text {
    margin: 10px 20px;
    align-items: center;
  }

  .line {
    margin: 0 auto;
    width: 50px;
    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;

  .left {
    width: 500px;

    .text {
      text-align: left;
      width: 100%;
      // 超出一行隐藏
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
      word-break: break-word;
    }
  }

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