<template>
  <view class="get-list">
    <!-- 顶部tab -->
    <view class="header">
      <view class="tabs">
        <view class="tab-l">
          <view class="tabs-item" @click="tabClick(1)">
            <text :class="params.activeId === 1 ? 'active-tab' : ''">全部</text>
          </view>
          <view class="tabs-item" @click="tabClick(2)">
            <text :class="params.activeId === 2 ? 'active-tab' : ''">进行中</text>
          </view>
          <view class="tabs-item" @click="tabClick(3)">
            <text :class="params.activeId === 3 ? 'active-tab' : ''">审批中</text>
          </view>
          <view class="tabs-item" @click="tabClick(4)">
            <text :class="params.activeId === 4 ? 'active-tab' : ''">已完成</text>
          </view>
        </view>
        <view class="tab-r">
          <view class="date" v-if="!dateStr.year || !dateStr.month" @click="showDatePicker">
            <text>日期</text>
            <uv-icon name="arrow-down-fill" color="#333" />
          </view>
          <view class="date" v-else>
            <text @click="showDatePicker">{{ dateStr.year + '年' + dateStr.month + '月' }}</text>
            <uv-icon name="close-circle" color="#333" @click="clearDate" />
          </view>
        </view>
      </view>
    </view>
    <view class="content" v-if="params.total != 0">
      <view class="notice">
        <uv-notice-bar
          text="任务包即将完成，请不要做积分较高的任务，避免浪费"
          bgColor="#6eb657"
          color="#fff"
        />
      </view>
      <!-- 列表 -->
      <view class="task-content">
        <view class="task-list">
          <view
            v-for="item in dataList"
            :key="item.statusId"
            @click="getDetail(item)"
            class="task-item"
            :class="{ disable: item.taskAddFlag === '0' }"
          >
            <view class="task-top">
              <view class="top-l">
                <text class="name">{{ item.scorePackageName }}</text>
                <view class="tag">
                  <template v-if="item.taskAddFlag === '0'">
                    <view class="gry">{{ getText(item.packageType1) }}</view>
                  </template>
                  <template v-else>
                    <view
                      v-if="item.packageType1 === '1' || item.packageType1 === '6'"
                      class="main"
                      >{{ getText(item.packageType1) }}</view
                    >
                    <view v-if="item.packageType1 === '2'" class="green">{{
                      getText(item.packageType1)
                    }}</view>
                    <view v-if="item.packageType1 === '3'" class="main">{{
                      getText(item.packageType1)
                    }}</view>
                    <view v-if="item.packageType1 === '4'" class="yellow">{{
                      getText(item.packageType1)
                    }}</view>
                  </template>
                </view>
              </view>
              <view class="top-r">
                <view class="point"></view>
                <template v-if="item.enableType == '0'">
                  <template v-if="item.settleNoteStatus == '1'">
                    <view class="is-settle">
                      <view class="settle-text">已结算</view>
                      <view>{{ item.notifyTime }}</view>
                    </view>
                  </template>
                  <template v-else>
                    <text v-if="item.status === '1'">审核中</text>
                    <text v-if="item.status === '2'">申领审核通过</text>
                    <text v-if="item.status === '3'">审核不通过</text>
                  </template>
                </template>
                <text v-else-if="item.enableType == '1'">已终止，全部收回</text>
                <text v-else-if="item.enableType == '2'">已终止，剩余收回</text>
                <text v-else>进行中</text>
              </view>
            </view>
            <view class="task-list-content">
              <view class="task-info">
                <text>发包方：{{ item.packageEntName }}</text>
              </view>
              <view class="task-info">
                <text>发包时间：{{ formatTime(item.createTime) }}</text>
                <text>接包时间：{{ formatTime(item.receiveTime) }}</text>
              </view>
              <view class="progress">
                <uv-line-progress
                  class="pro-one"
                  v-if="item.taskAddFlag === '0'"
                  round
                  :percentage="100"
                  activeColor="#999"
                  inactiveColor="#f8f8f0"
                ></uv-line-progress>
                <uv-line-progress
                  class="pro-two"
                  v-else
                  round
                  :percentage="item.speedProgress"
                  activeColor="#6eb657"
                  inactiveColor="#f8f8f0"
                ></uv-line-progress>
                <view class="progress-info">
                  <text
                    v-if="item.packageType1 === '1' || item.packageType1 === '6'"
                    class="pro-txt"
                    :class="{ 'disable-text': item.taskAddFlag === '0' }"
                  >
                    {{ item.finishScore }}/{{ item.score }}
                  </text>
                  <text
                    v-else
                    class="pro-txt"
                    :class="{ 'disable-text': item.taskAddFlag === '0' }"
                  >
                    {{ item.taskAddFlag === '1' ? item.taskFinishNum : item.taskNum2 }}/{{
                      item.taskNum2
                    }}
                  </text>
                </view>
              </view>
              <view class="btns" v-if="showBtn(item)">
                <view class="btn" @click.stop.self="toQuizDetail(item)"><text>去答题</text></view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 无数据展示 -->
    <noData v-else />
    <!-- 日期选择 -->
    <uv-datetime-picker
      ref="datetimePicker"
      v-model="dateValue"
      mode="year-month"
      @confirm="dateConfirm"
    ></uv-datetime-picker>
  </view>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import { ref, reactive } from 'vue'
import { onReachBottom, onLoad, onShow } from '@dcloudio/uni-app'
import { getPackageList } from '@/service/modules/getTask'
import noData from '@/components/noData/index.vue'
import { getQuizPltTextResultGroupingApi } from '@/service/modules/quiz'

class Params {
  total = -1
  current = 1
  size = 20
  selType = 2
  selDate = ''
  packageName = ''
  activeId = 1
}
const params = ref(new Params())

const resetParams = () => {
  params.value = new Params()
}
const dataList = ref<any[]>([])

async function getList() {
  uni.showLoading({
    title: '加载中...'
  })
  let res = await getPackageList(params.value)

  if (res.data) {
    uni.hideLoading()
    params.value.total = res.data.total
    dataList.value = dataList.value.concat(res.data.records)
  }
}

const type = ref()
onLoad((e) => {
  if (e?.activeId) {
    params.value.activeId = Number(e?.activeId)
  }
  type.value = e?.type

  dataList.value = []
  getQuizPltTextResultGroupingFn()
  getList()
})

const passList = ref<number[]>([])
const getQuizPltTextResultGroupingFn = async () => {
  const res = await getQuizPltTextResultGroupingApi()
  passList.value = res.data?.passed
}

const showBtn = (item: any) => {
  const pkgQuizIds = item?.quizIds || []
  let flag = pkgQuizIds.every((iten: number) => {
    return passList.value?.includes(iten)
  })
  return !flag
}

const toQuizDetail = (item: any) => {
  const pkgQuizIds = item?.quizIds || []
  if (pkgQuizIds[0]) {
    uni.navigateTo({
      url: `/pages-sub-mine/quizDetail/index?id=${pkgQuizIds[0]}`
    })
  }
}

// tab切换
const tabClick = (act: number) => {
  if (params.value.activeId === act) return
  resetParams()
  dataList.value = []
  params.value.activeId = act
  getList()
}

// 日期选择
const datetimePicker = ref()
const dateValue = ref(Number(new Date()))
const showDatePicker = () => datetimePicker.value.open()
const dateStr = reactive({
  year: '',
  month: ''
})
const clearDate = () => {
  dateStr.year = ''
  dateStr.month = ''
  dateValue.value = Number(new Date())
  params.value.selDate = ''
  params.value.current = 1
  dataList.value = []
  getList()
}
const dateConfirm = (e: any) => {
  const date = dayjs(e.value).format('YYYY-MM')
  params.value.selDate = date
  let arr = date.split('-')
  dateStr.year = arr[0]
  dateStr.month = arr[1]
  params.value.current = 1
  dataList.value = []
  getList()
}

// 底部
onReachBottom(() => {
  if (dataList.value.length === params.value.total) return
  params.value.current++
  getList()
})

function getDetail(item: any) {
  if (type.value !== '1') {
    uni.navigateTo({
      url: `/pages-sub-packages/get-task/detail?id=${item.id}`
    })
  } else {
    if (item.status === '3') {
      return uni.showToast({
        title: '当前无可查询的任务',
        icon: 'none'
      })
    }
    uni.navigateTo({
      url: `/pages-sub-packages/task-list/index?id=${item.id}`
    })
  }
}

function getText(item: any) {
  let str = ''
  item = item + ''
  switch (item) {
    case '1':
      str = '积分值'
      break
    case '2':
      str = '任务量'
      break
    case '3':
      str = '通用'
      break
    case '4':
      str = '特定'
      break
    case '6':
      str = '积分值'
      break
  }
  return str
}

const formatTime = (time: string) => dayjs(time).format('YYYY-MM-DD')
</script>

<style lang="scss" scoped>
.get-list {
  min-height: 96vh;
  background-color: #fafafa;
  padding: 0 30rpx;

  .header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 999;
    background-color: #fff;

    .tabs {
      margin-top: 8rpx;
      height: 80rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .tab-l {
        flex: 1;
        display: flex;
        justify-content: space-between;

        .tabs-item {
          flex: 1;
          text-align: center;
          color: #666;
          font-size: 28rpx;
          font-weight: 400;
          text {
            display: inline-block;
            &.active-tab {
              color: #333;
              font-weight: 600;
              position: relative;
              &::after {
                content: '';
                display: block;
                position: absolute;
                width: 50%;
                height: 6rpx;
                background-color: #6eb657;
                left: 50%;
                top: 50rpx;
                transform: translateX(-50%);
              }
            }
          }
        }
      }
      .tab-r {
        width: 240rpx;
        height: 40rpx;
        border-left: 2rpx solid #ebebeb;
        display: flex;
        align-items: center;
        justify-content: center;

        .date {
          display: flex;
          justify-content: center;
          align-items: center;
          text {
            margin-right: 10rpx;
            font-size: 28rpx;
            font-weight: 400;
          }
        }
      }
    }
  }

  .content {
    margin-top: 100rpx;
    padding-top: 24rpx;
    .notice {
      :deep(.uv-notice-bar) {
        border-radius: 8rpx;
        background: #f8b856;
      }
    }
    .task-content {
      background-color: #fafafa;
      .task-list {
        margin-top: 18rpx;
        .task-item {
          width: 100%;
          background: #ffffff;
          border-radius: 16rpx;
          margin-bottom: 24rpx;

          .task-top {
            display: flex;
            padding: 20rpx;
            background: #f8f8f0;
            border-top-left-radius: 16rpx;
            border-top-right-radius: 16rpx;
            .top-l {
              flex: 1;
              .name {
                color: #333;
                font-weight: 600;
                font-size: 34rpx;
                line-height: 50rpx;
              }
              .tag {
                display: inline-block;
                margin-left: 10rpx;
                vertical-align: text-bottom;
                view {
                  line-height: 34rpx;
                  text-align: center;
                  display: inline-block;
                  margin-left: 10rpx;
                  padding: 0 12rpx;
                  border-radius: 8rpx;
                  font-size: 24rpx;
                }
                .main {
                  border: 2rpx solid #6eb657;
                  color: #6eb657;
                }
                .green {
                  border: 2rpx solid #55d169;
                  color: #55d169;
                }
                .yellow {
                  border: 2rpx solid #ffbe1c;
                  color: #ffbe1c;
                }
                .gry {
                  border: 2rpx solid #d3d3d3;
                  color: #999999;
                }
              }
            }
            .top-r {
              max-width: 250rpx;
              display: flex;
              align-items: center;
              .point {
                line-height: 56rpx;
                display: inline-block;
                width: 10rpx;
                height: 10rpx;
                border-radius: 100%;
                background-color: #6eb657;
                margin-right: 10rpx;
              }
              text {
                font-weight: 400;
                line-height: 56rpx;
                color: #6eb657;
              }
              .is-settle {
                text-align: right;
                color: #6b8e23;
                font-size: 24rpx;
              }
            }
          }
          .task-list-content {
            padding: 24rpx;
            background: #fff;
            .task-info {
              margin-bottom: 8rpx;
              text {
                font-weight: 400;
                margin-right: 30rpx;
                font-size: 24rpx;
                color: #999;
              }
            }
            .progress {
              margin-top: 20rpx;
              display: flex;
              justify-content: space-between;
              .pro-one {
                ::v-deep .u-progress {
                  border: 2.5rpx solid #d5d5d5;
                  width: 444rpx;
                  .u-active {
                    border-radius: 100rpx;
                  }
                }
              }
              .pro-two {
                ::v-deep .u-progress {
                  border: 2.5rpx solid #6eb657;
                  width: 444rpx;
                  .u-active {
                    border-radius: 100rpx;
                  }
                }
              }
              .progress-info {
                margin-left: 30rpx;
                flex: 1;
                text-align: right;
              }
              .disable-text {
                color: #999;
              }
            }
          }
        }
        .disable {
          .task-top {
            background-color: #f5f5f5;
            .top-l {
              .name {
                color: #999;
              }
            }
            .top-r {
              .point {
                background: #999;
              }
              text {
                color: #999;
              }
            }
          }
        }
      }
    }
  }
}

.btns {
  margin-top: 30rpx;
  display: flex;

  .btn {
    display: flex;
    align-items: center;
    height: 34rpx;
    margin-right: 36rpx;

    text {
      color: #6eb657;
      font-size: 24rpx;
      line-height: 34rpx;
    }
  }
}
</style>
