<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统推送ID" prop="sysPushId" label-width="100px">
        <el-input
            v-model="queryParams.sysPushId"
            placeholder="请输入系统推送ID"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sysPushLogList" @selection-change="handleSelectionChange">
      <el-table-column label="系统推送id" align="center" prop="sysPushId" />
      <el-table-column label="推送到的用户id" align="center" prop="userJson" >
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="handleShowUser(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="推送人数" align="center" prop="num" />
      <el-table-column label="推送状态" align="center" prop="status">
        <template v-slot="scope">
          <cai-dict-tag :options="systemPushLogStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="推送返回的结果" align="center" prop="result" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" fixed="right"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="推送名单" :close-on-click-modal="false" :visible.sync="showUserOpen" width="700px" append-to-body>
      <el-form :model="userForm" ref="userForm" size="small" :inline="true" label-width="68px">
        <el-form-item :label="systemName+'号'" prop="name">
          <el-input
              v-model="userForm.name"
              :placeholder="'请输入'+systemName+'号'"
              clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="people_list" >
        <div class="people_list-item" v-for="value in userList">
<!--          <el-tag
              class="people_list-item_name"
              :key="value.usercode"
              type="info"
              effect="plain">
            {{ value.usercode }}
          </el-tag>-->
          <el-tooltip class="item" effect="dark" :content="value.nickname+'('+value.usercode+')'" placement="top-start">
            <div class="people_list-item_name">{{value.nickname}}({{ value.usercode }})</div>
          </el-tooltip>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysPushLog } from '@/api/cai/sysPushLog'
import {
  systemPushGroupIdList,
  systemPushLogStatus,
  systemPushSendTimeTypeList,
  systemPushStatusList,
  systemPushTypeList
} from '@/constant/statusMap'

export default {
  name: "SysPushLog",
  data() {
    return {
      systemPushGroupIdList,systemPushLogStatus,systemPushSendTimeTypeList,systemPushStatusList,systemPushTypeList,
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送系统消息记录表格数据
      sysPushLogList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysPushId: undefined,
        status: undefined,
      },
      sourceUserList:[],
      userList: [],
      showUserOpen: false,
      userForm:{
        name: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询推送系统消息记录列表 */
    getList() {
      this.loading = true;
      listSysPushLog(this.queryParams).then(response => {
        this.sysPushLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleShowUser(row){
      this.userList = JSON.parse(row.userJson);
      this.sourceUserList = JSON.parse(row.userJson);
      this.showUserOpen = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleUserQuery(){
      let name = this.userForm.name
      console.log(name)
      if(name){
        let list = []
        this.sourceUserList.forEach(value => {
          console.log(value)
          if(value.usercode == name){
            list.push(value)
          }
        })
        this.userList = list
      }
    },
    resetUserQuery(){
      this.userForm.name = undefined
      this.userList = this.sourceUserList
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
};
</script>

<style lang="scss" scoped>
.people_list {
  width: 100%;
  border: 1px solid #ccc;
  overflow: auto;
  padding: 5px;
  height: 500px;
}
.people_list-item {
  width: 16.5%;
  height: 22px;
  padding: 2px 4px;
  box-sizing: border-box;
  float: left;
  margin-bottom: 5px;
}
.people_list-item_name {
  box-sizing: border-box;
  border-radius: 2px;
  border: 1px solid #ccc;
  padding: 0 2px;
  overflow: hidden;
  text-overflow:ellipsis;
  white-space: nowrap;
}
</style>
