<template>
  <view class="task-page">
    <view class="task-header">
      <view class="task-header-title"
        >我的积分<text class="big">{{
          (userInfo && userInfo.score) || 0
        }}</text></view
      >
      <view class="task-header-desc">每日签到做任务可领取积分</view>
      <!-- <image
        :src="require('@/assets/task/icon-help.png')"
        class="invite-icon"
        @tap="$refs.popup_info.open()"
      ></image> -->
    </view>
    <view class="task-box">
      <view class="task-btns" v-if="userInfo && userInfo.phone">
        <button class="task-btn" @click="uploadFiles">拍照积分</button>
        <button class="task-btn" :disabled="isSing" @click="handleSing">
          {{ isSing ? "已签到" : "签到积分" }}
        </button>
      </view>
      <view class="task-btns" v-else>
        <button
          class="task-btn"
          @click="uploadFiles"
          open-type="getPhoneNumber"
          @getphonenumber="handlePhoneLogin"
        >
          拍照积分
        </button>
        <button
          class="task-btn"
          :disabled="isSing"
          open-type="getPhoneNumber"
          @getphonenumber="handlePhoneLogin($event, 'sing')"
        >
          {{ isSing ? "已签到" : "签到积分" }}
        </button>
      </view>
    </view>
    <view class="task-box" v-if="photoList.length">
      <view class="task-box-title">拍照积分记录</view>
      <view class="task-list">
        <view
          v-for="item in photoList"
          :key="item.id"
          class="task-item"
          @click="handlePreviewImage(item.img)"
        >
          <image class="task-item-icon" :src="item.img" />
          <view class="task-item-body">
            <view class="task-item-title" v-if="item.status === 1"
              >审核通过</view
            >
            <view class="task-item-title" v-else-if="item.status === 2"
              >审核失败</view
            >
            <view class="task-item-title" v-else>待审核</view>
            <view class="task-item-desc" v-if="item.reason">{{
              item.reason
            }}</view>
            <view class="task-item-desc">{{ item.upload_time }}</view>
          </view>
          <view class="task-after" v-if="item.score && item.status === 1"
            >{{ item.score }}积分</view
          >
          <!-- <button
            v-if="item.status === 1"
            class="task-item-btn receive"
            @click="handleReceive(item)"
          >
            去领取
          </button>
          <button v-else-if="item.status === 2" class="task-item-btn disabled">
            已完成
          </button>
          <button v-else class="task-item-btn">去完成</button> -->
        </view>
      </view>
    </view>
    <uni-popup
      ref="popup_info"
      type="center"
      mask-background-color="rgba(0,0,0,0.8)"
      :is-mask-click="true"
    >
      <view class="popup-wrap">
        <view class="popup-content">
          <view class="h1"><text>积分规则</text></view>
          <view class="p" v-html="singInfo.score_info || ''"></view>
        </view>
        <image
          class="popup_close"
          :src="require('@/assets/icon/icon-close.png')"
          @click="$refs.popup_info.close()"
        ></image>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import { getSingInfo, postSingInfo } from "@/lib/api/task";
import { getUserInfo } from "@/lib/api/user";
import { getMyPhotoList } from "@/lib/api/shop";
import request from "@/lib/utils/request";
import login from "@/lib/utils/login";

export default {
  name: "PageTask",
  data() {
    return {
      userInfo: null,
      singInfo: null,
      isSing: false,
      photoList: [],
    };
  },

  async onShow() {
    this.userInfo = await login.getUserInfo();
    if (this.userInfo && this.userInfo.session) {
      this.init();
    }
  },

  methods: {
    init() {
      return Promise.all([
        this.getUserInfo(),
        this.getSingInfo(),
        this.getMyPhotoList(),
      ]);
    },

    async getUserInfo() {
      const data = await getUserInfo();
      this.userInfo = { ...this.userInfo, ...data.data };
      uni.setStorageSync("userinfo", JSON.stringify(this.userInfo));
    },

    async getSingInfo() {
      const res = await getSingInfo();
      this.isSing = !!res.is_signin;
    },

    async handleSing() {
      if (this.isSing) {
        uni.showToast({
          icon: "none",
          title: "已签到",
        });
        return;
      }
      const res = await postSingInfo();
      if (res.errno === 10000) {
        uni.showToast({
          icon: "none",
          title: "签到成功",
        });
        this.isSing = true;
        this.getUserInfo();
        return;
      }
      uni.showToast({
        icon: "none",
        title: res.errmsg,
      });
    },

    async getMyPhotoList() {
      const res = await getMyPhotoList();
      this.photoList = res?.data || [];
    },

    uploadFiles() {
      uni.chooseImage({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          uni.uploadFile({
            url: "https://yggc.weapp.yergoo.com/weapp/score/photo/upload", //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: "f",
            header: request.getCommonHeader(),
            timeout: 30000,
            success: (uploadFileRes) => {
              const res = JSON.parse(uploadFileRes.data);
              console.log(res);
              uni.showToast({
                icon: "none",
                title: res.errno === 10000 ? "上传成功" : res.errmsg,
              });
              if (res.errno === 10000) this.getMyPhotoList();
            },
          });
        },
      });
    },

    handlePreviewImage(url) {
      uni.previewImage({
        urls: [url],
      });
    },

    async handlePhoneLogin(e, type) {
      if (e.detail.code) {
        login.setPhoneData({
          phone_code: e.detail.code,
          callback: async (data) => {
            this.userInfo = data;
            await this.init();
            if (type === "sing") {
              this.handleSing();
            } else {
              this.uploadFiles();
            }
          },
          fail: (e) => {
            console.log(e);
          },
        });
      }
    },
  },
};
</script>

<style lang="scss">
.task-page {
  width: 100%;
  overflow: hidden;
  background: #f6f6f6;
  min-height: 100vh;
  box-sizing: border-box;
  padding-bottom: 60rpx;

  .task-header {
    box-sizing: border-box;
    width: 830rpx;
    padding: 32rpx 100rpx 148rpx;
    margin-left: -40rpx;
    margin-bottom: -106rpx;
    background: #30303a;
    border-radius: 0 0 148rpx 148rpx;
    box-shadow: 0 0 50rpx rgba($color: #fff, $alpha: 1);
    .task-header-title {
      font-size: 30rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 52rpx;
      .big {
        font-size: 44rpx;
        font-weight: bold;
        color: #08c2c3;
        margin-left: 10rpx;
      }
    }
    .task-header-desc {
      font-size: 24rpx;
      color: #d1d1d1;
      line-height: 28rpx;
    }
    .invite-icon {
      position: absolute;
      width: 36rpx;
      height: 36rpx;
      right: 36rpx;
      top: 44rpx;
      cursor: pointer;
    }
  }
  .task-box {
    width: 690rpx;
    background: #ffffff;
    margin: 0 auto 20rpx;
    padding: 30rpx;
    box-sizing: border-box;
  }
  .task-box-title {
    height: 54rpx;
    font-size: 42rpx;
    // font-family: ysbth;
    color: #000000;
    line-height: 54rpx;
    margin-bottom: 12rpx;
  }
  .task-days {
    display: flex;
    justify-content: space-between;
    padding-top: 50rpx;
    .task-day {
      width: 94rpx;
      height: 96rpx;
      border-radius: 8rpx;
      background: #f9f9f9;
      font-size: 20rpx;
      color: #888888;
      line-height: 23rpx;
      text-align: center;
      box-shadow: inset 0 0 2rpx rgba($color: #000000, $alpha: 0.15);

      .task-day-integral {
        margin: -28rpx auto 14rpx;
        width: 68rpx;
        height: 68rpx;
        background: url(@/assets/task/icon-sign.png) no-repeat;
        background-size: contain;
        display: flex;
        align-items: center;
        justify-content: center;

        font-size: 30rpx;
        font-family: Helvetica Neue-Bold, Helvetica Neue;
        font-weight: bold;
        color: #ffffff;
        line-height: 35rpx;
      }
      &.task-day-active {
        background: linear-gradient(180deg, #fffceb 0%, #fffaf0 100%);
        color: #f2ab24;
        .task-day-integral {
          background-image: url(@/assets/task/icon-signed.png);
          color: #ffffe2;
          text-shadow: 0px 0px 2rpx #c18214;
        }
      }
    }
  }
  .task-days-btn {
    &::after {
      display: none;
    }
    width: 630rpx;
    height: 92rpx;
    background: url(@/assets/task/btn.png) no-repeat;
    background-size: contain;
    font-size: 32rpx;
    font-weight: bold;
    color: #292d3b;
    line-height: 38rpx;
    line-height: 92rpx;
    text-align: center;
    margin: 34rpx auto 0;
  }
  .task-list {
    .task-item {
      padding: 30rpx 0;
      display: flex;
      border-bottom: 2rpx solid #f5f5f5;
      align-items: center;
      &:last-child {
        border-bottom: 0;
        padding-bottom: 0;
      }
      .task-item-icon {
        width: 80rpx;
        height: 80rpx;
        margin-right: 26rpx;
      }
      .task-item-btn {
        box-sizing: border-box;
        width: 130rpx;
        height: 56rpx;
        background: #08c2c3;
        margin-left: 26rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24rpx;
        font-weight: 600;
        color: #292d3b;
        line-height: 28rpx;
        border-radius: 0;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        &::after {
          display: none;
        }
        &.receive {
          background: #ffffff;
          border: 2rpx solid #22c8c9;
          color: #22c8c9;
        }
        &.disabled {
          background: #c8c8c8;
          color: #ffffff;
        }
      }
      .task-after {
        color: #22c8c9;
      }
      .task-item-body {
        flex: 1;
        width: 0;
      }
      .task-item-title {
        height: 40rpx;
        font-size: 28rpx;
        font-weight: 500;
        color: #000000;
        line-height: 40rpx;
      }
      .task-item-label {
        margin-left: 16rpx;
        color: #17c6c7;
      }
      .task-item-desc {
        height: 34rpx;
        font-size: 24rpx;
        font-weight: 400;
        color: #888888;
        line-height: 34rpx;
      }
    }
  }
}
.task-btns {
  display: flex;
  .task-btn {
    flex: 1;
    border-radius: 1000px;
    background-color: #08c2c3;
    margin: 0 20rpx;
    &::after {
      display: none;
    }
  }
}
</style>
