<template>
  <div class="AppSensitive">
    <AiTopFixed>
      <div class="top-search">
        <div class="left">
          <AiPagePicker type="dept" :selected.sync="deptUserList" nodeKey="id" single @select="deptSelect">
            <span class="label" v-if="deptUserList.length">{{deptUserList[0].name}}</span>
            <span v-else style="color:#999;">部门名称</span>
            <div class="clear-btn" v-if="deptUserList.length" @click.stop="clearDept">
              <img src="./img/del-icon.png" alt="" class="del-icon">
            </div>
            <u-icon name="arrow-down" color="#999" size="24" style="margin-left:8px;" v-else />
          </AiPagePicker>
        </div>
        <div class="right">
          <u-search v-model="keyword" :clearabled="true" placeholder="触发敏感词" :show-action="false" bg-color="#F5F5F5"
            search-icon-color="#999" color="#999" height="58" @search="getListInit" @clear="getListInit">
          </u-search>
        </div>
      </div>
    </AiTopFixed>
    <div class="user-list">
      <div class="item" v-for="(item, index) in list" :key="index" @click="toConversationRecord(item)">
        <img src="./img/group-img.png" alt="" v-if="item.type == 1">
        <img :src="item.userAvatar" alt="" v-if="item.type != 1 && item.userAvatar">
        <img src="./img/user-img.png" alt="" v-if="item.type != 1 && !item.userAvatar">
        <div class="item-right">
          <div class="name-flex">
            <p v-if="item.type == 1">{{item.toName}}</p>
            <p v-else>{{item.userName}}</p>
            <div class="time">{{item.createTime.substring(0, 16)}}</div>
          </div>
          <div v-html="item.text" class="item-content"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "AppSensitive",
  appName: "敏感词触发记录",
  data() {
    return {
      keyword: '',
      current: 1,
      pages: 2,
      list: [],
      deptUserList: []
    };
  },
  onLoad() {
    this.getListInit()
  },
  onShow() {
    document.title = '触发记录'
  },
  methods: {
    clearDept() {
      this.deptUserList = []
      this.getListInit()
    },
    getListInit() {
      this.current = 1
      this.pages = 2
      this.list = []
      this.getList()
    },
    getList() {
      if(this.current > this.pages)  return
      this.$http.post(`/app/appsessionarchivekeywordrecord/list`, null, {
        params: {
          current: this.current,
          size: 15,
          content: this.keyword,
          departmentId: this.deptUserList.length ? this.deptUserList[0].id : ''
        }
      }).then(res=> {
        if(res?.data) {
          res.data.records.map((item) => {
            if(item.type == 1) {
              item.text = item.userName + ':' + this.changeKeyRed(item.content, item.wordName)
            }else {
              item.text = this.changeKeyRed(item.content, item.wordName)
            }
          })
          this.list = this.current > 1 ? [...this.list, ...res.data.records] : res.data.records
          this.pages = res.data.pages
        }
      })
    },
    toConversationRecord(row) {
      uni.navigateTo({url: `./conversationRecord?userId=${row.userId}&toUserId=${row.toUserId}&toName=${row.toName}&type=${row.type}&seq=${row.seq}&msgId=${row.msgId}`})
    },
    deptSelect(e) {
      this.deptUserList = e
      this.getListInit()
    },
    changeKeyRed (str, keyWord) {
      if (str != null && keyWord != null) {
        var substr = "/" + keyWord + "/g";
        var replaceStr = str.replace(eval(substr), "<span style='color:#e6736e'>" + keyWord + "</span>")
        return replaceStr;
      } else {
        return str;
      }
    },
  },
  onReachBottom() {
    this.current ++
    this.getList()
  }
};
</script>

<style lang="scss" scoped>
.AppSensitive {
  height: 100%;
  ::v-deep .AiTopFixed {
    .placeholder {
      .content {
        padding: 0 !important;
      }
    }
    .fixed {
      margin: 0 !important;
      .content {
        padding: 0 !important;
      }
    }
  }
  .top-search {
    padding: 20px 32px;
    display: flex;
    .left {
      width: 350px;
      line-height: 64px;
      .clear-btn {
        display: inline-block;
        padding: 0 16px;
      }
      .del-icon {
        width: 32px;
        height: 32px;
        vertical-align: middle;
      }
    }
    .right {
      width: calc(100% - 350px);
    }
  }
  .top-select {
    display: flex;
    padding: 28px 0;
    border-bottom: 1px solid #eee;
    .item {
      flex: 1;
      padding: 0 8px;
      text-align: center;
      span {
        display: inline-block;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        font-size: 28px;
        color: #666;
        line-height: 40px;
      }
    }
  }
  .user-list {
    
    background-color: #fff;
    .item {
      padding: 24px 0 0 32px;
      display: flex;
      box-sizing: border-box;
      img {
        width: 80px;
        height: 80px;
        margin-right: 32px;
        border-radius: 50%;
      }
      .item-right {
        width: calc(100% - 112px);
        border-bottom: 1px solid #eee;
        padding: 0 32px 20px 0;
        box-sizing: border-box;
        // display: flex;
        // justify-content: space-between;
        // border-bottom: 1px solid #eee;
        // padding-right: 32px;
        // box-sizing: border-box;
        // .user {
        //   width: calc(100% - 240px);
        //   p {
        //     line-height: 44px;
        //     font-family: PingFangSC-Medium;
        //     font-weight: 500;
        //     font-size: 32px;
        //     color: #333;
        //     letter-spacing: 0.3px;
        //     margin-bottom: 8px;
        //   }
        //   div {
        //     color: #666;
        //     font-size: 28px;
        //     font-family: PingFangSC;
        //     line-height: 40px;
        //     padding-bottom: 20px;
        //     span {
        //       color: #e6736e;
        //     }
        //   }
        // }
        // .time {
        //   font-family: PingFangSC-Regular;
        //   font-size: 26px;
        //   color: #999;
        // }

        .name-flex {
          display: flex;
          justify-content: space-between;
          width: 100%;
          p {
            width: calc(100% - 240px);
            line-height: 44px;
            font-family: PingFangSC-Medium;
            font-weight: 500;
            font-size: 32px;
            color: #333;
            margin-bottom: 8px;
          }
          .time {
            font-family: PingFangSC-Regular;
            font-size: 26px;
            color: #999;
          }
        }
        .content {
          color: #666;
          font-size: 28px;
          font-family: PingFangSC;
          line-height: 40px;
          padding-bottom: 20px;
          word-break: break-all;
          span {
            color: #e6736e;
          }
        }
      }
    }
    .item:nth-last-child(1) {
      p {
        border-bottom: 0;
      }
    }
  }
}
</style>