<template>
  <div class="m-container">
    <h2>
      <el-page-header @back="onCancel" :content="titleName"></el-page-header>
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="请输入搜索内容"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
          <!-- <el-button
            type="primary"
            @click="onExport"
            v-if="$helpers.permission('questionnaireUserResult/export')"
            >导出</el-button
          > -->
        </el-form-item>
      </save-form>

      <div class="tableWrap" v-if="tableList.length > 0">
        <el-table
          :data="tableList"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
        >
          <el-table-column
            align="center"
            :label="item.content"
            v-for="(item, index) in tableList[0].questionnaireResultDtoList"
            :key="item.userId"
          >
            <template slot-scope="scope">
              <div v-if=" scope.row.questionnaireResultDtoList[index]&&scope.row.questionnaireResultDtoList[index].type">
                <p v-if=" scope.row.questionnaireResultDtoList[index].type == 'radio'">
                  {{
                    scope.row.questionnaireResultDtoList[index].questionnaireQuestionItemIdList=='true' ? "是" : "否"
                  }}
                </p>
                <p v-if="scope.row.questionnaireResultDtoList[index].type == 'checkbox'">
                  {{ scope.row.questionnaireResultDtoList[index].answerValue }}
                </p>
                <p v-if="scope.row.questionnaireResultDtoList[index].type == 'textarea'">
                  <overflow-text  :text="scope.row.questionnaireResultDtoList[index].additionalValue" />
                </p>
                <div v-if="scope.row.questionnaireResultDtoList[index].type == 'file'">
                  <div v-if="scope.row.questionnaireResultDtoList[index].additionalValue">
                    <p
                      v-for="(url, index) in JSON.parse(scope.row.questionnaireResultDtoList[index].additionalValue)"
                      :key="index"
                    >
                      <a :href="url.url">{{ url.name }}</a>
                    </p>
                  </div>
                </div>
                <div v-if="scope.row.questionnaireResultDtoList[index].type == 'image'" >
                  <div v-if="scope.row.questionnaireResultDtoList[index].additionalValue && JSON.parse(scope.row.questionnaireResultDtoList[index].additionalValue).length > 0" class="imgWrap">
                    <el-image
                    class="refImg"
                    ref='refImg'
                      style="width: 50px; height: 50px;margin-right:5px"
                      :src="JSON.parse(scope.row.questionnaireResultDtoList[index].additionalValue)[0].url"
                      :preview-src-list="[
                       ...JSON.parse(scope.row.questionnaireResultDtoList[index].additionalValue).map(item=>{return item.url})
                      ]"
                    >
                    </el-image>
                    <div class="moreImg">
                      +{{JSON.parse(scope.row.questionnaireResultDtoList[index].additionalValue).length-1}}
                    </div>
                  </div>

                  <span v-else>--</span>
                </div>
              </div>
              <p v-else>--</p>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <empty v-if="tableList.length <= 0" />
      <pagination
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>
  </div>
</template>
<script>
import { questionnaireUserResult } from "./api";
import { Export } from "@/utils/downLoadFile";
import pagination from "@/components/Pagination/index";
export default {
  name: "operationQuestion",
  components: { pagination },
  data() {
    let query = this.$route.query;
    let titleName = "问答详情";
    return {
      titleName: query.name,
      id: query.id,
      searchForm: {
        search: null,
      },
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  async mounted() {
    this.getList();
  },
  methods: {
    search() {
      this.pageInfo.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs.searchForm.resetFields();
      this.search();
    },
    async getList() {
      try {
        let params = {
          topicId: this.id,
          page: this.pageInfo.page,
          rows: this.pageInfo.limit,
          ...this.searchForm,
        };
        let result = await questionnaireUserResult(params);
        this.tableList = result.data.rows;
        this.pageInfo.total = result.data.total;
      } catch(e) {
        console.log(e)
      }
    },
    onCancel() {
      this.$store.dispatch("delVisitedViews", this.$route);
      this.$router.push({ path: "/operateManager/setQuestions" });
    },
    // onExport() {
    //   Export(
    //     this,
    //     {
    //       url: "/api-web/questionnaireUserResult/export",
    //       fileName: "问答详情.xls",
    //       method: "get",
    //       params: { cooperationId: this.id, activeType: "REPLACEMENT" },
    //     },
    //     "您确定导出问答详情?"
    //   );
    // },
  },
};
</script>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
}
/deep/.el-collapse-item__header {
  background: #edeef0;
  color: #444;
  padding: 0 10px;
}
/deep/.el-collapse-item__wrap {
  border: 0;
}
/deep/.el-collapse-item__content {
  padding-bottom: 0;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 300px;
  span {
    width: 200px;
  }
}
.imgWrap {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.moreImg {
  width: 50px;
  height: 50px;
  background-color: #999;
  line-height: 50px;
  text-align: center;
  color: #fff;
}


</style>
<style lang="css">
.el-image-viewer__close {
    top: 40px;
    right: 40px;
    width: 40px;
    height: 40px;
    font-size: 24px;
    color: #fff;
    background-color: #606266;
}
</style>
