<template>
  <div class="wrapper">
    <div class="search-input">
      <van-search type="text" v-model="searchValue" show-action placeholder="请输入搜索关键词" @input="onInput"
        @search="onSearch" @cancel="onCancel" />
    </div>
    <search-result ref="child" v-if="userIsSearch"></search-result>
    <div class="history-box" v-else>
      <template v-if="historyList.length > 0">
        <div class="active-item-title">
          <div class="active-item-title-h2">历史记录</div>
          <div class="active-item-title-time" @click="deleteHistory()">
            <van-icon name="delete-o" />
          </div>
        </div>
        <div class="history-item-box">
          <div class="history-item" @click="onClickHistory(historyItem)"
            v-for="( historyItem, historyIndex ) in historyList" :key="historyIndex">{{ historyItem }}</div>
        </div>
      </template>


      <div class="hot-search-box">
        <div class="hot-item-cell-title-box">
          <div class="hot-item-cell-title">
            <div>
              <van-icon class="color-ee8c15" name="fire" />
            </div>
            <div class="hot-item-cell-title-h2">企业热搜榜</div>
          </div>
          <div class="hot-item-cell-logo">
            <van-icon name="replay" />
          </div>
        </div>
        <ul>
          <li class="hot-item-cell" v-for=" (hotItem, hotIndex) in hotSearchList " :key="hotIndex">
            <div class="hot-item-cell-title" @click="goUrlDetails(hotItem.companyId)">
              <div class="hot-item-cell-title-paixu" :class="[hotIndex < 3 ? 'color-2372eb' : '']">
                {{ hotIndex + 1 }}
              </div>
              <div class="hot-item-cell-title-title">{{ hotItem.companyName }}</div>
            </div>
            <div>
              <span v-if="!hotItem.attentionFlag" class="hot-item-cell-guanzhu" @click="guanzhuShow(hotIndex)">+ 关注</span>
              <span v-else class="hot-item-cell-guanzhu" @click="cancelGuanzhu(hotItem.companyId)">已关注</span>
            </div>
          </li>
        </ul>


      </div>
    </div>

    <van-action-sheet v-model="sheetShow" title="选择分组">
      <div class="add-fenzu-btn" @click="addFenzuShow = true">+ 新建分组</div>
      <van-radio-group v-model="updateResult">
        <van-cell-group class="fenzu-box">
          <van-cell v-for="(item, index) in groupList" clickable :key="index" style="padding-bottom: 20px">
            <template #title>
              <div style="margin-top: 15px;font-size: 15px;font-weight: bold;">{{ item.groupName }}</div>
            </template>
            <template #right-icon>
              <van-radio :name="index" />
            </template>
          </van-cell>
        </van-cell-group>
        <van-button plain color="#2372eb" @click="updateSure"
          style="width: 90%;border-radius: 5px;margin-left: 5%;">确定</van-button>
      </van-radio-group>
    </van-action-sheet>

    <van-popup :closeOnClickOverlay=false v-model="addFenzuShow" :style="{ width: '260px' }">
      <div class="dialog-title-box">新建分组</div>
      <div class="dialog-content-box">
        <input maxlength="12" v-model="groupName" type="text" placeholder="请输入分组名称">
      </div>
      <div class="dialog-footer-box">
        <div class="cancel-btn" @click="addFenzuCancel">取消</div>
        <div class="ok-btn" @click="addFenzuOk">确定</div>
      </div>
    </van-popup>


  </div>
</template>

<script>
import { ActionSheet, Icon, Radio, RadioGroup, Search, Toast, Button } from 'vant';//button组件
import { areaList } from '@vant/area-data';
import SearchResult from './components/searchResult/searchResult'
import { addAttentionCompany, addGroupInfo, getQueryCompanyHotSearch } from "@/service/api/home";
import { postCancelGuanzhu, postQueryByName } from "@/service/api/category";
import { clearInterval } from 'timers';
export default {
  name: "Search",
  components: {
    VanIcon: Icon,
    VanSearch: Search,
    SearchResult,
    Toast: Toast,
    VanActionSheet: ActionSheet,
    VanRadioGroup: RadioGroup,
    VanRadio: Radio,
    VanButton: Button,
  },
  data() {
    return {
      sheetShow: false,
      updateResult: '',
      groupList: [],
      currentOperateIndex: -1,
      addFenzuShow: false,
      groupName: '',


      searchValue: '',
      historyList: [],

      hotSearchList: '',
      userIsSearch: false,//用户是否进行了搜索

    }
  },
  mounted() {
    if (sessionStorage.getItem('searchValue')) {
      this.searchValue = sessionStorage.getItem('searchValue')
      this.onSearch();
    } else {
      this.getHistoryList();
      this.getQueryHotWordListData();
    }
    this.postGroupList();
  },
  methods: {
    onInput() {
      if (this.searchValue.length == 1) {
        Toast('搜索关键词必须大于两个字符！')
      }
      else if (this.searchValue.length == 0) {
        this.userIsSearch = false;
      }
      else {
        this.userIsSearch = true;
        sessionStorage.setItem('searchValue', this.searchValue)
        this.$nextTick(() => {
          this.$refs.child.getQueryCompanyPageData();
        })
        let historyListArray = [];
        if (this.historyList.length > 0) {
          historyListArray = this.historyList;
        } else {
          historyListArray = [];
        }
        historyListArray.unshift(this.searchValue)
        if (historyListArray.length > 10) {
          historyListArray.splice(9)
        }
        this.historyList = Array.from(new Set(historyListArray))
        sessionStorage.setItem('historyList', JSON.stringify(this.historyList))
      }
    },
    // 搜索关键字
    onSearch() {
      if (this.searchValue.length < 2) {
        Toast('搜索关键词必须大于两个字符！')
      } else {
        this.userIsSearch = true;
        sessionStorage.setItem('searchValue', this.searchValue)
        let historyListArray = [];
        if (this.historyList.length > 0) {
          historyListArray = this.historyList;
        } else {
          historyListArray = [];
        }
        historyListArray.unshift(this.searchValue)
        this.historyList = Array.from(new Set(historyListArray))
        sessionStorage.setItem('historyList', JSON.stringify(this.historyList))
        this.$nextTick(() => {
          this.$refs.child.getQueryCompanyPageData();
        })
      }
    },

    // 点击历史记录中的
    onClickHistory(historyItem) {
      this.userIsSearch = true;
      sessionStorage.setItem('searchValue', historyItem)
      this.$nextTick(() => {
        this.$refs.child.getQueryCompanyPageData();
      })
    },

    // 获取历史记录
    getHistoryList() {
      if (sessionStorage.getItem('historyList')) {
        this.historyList = JSON.parse(sessionStorage.getItem('historyList'));
      }
    },

    // 删除历史记录
    deleteHistory() {
      sessionStorage.removeItem('historyList');
      this.historyList = [];
    },

    // 搜索取消
    onCancel() {
      if (this.userIsSearch == true) {
        this.userIsSearch = false;
      } else {
        history.back();
      }
      // this.$router.push({path: '/dashboard/home'});
    },

    // 获取企业热搜榜
    async getQueryHotWordListData() {
      let result = await getQueryCompanyHotSearch();
      this.hotSearchList = result;
    },


    // 分组信息查询
    async postGroupList() {
      let result = await postQueryByName({ pageSize: 99, pageNum: 1 });
      this.groupList = result.records;
    },

    // 关注
    guanzhuShow(guanzhuIndex) {
      this.sheetShow = true;
      this.currentOperateIndex = guanzhuIndex;
    },

    async updateSure() {
      let params = {
        groupName: this.groupList[this.updateResult].groupName,
        groupId: this.groupList[this.updateResult].groupId,
        companyId: this.hotSearchList[this.currentOperateIndex].companyId,
        companyName: this.hotSearchList[this.currentOperateIndex].companyName,
      }
      let result = await addAttentionCompany(params);
      if (result) {
        this.sheetShow = false;
        this.getQueryHotWordListData();
        Toast.success('关注成功！');
      }
    },

    // 新建分组取消
    addFenzuCancel() {
      this.groupName = '';
      this.addFenzuShow = false;
    },


    // 新建分组确定
    async addFenzuOk() {
      let params = {
        groupName: this.groupName,
      }
      let result = await addGroupInfo(params);
      if (result) {
        this.postGroupList();
        this.addFenzuCancel();
        Toast.success('新建分组成功！');
      }
    },

    // 去详情页
    goUrlDetails(companyId) {
      sessionStorage.setItem('companyId', companyId);
      sessionStorage.setItem('tabBarActiveIndex', '1');
      this.$router.push('/dashboard/CustomerDashboard');
    },

    // 取消关注
    async cancelGuanzhu(companyId) {
      let params = { companyId: companyId };
      let result = await postCancelGuanzhu(params);
      Toast.success('取关成功');
      this.getQueryHotWordListData();
    }




  }
}
</script>

<style lang="less" scoped>
.wrapper {
  padding: 1rem;
  background: #ffffff;
  min-height: 100vh;
}

.search-input {
  margin-bottom: 10px;
}

.van-search {
  padding: 0;
}

.active-item-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.active-item-title-h2 {
  color: #333333;
  font-size: 0.9rem;
  font-weight: bold;
  display: flex;
  justify-self: start;
  align-items: center;
}

.history-box {
  margin-top: 6px;
}

.history-item-box {
  display: flex;
  justify-content: start;
  align-items: start;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.history-item {
  padding: 4px 6px;
  background: #f2f7f7;
  border-radius: 4px;
  font-size: 0.7rem;
  margin: 0 10px 10px 0;
}

.color-ee8c15 {
  color: #ee8c15;
  font-size: 1.2rem;
  margin-right: 6px;
  position: relative;
  top: 2px;
}

.hot-search-box {
  padding: 4px 1rem;
  border: solid 1px #b9d2f8;
  border-radius: 20px;
}

.hot-item-cell-title-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 3rem;
}

.hot-item-cell {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 3rem;
}

.hot-item-cell:not(:last-child) {
  border-bottom: solid 1px #dadce0;
}

.hot-item-cell-title {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 0.8rem;
  width: calc(100% - 61px);

}

.hot-item-cell-title-h2 {
  font-size: 1rem;
  font-weight: bold;
  color: #2372eb;
}

.hot-item-cell-title-paixu {
  font-size: 1rem;
  font-weight: bold;
  margin-right: 10px;
  color: #999999;
}

.hot-item-cell-title-title {
  color: #333333;
  width: 90%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.color-2372eb {
  color: #2372eb;
}

.hot-item-cell-logo i {
  font-size: 1rem;
  color: #2372eb;
}

.hot-item-cell-guanzhu {
  width: 61px;
  font-size: 0.8rem;
  color: #2372eb;
  border: solid 1px #2372eb;
  padding: 2px 8px;
  border-radius: 20px;
}


.fenzu-box {
  padding-bottom: 10px;
  max-height: 50vh;
  overflow: scroll;
}

.add-fenzu-btn {
  padding: 1rem;
  font-size: 0.9rem;
  color: #2372eb;
  border-bottom: solid 1px #f2f2f2;
}

.dialog-title-box {
  font-size: 0.9rem;
  font-weight: bold;
  text-align: center;
  margin: 16px 0 20px;
}

.dialog-content-box {
  padding: 0 1rem;
  margin-bottom: 16px;
}

.dialog-content-box input {
  width: 100%;
  background: #f2f2f2;
  border: none;
  height: 36px;
  line-height: 36px;
  padding-left: 6px;
  font-size: 0.8rem;
}

.dialog-footer-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  border-top: solid 1px #dddddd;
}

.dialog-footer-box .cancel-btn {
  font-size: 0.9rem;
  color: #999999;
  border-right: solid 1px #dddddd;
  text-align: center;
  width: 50%;
}


.dialog-footer-box .ok-btn {
  width: 50%;
  font-size: 0.9rem;
  color: #2372EB;
  text-align: center;
}
</style>
