<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: 165px"
          @on-change="searchList()"
        >
          <Option :value="0">所有事件</Option>
          <Option
            v-for="(eventName, eventVal) in breakRuleDoEvent"
            :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">
          <div>{{ breakRuleDoEvent[row.doEvent] }}</div>
        </template>
        <template slot-scope="{ row }" slot="noticedSlot">
          <div class="action_list">
            <!-- 是否通知：0否 1是 -->
            <div v-if="row.noticed === 1" class="action_success">是</div>
            <div v-else-if="row.noticed === 0" class="action_error">否</div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="lockedSlot">
          <div class="action_list">
            <!-- 是否锁定：0否 1是 -->
            <div v-if="row.locked === 1" class="action_success">是</div>
            <div v-else-if="row.locked === 0" class="action_error">否</div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="resetedSlot">
          <div class="action_list">
            <!-- 是否重置：0否 1是 -->
            <div v-if="row.reseted === 1" class="action_success">是</div>
            <div v-else-if="row.reseted === 0" class="action_error">否</div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="handledSlot">
          <div class="action_list">
            <!-- 是否处理：0否 1是 -->
            <div v-if="row.handled === 1" class="action_success">是</div>
            <div v-else-if="row.handled === 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, breakRuleDoEvent } from "@/utils";
import { exportToExcel } from "@/utils/exportToExcel";
import {
  logdoperate_list_violate,
  logdoperate_list_violate_all
} from "@/api/log";
export default {
  data() {
    return {
      breakRuleDoEvent,
      searchForm: {
        dataRange: [],
        doEvent: 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: "学校名称",
          minWidth: 150,
          key: "schoolName",
          align: "center"
        },
        {
          title: "班级",
          minWidth: 150,
          key: "classname",
          align: "center"
        },
        {
          title: "登录名",
          minWidth: 100,
          key: "loginname",
          align: "center"
        },
        {
          title: "姓名",
          minWidth: 80,
          key: "username",
          align: "center"
        },
        {
          title: "设备号",
          minWidth: 170,
          key: "sn",
          align: "center"
        },
        {
          title: "事件",
          minWidth: 150,
          slot: "doEventSlot",
          align: "center"
        },
        {
          title: "通知",
          slot: "noticedSlot",
          width: 70,
          align: "center"
        },
        {
          title: "锁定",
          slot: "lockedSlot",
          width: 70,
          align: "center"
        },
        {
          title: "重置",
          slot: "resetedSlot",
          width: 70,
          align: "center"
        },
        {
          title: "处理",
          slot: "handledSlot",
          width: 70,
          align: "center"
        },
        {
          title: "时间",
          key: "createtime",
          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 _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;
      let _regionids = this.$store.state.userPower.regionids;
      _regionids = [..._regionids];
      logdoperate_list_violate({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        regionid: _regionids.pop(),
        schoolid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        doEvent: this.searchForm.doEvent,
        begindate: _begindate,
        enddate: _enddate,
        name: this.searchForm.name
      }).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() {
      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;
      let _regionids = this.$store.state.userPower.regionids;
      _regionids = [..._regionids];
      logdoperate_list_violate_all({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        regionid: _regionids.pop(),
        schoolid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        doEvent: this.searchForm.doEvent,
        begindate: _begindate,
        enddate: _enddate,
        name: this.searchForm.name
      }).then((data) => {
        if (data.code === 0) {
          exportToExcel(
            [
              {
                list: data.obj.map((item) => {
                  return {
                    学校名称: item.schoolName,
                    班级: item.classname,
                    登录名: item.loginname,
                    姓名: item.username,
                    设备号: item.sn,
                    事件: this.breakRuleDoEvent[item.doEvent],
                    通知: item.noticed === 1 ? "是" : "否",
                    锁定: item.locked === 1 ? "是" : "否",
                    重置: item.reseted === 1 ? "是" : "否",
                    处理: item.handled === 1 ? "是" : "否",
                    时间: item.createtime
                  };
                }),
                name: "违规设备日志"
              }
            ],
            "违规设备日志"
          );
        } 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>
