<template>
  <view class="container activity-content">
    <view class="top-content-box">
      <view class="nav-bar-wrap" :style="{ height: navBarStyle.height, paddingTop: navBarStyle.paddingTop, paddingBottom: navBarStyle.paddingBottom }">
        <view class="content">
          <view class="activity-back" @click="activityPlayBack">
            <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/at_back.png"></image>
            活动回放
          </view>
          <view class="activity-search" @click="goSearch">
            <icon type="search" size="15" class="search_ico" style="margin-right: 10rpx" />
            {{ searchPlaceholderList.ActivitySearch || "" }}
          </view>
        </view>
      </view>
      <view class="screen-item" v-if="tabsActive == 0">
        <text :class="isShowJurisdiction ? 'active' : ''" @click="permissioActivity">有权限行业</text>
        <!-- 各种状态选择 -->
        <view class="select-conyent">
          <van-dropdown-menu active-color="#333333">
            <van-dropdown-item id="industry" :title="chartPermissionName">
              <view class="menu-items">
                <view class="menu-items-box" v-for="item in listChartPermission" :key="item.ChartPermissionId" @click="overallClick(item)">
                  <view class="items-box">
                    <u-icon v-if="item.IsChoose" name="checkbox-mark" :color="isShowJurisdiction ? '#ccc' : '#2C83FF'" size="24"></u-icon>
                  </view>
                  <text>{{ item.PermissionName }}</text>
                </view>
              </view>
              <view class="replacement">
                <text @click="replacementBtn" class="replacement-box">重置</text>
                <text @click="replacementConfirm">确定</text>
              </view>
            </van-dropdown-item>
          </van-dropdown-menu>
        </view>
        <text v-for="item in activityTimeList" :key="item.Id" :class="item.IsChoose ? 'active' : ''" @click="isActivityDate(item.Id)">{{ item.StatusName }}</text>
      </view>
    </view>

    <!-- 筛选状态 -->

    <!-- 活动列表 -->
    <view class="collect-activity" v-if="haveData">
      <view class="content-list">
        <view class="half">
          <view class="activity-ltem" v-for="(item, index) in collectTypeList" :key="index" v-if="item.Position == 1">
            <image class="zindex-one" :src="item.ImgUrl" lazy-load></image>
            <view class="content">
              <view class="item-img" @click="goDetails(item)"> </view>
              <view :style="{ 'background-image': 'url(' + item.ImgUrlBg + ')' }" class="activity-ui">
                <view class="activity-li" :style="{ 'background-image': 'url(' + val.ImgUrlBg + ')' }" v-for="val in item.List" :key="val.KeyWord" @click="goDetails(val, item.Resource)">
                  <image v-if="val.IsNew" class="item-image" lazy-load src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/new_report.png"></image>
                  <image
                    style="width: 73rpx; right: -6rpx"
                    v-if="val.IsExternalLabel"
                    class="item-image"
                    lazy-load
                    src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/activity_external.png"
                  ></image>
                  <text class="text_oneLine"> {{ val.KeyWord }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="half">
          <view class="activity-ltem" v-for="(item, index) in collectTypeList" :key="index" v-if="item.Position == 2">
            <image class="zindex-one" :src="item.ImgUrl" lazy-load></image>
            <view class="content">
              <view class="item-img" @click="goDetails(item)"> </view>
              <view :style="{ 'background-image': 'url(' + item.ImgUrlBg + ')' }" class="activity-ui">
                <view class="activity-li" :style="{ 'background-image': 'url(' + val.ImgUrlBg + ')' }" v-for="val in item.List" :key="val.KeyWord" @click="goDetails(val, item.Resource)">
                  <image v-if="val.IsNew" class="item-image" lazy-load src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/new_report.png"></image>
                  <image
                    style="width: 73rpx; right: -6rpx"
                    v-if="val.IsExternalLabel"
                    class="item-image"
                    lazy-load
                    src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/activity_external.png"
                  ></image>
                  <text class="text_oneLine"> {{ val.KeyWord }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <u-loadmore :status="status" icon-type="flower" :load-text="loadText" margin-top="20" v-if="totalPage > 1" />
    </view>
    <view class="nodata" v-if="!haveData">
      <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/act_search.png" mode="" class="nodata_ico"></image>
      <text>{{ tabsActive == 0 ? "暂时没有符合条件的活动" : "暂无我的日程" }}</text>
    </view>
    <freeCharge class="free-charge" :isShowFreeBtn="isShowFree" />
  </view>
</template>

<script>
import { activity } from "@/config/api.js";
import { Throttle } from "@/config/util.js";
import myActivityMixin from "@/components/activity/indexActivity.js";
import freeCharge from "@/components/freeCharge";
let app = getApp();
export default {
  mixins: [myActivityMixin],
  components: {
    freeCharge,
  },
  data() {
    return {
      whichDay: "",
      activityTypeId: "",
      chartPermissionIds: "",
      chartPermissionName: "所有行业",
      isGetJurisdiction: 0,
      isrefresh: true,
      isShowJurisdiction: false, //
      haveData: true,
      listChartPermission: [],
      listChartPermissionInit: [],
      collectTypeList: [],
      activityTimeList: [
        { Id: 1, IsChoose: false, StatusName: "今日活动" },
        { Id: 2, IsChoose: false, StatusName: "明日活动" },
      ],
    };
  },
  computed: {
    messageTitle() {  // 计算分享时候的title
      let arr = this.whichDay ? this.whichDay.split(",") : [];
      let whichData = arr.length == 2 ? "今日活动,明日活动" : arr == "1" ? "今日活动" : arr == "2" ? "明日活动" : "";
      let title = whichData ? whichData + "," + this.chartPermissionName : this.chartPermissionName !== "所有行业" ? this.chartPermissionName : "";
      return title || "近期所有行业活动预告";
    },
  },
  methods: {

    //获取胶囊位置
    initNavBar() {
      let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
      this.navBarStyle = {
        height: menuButtonInfo.height + menuButtonInfo.top + 8 + "px",
        paddingTop: menuButtonInfo.top - 4 + "px",
        paddingBottom: "4px",
      };
    },

    // 获取权限、所有的行业
    async getUserSearchContent() {
      const res = await activity.getUserSearchContent({
        IsShowJurisdiction: this.isGetJurisdiction,
      });
      if (res.Ret === 200) {
        this.isShowJurisdiction = res.Data.IsShowJurisdiction;
        this.listChartPermission = res.Data.ListChartPermission;
        this.listChartPermissionInit = res.Data.ListChartPermission2;
        this.clickPermission();
        if (this.chartPermissionIds) {
          this.closeTheWindow();
        }
      }
    },

    // 获取列表数据
    async getActivityLabelTypeList() {
      const res = await activity.getActivityLabelTypeList({
        ChartPermissionIds: this.chartPermissionIds,
        IsShowJurisdiction: this.isGetJurisdiction,
        ActiveState: "1",
        WhichDay: this.whichDay,
        IsPower: this.isGetJurisdiction,
      });
      if (res.Ret === 200) {
        this.collectTypeList = res.Data.List || [];
        this.haveData = this.collectTypeList.length ? true : false;
        if (this.refresh) {
          uni.stopPullDownRefresh();
          this.refresh = false;
        }
      }
    },

    // 下拉的选择的关闭事件
    closeTheWindow() {
      const types = this.chartPermissionIds ? this.chartPermissionIds.split(",").map(Number) : [];
      if (types.length > 0) {
        const str = [];
        this.listChartPermission.forEach((item) => {
          if (types.includes(item.ChartPermissionId)) {
            item.IsChoose = true;
            str.push(item.PermissionName);
          } else {
            item.IsChoose = false;
          }
        });
        this.chartPermissionName = str.length == 6 ? "所有行业" : str.join(",");
      }
    },

    // 下拉的选择的选中事件
    overallClick(item) {
      if (this.isShowJurisdiction) return;
      this.listChartPermission.forEach((key) => {
        if (key.ChartPermissionId == item.ChartPermissionId) {
          key.IsChoose = !key.IsChoose;
        }
      });
    },

    // 下拉的选择的重置事件
    replacementBtn() {
      this.listChartPermission = this.listChartPermissionInit;
      this.chartPermissionIds = "";
      this.isShowJurisdiction = false;
      this.listChartPermission.map((item) => (item.IsChoose = false));
      this.chartPermissionName = "所有行业";
      this.getActivityLabelTypeList();
    },

    // 下拉选择的确定事件
    replacementConfirm() {
      const arr = [];
      const str = [];
      this.listChartPermission &&
        this.listChartPermission.forEach((key) => {
          if (key.IsChoose) {
            arr.push(key.ChartPermissionId);
            str.push(key.PermissionName);
          }
        });
      if ((str.length == 6 && this.listChartPermission.length == 6) || str.length <= 0) {
        this.chartPermissionName = "所有行业";
      } else {
        this.chartPermissionName = str.join(",");
      }
      this.chartPermissionIds = arr.join(",");
      this.getActivityLabelTypeList();
      this.selectComponent("#industry").toggle(false);
    },

    // 今日明日的点击事件
    isActivityDate(id) {
      this.selectComponent("#industry").toggle(false);
      this.activityTimeList.forEach((item) => {
        if (item.Id == id) item.IsChoose = !item.IsChoose;
      });
      const arr = [];
      this.activityTimeList.forEach((item) => item.IsChoose && arr.push(item.Id));
      this.whichDay = arr.join(",");
      this.getActivityLabelTypeList();
    },

    // 是否有权限的点击事件
    async permissioActivity() {
      if (!this.$store.state.isAuth && !this.$store.state.isBind) {
        this.selectComponent("#industry").toggle(false);
        this.isShowJurisdiction = !this.isShowJurisdiction;
        this.isGetJurisdiction = this.isShowJurisdiction ? 1 : 2;
        await this.getUserSearchContent();
        this.selectComponent("#industry").toggle(false);
        if (this.isShowJurisdiction) {
          this.getActivityLabelTypeList();
        } else {
          this.replacementBtn();
        }
      } else {
        //已授权未绑定
        uni.navigateTo({
          url: "/pageMy/login/login",
        });
      }
    },

    // 点击后有权限的
    clickPermission() {
      if (this.isShowJurisdiction) {
        const arr = [];
        this.listChartPermission &&
          this.listChartPermission.forEach((key) => {
            if (key.IsChoose) {
              arr.push(key.ChartPermissionId);
            }
          });
        this.chartPermissionIds = arr.join(",");
      }
    },
    // 进入页面 判断是否是分享的
    loadShare(option) {
      if (option && Object.keys(option).length !== 0) {
        this.whichDay = option.whichDay || "";
        this.chartPermissionIds = option.chartPermissionIds || "";
        this.isGetJurisdiction = option.isGetJurisdiction;
        this.$nextTick(() => {
          let day = this.whichDay ? this.whichDay.split(",").map(Number) : [];
          this.activityTimeList.forEach((item) => {
            if (day.includes(item.Id)) {
              item.IsChoose = true;
            }
          });
        });
      }
    },
    // 去往详情
    goDetails(item, key) {
      if (item.Resource === 1 || key === 1) {
        let type = item.ActivityTypeName || "";
        let key = item.ActivityTypeId || item.KeyWord;
        let id = typeof key == "string" ? item.ActivityId : "";
        this.$store.dispatch(
          "checkHandle",
          "/activityPages/themeActivity/themeActivity?title=" + key + "&type=" + type + "&permissionIds=" + this.chartPermissionIds + "&whichDay=" + this.whichDay + "&id=" + id
        );
      } else {
        if (key) {
          this.$store.dispatch("checkHandle", "/activityPages/specialDetail/specialDetail?id=" + item.ActivityId);
        } else {
          this.$store.dispatch("checkHandle", "/activityPages/specialResearchPage/specialResearchPage");
        }
      }
    },
    //去往搜索事件
    goSearch() {
      this.$store.dispatch("checkHandle", "/pages-search/indedxSearch/indedxSearch");
    },
    //去往 活动回放activityPlayBack
    activityPlayBack() {
      this.$store.dispatch("checkHandle", "/activityPages/playBack/playBack");
    },
  },
  //load
  onLoad(option) {
    this.loadShare(option);
    this.getUserSearchContent();
    this.getActivityLabelTypeList();
    this.initNavBar();
  },
  async onShow() {
    await this.$store.dispatch("checkHandle", "noGO");
  },
  /** 用户点击分享 */
  onShareAppMessage: function (res) {
    return {
      title: this.isHorzMobile ? "好友向您推荐此内容，上传名片享查研观向免费月卡！" : this.messageTitle,
      path: "/pages/activity/activity?whichDay=" + this.whichDay + "&chartPermissionIds=" + this.chartPermissionIds + "&isGetJurisdiction=" + this.isGetJurisdiction,
    };
  },
  /* 下拉刷新 */
  onPullDownRefresh: Throttle(function () {
    this.refresh = true;
    this.page_no = 1;
    this.getActivityLabelTypeList();
  }),
  // 页面滚动事件
  onPageScroll() {},
};
</script>

<style scoped lang="scss">
.activity-content {
  background-color: #f7f7f7;
  padding-bottom: 90rpx;
  position: relative;
  .top-content-box {
    position: sticky;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 99;
    background-color: #fff;
    .nav-bar-wrap {
      background-color: #fff;
      width: 100%;
      .content {
        height: 100%;
        display: flex;
        align-items: center;
        padding-left: 30rpx;
        .activity-back {
          display: flex;
          flex-shrink: 0;
          align-items: center;
          color: #3385ff;
          font-size: 30rpx;
          image {
            width: 28rpx;
            height: 28rpx;
            margin-right: 10rpx;
          }
        }
        .activity-search {
          margin-left: 20rpx;
          width: 46%;
          height: 62rpx;
          color: #8d8d8d;
          font-size: 30rpx;
          display: flex;
          align-items: center;
          padding-left: 20rpx;
          background: #f3f3f3;
          border-radius: 82rpx;
        }
      }
    }
  }
  .screen-item {
    height: 100rpx;
    padding: 0 34rpx;
    font-size: 24rpx;
    color: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text {
      width: 150rpx;
      text-align: center;
      line-height: 51rpx;
      background-color: #f8f8fa;
      box-sizing: border-box;
      border-radius: 26rpx;
    }
    .active {
      background-color: #3385ff;
      color: #fff;
    }
  }
  .select-conyent {
    .decide {
      background-color: #f8f8fa;
      display: flex;
      align-items: center;
      margin: 0 auto;
      height: 110rpx;
      width: 100%;
      padding-left: 30rpx;
    }
    .items-box {
      width: 40rpx;
    }
    .menu-items {
      background-color: #f8f8fa;
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      padding: 30rpx 30rpx 0;
      .menu-items-box {
        display: flex;
        width: 50%;
        font-size: 28rpx;
        font-weight: 400;
        margin-bottom: 40rpx;
      }
      text {
        text-align: left;
      }
    }
    .replacement {
      margin: 0rpx 30rpx 20rpx;
      display: flex;
      background-color: #2c83ff;
      height: 60rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      border-radius: 30rpx;
      box-sizing: border-box;
      text {
        flex: 1;
        line-height: 58rpx;
        background-color: #2c83ff;
      }
      .replacement-box {
        border: 1rpx solid #2c83ff;
        color: #2c83ff;
        background: #ffffff;
        border-radius: 28rpx 0rpx 28rpx 28rpx;
      }
    }
  }
  .nodata_ico {
    width: 374rpx;
    height: 288rpx;
  }
  .content-list {
    display: flex;
    padding: 34rpx;
  }
  .half {
    width: 50%;
  }
  .activity-ltem {
    width: 331rpx;
    position: relative;
    margin-bottom: 30rpx;
    .zindex-one {
      position: absolute;
      top: 0;
      left: 0;
      height: 250rpx;
      width: 100%;
      z-index: 1;
    }
    .zindex-two {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 2;
    }
    .content {
      position: sticky;
      top: 0;
      left: 0;
      width: 100%;
      border-radius: 15rpx;
      overflow: hidden;
      z-index: 3;
    }
    .item-img {
      width: 100%;
      height: 147rpx;
      margin-bottom: 30rpx;
      font-size: 34rpx;
      color: #fff;
    }
    .activity-li {
      width: 100%;
      height: 86rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 0 20rpx;
      position: relative;
      font-size: 26rpx;
      color: #fff;
      .item-image {
        position: absolute;
        right: 0;
        top: 0;
        width: 60rpx;
        height: 30rpx;
        border-radius: 4rpx;
      }
      .new_img {
        width: 26rpx;
        height: 28rpx;
      }
    }
  }
  .more {
    width: 100%;
    text-align: center;
    font-size: 34rpx;
    color: #3385ff;
    margin-top: 90rpx;
  }
  .activity-ui {
    padding: 20rpx;
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
}
/deep/.van-dropdown-menu {
  background-color: #fff;
  box-shadow: none !important;
  align-items: center !important;
}
/deep/.van-dropdown-menu__item {
  box-sizing: border-box;
  width: 166rpx;
  padding-right: 20rpx;
  height: 51rpx;
  border-radius: 26rpx;
  background: #f8f8fa;
}
/deep/.van-dropdown-item {
  margin-top: -10rpx;
}
/deep/[data-index="2"] {
  margin-right: 0rpx;
}
/deep/ .van-ellipsis {
  font-size: 24rpx;
}
@import "@/components/activity/indexActivity.scss";
</style>
