<template>
  <view class="integral-rules">
    <image class="rules-top-img" :src="imgBg" />
    <view class="table-box">
      <view class="table-header">
        <view class="table-tab" style="border-right: 1rpx solid #ffffff">获取途径</view>
        <view class="table-tab">获得积分</view>
      </view>
      <image
        class="change-btn"
        src="../../static/images/common/change-btn.png"
        @click="changeBtn"
      />
      <view class="table-con">
        <view class="table-bg">
          <view v-for="item in tableData" :key="item.id" class="table-item">
            <view class="item-left">
              <text>{{ item.taskTypeName }} </text>
              <image
                src="https://yy-cloud-oss.oss-cn-beijing.aliyuncs.com/img/tips.png"
                alt=""
                class="tips"
                @click="descClick(item.taskTypeName)"
              />
            </view>
            <text>+{{ item.score }}积分</text>
          </view>
        </view>
      </view>
    </view>

    <uv-popup ref="popup" mode="bottom" round="10">
      <view class="info">
        <view class="title">
          <text>{{ select?.value }}</text>
          <u-icon name="close" color="#000" size="28" @click="popupClose"></u-icon>
        </view>
        <view class="cont">
          {{ select?.description }}
        </view>
        <view class="btn" @click="popupClose"> 知道了 </view>
      </view>
    </uv-popup>

    <uv-picker
      ref="picker"
      :columns="[drugentList]"
      keyName="label"
      :defaultIndex="defaultIndex"
      @confirm="confirm"
    />
  </view>
</template>

<script setup lang="ts">
import { integralRulesApi, getTaskTypeDeptList } from '@/service/modules/integralRules'
import { onShareAppMessage } from '@dcloudio/uni-app'
import { ref } from 'vue'

const imgBg = 'https://yy-cloud-oss.oss-cn-beijing.aliyuncs.com/img/jy/integralRules.png'
onShareAppMessage(() => {
  var pages = getCurrentPages()
  var prevPage = pages[pages.length - 1]
  return {
    title: '积分规则',
    path: prevPage.route
  }
})

// 获取任务规则描述
const descriptionMap = ref<any[]>([])
async function getMap() {
  let res = await integralRulesApi.getDict()
  descriptionMap.value = res.data
}
getMap()
const drugentList = ref<any[]>([])
// 获取药企列表
async function getDrugentList() {
  let res = await getTaskTypeDeptList()
  drugentList.value = res.data

  if (drugentList.value.length) {
    getTaskTypeScoreList(drugentList.value[0].value)
  }
}
getDrugentList()

const tableData = ref<any[]>([])
async function getTaskTypeScoreList(ruleId: string | number) {
  let res = await integralRulesApi.getTaskTypeListByRuleId(ruleId)
  tableData.value = res.data
}

const select = ref()
const popup = ref()
const descClick = (name: any) => {
  let item = descriptionMap.value.find((item) => item.value === name)
  select.value = item
  popup.value.open()
}
const popupClose = () => popup.value.close()

const defaultIndex = ref()
const picker = ref()
const changeBtn = () => {
  picker.value.open()
}
const confirm = (e: any) => {
  defaultIndex.value = e.indexs
  getTaskTypeScoreList(e.value[0].value)
}
</script>

<style lang="scss" scoped>
.integral-rules {
  position: relative;
  left: 0;
  right: 0;
  top: 0;
  .change-btn {
    position: fixed;
    top: 156rpx;
    right: -16rpx;
    width: 212rpx;
    height: 96rpx;
  }
  .rules-top-img {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    width: 750rpx;
    height: 526rpx;
  }
  .table-box {
    position: absolute;
    top: 526rpx;
    right: 0;
    left: 0;
    min-height: 100vh;
    background: #019cd0;
    .table-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 120rpx;
      .table-tab {
        width: 50%;
        text-align: center;
        color: #ffffff;
        font-size: 36rpx;
      }
    }
    .table-con {
      padding: 0 26rpx;
      padding-bottom: 50rpx;
      .table-bg {
        background: #ffffff;
        padding: 46rpx;
        box-shadow: inset 0px 8rpx 54rpx 0px #6edbff;
        .table-item {
          color: #733d7f;
          font-size: 32rpx;
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-top: 42rpx;
          .item-left {
            display: flex;
            align-items: center;
            .tips {
              width: 30rpx;
              height: 30rpx;
              margin-left: 10rpx;
            }
          }
          &:first-child {
            margin-top: 0;
          }
        }
      }
    }
  }
}

.info {
  padding: 30rpx;
  position: relative;
  .title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 60rpx;
    text {
      color: #000;
      font-weight: 600;
      font-size: 36rpx;
    }
  }
  .cont {
    min-height: 20vh;
    color: #666666;
    font-size: 30rpx;
    line-height: 42rpx;
    margin-bottom: 60rpx;
  }
  .btn {
    background-color: #f1f1f1;
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    color: #333333;
    font-size: 30rpx;
    position: absolute;
    width: 700rpx;
    left: 25rpx;
    bottom: 50rpx;
  }
}
</style>
