<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Select
          v-model="searchForm.atype"
          style="margin-right: 10px; width: 150px"
          @on-change="searchList()"
        >
          <!-- 1在线2离线 -->
          <Option :value="1">在线设备</Option>
          <Option :value="2">离线设备</Option>
        </Select>
        <Select
          v-if="searchForm.atype === 1"
          v-model="searchForm.doEvent"
          style="margin-right: 10px; width: 150px"
          @on-change="searchList()"
        >
          <Option :value="5">最近1小时</Option>
          <Option :value="4">最近48小时</Option>
          <Option :value="3">最近7天</Option>
          <Option :value="2">最近14天</Option>
          <Option :value="1">最近2个月</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">
        <template slot-scope="{ row }" slot="controlSlot">
          <div>
            {{
              row.control === 1 ? "管控中" : row.control === 2 ? "解除管控" : ""
            }}
          </div>
        </template>
      </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_od } from "@/api/device_manager";
export default {
  data() {
    return {
      searchForm: {
        // 1在线2离线
        atype: 1,
        // 1最近2个月 2最近14天 3最近7天 4最近48小时 5最近1小时
        doEvent: 5,
        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: "device",
          align: "center"
        },
        {
          title: "管控状态",
          slot: "controlSlot",
          align: "center"
        },
        {
          title: "设备型号",
          key: "deviceModel",
          align: "center"
        },
        {
          title: "设备号",
          key: "sn",
          align: "center"
        },
        {
          title: "Rom版本",
          key: "romVersion",
          align: "center"
        },
        {
          title: "最后在线时间",
          key: "lasttime",
          width: 190,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.searchList();
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  methods: {
    // 搜索
    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() {
      let form = {
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        schoolid: this.powerParams.objectid,
        atype: this.searchForm.atype,
        name: this.searchForm.name
      };
      if (this.searchForm.atype === 1) {
        form.doEvent = this.searchForm.doEvent;
      }
      device_manager_list_od(form).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>
