<template>
  <div id="checkOrder">
    <div class="goods_data top_border">
      <div class="top">
        <img :src="showCourseData.course_img" alt="" />
        <div>
          <p class="goods_title">{{ showCourseData.course_name }}</p>
          <p class="package_name">套餐名称:{{ TalentReserveGroupData.productSceneName }}</p>
          <p class="count">数量:x{{ count }}{{ TalentReserveGroupData.UnitName }}</p>
        </div>
      </div>
      <div class="bottom">
        <p class="time">使用时间: {{ Moment(TalentReserveData.date).format('YYYY-MM-DD') }}</p>
        <p class="price"><span>¥</span>{{ (TalentReserveData.actualPrice * 1000 * count) / 1000 }}</p>
      </div>
    </div>
    <!-- <div class="card top_border" v-if="TalentReserveData.actualPrice > 0" @click="openCardBox">
      <p>可用优惠券</p>
      <div>
        <span class="use_card" v-if="useCard">已使用红包抵扣{{ useCard.couponPrice }}</span>
        <van-icon name="arrow" />
      </div>
    </div> -->
    <div class="top_border">
      <contact-box :limit="count" :people="TalentReserveGroupData.adultCount" :child="TalentReserveGroupData.childCount"></contact-box>
    </div>
    <div class="card top_border" @click="showRemark = true">
      <p>其他要求</p>
      <div class="remark">
        <span>{{ remark }}</span>
        <van-icon name="arrow" />
      </div>
    </div>
    <!-- <div class="card top_border">
      <p>服务承诺和保障</p>
      <van-icon name="arrow" />
    </div>
    <div class="protocol">
      <van-checkbox v-model="checked" shape="square" icon-size="16px" @change="protocolChange">我已阅读并同意 </van-checkbox>
      <span style="text-decoration: underline" @click="goPayneed">购买须知</span>
      <span style="text-decoration: underline" @click="goProtocol">用户协议</span>
    </div> -->
    <div class="Collection">
      <div class="box box1">
        <p>
          <span>¥</span>{{ paymoney }} <del>{{ (TalentReserveData.productPrice * 1000 * count) / 1000 }}</del>
        </p>
      </div>
      <div class="box box2" v-if="userInfo">
        <span @click="createOrder()">提交信息</span>
      </div>
    </div>
    <van-popup class="remark_box" v-model="showRemark" round>
      <textarea class="remark_textarea" v-model="editRemark" placeholder="请输入" cols="30" rows="10"></textarea>
      <span class="remark_btn" @click="EditRemarkConfirm">完成</span>
    </van-popup>
  </div>
</template>
<script>
import Axios from 'axios'
import wx from 'weixin-js-sdk'
import ContactBox from './component/c_ContactBox.vue'
import { parse } from 'qs'
export default {
  name: 'ServiceCheckOrderReserve',
  data() {
    return {
      date: '',
      period: '',
      count: 1,
      checked: false,
      useCard: '',
      orderDetail: '',
      TalentReserveGroupData: '',
      TalentReserveData: '', //套餐
      showCourseData: '', //当前课程的信息
      publicName: '',
      userInfo: '', //支付用
      appId: 'wx1305e88d2bc74073',
      CourseParentId: '', //学习中心途径购买课程对应参数
      CourseParentIdBindCourseId: '', //学习中心途径购买课程对应参数
      customizedOrder: '',
      isSkipPay: false,
      remark: '', //留言
      editRemark: '', //留言
      showRemark: false, //留言弹窗显示
      businessId: '1614900216',
    }
  },
  mounted() {

    

    let userInfo = localStorage.getItem('userInfo')
    if (userInfo) {
      this.userInfo = JSON.parse(userInfo)
    }

    this.count = this.$route.query.count
    this.period = this.$route.query.period

    // 套餐
    let TalentReserveGroupData = localStorage.getItem('TalentReserveGroupData')
    if (TalentReserveGroupData) {
      this.TalentReserveGroupData = JSON.parse(TalentReserveGroupData)
    }
    let TalentReserveData = localStorage.getItem('TalentReserveData')
    if (TalentReserveData) {
      this.TalentReserveData = JSON.parse(TalentReserveData)
    }

    // 如果有优惠券,就展示
    let useCard = localStorage.getItem('useCard')
    let showCourseData = localStorage.getItem('showCourseData')
    if (useCard) {
      this.useCard = JSON.parse(useCard)
    }
    if (showCourseData) {
      this.showCourseData = JSON.parse(showCourseData)
    }
    // 留言恢复(防止跳转选择联系人后留言丢失)
    let remark = localStorage.getItem('checkOrderRemark')
    if (remark) {
      this.remark = remark
    }
    // 协议同意状态恢复(防止跳转选择联系人后选中状态丢失)
    let checked = localStorage.getItem('checkOrderProtocolChecked')
    if (checked == 'true') {
      this.checked = true
    }
    // 集体定制参数
    // orderId 集体定制订单 isSkipPay 是否跳过支付 dateComboBindId 档期id
    let customizedOrder = localStorage.getItem('customizedOrder')
    if (customizedOrder) {
      this.customizedOrder = JSON.parse(customizedOrder)
      this.isSkipPay = this.customizedOrder.isSkipPay
      if (this.isSkipPay) {
        localStorage.removeItem('useCard')
        this.useCard = ''
      }
    }

    // 学习中心途径购买课程
    this.CourseParentId = localStorage.getItem('CourseParentId')
    this.CourseParentIdBindCourseId = localStorage.getItem('CourseParentIdBindCourseId')
  },
  computed: {
    paymoney() {
      let paymoney = (this.TalentReserveData.actualPrice * 1000 * this.count - (this.useCard?.couponPrice * 1000 || 0)) / 1000
      if (paymoney < 0) paymoney = 0
      return paymoney
    },
  },
  methods: {
    // 进入选择优惠券
    openCardBox() {
      this.$router.push({ name: 'ServiceCardSelect' })
    },
    //购买须知
    goPayneed() {
      this.$router.push({ name: 'payneed' })
    },
    // 前往查看协议
    goProtocol() {
      this.$router.push({ name: 'protocol' })
    },
    // 协议点击同意
    protocolChange(checked) {
      localStorage.setItem('checkOrderProtocolChecked', checked)
    },
    // 留言点击完成
    EditRemarkConfirm() {
      this.remark = this.editRemark
      localStorage.setItem('checkOrderRemark', this.editRemark)
      this.showRemark = false
    },
    // 创建订单
    createOrder() {
      // 出行人数量判断
      let selectedContactArr = localStorage.getItem('selectedContactArr')
      selectedContactArr = JSON.parse(selectedContactArr)
      let selAdult = 0
      let selChild = 0
      for (let i in selectedContactArr) {
        if (selectedContactArr[i].userType == 2) {
          selAdult++
        } else {
          selChild++
        }
      }
      // 判断选择成人和学生数量是否匹配
      if (
        selAdult != this.count * this.TalentReserveGroupData.adultCount ||
        selChild != this.count * this.TalentReserveGroupData.childCount
      ) {
        this.$toast.fail('请选择正确数量的出行人')
        return
      }

      // 出行人字段格式
      let selectedContact = []
      for (let i in selectedContactArr) {
        selectedContact.push(selectedContactArr[i].travelerNum)
      }
      // 集体订单参数
      let orderId = '' //集体订单id
      if (this.customizedOrder?.orderId != 0 && this.showCourseData.id == this.customizedOrder?.courseId) {
        orderId = this.customizedOrder.orderId
        console.log('集体订单:', orderId)
      }
      selectedContact = selectedContact.join(',')

      // 如果跳过支付,就不使用优惠券
      let couponId = this.useCard?.id || 0
      if (this.isSkipPay) {
        couponId = 0
      }

      let params = {
        courseId: this.showCourseData.id,
        paymoney: this.paymoney, //时段个数*单价*人数-优惠券
        userId: this.userInfo?.centerNo, //升学汇和其他端接口字段不同
        orderCount: this.count, //人数
        // comboId: this.TalentReserveGroupData.id,
        // dateBindComboId: localStorage.getItem('selectComboBindId'),
        // orderTime: this.Moment(this.TalentReserveData.date).format('YYYY-MM-DD'), //时段集合 2018-10-15
        couponId: couponId,
        unionId: this.userInfo?.centerNo,
        price: (this.TalentReserveData.actualPrice * 1000 * this.count) / 1000, //课程减掉优惠券之前的价格
        travelNum: selectedContact, //出行人编号
        appId: this.appId,
        isSkipPay: this.isSkipPay, //集体定制是否跳过支付
        remark: this.remark, //留言
        // schoolShopName:'',//用户学校名称中文
        detailId: this.TalentReserveData.detailId, //团期编号
        productSceneId: this.TalentReserveGroupData.id, //行程id
        schoolId: 0,
        programComboId: this.TalentReserveData.programComboId,
        orderBuyType: this.TalentReserveData.orderBuyType, //1独享2共享
        baseId: this.showCourseData.baseId,
      }
      let schoolNamesChoose = localStorage.getItem('schoolNamesChoose')
      if (schoolNamesChoose) {
        params.schoolShopName = JSON.parse(schoolNamesChoose).schoolName
      }
      let inviteCode = localStorage.getItem('orderInviteCode')
      if (inviteCode) {
        params.RecommendUserNum = inviteCode
      }
      if (
        this.CourseParentIdBindCourseId &&
        this.CourseParentIdBindCourseId == this.showCourseData.id &&
        this.CourseParentId != 'undefined'
      ) {
        params.CourseParentId = this.CourseParentId //学习中心途径购买课程传参
      }
      this.$toast.loading({
        message: '正在创建订单...',
        duration: 0,
        forbidClick: true,
      })
      this.yxAxios.post(`${this.yanxueUrl}/api/Order/AddOrderSingleReserve`, params).then((res) => {
        if (res.data.data) {
          this.$toast('提交成功')
          setTimeout(() => {
            this.$router.push({ name: 'Home' })
          },1000)
        } else {
          this.$toast.fail(res.data?.result || res.data?.message)
        }
      })
    },
  },
  components: {
    ContactBox,
  },
}
</script>
<style lang="scss" scoped>
#checkOrder {
  padding-bottom: 260px;
  .top_border {
    border-top: 16px solid #f6f7fa;
  }
  .goods_data {
    padding: 0 22px;
    .top {
      padding: 20px 0;
      img {
        display: inline-block;
        width: 124px;
        height: 124px;
        border-radius: 8px;
      }
      div {
        vertical-align: top;
        display: inline-block;
        margin-left: 20px;
        width: 560px;
      }
      p {
        margin: 0;
      }
      .goods_title {
        font-size: 28px;
        font-weight: bold;
      }
      .package_name {
        font-size: 24px;
        margin-top: 18px;
      }
      .count {
        font-size: 24px;
      }
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 28px 0;
      border-top: 2px solid #f2f4f9;
      .time {
        font-size: 28px;
      }
      .price {
        font-size: 32px;
        font-weight: bold;
        span {
          font-size: 20px;
        }
      }
    }
  }
  .card {
    padding: 0 22px;
    height: 98px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 28px;
    font-weight: bold;
    .use_card {
      font-size: 26px;
      color: #f64c37;
      // padding: 2px 24px;
      // background: #ffecc0;
      // border-radius: 8px;
      margin-right: 10px;
      font-weight: normal;
    }
  }
  .remark {
    // display: flex;
    // align-items: center;
    span {
      font-size: 26px;
      margin-right: 10px;
      font-weight: normal;
      color: #999;
      max-width: 560px;
      overflow: hidden;
      display: inline-block;
      white-space: nowrap;
      vertical-align: middle;
      text-overflow: ellipsis;
    }
    i {
      vertical-align: middle;
    }
  }
  .remark_box {
    padding: 20px 20px 50px 20px;
    width: 90%;
    box-sizing: border-box;
    background: #fff;
    .remark_textarea {
      background: rgb(248, 248, 248);
      border-radius: 10px;
      width: 100%;
      height: 300px;
      margin: 0 auto;
      border: none;
      resize: none;
      color: #333;
      font-size: 28px;
      padding: 20px;
      box-sizing: border-box;
    }
    .remark_btn {
      display: block;
      width: 100%;
      margin: 0 auto;
      margin-top: 50px;
      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;
    }
  }
  .protocol {
    display: flex;
    align-items: center;
    padding: 0 22px;
    font-size: 24px;
    span {
      color: #3f92ff;
      margin: 0 4px;
    }
  }
  .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;
      }
      .coupon {
        font-size: 24px;
        color: #999;
      }
      .skip_pay_text {
        font-size: 28px;
        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;
}
.van-checkbox {
  display: inline-flex;
}
</style>