<template>
  <div class="statistical" v-if="pageShow">
    <div class="statistical-tab">
      <span :class="[currIndex === 0 ? 'active' : '']" @click="currIndex = 0">问卷统计</span>
      <span :class="[currIndex === 1 ? 'active' : '']" @click="currIndex = 1">人员统计</span>
    </div>
    <div class="statistical-content">
      <div class="list-wrapper" v-show="currIndex === 0">
        <div class="top">
          <span>共</span>
          <i>{{ subjectList.length }}</i>
          <span>题，其中</span>
          <span v-for="(item, index) in fieldTypeCount" :key="index">
            {{ mapType(item.field_type) }}<i>{{ item.c }}</i>道{{ fieldTypeCount.length - 1 === index ? '' : '，' }}
          </span>
        </div>
        <div class="topic-list">
          <div class="topic-item" :class="[['radio', 'checkbox', 'select'].indexOf(item.fieldType) === -1 ? 'topic-item__active' : '']" v-for="(item, index) in subjectList" :key="index">
            <template v-if="['radio', 'checkbox', 'select'].indexOf(item.fieldType) > -1">
              <h2>{{ item.fieldName }}（{{ item.fixedLabel }}）</h2>
              <div class="topic-item__wrapper">
                <div class="option-item" v-for="(filed, i) in item.options" :key="i">
                  <div class="option-item__left">
                    <h2>{{ filed.label }}：</h2>
                    <!-- <span>每周监测</span> -->
                  </div>
                  <div class="option-item__right">
                    <span>{{ filed.c || 0}}票</span>
                    <span>{{ (((filed.c || 0) / fieldDataCount[`field_${index}`]) * 100).toFixed(2) }}%</span>
                  </div>
                </div>
              </div>
            </template>
            <template v-else>
              <h2 class="topic-item__left">{{ item.fieldName }}（{{ item.fixedLabel }}）</h2>
              <span>共{{ fieldDataCount[`field_${index}`] }}条数据</span>
            </template>
          </div>
        </div>
      </div>
      <div class="user-list" v-show="currIndex === 1">
        <div class="user-item" v-for="(item, index) in list" :key="index">
          <div class="user-item__left">
            <image src="./components/img/user-icon.png" />
            <div class="right">
              <div class="right-top">
                <h2>{{ item.nickName }}</h2>
                <!-- <span>{{ $dict.getLabel('wxUserType', item.userType) }}</span> -->
              </div>
              <p>{{ item.commitTime }}</p>
            </div>
          </div>
          <div class="user-item__right">
            <p>分值: {{ item.totalScore }}</p>
            <div @click="toPreview(item.id)">查看表单</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    appName: '调查结果',

    name: 'Statistical',

    data () {
      return {
        id: '',
        currIndex: 0,
        targetList: [],
        info: {},
        subjectList: [],
        tableData: [],
        total: 0,
        pageShow: false,
        form: {},
        search: {
          size: 15,
          current: 1
        },
        isMore: false,
        fieldTypeCount: [],
        fieldValueDistribution: [],
        fieldDataCount: {},
        isShowForm: false,
        targetList: [],
        formInfo: {},
        list: []
      }
    },

    onLoad (query) {
      this.$loading()
      this.id = query.id

      this.$nextTick(() => {
        this.getInfo()
        this.getFormInfo()

        this.$dict.load(['wxUserType']).then(() => {
          this.getList()
        })
      })
    },

    methods: {
      getInfo () {
        this.$http.post(`/app/appquestionnairetemplate/queryDetailById?id=${this.id}`).then(res => {
          if (res.code == 0) {
            this.info = res.data
            this.targetList = res.data.fields.map(item => {
              return JSON.parse(item.fieldInfo)
            })
          }
        })
      },

      toPreview(id) {
        localStorage.setItem("toPreviewForm", JSON.stringify({form: this.info, targetList: this.targetList}))
        uni.navigateTo({url: `./PreviewForm?id=${id}&formId=${this.info.id}`})
      },

      getList() {
        if (this.isMore) return
        this.$http.post(`/app/appquestionnairetemplate/statisticsResident?id=${this.id}`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            if (this.search.current > 1) {
              this.list = [...this.list, ...res.data.records]
            } else {
              this.list = res.data.records
            }

            uni.hideLoading()

            if (res.data.records.length < 10) {
              this.isMore = true

              return false
            }

            this.search.current = this.search.current + 1
          } else {
            uni.hideLoading()
          }
        }).catch(() => {
          uni.hideLoading()
        })
      },

      mapType(type) {
        return {
          upload: '上传图片',
          input: '单行填空',
          textarea: '多行填空',
          radio: '单选',
          checkbox: '多选',
          select: '单下拉框'
        }[type]
      },

      getFormInfo() {
        this.$http.post(`/app/appquestionnairetemplate/statisticsTable?id=${this.id}`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          this.$hideLoading()
          if (res.code == 0) {
            this.fieldDataCount = res.data.fieldDataCount
            this.fieldTypeCount = res.data.fieldTypeCount
            this.fieldValueDistribution = res.data.fieldValueDistribution
            this.subjectList = res.data.appQuestionnaireTemplate.fields.map((item, index) => {
              const fieldInfo = JSON.parse(item.fieldInfo)
              let options = fieldInfo.options
              if (['radio', 'checkbox', 'select'].indexOf(item.fieldType) > -1) {
                options = fieldInfo.options.map(v => {
                  res.data.fieldValueDistribution[`field_${index}`].forEach(info => {
                    if (info.fieldValue === v.label) {
                      v.c = info.c
                    }
                  })

                  return v
                })

              }

              return {
                ...item,
                ...fieldInfo,
                options
              }
            })

            this.pageShow = true
          }
        })
      }
    },

    onReachBottom() {
      this.getList()
    }
  }
</script>

<style lang="scss" scoped>
  .statistical {
    padding-top: 112px;
    padding-bottom: 60px;

    .user-list {
      padding: 0 32px;
      background: #f5f5f5;

      .user-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        height: 160px;
        margin-top: 24px;
        padding: 32px;
        background: #FFFFFF;
        box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.02);
        border-radius: 16px;

        .user-item__right {
          font-size: 30px;
          text-align: center;

          p {
            line-height: 40px;
            margin-bottom: 16px;
            color: #333333;
          }

          div {
            color: #1365DD;
          }
        }

        .user-item__left {
          display: flex;
          align-items: center;

          .right {
            p {
              color: #999999;
              font-size: 30px;
            }
          }

          .right-top {
            display: flex;
            align-items: center;
            margin-bottom: 16px;
            line-height: 40px;

            h2 {
              max-width: 300px;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              margin-right: 20px;
              font-size: 30px;
              color: #333;
            }

            span {
              color: #999999;
              font-size: 28px;
            }
          }

          image {
            width: 96px;
            height: 96px;
            margin-right: 16px;
            border-radius: 50%;
          }
        }
      }
    }

    * {
      box-sizing: border-box;
    }

    i {
      font-style: normal;
    }

    .list-wrapper {
      padding-bottom: 40px;

      .top {
        padding: 36px 32px 32px;
        text-align: justify;
        font-size: 34px;
        color: #333;
        border-bottom: 4px solid #f3f6f9;

        i {
          color: #347FFF;
        }
      }

      .topic-item {
        padding: 40px 32px 0;

        & > h2 {
          line-height: 44px;
          color: #333333;
          font-size: 32px;
        }

        &.topic-item__active {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin: 40px 32px 0;
          padding: 34px 32px;
          background: #EFF7FF;
          border-radius: 16px;
          border: 1px solid #CCCCCC;

          h2 {
            flex: 1;
            margin-right: 9px;
            font-weight: 600;
            font-size: 32px;
            color: #333333;
          }

          span {
            color: #666666;
          }
        }

        .option-item {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-top: 24px;
          padding: 34px 32px;
          border-radius: 16px;
          border: 1px solid #CCCCCC;
          background: #F5F5F5;

          & > div {
            display: flex;
          }

          h2 {
            font-weight: 600;
            font-size: 32px;
          }

          .option-item__left {
            flex: 1;
            margin-right: 18px;
            font-weight: 600;
            font-size: 32px;
            color: #333333;

            span {
              flex: 1;
              text-align: justify;
            }
          }

          .option-item__right {
            align-items: center;
            span {
              color: #666666;
              font-size: 28px;

              &:first-child {
                margin-right: 10px;
              }
            }
          }
        }
      }
    }

    .statistical-content {
      background: #fff;
    }

    .statistical-tab {
      display: flex;
      position: fixed;
      align-items: center;
      top: 0;
      left: 0;
      z-index: 11;
      width: 100%;
      height: 96px;
      padding: 0 32px;
      border-bottom: 1px solid #D4D4D4;
      background: #fff;

      span {
        position: relative;
        flex: 1;
        height: 100%;
        line-height: 96px;
        text-align: center;
        color: #000;
        font-size: 32px;
        font-weight: 600;

        &::after {
          position: absolute;
          bottom: 0;
          left: 50%;
          z-index: 1;
          width: 192px;
          height: 6px;
          background: transparent;
          transform: translateX(-50%);
          content: ' ';
        }

        &.active {
          color: #1365DD;

          &::after {
            background: #1365DD;
          }
        }
      }
    }
  }
</style>