<template>
  <div style=" background: #0078fc;height: 150px;width: 100%;position: absolute;top: 0;z-index:-9999;"></div>
  <div class="moreactive" style="padding: 30px">
    <!-- 面包屑导航 -->
    <div class="crumb">
      <div>{{ pName }}</div>
      <div style="margin-left: 6px; margin-right: 6px">/</div>
      <div>{{ sName }}</div>
      <div style="margin-left: 6px; margin-right: 6px">/</div>
      <div style="font-weight: 700; font-size: 16px">投票详情</div>
      <div class="preNext">
        <span @click="prevPage" v-if="hasPrev">
          <button class="btn btn01"></button>
          <span class="content" style="margin-left: 6px">上一个</span>
        </span>
        <span @click="nextPage" v-if="hasNext">
          <span class="content" style="margin-left: 31px">下一个</span>
          <button class="btn btn02" style="margin-left: 6px"></button>
        </span>
      </div>
      <div class="return">
        <div style="display: flex" @click="returnclick">
          <el-button style="color: #0073fb">
            <img
              class="img2"
              style="margin-right: 11px; cursor: pointer"
              src="../../assets/image/return.png"
            />返回
          </el-button
          >
        </div>
      </div>
    </div>
    <!-- 面包屑导航 -->
    <div class="debateTitle">【投票】{{ dataInfo?.voteName }}</div>
    <!-- 详细信息 -->
    <div class="detailinfo">
      <div class="detail">
        <div class="detailT">
          <div class="left">
            <div class="title">
              <img
                style="width: 20px; height: 20px"
                src="../../assets/image/yuan.png"
              />
              <div class="text">参与情况</div>
              <div class="box"></div>
            </div>
            <!-- <div style="display: flex; align-items: center"></div> -->
            <div class="all">
              <div class="item allbox1">
                <div class="item1" style="color: #089dff">
                  {{ dataInfo.numberOfInvolved || "-" }}
                </div>
                <div class="item2">参与数</div>
              </div>
              <div class="item allbox2">
                <div class="item1" style="color: #387df7">
                  {{ dataInfo.votesTotal || "-" }}
                </div>
                <div class="item2">总票数</div>
              </div>
              <div class="item allbox3">
                <div class="item1" style="color: #00c6ff">
                  {{ dataInfo.numberOfBrowse || "-" }}
                </div>
                <div class="item2">浏览数</div>
              </div>
            </div>
            <div class="title">
              <img
                style="width: 20px; height: 20px;border-radius: 5px"
                src="../../assets/image/yuan.png"
              />
              <div class="text">参与投票</div>
              <div class="box"></div>
            </div>
            <div class="join">
              <div
                v-for="(item, index) in dataInfo.voteStemDtoList"
                :key="index"
                style="margin-bottom: 10px"
              >
                <div class="stem">
                  <div>{{ index + 1 }}、</div>
                  <div>{{ item.voteStemName }}</div>
                </div>
                <div class="options" :style="item.optionDetailList?.some(t=>t.optionPictureAddress)?'display:flex;':'display:block;'">
                  <div
                    v-for="(value, key) in item.optionDetailList"
                    :key="key"
                    style="
                      margin-right: 114px;
                      margin-bottom: 25px;
                    "
                  >
                    <img
                      v-if="value.optionPictureAddress"
                      style="width: 140px; height: 140px; border-radius: 8px"
                      :src="value.optionPictureAddress"
                    />
                    <div v-else-if="isExistImg(item.optionDetailList)" style="width: 140px; height: 140px; border-radius: 8px"></div>
                    <div class="radio">
                      <label @click="choiceQuestion(item,value)">
                        <div class="radio-img">
                          <div
                            v-if="value.isAnswer"
                            style="
                            width: 10px;
                            height: 10px;
                            background: #4a9cf8;
                            position: relative;
                            left: 15px;
                            border-radius: 10px;
                          "></div>
                          <div
                            v-else
                            style="
                            width: 10px;
                            height: 10px;
                            background: #fff;
                            position: relative;
                            left: 15px;
                            border-radius: 10px;
                          "></div>
                          <div class="option"></div>
                        </div>
                        <div class="opt-text">{{ value.optionName }}</div>
                      </label>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div
              style="
                width: 100%;
                display: flex;
                justify-content: center;
                margin-top: 30px;
              ">
              <button
                class="submitbtn btn01"
                :style="{ background: new Date().getTime() > new Date(dataInfo.voteEndTime).getTime() ||new Date().getTime() < new Date(dataInfo.voteStartTime).getTime() ? '#ccc': dataInfo.isSubmit? '#ccc': '', }" @click="submitVote(dataInfo)">
                {{ dataInfo.isSubmit ? "已提交" : "提交" }}
              </button>
            </div>
          </div>
          <div class="right">
            <div class="righttitle">
              <img
                style="width: 20px; height: 20px"
                src="../../assets/image/yuan.png"
              />
              <div class="text">投票时间</div>
              <div class="box"></div>
            </div>
            <div class="timebox clearfix">
              <div class="innertime">
                {{dataInfo?.voteStartTime }}&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;{{dataInfo?.voteEndTime }}
              </div>
              <div class="endtime clearfix">
                <div class="endtimetext">
                  距离结束还有&nbsp;&nbsp;
                  <span class="te">{{
                      parseInt(dayjs(dataInfo?.voteEndTime).diff(dayjs(), "minute") / 60)}}</span>&nbsp;&nbsp; 小时&nbsp;&nbsp;<span class="te">{{dayjs(dataInfo?.voteEndTime).diff(dayjs(), "minute") % 60 }}</span>&nbsp;&nbsp;分钟</div>
              </div>
            </div>
            <div class="righttitle">
              <img
                style="width: 20px; height: 20px"
                src="../../assets/image/yuan.png"
              />
              <div class="text">投票说明</div>
              <div class="box"></div>
            </div>
            <div class="explain clearfix">
              <div
                class="explaincontent"
                v-html="dataInfo?.voteExplain||'暂无说明'"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import {useRequest} from "@/api/request";
import {VOTE_DETAIL3, VOTE_DETAIL_SUBMIT, EditVoteInvolvedAndBrowse,NO_COMMIT} from "@/api/api";
import dayjs from "dayjs";
import store from "@/store";
import {ElMessage} from "element-plus";
import {useRoute, useRouter} from "vue-router/dist/vue-router";
import {computed,watch} from "vue";
import {useTaskPage} from "@/api/useCommon";

const {
  query: { courseId, pName, sName, chapterOrStageId, infoId, id, btype, type },
} = useRoute();
const { nextPage, prevPage, hasPrev, hasNext } = useTaskPage();
const router = useRouter();
const returnclick = () => {
  router.back();
};
const { data: dataInfo } = useRequest(VOTE_DETAIL3(courseId), {type});
const userInfo = computed(() => store.state.userInfo);
const answerTime = dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss");

// 增加浏览量
watch(dataInfo, () => {
  useRequest(EditVoteInvolvedAndBrowse, {operationType:'2', voteId:dataInfo.value.id });
})

// 选择题目
const choiceQuestion = (item, value) => {
  if (dataInfo.value.isSubmit) {
    return;
  }
  item.optionDetailList?.forEach(t => t.isAnswer = false);
  value.isAnswer = true;
};
// 判断当前题目中是否有的选项有图片有的没有
const isExistImg = (data) => {
  let exist = false;
  data.forEach((i,n)=>{
    console.log(i,n)
    if(i.optionPictureAddress){
      exist = true;
      return exist;
    }
  })
  return exist;
}
// 提交投票
const submitVote = () => {
  let nowTime = new Date().getTime();
  let maxTime = new Date(dataInfo.value.voteEndTime).getTime();
  let minTime = new Date(dataInfo.value.voteStartTime).getTime();
  // 当未到开始时间
  if (nowTime < minTime || nowTime > maxTime) {
    ElMessage.error("未到投票开始时间");
    return;
  }
  // 当已经提交过时候 不让提交即可
  if (dataInfo.value.isSubmit) {
    return;
  }
  if (dataInfo.value.voteStemDtoList.some(t => t.optionDetailList.every(s => !s.isAnswer))) {
    ElMessage.error("请选择投票问题后进行提交");
    return;
  }
  dataInfo.value.isSubmit = !dataInfo.value.isSubmit;
  ElMessage.success("投票成功");
  let obj = {
    beginTime: answerTime,
    chapterOrStageId: chapterOrStageId ? chapterOrStageId : 0,
    result: JSON.stringify(dataInfo.value),
    targetId: infoId, // 项目 路径图 id
    taskId: id,
    type: btype, // 1 项目  2 路径图
    voteId: dataInfo.value.id,
    voteName: dataInfo.value.voteName,
    studentId: userInfo.value.userId,
    studentName: userInfo.value.realName,
  };
  useRequest(VOTE_DETAIL_SUBMIT, obj);
  // 增加参与数目
  useRequest(EditVoteInvolvedAndBrowse, {operationType:'1', voteId:dataInfo.value.id });
};
const noCommit = () => {
let noCommitParams = {
  beginTime: answerTime,
    chapterOrStageId: chapterOrStageId ? chapterOrStageId : 0,
    result: JSON.stringify(dataInfo.value),
    targetId: infoId, // 项目 路径图 id
    taskId: id,
    type: btype, // 1 项目  2 路径图
    voteId: dataInfo.value.id,
    voteName: dataInfo.value.voteName,
    studentId: userInfo.value.userId,
    studentName: userInfo.value.realName,
}
  useRequest(NO_COMMIT, noCommitParams);
}
noCommit()
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

.moreactive {
  .crumb {
    color: #fff;
    display: flex;
    font-size: 14px;
    line-height: 24px;
    position: relative;
  }

  .return {
    position: absolute;
    right: 10%;

    .text {
      text-align: center;
      display: flex;
      flex-direction: row;
      align-items: center;
    }
  }

  .preNext {
    position: absolute;
    right: 0px;

    .content {
      font-size: 14px;
      color: #fff;
      width: 43px;
      height: 14px;
      display: inline-block;
      position: relative;
      top: -6px;
      cursor: pointer;
    }

    .btn {
      width: 23px;
      height: 23px;
      border-radius: 50%;
      border: 0;
      cursor: pointer;
    }

    .btn01 {
      background-image: url("../../assets/image/prev.png");
    }

    .btn02 {
      background-image: url("../../assets/image/next.png");
    }
  }

  .debateTitle {
    margin-top: 15px;
    font-size: 20px;
    line-height: 24px;
    height: 24px;
    font-weight: 600;
    color: #fff;
    margin-left: -10px;
  }

  .detailinfo {
    width: 100%;
    margin-top: 20px;
    display: flex;

    .detail {
      flex: 1;
      // margin-right: 20px;
      .detailT {
        min-height: 800px;
        background: #ffffff;
        border-radius: 8px;
        color: rgba(51, 51, 51, 1);
        display: flex;
        padding-bottom: 20px;

        .left {
          margin-left: 48px;
          flex: 1;
        }

        .right {
          width: 417px;
          margin-right: 48px;

          .righttitle {
            display: flex;
            padding-top: 39px;
            position: relative;

            .text {
              margin-left: 8px;
              font-size: 16px;
              color: rgba(51, 51, 51, 1);
              font-weight: 800;
            }

            .box {
              width: 75px;
              height: 10px;
              background-color: rgba(36, 120, 255, 0.15);
              position: absolute;
              left: 23px;
              top: 53px;
            }
          }

          .timebox {
            width: 417px;
            height: 149px;
            background: linear-gradient(90deg, #b6c6e1 0%, #89aed6 100%);
            border-radius: 4px;
            margin-top: 42px;
          }

          .innertime {
            margin-top: 17px;
            margin-left: 55px;
            font-size: 14px;
            font-weight: 500;
            color: #ffffff;
            line-height: 24px;
          }

          .endtime {
            margin-left: 10px;
            margin-top: 16px;
            width: 397px;
            height: 81px;
            background: #f2f5f7;
            border-radius: 0px 8px 0px 8px;

            .endtimetext {
              margin-top: 25px;
              margin-left: 46px;

              .te {
                font-size: 28px;
                font-weight: 800;
                color: #4a9cf8;
                line-height: 24px;
              }
            }
          }

          .explain {
            margin-top: 30px;
            width: 416px;
            padding-bottom: 50px;
            background: #f2f5f7;
            border-radius: 8px;
          }

          .explaincontent {
            width: 368px;
            font-size: 16px;
            font-weight: 500;
            color: #333330;
            line-height: 24px;
            margin-left: 24px;
            margin-top: 47px;
          }
        }

        .title {
          display: flex;
          align-items: center;
          padding-top: 39px;
          position: relative;
        }

        .title .text {
          margin-left: 8px;
          font-size: 16px;
          color: rgba(51, 51, 51, 1);
          font-weight: 800;
        }

        .title .box {
          width: 75px;
          height: 10px;
          background-color: rgba(36, 120, 255, 0.15);
          position: absolute;
          left: 23px;
          top: 53px;
        }

        .all {
          display: flex;
          justify-content: space-between;
          // width: 1280px;
          margin-right: 48px;
          margin-top: 43px;

          .allbox1 {
            margin-right: 22px;
            background: linear-gradient(
                0deg,
                rgba(160, 193, 230, 0) 0%,
                rgba(161, 195, 231, 0.2) 100%
            );
          }

          .allbox2 {
            margin-right: 22px;
            background: linear-gradient(
                0deg,
                rgba(177, 219, 229, 0) 0%,
                rgba(172, 216, 227, 0.2) 100%
            );
          }

          .allbox3 {
            background: linear-gradient(
                0deg,
                rgba(195, 209, 234, 0) 0%,
                rgba(191, 206, 231, 0.2) 100%
            );
          }

          .item {
            // width: 410px;
            width: calc(100% - 44px);
            height: 149px;
            text-align: center;
            border-radius: 4px;

            .item1 {
              color: #089dff;
              font-size: 24px;
              font-weight: bold;
              margin-top: 36px;
            }

            .item2 {
              color: #333330;
              font-size: 14px;
              margin-top: 29px;
            }
          }
        }

        .join {
          // width: 1280px;
          margin-right: 48px;
          // min-height: 408px;
          // background: #f5f6f7;
          // border-radius: 8px;
          margin-top: 32px;
          margin-left: 21px;
          flex: 1;

          .stem {
            display: flex;
            font-size: 16px;
            font-weight: 500;
            color: #333330;
            line-height: 38px;
          }

          .options {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
          }

          .radio {
            margin-top: 14px;
            margin-left: -16px;
            position: relative;
            .radio-img{
              display: flex;
              align-items: center;
            }
          }

          .radio label {
            width:100%;
            line-height: 20px;
            position: relative;
            text-align: center;
            display: grid;
            grid-template-columns: 40px 1fr;
            justify-content: center;
            height: 35px;
            font-weight: normal;

            .opt-text {
              justify-self: stretch;
              font-size: 14px;
              font-weight: bold;
              color: #333330;
              align-self: center;
              overflow: hidden;
              white-space: nowrap;
              cursor: pointer;
              text-overflow: ellipsis;
              text-align: left;
            }
          }

          .radio .option {
            width: 19px;
            height: 18px;
            // position: absolute;
            // top: 1px;
            // // top: 32px;
            // left: 0px;
            background-size: 100%;
            background: url(../../assets/image/noselect.png) no-repeat;
          }

          .radio input[type="radio"] {
            display: inline-block;
            margin-right: -3px;
            opacity: 0;
          }

          .radio input[type="radio"]:checked + div {
            background: url(../../assets/image/select.png) no-repeat;
            background-size: cover;
          }

          .imgcontent {
            display: flex;

            .imgone {
              width: 140px;
              height: 140px;
              border-radius: 8px;
              background-image: url(../../assets/image/px.png);
              background-size: 100% 100%;
              background-position: center;
            }
          }

          .ontitle {
            margin-top: 27px;
            font-size: 14px;
            color: #333330;
          }
        }
      }

      .detailB {
        min-height: 363px;
        background: #ffffff;
        border-radius: 8px;
        margin-top: 20px;

        .el-tabs__item {
          height: 69px;
          padding: 25px 7px 0px 52px;
          font-size: 14px;
          font-weight: 500;
        }

        .el-tabs__nav-wrap::after {
          background-color: rgba(56, 125, 247, 0.2);
        }

        .enclosure {
          height: 89px;
          margin-left: 51px;
          margin-right: 40px;
          // border-bottom: 1px solid rgba(56, 125, 247, 0.2);
          display: flex;
          justify-content: space-between;
          align-items: center;

          .enclosureL {
            display: flex;
            align-items: center;
            font-size: 14px;
            font-weight: 400;
            color: #677d86;
            line-height: 24px;
          }

          .download {
            display: flex;
            align-items: center;
            font-size: 16px;
            font-weight: 400;
            color: #2478ff;
            line-height: 24px;
            cursor: pointer;
          }
        }

        .work {
          margin-left: 51px;
          margin-right: 40px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-top: 37px;
        }

        .work .question {
          font-size: 14px;
          font-weight: 500;
          color: #333330;
          line-height: 18px;
        }

        .work .active {
          width: 82px;
          height: 28px;
          background: linear-gradient(90deg, #a5d4e0 0%, #b4dce6 100%);
          border-radius: 4px;
          font-size: 14px;
          font-weight: 500;
          color: #ffffff;
          line-height: 24px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          margin-right: 11px;
        }

        .work .unactive {
          width: 80px;
          height: 26px;
          border: 1px solid #a5d4e0;
          border-radius: 4px;
          font-size: 14px;
          font-weight: 500;
          color: #a5d4e0;
          line-height: 24px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 11px;
          cursor: pointer;
        }

        .work .btncolor {
          background: linear-gradient(90deg, #84aad2 0%, #a4c5e9 100%);
        }

        .work .bordercolor {
          border: 1px solid #85aad2;
        }

        .work .fontcolor {
          color: rgba(133, 170, 210, 1);
        }

        .work .submit {
          width: 126px;
          height: 46px;
          background: #2478ff;
          box-shadow: 0px 1px 8px 0px rgba(56, 125, 247, 0.7);
          border-radius: 4px;
          font-size: 16px;
          font-weight: 800;
          color: #ffffff;
          line-height: 24px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
      }
    }
  }
}
</style>