<template>
  <full-page :customScroll="true">
    <survey-record v-if="survey" :customerName="customerName" :survey="survey" :list="showList" />
    <view v-else class="empty">
      <image class="empty__icon" src="/static/empty.svg"></image>
      <text class="empty__txt">{{ emptyTxt }}</text>
    </view>
  </full-page>
</template>

<script setup>
import { computed, ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import api from "@/utils/api.js";
import { loading, toast, hideLoading } from "@/utils/widget";

import FullPage from '@/components/full-page.vue';
import surveyRecord from "./components/survey-record.vue";

const corpId = ref('');
const surveryId = ref('');
const answerId = ref('');
const memberId = ref('');
const survey = ref(null);
const emptyTxt = ref('')
const customerName = ref('');

const list = computed(() => survey.value && Array.isArray(survey.value.list) ? survey.value.list : []);
const showList = computed(() => {
  return list.value.filter(item => {
    const showCase = Array.isArray(item.showCase) ? item.showCase : [];
    if (showCase.length) {
      const res = showCase.every(({
        type,
        id,
        value
      }) => {
        const question = this.list.find(i => i.id === id);
        const val = question ? question.value : '';
        return type === 'in' ? value.includes(val) : val === value.join()
      })
      return res
    }
    return true
  })
})

async function init() {
  loading('加载中...');
  try {
    const record = await getAnswerRecord();
    if (record && record.submitTime) {
      record.status = 'enable';
      survey.value = record;
      hideLoading()
      return;
    }
    survey.value = await getSurvey();
    if (survey.value.status == 'stop') {
      emptyTxt.value = '问卷已经停用'
    } else if (survey.value.status == 'init') {
      emptyTxt.value = '问卷暂未启用'
    }
  } catch (e) {
    hideLoading();
    toast('获取问卷失败');
    uni.navigateBack();
  }
  console.log(survey.value);
}

async function getAnswerRecord() {
  const res = await api('getAnswer', { corpId: corpId.value, surveryId: surveryId.value, answerId: answerId.value, memberId: memberId.value });
  if (res && res.success) {
    return res.record
  }
  return Promise.reject();
}

async function getSurvey() {
  const res = await api('getSurveyDetail', { corpId: corpId.value, id: surveryId.value })
  if (res && res.success && res.data) {
    return res.data
  }
  return Promise.reject();
}

onLoad(opts => {
  corpId.value = opts.corpId;
  surveryId.value = opts.surveryId;
  answerId.value = opts.answerId;
  memberId.value = opts.memberId;
  customerName.value = opts.customerName;
  init();
})



</script>

<style scoped lang="scss">
.survey {
  position: relative;
  height: 100%;
  width: 100%;
  height: 100vh;
  width: 100vw;
  color: ragb(0, 0, 0, .9);
  background: #fff;

  @at-root &__wrapper {
    padding: 30rpx 40rpx;
  }

  @at-root &__scroll {
    height: 100%;
  }

  @at-root &__customer {
    text-align: right;
    font-size: 30rpx;
    padding-bottom: 16rpx;
    margin-bottom: 24rpx;
    color: #333;
    border-bottom: 1px solid #eee;
  }

  @at-root &__title {
    text-align: center;
    font-size: 34rpx;
    font-weight: 600;
    margin-bottom: 24rpx;
  }

  @at-root &__desc {
    text-align: left;
    color: #666;
    font-size: 30rpx;
    margin-bottom: 24rpx;
  }

  @at-root &__item {
    padding: 0 10rpx;
    margin-bottom: 24rpx;
  }

  @at-root &__question {
    position: relative;
    font-size: 34rpx;
    margin-bottom: 24rpx;

    @at-root &--require::before {
      position: absolute;
      left: -20rpx;
      top: 0;
      content: '*';
      color: #f56c6c;
    }
  }

  @at-root &__input {
    padding: 10rpx 24rpx;
    font-size: 30rpx;
    border: 1px solid #eee;
    border-radius: 8rpx;
  }

  @at-root &__radio {
    display: flex;
    align-items: center;
    padding-bottom: 24rpx;
    cursor: pointer;
  }

  @at-root &__btn {
    margin-top: 30rpx;
    padding: 24rpx 30rpx;
    border-radius: 8rpx;
    font-size: 30rpx;
    color: #fff;
    background: #006eff;
    text-align: center;
  }
}

.radio {
  @at-root &__icon {
    flex-shrink: 0;
    margin-top: 4rpx;
    margin-right: 10rpx;
    width: 40rpx;
    height: 40rpx;
  }

  @at-root &__label {
    flex-grow: 1;
    font-size: 34rpx;
  }
}

.empty {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  text-align: center;

  @at-root &__icon {
    display: block;
    margin-bottom: 16rpx;
    width: 240rpx;
    height: 240rpx;
  }

  @at-root &__txt {
    font-size: 34rpx;
    color: #666;
  }
}
</style>