<template>
  <div class="collect-page">
    <div class="gather-title">{{ title }}</div>
    <div class="gather-con">
      <van-form @submit="onSubmit">
        <div v-for="(item,index) in datasource" :key="item.qainfoQuestionId">
          <div class="gather-label"> {{ index + 1 }} 、{{ item.questionName }}</div>
          <van-field
            v-if="item.questionType === 1"
            v-model="item.qainfoAnswer"
            :rules="[{ required: true, message: '请选择一个选项' }]"
          >
            <template #input>
              <single
                :item="item"
                v-model="item.qainfoAnswer"
              ></single>
            </template>
          </van-field>
          <van-field
            v-if="item.questionType === 2"
            v-model="item.qainfoAnswer"
            :rules="[{ required: true, message: '请选择至少一个选项' }]"
          >
            <template #input>
              <multiple
                :item="item"
                v-model="item.qainfoAnswer"
              ></multiple>
            </template>
          </van-field>
          <!-- 输入框 -->
          <template v-if="item.questionType === 3">
            <van-field
              v-if="item.checkType === 0"
              v-model="item.qainfoAnswer"
              :rules="[{ required: true, message: '请填写内容' },
              { validator: trimValidator,message:'请填写内容'}]"
            >
              <template #input>
                <input
                  class="lz-input"
                  v-model="item.qainfoAnswer"
                  maxlength="300"
                />
              </template>
            </van-field>
            <van-field
              v-if="item.checkType === 1"
              v-model="item.qainfoAnswer"
              :rules="[{ required: true, message: '请填写电话号码' },
              { validator: phoneValidator,message:'请输入正确的电话号码'}]"
            >
              <template #input>
                <input
                  class="lz-input"
                  type="tel"
                  maxlength="11"
                  v-model="item.qainfoAnswer"
                />
              </template>
            </van-field>
          </template>
        </div>
        <van-button type="primary" class="lz-btn-submit">提交去抽奖</van-button>
      </van-form>
    </div>
  </div>
</template>
<script>
import single from "./single/index.vue";
import multiple from "./multiple/index.vue";
import {phoneCheck} from "../../../../../util/tools.js";

export default {
  props: {
    items: {
      type: Array,
      default() {
        return {};
      },
    },
  },
  components: {
    single,
    multiple,
  },
  computed: {
    title() {
      const at = sessionStorage.getItem("activityType");
      return at === '10011' ? '进行简单的信息填写就可以查看答题结果，抽奖啦~' : '进行简单的信息填写就可以抽奖啦~';
    }
  },
  data() {
    return {
      datasource: []
    }
  },
  created() {
    this.items.forEach(o => {
      this.datasource.push({...JSON.parse(JSON.stringify(o)), qainfoAnswer: ''})
    });
  },
  methods: {
    // 异步校验函数返回 Promise
    phoneValidator(val) {
      return phoneCheck(val);
    },
    // 空格验证
    trimValidator(val) {
      return val && val.trim().length > 0;
    },
    onSubmit() {
      const taskQaCollectInfo = this.datasource.map(o => {
        return {
          qainfoQuestionId: o.qainfoQuestionId,
          questionType: o.questionType,
          qainfoAnswer: o.qainfoAnswer,
        }
      })
      this.$emit("onFinished", taskQaCollectInfo)
    },
  }
};
</script>
<style lang="scss" scoped>
.collect-page {

  .gather-title {
    width: 6.9rem;
    margin: .54rem auto;
    line-height: 0.66rem;
    text-align: center;
    color: #fff;
    font-size: 0.44rem;
    word-break: break-all;
  }

  .gather-con {
    width: 7rem;
    padding: .5rem .3rem;
    background: #fff;
    border-radius: 0.44rem;
    margin: .44rem auto 1rem;
  }

  .gather-label {
    font-size: 0.3rem;
    font-stretch: normal;
    color: #495975;
    text-align: left;
    font-weight: bold;
    margin: 0.23rem 0;
    word-break: break-all;
  }

  .van-field {
    padding: 0 0.05rem;
  }

  .lz-input {
    width: 6.15rem;
    height: 0.53rem;
    line-height: 0.26rem;
    font-size: 0.28rem;
    color: #464646;
    margin: 0 2px 0.16rem 2px;
    border-radius: 0.26rem;
    background: #eaebf0;
    text-align: left;
    text-indent: 0.46rem;
    -webkit-writing-mode: horizontal-tb !important;
    text-rendering: auto;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-shadow: none;
    display: inline-block;
    appearance: auto;
    -webkit-rtl-ordering: logical;
    cursor: text;
    padding: 1px 2px;
    border: none;
  }

  .lz-btn-submit {
    width: 6.26rem;
    height: 0.94rem;
    background-color: #495975;
    border-radius: 0.1rem;
    font-size: 0.46rem;
    font-weight: normal;
    font-stretch: normal;
    color: #ffffff;
    border: none;
    margin-top: 0.46rem;
  }
}
</style>
