<template>
  <div id="datePackage" v-if="comboList">
    <!-- <div class="tag_item">
      <p class="tag_title">商品名称</p>
      <p class="package_name">{{showCourseData.course_name}}</p>
    </div> -->
    <div class="tag_item">
      <p class="tag_title">出行时间</p>
      <p class="package_name" :class="date?'active':''" @click="showCalendar=true">{{date||'选择日期'}}<van-icon name="arrow" /></p>
      <van-calendar v-model="showCalendar" :default-date="defaultDate" @confirm="chooseDate" :show-confirm="false" color="#3F92FF" :formatter="formatCalendar" />
    </div>
    <div class="tag_item" v-if="date">
      <p class="tag_title">行程选择</p>
      <div class="period">
        <div class="period_item" :class="Index1==index?'active':''" v-for="(item,index) in comboList" :key="index" @click="choosePeriod(index)">行程{{index + 1}} :{{item.dateComboName}}</div>
      </div>
    </div>
    <div class="tag_item" v-if="comboList[Index1]">
      <p class="tag_title">成团情况<span class="pintuan_status" v-if="comboList[Index1].clusterName">{{comboList[Index1].clusterName}}</span></p>
      <p class="package_name">最低拼团数 {{comboList[Index1].peopleCount}}{{comboList[Index1].unitName}} 已报名{{comboList[Index1].unitName}}数{{numOne}}/{{comboList[Index1].stockCount}}{{comboList[Index1].unitName}}</p>
    </div>
    <div class="tag_item" v-if="date">
      <p class="tag_title">套餐类型</p>
      <div class="period">
        <div class="period_item" :class="Index2==index?'active':''" v-for="(item,index) in comboList[Index1].comboInfoList" :key="index" @click="choosePeriod2(index)">套餐{{index + 1}} :{{item.comboName}} ¥{{item.actualPrice}}</div>
      </div>
    </div>
    <div class="tag_item">
      <div class="tag_title tag_title_flex">
        <span>请选择套餐数量</span>
        <div class="tag_title_right">
          <span class="btn" @click="countReduce">-</span>
          <span class="count">{{count}}</span>
          <span class="btn" @click="count++">+</span>
        </div>
      </div>
      <div class="package_num">
        <span>库存数量({{numSecond}} /</span>
        <span class="tint">{{numOne+numSecond}}{{comboList[Index1].unitName}})</span>
      </div>
    </div>
    <p class="package_des" v-if="comboList[Index1].comboInfoList[Index2].comboDescrption">套餐说明<br>{{comboList[Index1].comboInfoList[Index2].comboDescrption}}</p>
    <div class="Collection">
      <div class="box box1">
        <p><span>¥</span>{{comboList[Index1].comboInfoList[Index2].actualPrice*1000*count/1000}} <del>{{comboList[Index1].comboInfoList[Index2].productPrice*1000*count/1000}}</del></p>
      </div>
      <div class="box box2">
        <span @click="checkOrder()">去下单</span>
      </div>
    </div>

    <van-popup style="background: transparent;" get-container="body" v-model="showSchool" @close='closeSchool'>
      <school-add @closeAdd='closeAdd'></school-add>
    </van-popup>
  </div>
</template>
<script>
import schoolAdd from '@/views/Travel/component/schoolAdd'
export default {
  name: 'ServiceDatePackage',
  data () {
    return {
      date: null,
      defaultDate: null,
      packageData: '',
      showCourseData: '',
      comboList: '',
      Index1: 0,//选中的档期
      Index2: 0,//选中的套餐
      showCalendar: false,
      count: 1,
      days: 1,
      numOne: 0,
      numSecond: 0,
      showSchool: false
    }
  },
  mounted () {
    localStorage.removeItem('selectedContactArr')
    // date 日期
    const date = this.$route.query.date;
    if (date) {
      this.date = date
      this.defaultDate = new Date(date)
    }
    // 缓存中读取排期
    let packageData = localStorage.getItem('packageData')
    if (packageData) {
      this.packageData = JSON.parse(packageData)

      console.log(this.packageData)
      if (this.date) {
        this.setComboList()
      }
    }
    // 当前的商品信息
    let showCourseData = localStorage.getItem('showCourseData')
    if (showCourseData) {
      this.showCourseData = JSON.parse(showCourseData)
    }
    console.log(showCourseData)
  },
  methods: {
    // 获取库存
    GetOrderPayCount (bindId) {
      let showCourseData = JSON.parse(localStorage.getItem('showCourseData'))
      this.mgop({
        api: 'mgop.sz.hswsy.OrderPayCount', // 必须
        host: 'https://mapi.zjzwfw.gov.cn/',
        dataType: 'JSON',
        type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          "bindId":bindId,
          "productId": showCourseData.id,
          "startDate": this.date,
        },
        onSuccess: res => {
          if (res.data.data) {
            this.numOne = res.data.data
          }
        },
        onFail: err => {
          console.log('err', err)
        }
      });
    },
    // 获取库存第二个参数
    getSurplusComboCount (bindId) {
      let showCourseData = JSON.parse(localStorage.getItem('showCourseData'))
      this.mgop({
        api: 'mgop.sz.hswsy.SurplusComboCount', // 必须
        host: 'https://mapi.zjzwfw.gov.cn/',
        dataType: 'JSON',
        type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          "bindId": bindId,
          "productId": showCourseData.id,
          "startDate": this.date,
        },
        onSuccess: res => {
          if (res.data.data) {
            this.numSecond = res.data.data
          }
        },
        onFail: err => {
          console.log('err', err)
        }
      });
    },
    // 选择排期
    choosePeriod (index) {
      this.Index1 = index;
      this.Index2 = 0;
      let bindId = this.comboList[index].id
      console.log(this.comboList[index])
      this.GetOrderPayCount(bindId)
      this.getSurplusComboCount(bindId)
    },
    // 选择套餐
    choosePeriod2 (index) {
      this.Index2 = index;
    },
    // 选择日期
    chooseDate (date) {
      let canChoose = false
      for (let i in this.packageData) {
        if (this.Moment(date, 'YYYY-MM-DD').format('YYYY-MM-DD') == this.Moment(this.packageData[i].dayName, 'YYYY-MM-DD').format('YYYY-MM-DD')) {
          canChoose = true;
        }
      }
      if (!canChoose) {
        this.$toast('当前日期无可选套餐')
        return;
      }
      this.date = this.Moment(date, 'YYYY-MM-DD').format('YYYY-MM-DD')
      this.Index1 = 0;
      this.Index2 = 0;
      this.showCalendar = false;
      this.setComboList()
    },
    // 设置日历文字
    formatCalendar (day) {
      // console.log(day)
      for (let i in this.packageData) {
        if (this.Moment(day.date, 'YYYY-MM-DD').format('YYYY-MM-DD') == this.Moment(this.packageData[i].dayName, 'YYYY-MM-DD').format('YYYY-MM-DD')) {
          day.bottomInfo = '¥' + this.packageData[i].comboList[0].comboInfoList[0].actualPrice;
        }
      }
      return day;
    },
    // 根据日期设置套餐
    setComboList () {
      for (let i in this.packageData) {
        if (this.Moment(this.packageData[i].dayName, 'YYYY-MM-DD').format(('YYYY-MM-DD')) == this.date) {
          let comboList = this.packageData[i].comboList;
          this.comboList = comboList
          let bindId = comboList[this.Index1].id
          this.days = this.packageData[i].days
          this.GetOrderPayCount(bindId)
          this.getSurplusComboCount(bindId)
          break;
        }
      }
    },
    // 减少数量
    countReduce () {
      if (this.count > 1) {
        this.count--
      }
    },

    checkOrder () {
      if (this.count > this.numSecond) {
        this.$toast('库存不足');

      } else {
        const query = {
          date: this.date,
          count: this.count
        }
        localStorage.setItem('selectCombo', JSON.stringify(this.comboList[this.Index1].comboInfoList[this.Index2]))
        localStorage.setItem('selectComboBindId', JSON.stringify(this.comboList[this.Index1].id))
        localStorage.setItem('selectComboUnitName', this.comboList[this.Index1].unitName)
        this.$router.push({ name: 'ServiceCheckOrder', query: query })
      }
    },
    closeSchool () {
      if (localStorage.getItem('schoolNames') == 'undefined' || !localStorage.getItem('schoolNames')) {
        // this.showSchool = true
      }
    },
    closeAdd () {
      this.showSchool = false
    }
  },
  components: {
    schoolAdd
  }
}
</script>
<style lang="scss" scoped>
#datePackage {
  padding-top: 40px;
  .tag_item {
    padding: 22px;
    .tag_title {
      font-size: 34px;
      font-weight: bold;
      margin-top: 0;
      margin-bottom: 32px;
      display: flex;
      align-items: center;
      .pintuan_status {
        font-size: 24px;
        padding: 2px 10px;
        background: #ffe9b1;
        border-radius: 6px;
        margin-left: 12px;
      }
    }
    .package_name {
      display: flex;
      justify-content: space-between;
      font-size: 28px;
      width: 100%;
      background: #f5f6fa;
      border-radius: 12px;
      padding: 20px 18px;
      box-sizing: border-box;
      margin: 0;
      &.active {
        background: #3f92ff;
        color: #fff;
      }
    }
    // 时间段
    .period {
      .period_item {
        padding: 10px 20px;
        font-size: 28px;
        margin-bottom: 15px;
        text-align: left;
        background: #f5f6fa;
        border-radius: 10px;
        box-sizing: border-box;
        &.active {
          background: #3f92ff;
          color: #fff;
        }
      }
    }
    .tag_title_flex {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0;
      padding: 50px 0;
      .tag_title_right {
        display: flex;
        align-items: center;
      }
      .btn {
        width: 48px;
        height: 48px;
        background: #f5f6fa;
        border-radius: 10px;
        font-size: 32px;
        line-height: 48px;
        text-align: center;
      }
      .count {
        width: 100px;
        text-align: center;
        font-size: 32px;
      }
    }
    .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;
      }
    }
  }
  .package_des {
    margin: 0 24px;
    margin-top: 28px;
    padding: 20px 0;
    font-size: 26px;
    line-height: 40px;
    color: #a9a9aa;
    border-top: 1px solid #e2e7ee;
  }
  .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%;
    }
  }
}
::v-deep .van-calendar__bottom-info {
  color: #f95555;
}
::v-deep .van-calendar__selected-day {
  background: #ecf2fc !important;
  border: 4px solid #79abff;
  box-sizing: border-box;
  color: #333;
}
</style>
<style lang="scss">
.elder {
  #datePackage {
    .tag_item {
      .tag_title {
        font-size: 44px;
      }
      .package_name {
        font-size: 40px;
      }
      .period .period_item {
        font-size: 40px;
      }
      .tag_title_flex .count {
        font-size: 40px;
      }
      .package_num {
        font-size: 40px;
        .tint {
          font-size: 40px;
        }
      }
    }
    .Collection {
      .box1 p {
        font-size: 56px;
        span {
          font-size: 40px;
        }
        del {
          font-size: 40px;
        }
      }
      .box > span {
        font-size: 44px;
      }
    }
  }
}
</style>