<template>
  <div class="app-container">
    <div class="mb10">
      <el-input
        class="w260 mb10"
        clearable
        v-model="queryParams.videoTitle"
        placeholder="请输入直播名称"
      />
      <el-input
        class="w260 mb10"
        clearable
        v-model="queryParams.title"
        placeholder="请输入问卷名称"
      />
      <el-date-picker
        v-model="valueTime"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>
      <el-button type="primary" @click="queryList(1)">查询</el-button>
    </div>
    <div class="mb10">
      <el-table :data="data" v-loading="loading" border style="width: 100%">
        <el-table-column label="序号" type="index" fixed width="180">
        </el-table-column>
        <el-table-column prop="title" label="问卷名称" width="180">
        </el-table-column>
        <el-table-column prop="videoTitle" label="直播名称" width="180">
        </el-table-column>
        <el-table-column prop="startTime" label="发起时间" width="180">
        </el-table-column>
        <el-table-column prop="endTime" label="结束收集时间" width="180">
        </el-table-column>
        <el-table-column prop="writeNum" label="填写人数"> </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ statusList[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.writeNum"
              type="text"
              @click="toExport(scope.row)"
              >导出</el-button
            >
            <el-button type="text" @click="question(scope.row, -2)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="queryParams.current"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import {
  questionList,
  questionUpdateStatus,
  questionExport
} from "@/api/videoLive";
import { mapGetters } from "vuex";
import dayjs from "dayjs";
import { timeFormat } from "@/utils/index";
export default {
  data() {
    return {
      queryParams: {
        name: "",
        current: 1,
        size: 10,
        title: ""
      },
      startTime: "",
      endTime: "",
      total: 1,
      loading: false,
      data: [],
      valueTime: "",
      statusList: {
        1: "未开始",
        2: "进行中",
        3: "已结束"
      },
      downloadLoading: false
    };
  },
  computed: {
    ...mapGetters(["user"])
  },
  mounted() {
    this.queryList();
  },
  watch: {},
  methods: {
    timeFormat,
    queryList(val) {
      if (val) {
        this.queryParams.current = 1;
      }
      this.loading = true;
      if (this.valueTime) {
        this.queryParams.startTime = this.valueTime[0]
          ? dayjs(this.valueTime[0]).format("YYYY-MM-DD HH:mm:ss")
          : "";
        this.queryParams.endTime = this.valueTime[1]
          ? dayjs(this.valueTime[1]).format("YYYY-MM-DD HH:mm:ss")
          : "";
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      questionList(this.queryParams).then(data => {
        this.data = data.data.list;
        this.total = data.data.total;
        this.loading = false;
      });
    },
    searchList() {
      this.queryParams.size = 10;
      this.queryParams.current = 1;
      this.queryList();
    },
    handleSizeChange(val) {
      this.$set(this.queryParams, "size", val);
      this.$set(this.queryParams, "current", 1);
      this.queryList();
    },
    handleCurrentChange(val) {
      this.$set(this.queryParams, "current", val || 1);
      this.queryList();
    },
    question(item, type) {
      const label = {
        "-1": "发起问卷",
        "-2": "删除问卷",
        "-3": "重发问卷"
      };
      this.$confirm(`您确定${label[type]}?`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const data = {
            questionId: item.id,
            operate: type
          };
          questionUpdateStatus(data).then(res => {
            if (res.code === 0) {
              this.$message({
                type: "success",
                message: "操作成功！"
              });
              this.queryList();
            }
          });
        })
        .catch(() => {});
    },
    async toExport(item) {
      const { data, code, message } = await questionExport({
        questionId: item.id
      });
      if (code === 0) {
        import("@/utils/export2Excel").then(excel => {
          const tHeader = [
            "用户昵称",
            "开始答题时间",
            "结束答题时间",
            "答题时长"
          ];
          const filterVal = ["nickName", "startTime", "endTime", "duration"];
          if (data[0].titleList.length) {
            for (let index = 0; index < data[0].titleList.length; index++) {
              filterVal.push(index);
            }
          }
          tHeader.push(...data[0].titleList);
          const excelData = this.formatJson(filterVal, data);
          excel.export_json_to_excel({
            header: tHeader,
            data: excelData,
            filename: `${item.title}问卷数据`
          });
          console.log("list=>", data.list);
        });
      } else {
        this.$message.error(message);
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (!isNaN(j)) {
            return v.answers[j];
          } else if (j === "duration") {
            return this.timeFormat(v[j] * 1000);
          } else {
            return v[j];
          }
        })
      );
    },
    toDetail(val) {}
  }
};
</script>

<style scoped lang="scss">
.mb10 {
  margin-bottom: 20px;
}
.w260 {
  width: 260px;
}

.selected {
  background-color: #409eff !important;
  color: #ffffff !important;
}
</style>
