<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <DatePicker
          v-model="searchForm.dataRange"
          @on-change="searchList()"
          :editable="false"
          :transfer="true"
          format="yyyy-MM-dd"
          type="daterange"
          placement="bottom-start"
          placeholder="请选择日期范围"
          style="margin-right: 10px; width: 220px"
        ></DatePicker>
        <Select
          v-model="searchForm.doEvent"
          :transfer="true"
          style="margin-right: 10px; width: 150px"
          @on-change="searchList()"
        >
          <Option :value="0">所有事件</Option>
          <Option
            v-for="(eventName, eventVal) in doEventInfo"
            :value="Number(eventVal)"
            :key="eventVal"
            >{{ eventName }}</Option
          >
        </Select>
        <Input
          v-model="searchForm.name"
          placeholder="请输入登录名、设备号"
          search
          @on-search="searchList()"
          style="width: 180px"
        />
      </div>
      <Button type="primary" class="primary_btn" @click="toExport()"
        >导出</Button
      >
    </div>
    <div class="table_wrap">
      <Table :columns="columns" :data="searchForm.list">
        <template slot-scope="{ row }" slot="doEventSlot">
          <!-- 1发送消息 2解除限制 3限制使用 4更新策略 5重启设备 6恢复出厂 -->
          <div>{{ doEventInfo[row.doEvent] }}</div>
        </template>
        <template slot-scope="{ row }" slot="violatedSlot">
          <div class="action_list">
            <!-- 0不违规1违规 -->
            <div v-if="row.violated === 1" class="action_success">是</div>
            <div v-else-if="row.violated === 0" class="action_error">否</div>
          </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 { dateFormat, doEventInfo } from "@/utils";
import { logdoperate_list_event } from "@/api/log";
export default {
  data() {
    return {
      doEventInfo,
      searchForm: {
        dataRange: [],
        doEvent: 0,
        name: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      userInfo: {},
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "学校名称",
          key: "schoolName",
          align: "center"
        },
        {
          title: "班级",
          key: "classname",
          align: "center"
        },
        {
          title: "登录名",
          key: "loginname",
          align: "center"
        },
        {
          title: "姓名",
          key: "username",
          align: "center"
        },
        {
          title: "设备号码",
          key: "sn",
          align: "center"
        },
        {
          title: "事件",
          slot: "doEventSlot",
          align: "center"
        },
        {
          title: "描述",
          key: "comm",
          align: "center"
        },
        {
          title: "违规",
          slot: "violatedSlot",
          align: "center"
        },
        {
          title: "时间",
          key: "createtime",
          width: 190,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.userInfo = JSON.parse(
      localStorage.getItem("xh_control_userInfo")
    ).content;
    this.searchList();
  },
  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 _begindate = this.searchForm.dataRange[0];
      _begindate = _begindate ? dateFormat(_begindate, "yyyy-MM-dd") : null;
      let _enddate = this.searchForm.dataRange[1];
      _enddate = _enddate ? dateFormat(_enddate, "yyyy-MM-dd") : null;
      logdoperate_list_event({
        page: this.searchForm.page,
        size: this.searchForm.size,
        doEvent: this.searchForm.doEvent,
        begindate: _begindate,
        enddate: _enddate,
        name: this.searchForm.name,
        regionid: this.userInfo.regionid
      }).then((data) => {
        if (data.code === 0) {
          this.searchForm.list = data.obj.data;
          this.searchForm.total = data.obj.total;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    toExport() {}
  }
};
</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>
