<template>
  <section class="AppConversation">
    <AiTopFixed>
      <u-search v-model="keyword" :clearabled="true" placeholder="搜索" :show-action="false" bg-color="#F5F5F5"
        search-icon-color="#999" color="#999" height="58" @search="getUserListInit" @clear="getDeptListInit">
      </u-search>
      <div class="hint">
        <span v-for="(item, index) in selectDeptPath" :key="index">
          <span v-if="index>0" class="mar-h4">/</span>
          <span class="color-3F8DF5" @click="deptNameClick(item, index)">{{ item.name }}</span>
        </span>
      </div>
    </AiTopFixed>
    <div class="header-middle">
      <div class="cards" v-for="item in treeList" :key="item.id" @click="itemClick(item)">
        <div class="imges">
          <img src="./img/dept-img.png" alt="" class="avatras"/>
        </div>
        <div class="rightes">
          <div class="applicationNames">{{ item.name }}</div>
        </div>
      </div>
      <div v-if="userList.length">
        <div class="userCards" v-for="e in userList" :key="e.id" @click="toList(e)">
          <div class="imges">
            <img :src="e.avatar" alt="" class="avatras" v-if="e.avatar">
            <img src="./img/user-img.png" alt="" class="avatras" v-else/>
          </div>
          <div class="rights fill">
            <div class="applicationNames" v-html="brightenKeyword(e.name, keyword)"></div>
            <div class="idNumbers">{{ e.mobile }}</div>
          </div>
        </div>
      </div>
      <AiEmpty description="暂无数据" v-if="!hasData"/>
    </div>
  </section>
</template>

<script>
export default {
  name: "AppConversation",
  appName: "会话存档",
  data() {
    return {
      selected: [],
      allData: null,
      treeList: [],
      selectDeptPath: [],
      userList: [],
      keyword: ''
    }
  },
  computed: {
    hasData() {
      return this.treeList?.length > 0 || this.userList?.length > 0
    }
  },
  onLoad() {
    this.getAllDepts()
  },
  methods: {
    isSelected(id, corpId) {
      return !!this.selected.find(e => e.id == id && e.corpId == corpId)
    },
    getAllDepts() {
      this.$http.post('/app/wxcp/wxdepartment/listAllByCorp').then((res) => {
        if (res?.data) {
          let parents = res.data.map(e => e.parentid)
          this.allData = res.data.map(e => ({...e, hasChildren: parents.includes(e.id), isChecked: this.isSelected(e.id, e.corpId)}))
          this.deptInit()
        }
      })
    },
    deptInit() {
      this.treeList = this.allData.filter(e => !e.parentid)
      this.selectDeptPath = [{name: "可选范围", id: ''}]
    },
    itemClick({id, name, corpId}) {
      let index = this.selectDeptPath.findIndex(e => e.id == id && e.corpId == corpId)
      if (index == -1) {
        this.selectDeptPath.push({name, id, corpId})
        this.getDeptsAndUsersByParent(id, corpId)
      }
    },
    getDeptsAndUsersByParent(departmentId, corpId) {
      this.treeList = this.allData.filter(e => e.parentid == departmentId && e.corpId == corpId)
      this.userList = []
      this.$http.post(`/app/wxcp/wxuser/listByDeptId`, null, {
        params: {departmentId, status: 1, cid: corpId, name: this.keyword}
      }).then(res => {
        if (res?.data) {
          if(this.keyword) {
            res.data.map((item) => {
              var nameList = item.name.split(this.keyword)
              console.log(nameList)
            })
          }
          
          this.userList = res.data
        }
      })
    },
    getRedWords(contentText, keyword) {
      let keywordArray = keyword.split('');
      let wordsArray = [];
      for(let key of keywordArray){
        if(contentText.includes(key)){
          wordsArray.push(key)
        }
      }
      return wordsArray;
    },
    brightenKeyword(contentText, keyword) {
      let wordsArray = this.getRedWords(contentText, keyword);
      let res = contentText;//res的初始值是不带任何红色格式的
      //遍历相同字数组，
      for(let word of wordsArray){
        const Reg = new RegExp(word, 'i');
        //替换每一个相同字
        res = res.replace(Reg, `<span style="color: #1365DD;">${word}</span>`);
      }
      return res;
    },
    getUserListInit() {
      var e = this.selectDeptPath[this.selectDeptPath.length - 1]
      if(!e.id) {  //第一级 根节点
        return this.$u.toast('请选择部门后再搜索人员')
      }
      this.getDeptsAndUsersByParent(e.id, e.corpId)
    },
    getDeptListInit() {
      var e = this.selectDeptPath[this.selectDeptPath.length - 1]
      if(!e.id) {  //第一级
        this.treeList = this.allData.filter(e => !e.parentid)
        this.userList = []
      }else {
        this.getDeptsAndUsersByParent(e.id || '', e.corpId || '')
      }
    },
    deptNameClick(row, index) {
      this.userList = []
      if (!index) { //第一级别
        this.deptInit()
      } else {
        let length = this.selectDeptPath.length - index
        this.selectDeptPath.splice(index + 1, length)
        this.getDeptsAndUsersByParent(row.id, row.corpId)
      }
    },
    itemCheck(row, kind) {
      row.isChecked = !row.isChecked
      if (row.isChecked) {
        this.selected.push({...row, kind})
      } else {
        let index = this.selected.findIndex(e => e.id == row.id)
        this.selected.splice(index, 1)
      }
      this.$forceUpdate()
    },
    toList(row) {
      uni.navigateTo({url: `./list?id=${row.id}`})
    }
  }
}
</script>

<style lang="scss" scoped>
.AppConversation {
  height: 100%;
  background: #fff;

  .header-top {
    background: #fff;
    padding: 20px 32px;
  }

  .header-middle {
    // padding-bottom: 140px;

    .hint {
      padding: 28px 20px 28px 32px;
      line-height: 56px;
      box-shadow: 0 1px 0 0 #e4e5e6;
      font-size: 30px;
      font-weight: 500;
      word-break: break-all;
    }

    .empty-div {
      height: 16px;
      background: #f5f5f5;
    }

    .imges {
      display: flex;
      align-items: center;

      .avatras {
        width: 74px;
        height: 74px;
        border-radius: 8px;
      }
    }

    .cards {
      display: flex;
      align-items: center;
      height: 120px;
      line-height: 120px;
      padding: 0 0 0 32px;


      img {
        width: 74px;
        height: 74px;
        border-radius: 8px;
      }

      .rightes {
        width: calc(100% - 106px);
        display: flex;
        align-items: center;
        margin-left: 32px;
        border-bottom: 1px solid #e4e5e6;

        .applicationNames {
          flex: 1;
          min-width: 0;
          font-size: 36px;
          font-weight: 500;
          color: #333333;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .imgs {
          flex-shrink: 0;
          width: 40px;
          height: 40px;
          margin-right: 20px;
        }
      }
    }

    .userCards {
      display: flex;
      align-items: center;
      height: 120px;
      line-height: 120px;
      padding: 0 0 0 32px;

      .rights {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-left: 32px;
        border-bottom: 1px solid #e4e5e6;
        padding-right: 40px;
        height: inherit;

        .applicationNames {
          font-size: 36px;
          font-weight: 500;
          color: #333333;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .idNumbers {
          color: #666;
        }
      }
    }
  }

  .subBtn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 118px;
    background: #f4f8fb;

    div {
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background: #1365dd;
      border-radius: 4px;
      font-size: 32px;
      color: #fff;
      margin: 20px 34px 0 0;
      float: right;
    }
  }

  .color-3F8DF5 {
    color: #3F8DF5;
  }

  .mar-h4 {
    margin: 0 4px;
  }
}
</style>
