<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Select
          v-model="searchForm.doEvent"
          style="margin-right: 10px; width: 120px"
          placeholder="选择事件"
          @on-change="doEventChange()"
        >
          <!-- 1Token变化 2一周未通信 3安装可疑应用 -->
          <Option :value="1">Token变化</Option>
          <Option :value="2">一周未通信</Option>
          <Option :value="3">安装可疑应用</Option>
        </Select>
        <Select
          v-model="searchForm.atype"
          style="margin-right: 10px; width: 120px"
          placeholder="关注状态"
          @on-change="searchList()"
        >
          <Option :value="0">全部</Option>
          <Option :value="1">重点关注</Option>
          <Option :value="2">不关注</Option>
        </Select>
        <Select
          v-if="searchForm.doEvent === 3"
          v-model="searchForm.handled"
          style="margin-right: 10px; width: 120px"
          placeholder="关注状态"
          @on-change="searchList()"
        >
          <Option :value="0">未处理</Option>
          <Option :value="1">已处理</Option>
        </Select>
        <Input
          v-model="searchForm.name"
          placeholder="请输入登录账号、姓名、设备型号、设备号"
          search
          @on-search="searchList()"
          style="width: 310px"
        />
      </div>
    </div>
    <div class="table_wrap">
      <Table :columns="columns" :data="searchForm.list"></Table>
    </div>
    <div class="page_wrap">
      <Page
        :transfer="true"
        :total="searchForm.total"
        :current="searchForm.page"
        :page-size="searchForm.size"
        :page-size-opts="[10, 20, 40, 60]"
        @on-change="pageChange"
        @on-page-size-change="pageSizeChange"
        show-total
        show-sizer
      ></Page>
    </div>
  </div>
</template>

<script>
import { device_manager_list_hd } from "@/api/device_manager";
export default {
  data() {
    return {
      searchForm: {
        atype: 0,
        doEvent: 1,
        handled: 0,
        name: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "登录账号",
          key: "loginname",
          align: "center"
        },
        {
          title: "姓名",
          key: "username",
          align: "center"
        },
        {
          title: "班级",
          key: "classname",
          align: "center"
        },
        {
          title: "设备名称",
          key: "device",
          align: "center"
        },
        {
          title: "设备型号",
          key: "deviceModel",
          align: "center"
        },
        {
          title: "设备号",
          key: "sn",
          align: "center"
        },
        {
          title: "Rom版本",
          key: "romVersion",
          align: "center"
        },
        {
          title: "Token更新时间",
          key: "lasttime",
          width: 190,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.searchList();
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  methods: {
    doEventChange() {
      if (this.searchForm.doEvent === 1) {
        this.columns[this.columns.length - 1].title = "Token更新时间";
      } else if (this.searchForm.doEvent === 2) {
        this.columns[this.columns.length - 1].title = "最后在线时间";
      } else if (this.searchForm.doEvent === 3) {
        this.columns[this.columns.length - 1].title = "上报时间";
      }
      this.searchList();
    },
    // 搜索
    searchList() {
      this.searchForm.page = 1;
      this.getList();
    },
    // 页码改变
    pageChange(page) {
      this.searchForm.page = page;
      this.getList();
    },
    // 每页显示数量改变
    pageSizeChange(size) {
      this.searchForm.size = size;
      this.searchForm.page = 1;
      this.getList();
    },
    // 获取列表
    getList() {
      device_manager_list_hd({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        schoolid: this.powerParams.objectid,
        name: this.searchForm.name,
        atype: this.searchForm.atype,
        doEvent: this.searchForm.doEvent,
        handled: this.searchForm.handled
      }).then((data) => {
        if (data.code === 0) {
          this.searchForm.list = data.obj.data;
          this.searchForm.total = data.obj.total;
        } else {
          this.$Message.error(data.msg);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.search_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 16px;
  .search_left {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }
}
</style>
