<template>
  <view class="container activity-content activity-search-content">
    <view class="top-content">
      <!-- 搜索 -->
      <view class="searchTarget-header" v-if="!dynamicName">
        <input type="text" placeholder="请输入关键字" placeholder-class="sea_ipt_placeholder" class="sea_ipt" v-model="searchTxt" focus="true" confirm-type="search" @confirm="searchHandle" />
        <icon type="search" size="15" class="sea_ico" />
        <view class="ipt-right">
          <icon type="clear" size="16" color="#E0E0E0" v-show="searchTxt" @click.stop="clearIpt" />
          <text class="line">|</text>
          <text @click="searchHandle" style="color: #3385ff">搜索</text>
        </view>
      </view>
      <!-- 选中的气泡主题 -->
      <view class="dynamic-name" v-if="dynamicName">
        <text>{{ dynamicName }}</text>
        <icon type="clear" size="16" color="#E0E0E0" @click="clearDynamicName" />
      </view>
      <!-- 搜索历史 -->
      <view class="search-cont-top" v-if="!isShowSearch && historySearchList.length">
        <view class="cont-tit">
          <text>搜索历史</text>
          <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/empty_ico.png" class="empty_ico" @click="clearHistory"></image>
        </view>
        <view class="targetList">
          <view class="target-item" v-for="(item, index) in historySearchList" :key="index" @click="chooseTarget(item)">{{ item }}</view>
        </view>
      </view>
      <!-- 气泡主题list -->
      <view class="dynamic-content" v-if="!isShowSearch">
        <view class="target-title"> 推荐关键词： </view>
        <view class="target-item">
          <view class="target-ul target-left">
            <view class="target-text" v-for="(item, index) in dynamicList" :key="index" v-if="index % 2 === 0">
              <text @click="changeTarget(item.KeyWord)">
                {{ item.KeyWord }}
              </text>
            </view>
            <view class="lucency" style="width: 65rpx">
              <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/broadside-ico.png" mode=""></image>
            </view>
          </view>
          <view class="target-ul target-right">
            <view class="target-text" v-for="(item, index) in dynamicList" :key="index" v-if="index % 2 !== 0">
              <text @click="changeTarget(item.KeyWord)">
                {{ item.KeyWord }}
              </text>
            </view>
            <view class="lucency" style="width: 35rpx">
              <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/broadside-ico.png" mode=""></image>
            </view>
          </view>
        </view>
      </view>
      <!-- 各种状态选择 -->
      <view class="select-conyent" v-if="isShowSearch && !dynamicName">
        <text @click="handelState(item)" :class="['state-item', activeState == item.state && 'state-active-item']" v-for="item in activeStateOption" :key="item.name">{{ item.name }}</text>
      </view>
    </view>
    <!-- 内容列表部分 -->
    <!-- 活动列表 -->
    <view v-if="haveData" class="activity-pages" :class="dynamicName ? 'activity-page' : ''">
      <view class="collect-ul">
        <view class="collect-ltem" v-for="(item, index) in collectList" :key="index">
          <view class="title-date" @click="goDetail(item)">
            <text :class="item.ActivityType == 1 ? '' : 'xianxia'">{{ item.ActivityType == 1 ? "线上" : "线下" }}</text>
            {{ item.ActivityTimeText }}
            <view class="audio-back" v-if="item.AudioLink" @click.stop="audioPlayBack(item)">
              <image
                class="audio-img"
                :src="
                  curVoiceId === item.ActivityId && !curAudioPaused
                    ? 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/back_stop.png'
                    : 'https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/back_group.png'
                "
              ></image>
              回放
            </view>
          </view>
          <view class="item-li">
            <view class="item-img" @click="goDetail(item)">
              <image :src="item.ImgUrl"> </image>
              <text v-if="item.ActiveState == 1" class="img-status begin">未开始</text>
              <text v-else-if="item.ActiveState == 2" class="img-status proceed">进行中</text>
              <text v-else class="img-status">已结束</text>
              <view class="img-type">
                <image :src="item.ImgUrlText" mode=""></image>
              </view>
            </view>
            <view class="item">
              <view class="item-text" @click="goDetail(item)">
                <text class="activity-title"> {{ item.ActivityName }} </text>
                <text class="text_twoLine" v-if="item.Expert">专家背景：{{ item.Expert }} </text>
                <text class="text_twoLine" v-if="item.DistinguishedGuest">嘉宾：{{ item.DistinguishedGuest }} </text>
                <text class="text_twoLine" v-if="item.Speaker">主讲人：{{ item.Speaker }}</text>
              </view>
              <block v-if="item.ActiveState == 1">
                <view :class="['bottom-box', item.ActivityTypeId == 1 && 'expert-item', item.City && 'city']">
                  <view class="city-img" v-if="item.City">
                    <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/location.png"></image>
                    {{ item.City }}
                  </view>
                  <view style="display: flex">
                    <text v-if="item.IsShowOutboundCall && item.ActivityTypeId == 1" class="button" @click="signupIsAddOfCancel(item, 1)">{{ item.IsSignup == 1 ? "取消外呼" : "预约外呼" }}</text>
                    <text v-if="item.IsShowAppointment" @click="summaryIsHandel(item)">{{ item.IsAppointment == 1 ? "取消纪要" : "预约纪要" }}</text>
                    <text v-if="item.IsShowMeetingReminder" @click="meetingReminderAdd(item.ActivityId, item.IsCancelMeetingReminder)">
                      {{ item.IsCancelMeetingReminder == 0 ? "消息提醒" : "取消提醒" }}
                    </text>
                    <view v-if="item.IsShowHelpSsk" style="width: 130rpx">
                      <text @click="askingGo(item)">帮我带问</text>
                    </view>
                    <text v-if="item.IsShowOutboundCall && item.ActivityTypeId != 1" class="button" @click="signupIsAddOfCancel(item, 1)">{{ item.IsSignup == 1 ? "取消外呼" : "预约外呼" }}</text>
                    <block v-if="item.IsShowSignup">
                      <block v-if="item.IsCClassMeeting">
                        <text class="button" @click="signupIsAddOfCancel(item, 3, 'CClass')">{{ item.IsSignup == 1 ? "取消报名" : "我要报名" }}</text>
                      </block>
                      <block v-else>
                        <text class="button" v-if="item.IsSignup !== 1" @click="wanttosignup(item.ActivityId)">我要报名</text>
                        <text class="button" v-else @click="signupIsAddOfCancel(item, 2)">{{ item.SignupType == 1 ? "取消外呼" : "取消报名" }}</text>
                      </block>
                    </block>
                  </view>
                </view>
              </block>
              <block v-else>
                <view class="bottom-box city" v-if="item.City">
                  <view class="city-img">
                    <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/location.png" />
                    {{ item.City }}
                  </view>
                </view>
                <view class="bottom-box real-time" v-if="item.ActiveState == 2 && item.ActivityTypeId == 1">
                  <text class="button" @click="askingGo(item, '提问')">实时提问</text>
                </view>
              </block>
            </view>
          </view>
        </view>
        <u-loadmore :status="status" icon-type="flower" :load-text="loadText" margin-top="20" v-if="totalPage > 1" />
      </view>
    </view>
    <!-- 缺审部分 -->
    <view class="nodata" v-else>
      <image src="https://hzstatic.hzinsights.com/cygx/czbk/act_search.png" mode="" class="nodata_ico"></image>
      <text>未找到搜索结果</text>
    </view>
    <!-- 各种自定义弹框 -->
    <modalDialog
      :isShow="isShow"
      :signupType="signupType"
      :goFollow="goFollow"
      :signupStatus="signupStatus"
      :hasPermission="hasPermission"
      :jurisdictionList="jurisdictionList"
      :editIsShow="editIsShow"
      :isCancelShow="isCancelShow"
      :idTypeCancel="idTypeCancel"
      @cancelShowBtn="cancelEnsure"
      :countryCode="countryCode"
      :mobileEdit="mobileEdit"
      :goOnNextStep="goOnNextStep"
      :isShowhasPermission="isShowhasPermission"
      :applyForIsShow="applyForIsShow"
      :mailboxBinding="mailboxBinding"
    />
    <!-- 选择区号弹出层 -->
    <areaCode :isAreaCode="isAreaCode" :areaCode="areaCode" />
    <!-- 参会方式弹出层 -->
    <view class="select-box">
      <u-popup v-model="selectShow" mode="bottom">
        <view style="color: #333333; font-size: 28rpxrpx">请选择参会方式</view>
        <view style="color: #2c83ff" @click="signupAdd('id', 1)">预约外呼</view>
        <view style="color: #2c83ff" @click="signupAdd('id', 2)">自主拨入</view>
        <view style="color: #a9afb8" @click="selectShow = false">取消</view>
      </u-popup>
    </view>
    <audioModule :showAudioPop.sync="showAudioPop" />
    <freeCharge class="free-charge" :isShowFreeBtn="isShowFree" />
  </view>
</template>

<script>
import { activity, User } from "@/config/api.js";
import { Throttle } from "@/config/util.js";
import modalDialog from "@/components/modalDialog.vue";
import areaCode from "@/activityPages/components/areaCode.vue";
import myMixin from "../components/index.js";
import myActivityMixin from "../components/indexActivity.js";
import freeCharge from "@/components/freeCharge";
import audioModule from "@/components/audioModule/index";

let app = getApp({ allowDefault: true });
export default {
  data() {
    return {
      searchTxt: "",
      isShowSearch: false,
      status: "loadmore",
      totalPage: "",
      haveData: true,
      activeState: 0, //活动进行状态
      activeStateOption: [
        {
          name: "全部",
          state: 0,
        },
        {
          name: "可回放",
          state: 1,
        },
      ],
      selectShow: false,
      dynamicList: [],
      dynamicName: "",
      showAudioPop: false,
    };
  },
  mixins: [myMixin, myActivityMixin],
  components: {
    modalDialog,
    areaCode,
    freeCharge,
    audioModule,
  },
  methods: {
    //获取快捷主题
    fastSearchKeWord() {
      activity
        .fastSearchKeWord({
          ActiveState: this.activeState,
          Label: "More",
        })
        .then((res) => {
          if (res.Ret !== 200) return;
          this.dynamicList = res.Data.List || [];
        });
    },
    //主题的点击事件
    //点击了快捷主题
    changeTarget(val) {
      if (val) {
        this.dynamicName = val;
      }
      this.getList();
      this.isShowSearch = true;
    },
    //清除主题的点击事件
    //清除了快捷主题
    clearDynamicName() {
      this.dynamicName = "";
      this.searchTxt = "";
      this.collectList = [];
      this.haveData = true;
      this.isShowSearch = false;
    },
    //搜索记录进来
    searchHandle() {
      if (this.searchTxt) {
        this.page_no = 1;
        //添加搜索记录
        if (!this.historySearchList.includes(this.searchTxt)) {
          this.historySearchList.unshift(this.searchTxt);
          this.$db.set("historySearchListActivity", JSON.stringify(this.historySearchList));
        }
        this.isShowSearch = true;
        this.getList();
      } else {
        this.$util.toast("请输入关键字");
      }
    },
    // 选择历史搜索
    chooseTarget(item) {
      this.searchTxt = item;
      this.collectList = [];
      this.page_no = 1;
      this.searchHandle();
    },
    /* 历史搜索清空 */
    clearHistory() {
      this.historySearchList = [];
      this.$db.del("historySearchListActivity");
    },
    //获取列表
    async getList() {
      const res = this.dynamicName
        ? await activity.getActivityListNew({
            PageSize: this.pageSize,
            CurrentIndex: this.page_no,
            ActiveState: "2,3", //活动进行状态
            Label: this.dynamicName,
          })
        : await activity.getActivityListNew({
            PageSize: this.pageSize,
            CurrentIndex: this.page_no,
            KeyWord: this.searchTxt,
            ActiveState: "1,2,3", //活动进行状态
            PlayBack: this.activeState,
          });
      if (res.Ret !== 200) return;
      this.status = this.page_no < res.Data.Paging.Pages ? "loadmore" : "nomore";
      this.totalPage = res.Data.Paging.Pages; //总页数
      if (this.page_no === 1) {
        this.collectList = res.Data.List || [];
        this.haveData = this.collectList.length ? true : false;
        if (this.refresh) {
          uni.stopPullDownRefresh();
          this.refresh = false;
        }
      } else {
        this.collectList.push(...res.Data.List);
      }
    },
    /* 表单清空 */
    clearIpt() {
      this.searchTxt = "";
      this.collectList = [];
      this.haveData = true;
      this.isShowSearch = false;
    },
    //获取是否需要填写区号接口
    countryCcode() {
      User.countryCcode().then((res) => {
        if (res.Ret == 200) {
          this.isNeedAddCountryCode = res.Data.IsNeedAddCountryCode;
        }
      });
    },
    //全部 或者可回放的点击事件
    handelState(item) {
      this.activeState = item.state;
      this.page_no = 1;
      this.getList();
    },
    //点击了回放
    audioPlayBack(item) {
      // 判断是否为同一个音频
      if (this.$store.state.audioBg.activityId == item.ActivityId) {
        if (this.globalBgAudioManager.paused) {
          this.globalBgAudioManager.play();
        } else {
          this.globalBgAudioManager.pause();
        }
      } else {
        this.showAudioPop = true;
        this.$store.commit("audioBg/addAudio", { list: item.VoiceList, activityId: item.ActivityId, activityTitle: item.ActivityName });
      }
    },
  },
  computed: {
    curVoiceId() {
      //当前正在播放的音频id
      return this.$store.state.audioBg.activityId;
    },
    curAudioPaused() {
      //当前音频是否暂停状态
      return this.$store.state.audioBg.paused;
    },
  },
  onShow() {
    this.countryCcode(); //判断是否加区号
    this.$store.dispatch("statistics", { PageType: "ActivitSearch" });
  },
  //load
  onLoad(option) {
    if (this.$db.get("historySearchListActivity")) {
      let historyList = JSON.parse(this.$db.get("historySearchListActivity"));
      this.historySearchList = historyList;
    }
    this.searchTxt = option.text || "";
    this.dynamicName = option.dycName || "";
    this.activeState = option.statesId || 0;
    if (option.text) {
      this.searchHandle();
    } else if (option.dycName) {
      this.changeTarget(option.dycName);
    }
    this.fastSearchKeWord();
  },
  /* 触底 */
  onReachBottom: Throttle(function () {
    if (this.status === "nomore") return;
    this.status = "loading";
    this.page_no++;
    this.getList();
  }),
  /* 用户点击分享  */
  onShareAppMessage: function (res) {
    return {
      title: this.isHorzMobile ? "好友向您推荐此内容，上传名片享查研观向免费月卡！" : "活动搜索",
      path: "/activityPages/activitySearch/activitySearch?statesId=" + this.activeState + "&text=" + this.searchTxt + "&dycName=" + this.dynamicName,
      success: (res) => {},
      fail: (err) => {},
    };
  },
  /* 下拉刷新 */
  onPullDownRefresh: Throttle(function () {
    this.page_no = 1;
    this.refresh = true;
    if (this.tabsActive == 0) {
      this.getList();
    } else {
      this.getScheduleList();
    }
  }),
};
</script>

<style scoped lang="scss">
@import "../components/index.scss";
.dynamic-content {
  padding: 0 0 0 0 !important;
  .target-title {
    color: #333333 !important;
    font-size: 28rpx !important;
  }
}
.dynamic-name {
  display: flex;
  align-items: center;
  padding: 36rpx 10rpx 20rpx !important;
  color: #3385ff;
  font-size: 28rpx;
  text {
    max-width: 100%;
    padding-right: 40rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  icon {
    width: 32rpx;
    height: 32rpx;
  }
}

.activity-pages {
  padding-top: 200rpx;
  padding-bottom: 30rpx;
}
.activity-page {
  padding-top: 100rpx !important;
}
@import "../components/indexActivity.scss";
.activity-search-content {
  .select-conyent {
    display: flex;
    // justify-content: flex-end;
    float: right;
    margin-bottom: 20rpx;
    height: 51rpx;
    width: 252rpx;
    background: #f8f8fa;
    border-radius: 47rpx;
    .state-item {
      width: 126rpx;
      height: 51rpx;
      border-radius: 47rpx;
      text-align: center;
      line-height: 51rpx;
      color: #999999;
      font-size: 24rpx;
    }
    .state-active-item {
      background: #3385ff;
      color: #fff;
    }
  }
  .title-date {
    position: relative;
    .audio-back {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 99rpx;
      height: 39rpx;
      font-size: 24rpx;
      color: #fff;
      display: flex;
      align-items: center;
      background-color: #3385ff;
      border-radius: 20rpx;
      .audio-img {
        width: 30rpx;
        height: 30rpx;
        margin: 0 5rpx;
      }
    }
  }
}
</style>
