<template>
  <div class="ActivityQRCode">
    <van-form
      ref="tabstoreVal"
      class="QRCodeActivity"
      :readonly="
        approvalStatus == '1' ||
        approvalStatus == '2' ||
        (approvalStatus == null && !!userSummaryId)
      ">
      <div class="activityItem">
        <span class="van-f-red">*</span>
        <van-field
          v-model="activityForm.summaryEventName"
          name="summaryEventName"
          label="活动名称"
          placeholder="请输入活动名称"
          :rules="[{ required: true }]" />
      </div>
      <div class="activityItem">
        <span class="van-f-red">*</span>
        <van-field
          readonly
          clickable
          name="summaryEventDate"
          :value="activityForm.summaryEventDate"
          label="活动日期"
          placeholder="请选择活动日期"
          @click="activityShowCalendarFun"
          :rules="[{ required: true }]" />
      </div>
      <div class="activityItem">
        <span class="van-f-red">*</span>
        <van-field
          readonly
          clickable
          name="qrStartTime"
          :value="activityForm.qrStartTime"
          label="生效开始日期"
          placeholder="请选择开始日期"
          @click="clickValidDateShow('start')"
          :rules="[{ required: true }]" />
      </div>
      <div class="activityItem">
        <span class="van-f-red">*</span>
        <van-field
          readonly
          clickable
          name="qrEndTime"
          :value="activityForm.qrEndTime"
          label="生效结束日期"
          placeholder="请选择结束日期"
          @click="clickValidDateShow('end')"
          :rules="[{ required: true }]" />
      </div>
      <div class="activityItem">
        <div class="van-cell QRCodeBtnBox">
          <van-button style="width: 88px" class="QRCodeBtn" color="#0057ba" @click="openQRCode">
            生成签到码
          </van-button>
          <div class="tips">
            1、生成签到码后自动暂存任务；<br />2、请门店老板使用企微扫码签到
            <!-- <div>1、点击【暂存】后签到码生效；</div>
            <div>2、请门店老板使用企微扫码签到</div> -->
          </div>
        </div>
      </div>
      <div class="activityItem">
        <div class="van-cell refreshBtnBox" style="padding: 5px 16px" @click="getQRChainList">
          <div style="font-size: 16px; margin-right: 10px">
            已签到成功门店:
            <span style="margin-left: 5px; color: red">{{ QRChainList.length }}家 </span>
          </div>
          <div class="refresh">
            <van-icon
              style="font-weight: 600; margin-top: 1px"
              name="replay"
              size="20"
              color="#1989fa" />
          </div>
        </div>
      </div>
      <div class="activityItem">
        <div class="van-cell chainListBox">
          <div class="jxsContent">
            <div class="item" v-for="(val, index) in QRChainList">
              <el-popover
                placement="bottom"
                trigger="click"
                :content="val.storeName + '(' + val.storeCode + ')'">
                <template slot="reference">
                  <div class="selectItem">{{ val.storeName }} &nbsp; ({{ val.storeCode }})</div>
                </template>
              </el-popover>
            </div>
          </div>
        </div>
      </div>
    </van-form>
    <!-- 活动日期 -->
    <van-calendar v-model="activityShowCalendar" @confirm="activityOnConfirm" />
    <!-- 生效日期 -->
    <van-popup v-model="validDateShow" capture position="bottom">
      <van-datetime-picker
        v-model="validDate"
        :title="activate.title"
        :min-date="activate.minDate"
        :max-date="activate.maxDate"
        type="datetime"
        @cancel="validDateShow = false"
        @confirm="onValidDateConfirm" />
    </van-popup>
    <!-- 二维码 -->
    <div class="QRCodeBox" v-if="QRCodeBox">
      <div class="centerBox">
        <div class="title">{{ activityForm.summaryEventName }}</div>
        <div class="activityDate">{{ activityForm.summaryEventDate }}</div>
        <img class="QRCodeUrl" :src="this.QRCodeUrl" />
        <div class="validTimeBox">
          <div class="text">二维码有效期</div>
          <div class="validTime">
            <div>{{ activityForm.qrStartTime }}</div>
            <div>~</div>
            <div>{{ activityForm.qrEndTime }}</div>
          </div>
        </div>
        <div class="close" @click="QRCodeBox = false">关闭</div>
      </div>
    </div>
  </div>
</template>
<script>
import { getSummaryQrCheckList, getSummaryQrCodeUUID } from '@/api/index';
export default {
  props: {
    ActivityQRCodeData: {
      type: Object,
    },
    qrUuid: {
      type: String,
    },
    approvalStatus: {
      type: String,
    },
    userSummaryId: {
      type: [String, Number],
    },
  },
  data() {
    return {
      // 活动数据
      activityForm: {
        summaryEventName: '',
        summaryEventDate: '',
        qrStartTime: '',
        qrEndTime: '',
      },
      activityShowCalendar: false,
      validDateShow: false,
      QRCodeUrl: '',
      QRCodeBox: false,
      activate: {
        type: '',
        title: '',
        minDate: new Date(),
        maxDate: new Date(2035, 10, 1),
      },
      validDate: new Date(),
      QRChainList: [],
      qrUuids: null,
    };
  },
  watch: {
    ActivityQRCodeData: {
      handler(val) {
        if (val) this.activityForm = val;
        if (this.qrUuid) {
          this.qrUuids = this.qrUuid;
          this.getQRChainList();
        }
        console.log(val);
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    activityOnConfirm(date) {
      this.activityForm.summaryEventDate = this.formatDate(date);
      this.activityShowCalendar = false;
    },
    // 打开二维码
    openQRCode(value) {
      this.toastLoading(0, '加载中...', true);
      // approvalStatus//1-待审批 2-审批通过 不支持修改，直接查看二维码
      if (
        this.approvalStatus == '1' ||
        this.approvalStatus == '2' ||
        this.approvalStatus == '3' ||
        (this.approvalStatus == null && this.userSummaryId) ||
        (this.qrUuid && this.userSummaryId)
      ) {
        getSummaryQrCodeUUID({ qrUuid: this.qrUuid, userSummaryId: this.userSummaryId }).then(
          (res) => {
            this.toastLoading().clear();
            this.QRCodeUrl = res.data.base64Png;
            this.QRCodeBox = true;
          }
        );
      } else {
        this.$refs.tabstoreVal
          .validate()
          .then(() => {
            // 验证通过
            this.$emit('onSubmit', (res) => {
              this.toastLoading().clear();
              this.QRCodeBox = true;
              this.QRCodeUrl = res.data.base64Png;
              this.qrUuids = res.data.qrUuid;
            });
          })
          .catch((errors) => {
            this.toastLoading().clear();
            //验证失败
            window.scrollTo(0, 0);
          });
      }
    },
    activityShowCalendarFun() {
      if (
        this.approvalStatus == '1' ||
        this.approvalStatus == '2' ||
        (this.approvalStatus == null && this.userSummaryId)
      )
        return;
      this.activityShowCalendar = true;
    },
    clickValidDateShow(type) {
      if (
        this.approvalStatus == '1' ||
        this.approvalStatus == '2' ||
        (this.approvalStatus == null && this.userSummaryId)
      )
        return;
      if (type == 'start') {
        this.activate = {
          type: type,
          title: '请选择开始日期',
          minDate: new Date(),
          maxDate: new Date(2035, 9, 1),
        };
        this.validDate = new Date();
      } else {
        this.activate = {
          type: type,
          title: '请选择结束日期',
          minDate: this.activityForm.qrStartTime
            ? new Date(this.activityForm.qrStartTime)
            : new Date(),
          maxDate: new Date(2035, 9, 1),
        };
        this.validDate = this.activityForm.qrEndTime
          ? new Date(this.activityForm.qrEndTime)
          : new Date();
      }
      this.validDateShow = true;
    },
    onValidDateConfirm(date) {
      let time = this.parseTime(new Date(date), '{y}-{m}-{d} {h}:{i}:{s}') + '';
      if (this.activate.type == 'start') {
        this.activityForm.qrStartTime = time;
        if (
          new Date(this.activityForm.qrStartTime).getTime() >
          new Date(this.activityForm.qrEndTime).getTime()
        ) {
          this.activityForm.qrEndTime = null;
        }
      } else {
        this.activityForm.qrEndTime = time;
      }
      this.validDateShow = false;
    },
    formatDate(date) {
      var Month = date.getMonth() + 1;
      var Day = date.getDate();
      if (Month < 10) {
        Month = '0' + Month;
      }
      if (Day < 10) {
        Day = '0' + Day;
      }
      return `${date.getFullYear()}-${Month}-${Day}`;
    },
    getQRChainList() {
      if (!this.qrUuids) return;
      this.QRChainList = [];
      this.toastLoading(0, '加载中...', true);
      getSummaryQrCheckList({ qrUuid: this.qrUuids }).then((res) => {
        this.toastLoading().clear();
        if (res.code == 200) {
          this.QRChainList = res.data || [];
        } else {
          this.QRChainList = [];
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.ActivityQRCode {
  .QRCodeActivity {
    background-color: white;
    margin-bottom: 10px;
    .activityItem {
      display: flex;
      .van-f-red {
        position: relative;
        left: 16px;
        top: 10px;
        z-index: 1;
      }
      .QRCodeBtnBox {
        justify-content: space-between;
        align-items: end;
        .QRCodeBtn {
          width: auto;
          border-radius: 10px;
        }
        .tips {
          flex: 1;
          font-size: 14px;
          line-height: 15px;
          text-align: right;
          color: red;
        }
        .van-button--normal {
          padding: 0 8px;
        }
      }
      .jxsContent {
        margin-bottom: 10px;
        .item {
          padding: 5px;
          background: #e9e9e9;
          margin: 5px 0;
          .el-popover__reference-wrapper {
            display: flex;
            justify-content: space-between;
            align-items: center;
          }
          .selectItem {
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 14px;
          }
        }
      }
    }
  }
  .QRCodeBox {
    position: fixed;
    width: 100%;
    height: 100%;
    z-index: 9999999999;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    .centerBox {
      width: 90%;
      // height: 100%;
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(10px);
      border-radius: 20px;
      padding: 10px 20px;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
      text-align: center;
      color: #fff;
      font-size: 18px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .title {
        font-size: 24px;
        font-weight: 600;
        margin-bottom: 10px;
      }
      .activityDate {
        // font-size: 20px;
        margin: 10px 0;
      }
      .QRCodeUrl {
        // width: 100%;
        margin: 10px 0;
      }
      .validTimeBox {
        margin-top: 10px;
        .validTime {
          margin: 10px 0;
          font-size: 12px;
        }
      }
      .close {
        width: 40%;
        padding: 10px 15px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 12px;
      }
    }
  }
}
</style>
