<template>
  <div>
    <view class="bank-control fl_b">
      <view class="fl_b left">
        <view class="btn-item" style="" @click="openFooterTab">
          <image src="/static/icon_sheet.png"></image>
          <text>答题卡</text>
        </view>
        <view class="btn-item" @click="$emit('submit')">
          <image src="/static/icon_paper.png" mode=""></image>
          <text @click="$emit('submit')">交卷</text>
        </view>
      </view>
      <view class="fl">
        <view class="tab up" @click="prev">上一题</view>
        <view class="tab down" @click="next">下一题</view>
      </view>
    </view>
    <u-popup v-model="show" mode="bottom" border-radius="14" height="680rpx">
      <view class="popupView">
        <view class="popupTops">
          <view class="topIcon"></view>
          点击编号即可跳转至对应题目
        </view>
        <view class="popupContent">
          <scroll-view scroll-y="true" style="height: 506rpx">
            <view class="boxSty">
              <view
                v-for="(item, index) in questionList"
                :key="index"
                @click="changeIndex(index)"
                :class="{
                  isRight: bankType == 1 && isRight(item, index),
                  isWrong: bankType == 1 && isWrong(item, index),
                  isPart: bankType == 1 && isPart(item, index),
                  isOver: bankType == 1 && isOver(item, index),
                  check_ans: bankType == 2 && isCheck(item, index),
                }"
                class="liListSty"
              >
                {{ index + 1 }}
              </view>
            </view>
          </scroll-view>
        </view>
      </view>
    </u-popup>
  </div>
</template>

<script>
export default {
  name: "SaasMiniprogramBankBom",
  props: {
    questionList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    bankType: {
      type: Number,
    },
    current: {
      type: Number,
    },
  },
  data() {
    return {
      show: false,
    };
  },
  mounted() {},
  methods: {
    changeIndex(index) {
      this.$emit("update:current", index);
    },
    prev() {
      if (this.current == 0) {
        uni.showToast({
          icon: "none",
          title: "已经是第一题了！",
        });
        return;
      }
      this.changeIndex(this.current - 1);
    },
    next() {
      if (this.current >= this.questionList.length - 1) {
        uni.showToast({
          icon: "none",
          title: "已经是最后一题了！",
        });
        return;
      }
      this.changeIndex(this.current + 1);
    },
    openFooterTab() {
      this.show = true;
    },
    isCheck(item, index) {
      // 案例题处理
      let { ques, type, ans } = item;
      if (type == 4) {
        return ans.length == ques.length;
      }
      if (ques) {
        return true;
      }
    },
    isRight(item, index) {
      //单选
      if (this.questionList[index].ques) {
        if (item.type == 1) {
          return this.questionList[index].ques == this.questionList[index].ans;
          //多选
        } else if (item.type == 2) {
          //每一项都相等
          return this.questionList[index].ans.every((item, i) => {
            return item == this.questionList[index].ques[i];
          });
          //判断
        } else if (item.type == 3) {
          return this.questionList[index].ques == this.questionList[index].ans;
          // } else if (item.type == 5) {
          // 	if(this.questionList[index].ques.text){
          // 		return true
          // 	}else{
          // 		return false
          // 	}
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    isWrong(item, index) {
      if (this.questionList[index].ques) {
        //单选
        if (item.type == 1) {
          return this.questionList[index].ques != this.questionList[index].ans;
          //多选
        } else if (item.type == 2) {
          //每一项都相等
          return this.questionList[index].ques.some((item, i) => {
            return this.questionList[index].ans.indexOf(item) == -1;
          });
          //判断
        } else if (item.type == 3) {
          return this.questionList[index].ques != this.questionList[index].ans;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    isPart(item, index) {
      if (this.questionList[index].ques) {
        if (item.type == 2) {
          let isWrong = this.questionList[index].ques.some((item, i) => {
            return this.questionList[index].ans.indexOf(item) == -1;
          });

          let isRight = this.questionList[index].ans.every((item, i) => {
            return item == this.questionList[index].ques[i];
          });

          if (!isRight && !isWrong) {
            return true;
          }
        }
      } else {
        return false;
      }
    },
    isOver(item, index) {
      if (this.questionList[index].ques) {
        if (item.type == 4) {
          //案例题
          let isOver = item.jsonStr.every((jsonItem, indexs) => {
            if (
              jsonItem.type == 1 ||
              jsonItem.type == 2 ||
              jsonItem.type == 3
            ) {
              if (item.ques[indexs]) {
                return true;
              } else {
                return false;
              }
            } else if (jsonItem.type == 5) {
              if (
                item.ques[indexs] &&
                (item.ques[indexs].text || item.ques[indexs].imageList.length)
              ) {
                console.log("chil");
                return true;
              } else {
                return false;
              }
            }
          });

          if (isOver) {
            return true;
          } else {
            return false;
          }
        } else if (item.type == 5) {
          //简答题
          //每一项都相等
          if (item.ques && (item.ques.imageList.length || item.ques.text)) {
            return true;
          }
          //判断
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.bank-control {
  background-color: #fff;
  z-index: 78;
  position: fixed;
  bottom: 0rpx;
  width: 100%;
  height: 120rpx;
  padding: 16rpx 28rpx 16rpx 48rpx;
  .left {
    width: 180rpx;
  }
  .btn-item {
    text {
      color: #333333;
      font-size: 24rpx;
      margin-top: 10rpx;
      display: block;
    }
  }
  image {
    width: 40rpx;
    height: 40rpx;
    display: block;
    margin: 0 auto;
  }
  .tab {
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    border-radius: 16rpx;
    font-size: 30rpx;
    padding: 0 56rpx;
  }
  .up {
    background: #e5f1ff;
    color: #498afe;
  }
  .down {
    background: #498afe;
    color: #ffffff;
    margin-left: 28rpx;
  }
}
.popupView {
  height: 100%;
  padding-bottom: 100rpx;
  .popupTops {
    height: 77rpx;
    border-bottom: 1rpx solid #eee;
    text-align: center;
    line-height: 77rpx;
    font-size: 24rpx;
    color: #999;
    position: relative;
    .topIcon {
      position: absolute;
      top: 10rpx;
      left: 50%;
      transform: translateX(-50%);
      width: 80rpx;
      height: 8rpx;
      background-color: #999;
      border-radius: 4rpx;
    }
  }
  .boxSty {
    padding: 44rpx 41rpx 0rpx;
  }
  .liListSty {
    border: 1rpx solid #eeeeee;
    width: 88rpx;
    height: 88rpx;
    border-radius: 32rpx;
    text-align: center;
    line-height: 88rpx;
    color: #333;
    font-size: 32rpx;
    float: left;
    margin: 20rpx 23rpx;

    &.isRight {
      border: 1rpx solid #eeeeee;
      color: #fff;
      background: #36c75a;
    }

    &.isWrong {
      border: 1rpx solid #eeeeee;
      color: #fff;
      background: #ff3b30;
    }

    &.isPart {
      border: 1rpx solid #eeeeee;
      color: #fff;
      background: #ffc53d;
    }

    &.isOver {
      border: 1rpx solid #eeeeee;
      color: #fff;
      background: blue;
    }
    &.check_ans {
      border: 1rpx solid #eeeeee;
      color: #fff;
      background: #007aff;
    }
  }
}
</style>
