<template>
  <div
    v-loading.fullscreen.lock="fullscreenLoading"
    class="userManagement_list"
  >
    <div class="userManagement_list_headerSelect">
      <div class="userManagement-list-btn">
        <el-form :model="form" ref="form" style="display: inline" inline>
          <el-form-item class="header_top_form" label="群名称：" prop="account">
            <el-input
              placeholder="请输入"
              size="small"
              v-model.trim="form.conversationName"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item class="header_top_form" label="群主名称：" prop="ownerName">
            <el-input
              placeholder="请输入"
              size="small"
              v-model.trim="form.ownerName"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
        <div style="margin-right: 23px">
          <el-button
            size="small"
            type="primary"
            style="width: 88px"
            @click="search"
            >查询</el-button
          >
          <el-button
            size="small"
            type="primary"
            style="width: 108px"
            @click="batchSynchronous"
            >一键同步会话</el-button
          >
          <el-button size="small" style="width: 88px" @click="resetForm('form')"
            >重置</el-button
          >
        </div>
      </div>
      <div style="text-align: right">
        <el-button
          size="small"
          style="width: 104px; margin-right: 23px"
          icon="iconfont icon-a-zu13"
          @click="addList"
          >新增</el-button
        >
      </div>
    </div>
    <div>
      <el-table
        :data="tableData"
        ref="editTable"
        stripe
        :header-cell-style="{ background: '#F7F7F7' }"
        :height="screenHeight"
      >
        <el-table-column
          label="群名称"
          prop="conversationName"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="chatid"
          prop="chatId"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="群主"
          prop="ownerName"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="140px" align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="detail(scope.row)">查看</el-button>
              <el-button type="text" @click="synchronous(scope.row)"
                >同步会话</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.pageNum"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="form.pageSize"
      background
      layout="total,prev, pager, next,jumper"
      :total="form.total"
    >
    </el-pagination>
    <editDialog ref="editDialogRefs" />
    <detailDialog ref="detailDialogRefs" />
  </div>
</template>
<script>
import editDialog from './edit'
import detailDialog from './detail'
import * as api from '@/api/api'
export default {
  name: 'userManagement',
  components: {
    editDialog,
    detailDialog
  },
  data() {
    return {
      fullscreenLoading: false,
      tableData: [],
      form: {
        pageNum: 1,
        pageSize: 10,
        conversationName: '',
        ownerName:'',
        start: '',
        total: 0
      },
      screenHeight: document.body.clientHeight - 320,
      tableDataheight: 0
    }
  },
  mounted() {
    this.screenHeight = document.body.clientHeight - 320
    window.onresize = () => {
      return (() => {
        this.screenHeight = document.body.clientHeight - 320
      })()
    }
    this.listDataInit()
  },
  methods: {
    /**
     * @method 重置
     * **/
    resetForm(formName) {
      this.form = {
        pageNum: 1,
        pageSize: 10,
        conversationName: '',
        start: '',
        total: 0
      }
      this.$refs[formName].resetFields()
      this.search()
    },
    /**
     * @method 搜索
     * **/
    search() {
      if (sessionStorage.getItem('sessionManagement')) {
        this.form.pageNum = Number(sessionStorage.getItem('sessionManagement'))
        sessionStorage.removeItem('sessionManagement')
      } else {
        this.form.pageNum = 1
      }

      this.listDataInit()
    },
    /**
     * @method 用户数据列表
     * **/
    listDataInit() {
      this.tableData = []
      this.fullscreenLoading = true
      let code = {
        conversationName: this.form.conversationName,
        ownerName: this.form.ownerName,
        page: this.form.pageNum,
        pageSize: this.form.pageSize,
        start: this.form.start
      }
      api
        .GET('conversation/list', code)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list
            this.tableData.forEach(item => {
              item.status == 1 ? (item.status = true) : (item.status = false)
            })
            this.form.pageNum = res.data.currPage
            this.form.pageSize = res.data.pageSize
            this.form.total = res.data.totalCount
            this.fullscreenLoading = false
            this.$nextTick(() => {
              this.$refs.editTable.bodyWrapper.scrollTop = this.tableDataheight
              this.tableDataheight = 0
            })
          }
        })
        .catch(() => {
          this.fullscreenLoading = false
        })
    },
    /**
     * @method 条数分页
     * **/
    handleSizeChange(val) {
      this.form.pageSize = val
      this.form.pageNum = 1
      this.listDataInit()
    },
    /**
     * @method 分页
     * **/
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.listDataInit()
    },
    /**
     * @method 新增
     * **/
    addList() {
      let code = {
        show: true
      }
      this.$refs.editDialogRefs.openClose(code)
    },
    // 编辑
    detail(val) {
      this.tableDataheight = this.$refs.editTable.bodyWrapper.scrollTop
      sessionStorage.setItem('sessionManagement', this.form.pageNum)
      let code = {
        show: false,
        id: val.chatId
      }
      this.$refs.detailDialogRefs.openClose(code)
    },
    // 同步
    synchronous(val) {
      this.tableDataheight = this.$refs.editTable.bodyWrapper.scrollTop
      sessionStorage.setItem('sessionManagement', this.form.pageNum)
      this.$confirm('此操作将同步会话信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          api.PUT(`conversation/${val.chatId}`).then(res => {
            if (res.code == 0) {
              this.$message.success(res.message)
              this.search()
            }
          })
        })
        .catch(() => {})
    },
    batchSynchronous() {
      this.tableDataheight = this.$refs.editTable.bodyWrapper.scrollTop
      sessionStorage.setItem('sessionManagement', this.form.pageNum)
      this.$confirm('此操作将根据查询条件一键同步会话信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var url = 'conversation/batch/sync?conversationName=' + this.form.conversationName + '&ownerName=' + this.form.ownerName;
          api.PUT(url).then(res => {
            if (res.code == 0) {
              this.$message.success(res.message)
              this.search()
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="less">
.userManagement_list {
  .userManagement_list_headerSelect {
    .userManagement-list-btn {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
    }
    margin-bottom: 20px;
    .el-form-item {
      margin-bottom: 10px;
      margin-right: 60px;
      .el-input__inner {
        background: #f5f5f5;
        opacity: 1;
        border-radius: 17px;
      }
    }
    .icon-a-zu13 {
      font-size: 11px;
      margin-right: 5px;
    }
  }
}
</style>