<template>
  <div class="appointment-add">
    <div class="appointment-add__header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          v-for="(item, index) in $route.matched"
          :key="index"
          :to="{ path: item.path }"
          >{{ item.name }}</el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <div class="appointment-add__body">
      <el-form
        class="appointment-content"
        ref="form"
        :model="form"
        label-width="100px"
      >
        <div class="appointment-content__body">
          <el-descriptions title="预约考试"></el-descriptions>
          <el-form-item label="考试地点：">
            <el-select
              v-model="addressId"
              placeholder="请选择"
              @change="addressChange"
            >
              <el-option
                :label="item.siteAddress"
                :value="item.id"
                v-for="(item, index) in listData"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="考试时间：">
            <el-select
              v-if="activeList && activeList.length"
              v-model="timeIndex"
              placeholder="请选择"
            >
              <el-option
                :label="
                  item.dataTime +
                  ' ' +
                  item.startTime +
                  ' ~ ' +
                  item.endTime +
                  `已报：${item.registration}/ ${item.num} ` +
                  (item.status === 2
                    ? '预约名额已满'
                    : item.status === 1
                    ? '此时段您已经有其他考试预约'
                    : '')
                "
                :disabled="item.status !== 0"
                :value="index"
                v-for="(item, index) in activeList"
                :key="index"
              ></el-option>
            </el-select>
            <span v-if="addressId && !activeList.length">该考试地点暂无考试时间，建议重新选择考试地点</span>
          </el-form-item>
        </div>

        <div class="appointment-content__footer">
          <div class="btns">
            <div class="cancel" @click="cancel">上一步</div>
            <div class="ok" @click="sureOppoint">
              {{ dataId == 1 ? "下一步" : dataId == 2 ? "确定预约" : "异常" }}
            </div>
          </div>
        </div>
      </el-form>
    </div>

    <el-dialog
      width="400px"
      class="appointment-modal"
      :visible.sync="showDetailModal"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <div class="appointment-modal__content">
        <div class="appointment-modal__close" @click="showDetailModal = false">
          X
        </div>
        <div class="appointment-modal__header">提示</div>
        <div class="appointment-modal__body">
          <div class="content">
            <div class="content__body">
              <div class="icon-text">
                <div class="icon">✔</div>
                <div class="text">考试预约成功</div>
              </div>
              <div class="confirm" @click="viewAppointment">查看预约</div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  name: "Myappointment",
  data() {
    return {
      form: {},
      showDetailModal: false,
      addressId: "", //当前选中考试点ID
      timeIndex: "", //当前选中考试点ID
      listData: [], //考试地点数据
      activeList: [], //选中考试地点列表
      applyId: "",
      goodsId: "",
      applyStatus: "",
      dataId: "",
      orderGoodsId: "",
      addressName: "",
    };
  },
  mounted() {
    this.applyId = this.$route.query.applyId;
    this.goodsId = this.$route.query.goodsId;
    this.applyStatus = this.$route.query.applyStatus;
    this.dataId = this.$route.query.dataId;
    this.orderGoodsId = this.$route.query.orderGoodsId;

    this.getApplysubscribeApplySite();
  },
  methods: {
    ...mapMutations(["updateApplyData"]),
    cancel() {
      this.$router.go(-1);
    },
    viewAppointment(){
      this.$router.push({
        path:"/person-center/my-examination"
      })
    },
    addressChange() {
      var arrays = [];
      var self = this;
      const index = this.listData.findIndex(
        (item) => item.id == this.addressId
      );
      this.addressName = this.listData[index].siteAddress;
      this.listData[index].examUserApplySiteTime.forEach((item) => {
        item.examApplySiteTimeTwoVo.forEach((items) => {
          arrays.push({
            examTime: item.examTime,
            startTimeC: items.startTime,
            endTimeC: items.endTime,
            dataTime: self.$tools
              .timestampToTime(item.examTime)
              .replace(/-/g, "/"),
            startTime: items.startTime,
            endTime: items.endTime,
            num: items.num,
            registration: items.registration,
            checked: false,
            status:
              items.status === 1
                ? items.status
                : items.registration >= items.num
                ? 2
                : items.status,
          });
        });
      });
      this.timeIndex = "";
      this.activeList = arrays.filter((item, index) => {
        console.log(item);
        const newTime = parseInt(new Date().getTime() / 1000);
        console.log(
          item.dataTime.replace(/-/g, "/") + "" + item.startTime + ":00"
        );
        const liTime = parseInt(
          new Date(
            item.dataTime.replace(/-/g, "/") + "" + item.startTime + ":00"
          ).getTime() / 1000
        );
        console.log(liTime, "liTime");
        console.log(newTime, "newTime");
        if (liTime > newTime) {
          return item;
        }
      });

      console.log(this.activeList);
    },

    //获取考试地点
    getApplysubscribeApplySite() {
      this.$request
        .getApplysubscribeApplySite({ applyId: this.applyId })
        .then((res) => {
          res.data.forEach((item, index) => {
            item.checked = false;
          });
          this.listData = res.data;
        });
    },

    sureOppoint() {
      var self = this;
      if (self.addressId) {
        if (this.timeIndex !== "") {
          var copyData = JSON.parse(JSON.stringify(self.activeList));
          const index = copyData.findIndex(
            (item, index) => this.timeIndex == index
          );
          var data = {
            applyId: self.applyId,
            goodsId: self.goodsId,
            studentType: self.applyStatus,
            applySiteAddress: self.addressName,
            applySiteExamTime: copyData[index].examTime,
            applySiteStartTime: copyData[index].startTimeC,
            applySiteEndTime: copyData[index].endTimeC,
            orderGoodsId: self.orderGoodsId,
          };
          if (self.dataId == 1) {
            this.updateApplyData(data);
            this.$router.push({
              path: "/person-center/my-classhour/appointment-add-kp",
              query: {
                applyId: self.applyId,
                applyStatus: self.applyStatus,
                goodsId: self.goodsId,
                orderGoodsId: self.orderGoodsId,
              },
            });
          }
          if (self.dataId == 2) {
            self.$request.addApply(data).then((res) => {
              this.showDetailModal = true;
            });
          }
        } else {
          this.$message({
            type: "warning",
            message: "请选择考试时间",
          });
        }
      } else {
        this.$message({
          type: "warning",
          message: "请选择考试地点",
        });
      }
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.appointment-add {
  overflow: hidden;
  &__header {
    border-bottom: 1px solid #eee;
    margin-top: 24px;
    padding-bottom: 20px;
  }
  &__body {
    .appointment-content {
      &__header {
        margin-top: 20px;
        width: 1110px;
        padding: 10px 12px;
        background: #fafbfc;
        border: 1px solid #eeeeee;
        box-shadow: 0px 0px 7px 1px rgba(0, 0, 0, 0.04);
        border-radius: 8px;

        .select {
          width: 1000px;
        }

        .total {
          margin-top: 10px;
          margin-left: 80px;
          font-size: 14px;

          .note {
            color: #ff3b30;
          }
        }
      }

      &__body {
        margin-top: 20px;
        width: 1110px;
        padding: 10px 12px;
        background: #fafbfc;
        border: 1px solid #eeeeee;
        box-shadow: 0px 0px 7px 1px rgba(0, 0, 0, 0.04);
        border-radius: 8px;

        .el-select {
          width: 300px;
        }
      }

      &__footer {
        margin-top: 20px;
        padding-bottom: 40px;

        .appointment-price {
          border-top: 1px solid #eee;
          border-bottom: 1px solid #eee;
          line-height: 50px;
          font-size: 18px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #333333;
          .note {
            font-size: 18px;
            color: #ff3b30;
            font-weight: bold;
            margin-left: 10px;
          }
        }

        .btns {
          margin-top: 20px;
          .cancel {
            display: inline-block;
            margin: 0 10px;
            cursor: pointer;
            width: 122px;
            height: 32px;
            background: #ffffff;
            border: 1px solid #3f8dfd;
            border-radius: 16px;
            text-align: center;
            font-size: 16px;
            line-height: 30px;
            color: #3f8dfd;
          }

          .ok {
            display: inline-block;
            margin: 0 10px;
            cursor: pointer;
            width: 122px;
            height: 32px;
            background: #3f8dfd;
            border-radius: 16px;
            color: #fff;
            text-align: center;
            line-height: 32px;
            font-size: 16px;
          }
        }
      }
    }
  }

  .appointment-modal {
    /deep/ .el-dialog__header {
      display: none;
    }
    /deep/ .el-dialog__body {
      padding: 0;
      overflow: unset;
    }

    &__close {
      cursor: pointer;
      position: absolute;
      right: 0;
      top: 0;
      width: 24px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      color: #999999;
    }

    &__header {
      height: 40px;
      line-height: 40px;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #333333;
      padding-left: 24px;
    }

    &__body {
      height: 200px;
      padding: 0 24px;

      .content {
        &__body {
          .icon-text {
            .icon {
              margin: 0 auto;
              border-radius: 50%;
              width: 64px;
              height: 64px;
              background: #34c759;
              text-align: center;
              line-height: 64px;
              font-size: 50px;
              color: #fff;
              font-weight: bold;
            }

            .text {
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: bold;
              color: #666666;
              text-align: center;
              margin-top: 15px;
            }
          }
          .confirm {
            margin: 35px auto 0;
            cursor: pointer;
            width: 96px;
            height: 40px;
            background: #3f8dfd;
            border-radius: 4px;
            text-align: center;
            line-height: 40px;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>
