<script>
import axios from "axios";
import { SearchUserService } from "@/api/user.js";
import router from "@/router/index.js";

export default {
  data() {
    return {
      ip: "",
      tableData: [],
      tokenData: {
        token: localStorage.getItem("token"),
        ip: localStorage.getItem("ip"),
        userId: localStorage.getItem("userId"),
        username: localStorage.getItem("username"),
        id: 2002,
        keyword: "",
      },
    };
  },

  // methods 是一些用来更改状态与触发更新的函数
  // 它们可以在模板中作为事件处理器绑定
  methods: {
    async getUserList() {
      let result = {};
      try {
        result = await SearchUserService(this.tokenData);
      } catch (e) {
        console.log(e);
      }
      let data = result.data;

      //   for(let d in data){
      //     let res = JSON.parse(d);
      //     console.log("res=",res);
      //     this.tableData.push(res);
      //   }
      this.tableData = data;
    },
    onSubmit() {
      getUserList({ token: token });
    },
    handleSizeChange() {
      alert("每页记录数变化" + val);
    },
    handleCurrentChange() {
      alert("页码发生变化" + val);
    },

    //设置设备重启
    async startChat(index) {
      var id = this.tableData[index].ID;
      var name = this.tableData[index].Name;
      //   var user_data = {
      //     to_user_id: id,
      //     to_user_name: this.tableData[index].Name,
      //   };
      //转到聊天页面
      localStorage.setItem("to_user_id", id);
      localStorage.setItem("to_user_name", name);
      router.push("/im");
    },

    handleMenuSelect(val) {
      router.push(val);
    },
    toVideoList() {
      router.push("/videoList");
    },
    // 修改条纹颜色
    tableRowClassName({ row, rowIndex }) {
      if (row.human === 1) {
        return {
          background: "#488aff",
        };
      } else {
        return "";
      }
    },
  },

  // 生命周期钩子会在组件生命周期的各个不同阶段被调用
  // 例如这个函数就会在组件挂载完成后被调用
  async mounted() {
    let now = new Date();
    if (localStorage.getItem("token") === null) {
      router.push("/login");
    }
  },
};
</script>

<template>
  <div>
    <el-button
      type="primary"
      size="mini"
      @click.prevent="handleMenuSelect('/videoList')"
      >视频列表</el-button
    >
    <el-button
      type="primary"
      size="mini"
      @click.prevent="handleMenuSelect('/device')"
      >设备管理</el-button
    >
    <el-button
      type="primary"
      size="mini"
      @click.prevent="handleMenuSelect('/User')"
      >用户</el-button
    >
    <el-container style="height: 700px; border: 1px solid #eee">
      <el-header style="font-size: 40px; background-color: rgb(238, 241, 246)"
        >用户搜索列表</el-header
      >
      <el-container>
        <el-main>
          <el-col :span="8">
            <!-- 搜索与添加区域 -->
            <el-input
              placeholder="请输入ID"
              v-model="tokenData.id"
              clearable
              @clear="getUserList"
            >
            </el-input>
            <el-input
              placeholder="请输入关键字"
              v-model="tokenData.keyword"
              clearable
              @clear="getUserList"
            >
            </el-input>
            <template #append>
              <el-button @click="getUserList"
                ><el-icon><search /></el-icon
              ></el-button>
            </template>
          </el-col>
          <!-- 表单 -->
          <el-form :inline="true" :model="tokenData" class="demo-form-inline">
            <el-form-item>
              <el-button
                class="el-button--danger"
                type="primary"
                @click="getUserList()"
                >查询</el-button
              >
            </el-form-item>
          </el-form>

          <!-- 表格  :row-style="this.tableRowClassName"-->
          <el-table :data="tableData" width="100%" border>
            :row-style="this.tableRowClassName"
            <el-table-column prop="ID" label="id" width="80"></el-table-column>
            <el-table-column
              prop="Name"
              label="名称"
              width="100"
            ></el-table-column>
            <el-table-column
              prop="Email"
              label="用户邮箱"
              width="180"
            ></el-table-column>
            <el-table-column
              prop="Age"
              label="用户Age"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="Gender"
              label="用户性别"
              width="80"
            ></el-table-column>
            <el-table-column label="操作" width="300">
              <template #default="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click.prevent="startChat(scope.$index)"
                  >聊天</el-button
                >
                <!-- <el-button type="danger" size="mini">删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <br />

          <!-- 分页条 -->
          <!-- Pagination 分页 -->
          <!-- <el-pagination
            background
            layout="total,sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :total="1000"
          ></el-pagination> -->
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<style>
.blueRowbg {
  background: "#488aff";
}
</style>