<template>
  <div class="talent_reserve">
    <!-- <div class="course">
      <img :src="showCourseData.course_img" alt="" />
      <div>
        <p class="goods_title">{{ showCourseData.course_name }}</p>
        <div class="tag_box">
          <template v-for="(tag, index) in showCourseData.course_labels">
            <span class="tag" v-if="tag != ''" :key="index">{{ tag }}</span>
          </template>
        </div>
      </div>
    </div> -->
    <div class="talent">
      <van-cell-group>
        <van-field
          is-link
          readonly
          name="团期"
          label="团期"
          :placeholder="group_date ? group_date : '请选择研学出行日期'"
          @click="showCalendar = true"
        />
      </van-cell-group>
      <p class="group_count" v-if="selectedDate">
        本团最低成团人数：{{ orderBuyType == 1 ? selectedDate.mininumCount : selectedDate.shareMinCount }}人 满团人数：{{
          selectedDate.stockCount
        }}人
      </p>
      <div class="tag_title tag_title_flex">
        <span>我自己参加数量</span>
        <van-stepper v-model="count" />
      </div>
      <div class="package_num" v-if="selectedDate">
        <span>库存数量({{ selectedDate.stockCount - selectedDate.bookingCount }} /</span>
        <span class="tint">{{ selectedDate.stockCount }})</span>
      </div>
    </div>
    <div class="Collection">
      <div class="box box1">
        <p v-if="selectedDate">
          <span>¥</span>{{ (selectedDate.actualPrice * 1000 * count) / 1000 }}
          <del>{{ (selectedDate.productPrice * 1000 * count) / 1000 }}</del>
        </p>
      </div>
      <div class="box box2">
        <span @click="checkOrder()" v-if="BoardData.length > 0">确认信息</span>
        <span class="disabled" v-else>暂无可选团期</span>
      </div>
    </div>
    <van-calendar
      v-model="showCalendar"
      :default-date="defaultDate"
      :min-date="defaultDate"
      @confirm="chooseDate"
      :show-confirm="false"
      color="#3F92FF"
      :formatter="formatCalendar"
    />
    <van-popup v-model="showCodeToast" round>
      <CodeToast @hiddenToast="showCodeToast = false"></CodeToast>
    </van-popup>
  </div>
</template>
<script>
import CodeToast from './c_CodeToast.vue'
export default {
  components: {
    CodeToast,
  },
  data() {
    return {
      showCourseData: '', //当前课程的信息
      // peopleCount: '',
      group_date: '',
      selectedDate: '',
      defaultDate: new Date(),
      showCalendar: false, //显示日历
      BoardData: [], //团期数组
      periodArr: [],
      ReservePeriod: '', //选中预约的套餐
      comboName: '',
      actualPrice: '',
      orderBuyType: 2, //1独享2共享
      count: 1,
      disabledType: false, //常规拼团禁用独享团
    }
  },
  mounted() {
    this.defaultDate = new Date(
      this.Moment()
        .add(1, 'day')
        .format('YYYY-MM-DD')
    )

    let showCourseData = sessionStorage.getItem('showCourseData')
    if (showCourseData) {
      this.showCourseData = JSON.parse(showCourseData)
    }
    let TalentReserveGroupData = sessionStorage.getItem('TalentReserveGroupData')
    if (TalentReserveGroupData) {
      TalentReserveGroupData = JSON.parse(TalentReserveGroupData)
      if (TalentReserveGroupData.dateBindTypeName == '常规拼团') {
        this.disabledType = true
      }
    }
    this.getBoard() // 获取可使用团期
    this.getStandardCombo() //获取默认团期套餐
  },
  methods: {
    // 获取可使用团期
    getBoard(BoardData = [], month = 0) {
      let date = this.Moment()
        .add(month, 'month')
        .format('YYYY-MM-DD')
      let ProgramType = this.$route.query.ProgramType || null
      let productSceneId = this.$route.query.productSceneId
      if (!productSceneId) {
        this.$toast('页面参数错误')
        return
      }
      this.yxAxios
        .get(
          `${this.yanxueUrl}/api/GroupPeriod/SchoolBooking/Board?productSceneId=${productSceneId}&date=${date}${
            ProgramType ? '&ProgramType=' + ProgramType : ''
          }`
        )
        .then((res) => {
          // console.log('团期列表:', res.data.data)
          if (res.data.data) {
            let data = res.data.data
            for (let i in data) {
              if (data[i].isOpen) {
                BoardData.push(data[i])
              }
            }
            console.log(`加第${month + 1}个月：`, BoardData)
            if (month >= 4) {
              this.BoardData = BoardData
              this.showCalendar = true;
              console.log('团期列表:', BoardData)
            } else {
              this.getBoard(BoardData, month + 1)
            }
          }
        })
    },
    //获取默认团期套餐
    getStandardCombo() {
      this.yxAxios.get(`${this.yanxueUrl}/api/Product/StandardCombo/ByProductId?productId=${this.showCourseData.id}`).then((res) => {
        if (res.data.status == 1) {
          this.comboName = res.data.data.comboName
          this.actualPrice = res.data.data.actualPrice
        }
      })
    },
    // 达人定制提交订单
    checkOrder() {

      if (!this.group_date) {
        this.$toast('请先选择团期')
        return
      }
      let isLogin = this.checkLogin()
      if (!isLogin) return
      const query = {
        date: this.date,
        count: this.count,
      }
      sessionStorage.setItem(
        'TalentReserveData',
        JSON.stringify({
          ...this.selectedDate,
          orderBuyType: this.orderBuyType,
        })
      )
      this.$router.push({ name: 'ServiceCheckOrderReserve', query: query })
    },
    // 判断是否已登录
    checkLogin() {
      // if (process.env.NODE_ENV != "production") return true;

      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      const publicName = sessionStorage.getItem('publicName')
      if (!userInfo?.phone) {
        sessionStorage.setItem('prePage', 'TalentReserve')
        sessionStorage.setItem('prePageQuery', JSON.stringify({ ProgramType: this.$route.query.ProgramType,
          productSceneId: this.$route.query.ProgramType }))
        this.$router.push({ name: 'LoginQYX', query: { publicName: publicName } })
        return false
      }
      //  else if (sessionStorage.getItem('schoolNames') == 'undefined' || !sessionStorage.getItem('schoolNames')) {
      //   this.showSchool = true
      //   return false;
      // }
      return true
    },
    // 选择日期
    chooseDate(date) {
      let hasDate = false
      let hasCount = true
      let isCanNotBuy = false
      if (date) {
        for (let i in this.BoardData) {
          if (
            this.Moment(date, 'YYYY-MM-DD').format('YYYY-MM-DD') == this.Moment(this.BoardData[i].date, 'YYYY-MM-DD').format('YYYY-MM-DD')
          ) {
            hasDate = true
            // 锁单人数大于等于库存
            if (this.BoardData[i].bookingCount >= this.BoardData[i].stockCount) {
              hasCount = false
            } else if (this.BoardData[i].isCanNotBuy) {
              isCanNotBuy = true
            } else {
              let group_date = this.BoardData[i].date
              this.group_date = this.Moment(group_date, 'YYYY-MM-DD').format('YYYY-MM-DD')
              this.selectedDate = this.BoardData[i]
            }
          }
        }
      }
      if (!hasDate) {
        this.$toast('当前日期无可选团期')
      } else if (!hasCount) {
        this.$toast('当前日期对应团期已售罄')
      } else if (isCanNotBuy) {
        this.$toast('当前日期对应团期已过成团时间')
      } else {
        this.showCalendar = false
        this.orderBuyType = 2
      }
    },
    // 设置日历文字
    formatCalendar(day) {
      for (let i in this.BoardData) {
        if (
          this.Moment(day.date, 'YYYY-MM-DD').format('YYYY-MM-DD') == this.Moment(this.BoardData[i].date, 'YYYY-MM-DD').format('YYYY-MM-DD')
        ) {
          day.topInfo = '￥' + this.BoardData[i].actualPrice
          let Remaining = this.BoardData[i].stockCount - this.BoardData[i].bookingCount
          if (Remaining <= 0) {
            day.bottomInfo = '售罄'
          } else if (this.BoardData[i].isCanNotBuy) {
            day.bottomInfo = '停售'
          } else {
            day.bottomInfo = '剩余' + Remaining
          }
        }
      }
      return day
    },
    // 减少数量
    countReduce() {
      if (this.count > 1) {
        this.count--
      }
    },
  },
}
</script>
<style lang="scss">
.talent_reserve {
  .course {
    display: flex;
    justify-content: left;
    padding: 20px 0;
    border-bottom: 3px dashed #e2e2e2;
    margin: 0 auto;
    margin-bottom: 40px;
    width: 702px;
    img {
      display: inline-block;
      width: 128px;
      height: 128px;
      border-radius: 8px;
    }
    > div {
      vertical-align: top;
      display: inline-block;
      margin-left: 20px;
      // width: 560px;
    }
    p {
      margin: 0;
    }
    .goods_title {
      width: 520px;
      font-size: 30px;
      font-weight: bold;
      white-space: pre-wrap;
    }
    .tag_box {
      padding-top: 10px;
      .tag {
        height: 44px;
        line-height: 44px;
        display: inline-block;
        margin-top: 10px;
        margin-right: 12px;
        color: #4092ff;
        font-size: 24px;
        border-radius: 8px;
        background: #eef8ff;
        padding: 0 22px;
      }
    }

    .package_name {
      width: 520px;
      font-size: 28px;
      margin-top: 12px;
      color: #999;
      white-space: pre-wrap;
      .name {
        color: #999;
      }
    }
    .price {
      font-size: 28px;
      color: #ff5f5f;
      margin-top: 20px;
      .old_price {
        text-decoration: line-through;
        color: #999;
      }
    }
  }
  .period_box {
    padding: 20px;
    .title {
      font-size: 28px;
    }
    .period_item_box {
      background: rgb(246, 249, 254);
      border-radius: 20px;
      padding: 10px 20px;
      margin-top: 20px;
    }
    .period_nodata {
      margin-top: 20px;
      font-size: 28px;
      color: #999;
    }
    .period_item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 0;
      .period_des {
        font-size: 24px;
        margin-top: 10px;
      }
      .period_title {
        font-weight: bold;
        font-size: 28px;
      }
      .price {
        font-size: 28px;
        color: rgb(255, 47, 81);
        .old_price {
          text-decoration: line-through;
          font-size: 24px;
          color: #999;
        }
      }
      .btn {
        display: block;
        font-size: 24px;
        color: #fff;
        background: rgb(255, 47, 81);
        border-radius: 8px;
        padding: 10px 24px;
        margin-top: 10px;
      }
    }
  }
  .talent {
    background: #fff;
    padding: 36px 28px;
    height: 1100px;
    max-height: 70vh;
    overflow: auto;
    box-sizing: border-box;
    .group_type_box {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      margin-top: 40px;
      .group_type_item {
        width: 340px;
        height: 164px;
        padding: 20px 18px;
        box-sizing: border-box;
        border-radius: 12px;
        border: 2px solid #e7e7e7;
        &.active {
          border-color: #ff9016;
          background: #fffcf2;
          .group_type_title {
            color: #ff9016;
          }
        }
        img {
          display: inline-block;
          width: 36px;
          vertical-align: middle;
        }
        .group_type_title {
          display: inline-block;
          font-size: 30px;
          font-weight: bold;
          color: #333;
          margin-left: 10px;
          vertical-align: middle;
        }
        .group_type_des {
          font-size: 24px;
          line-height: 34px;
          margin-top: 14px;
          color: #666;
        }
      }
      .group_type_tip {
        width: 100%;
        padding: 20px 18px;
        border-radius: 12px;
        border: 2px solid #e7e7e7;
        margin-top: 30px;
        position: relative;
        margin-bottom: 40px;
        img {
          width: 26px;
          display: inline-block;
          vertical-align: top;
        }
        p {
          width: 600px;
          display: inline-block;
          vertical-align: top;
          font-size: 24px;
          line-height: 36px;
          color: #666;
          margin-left: 10px;
        }
        .arrow {
          position: absolute;
        }
      }
    }
    .tag_title {
      font-size: 34px;
      font-weight: bold;
      margin-top: 0;
      margin-bottom: 32px;
      display: flex;
      align-items: center;
    }
    .tag_title_flex {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0;
      padding: 50px 0;
    }
    .package_num {
      font-size: 30px;
      width: 50%;
      float: right;
      text-align: right;
      border-radius: 12px;
      padding: 0px 18px;
      box-sizing: border-box;
      .tint {
        font-size: 30px;
        font-weight: normal;
        color: #909399;
      }
    }
    .group_count {
      font-size: 28px;
      line-height: 54px;
      color: #ff5353;
      background: linear-gradient(90deg, #ffffff 0%, #ffe9e9 49%, #ffffff 100%);
      text-align: center;
      margin-top: 48px;
    }
    .submit_btn {
      width: 448px;
      height: 92px;
      line-height: 92px;
      text-align: center;
      border-radius: 58px;
      color: #fff;
      background: #4092ff;
      display: block;
      margin: 0 auto;
      box-shadow: 0px 10px 40px 0px rgba(160, 201, 255, 1);
      font-size: 32px;
      margin-top: 50px;
      &.disable {
        opacity: 0.5;
      }
    }
  }
  .Collection {
    background-color: white;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 28px 22px;
    box-sizing: border-box;
    padding-bottom: 50px;
    z-index: 500;
    box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.34);

    .box {
      display: inline-block;
      vertical-align: middle;
      color: #666666;
      .icon {
        display: inline-block;
        width: 45%;
        text-align: center;
        font-size: 24px;
        p {
          // margin: 0;
          color: #999999;
          margin-top: 8px;
        }
        i {
          font-size: 48px;
          color: rgb(45, 45, 45);
          font-weight: bold;
        }
      }

      > span {
        display: inline-block;
        width: 100%;
        line-height: 92px;
        box-sizing: border-box;
        border-radius: 100px;
        color: white;
        font-size: 32px;
        background: #4092ff;
        text-align: center;
        box-shadow: 0px 10px 40px 0px #a0c9ff;
        b {
          display: inline-block;
          vertical-align: middle;
          margin-left: 20px;
          color: white;
        }
        i {
          vertical-align: middle;
          font-size: 44px;
          font-weight: bold;
        }
      }
    }
    .box1 {
      width: 40%;
      p {
        font-size: 49px;
        font-weight: bold;
        color: #f64c37;
      }
      span {
        font-size: 32px;
      }
      del {
        font-size: 20px;
        color: #999;
      }
    }
    .box2 {
      width: 60%;
      .disabled {
        opacity: 0.5;
      }
    }
  }
}
</style>
<style>
.van-calendar__top-info {
  color: #ff2f51;
}
</style>
