<template>
  <div class="AppConversation">
    <AiTopFixed>
      <u-tabs :list="tabList" :is-scroll="false" :current="currentTabs" height="96" bg-color="#3975C6"
        inactive-color="#A1C1E8" :bar-style="barStyle" :active-item-style="activeStyle" active-color="#ffffff " @change="change">
      </u-tabs>
      <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="currentTabs != 1 ? '请输入群名称' : '请输入昵称'" :show-action="false" bg-color="#F5F5F5"
            search-icon-color="#999" color="#999" height="58" @search="getListInit" @clear="getListInit">
          </u-search>
        </div>
      </div>
      <!-- <div class="top-search">
        <div class="left">
          <u-search v-model="keyword" :clearabled="true" :placeholder="currentTabs != 1 ? '请输入群名称' : '请输入昵称'" :show-action="false" bg-color="#F5F5F5"
            search-icon-color="#999" color="#999" height="58" @search="getListInit" @clear="getListInit">
          </u-search>
        </div>
      </div>
      <div class="top-select">
        <div class="item">
          <AiAreaPicker v-model="areaId" :areaId="user.areaId" @select="areaSelect" :name.sync="areaName" style="color: #666" selectRoot>
            <span style="margin-left: 4px" v-if="areaName">{{ areaName }}</span>
            <span v-else style="color:#999;">行政区划</span>
            <u-icon name="arrow-down" color="#999" size="28" style="margin-left: 4px" />
          </AiAreaPicker>
        </div>
        <div class="item">
          <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>
            <u-icon name="arrow-down" color="#999" size="24" style="margin-left:8px;"/>
          </AiPagePicker>
        </div>
      </div> -->
    </AiTopFixed>
    <div class="user-list" v-if="list.length">
      <div class="item" v-for="(item, index) in list" :key="index" @click="toDetail(item)">
        <img :src="item.toUserAvatar" alt="" v-if="currentTabs == 1">
        <img src="./img/group-img.png" alt="" v-else>
        <div class="item-border">
          <div class="flex-left">
            <p>{{currentTabs == 1 ? item.toUserName : item.roomName}}</p>
          </div>
          <div class="flex-right" :class="`type`+item.userType" v-if="item.userType > 0">{{item.userType == 1 ? '内部' : '外部'}}</div>
        </div>
      </div>
    </div>
    <AiEmpty v-else/>
  </div>
</template>

<script>
import {mapState} from "vuex";
export default {
  name: "AppConversation",
  appName: "会话存档",
  data() {
    return {
      tabList: [
        {name: '群聊会话'},
        {name: '私聊会话'},
      ],
      currentTabs: 0,
      barStyle: {
        'width': '24px',
        'height': '2px',
        'border-radius': '0',
        'bottom': '5px'
      },
      activeStyle: {
        'font-weight' : '400',
      },
      keyword: '',
      areaId: '',
      areaName: '',
      current: 1,
      pages: 2,
      list: [],
      deptUserList: []
    };
  },
  computed: {
    ...mapState(['user']),
  },
  onLoad() {
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    // this.deptUserList = [{id: this.user.departId, name: this.user.departName}]
    this.getList()
  },
  onShow() {
    
  },
  methods: {
    clearDept() {
      this.deptUserList = []
      this.getListInit()
    },
    change(index) {
      this.keyword = ''
      this.currentTabs = index
      this.getListInit()
    },
    getListInit() {
      this.current = 1
      this.pages = 2
      this.list = []
      this.getList()
    },
    areaSelect(e) {
      this.areaId = e
      this.getListInit()
    },
    deptSelect(e) {
      this.deptUserList = e
      this.getListInit()
    },
    getList() {
      if(this.current > this.pages)  return
      this.$http.post(`/app/appsessionarchiveindex/listByUser`, null, {
        params: {
          current: this.current,
          size: 10,
          type: this.currentTabs == 1 ? 0 : 1,
          // areaId: this.areaId,
          toUserName: this.currentTabs == 1 ? this.keyword : '',
          roomName: this.currentTabs == 1 ? '' : 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.userType = item.roomType
            }else {
              item.userType = item.toUserType
            }
          })
          this.list = this.current > 1 ? [...this.list, ...res.data.records]: res.data.records
          this.pages = res.data.pages
        }
      })
    },
    toDetail(row) {
      var query = row.type == 1 ? `?roomId=${row.roomId}` : `?toUserId=${row.toUserId}`
      uni.navigateTo({ url: `./conversationDetail${query}&type=${row.type}`})
    }
  },
  onReachBottom() {
    this.current ++
    this.getList()
  }
};
</script>

<style lang="scss" scoped>
.AppConversation {
  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;
    .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 {
    border-top: 1px solid #eee;
    background-color: #fff;
    .item {
      padding: 24px 0 0 32px;
      display: flex;
      .item-border {
        width: calc(100% - 112px);
        padding-right: 32px;
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #eee;
      }
      .flex-left {
        padding-right: 8px;
        box-sizing: border-box;
      }
      .flex-right {
        font-size: 24px;
        margin-top: 26px;
        width: 78px;
        height: 34px;
        line-height: 34px;
        text-align: center;
        border-radius: 4px;
      }
      .type1 {
        background-color: #EAF4FF;
        color: #267EF0;
      }
      .type2 {
        background-color: #FDEEE1;
        color: #FB7D29;
      }
      img {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        margin-right: 32px;
      }
      p {
        padding-bottom: 32px;
        width: calc(100% - 112px);
        line-height: 80px;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        font-size: 32px;
        color: #333;
        letter-spacing: 0.3px;
        display: inline-block;
        vertical-align: top;
        width: 100%;
      }
    }
    .item:nth-last-child(1) {
      p {
        border-bottom: 0;
      }
    }
  }
}
</style>