<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left"></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="violated">
          <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 { exportToExcel } from "@/utils/exportToExcel";
import { logDeviceBind_list_school, logDeviceBind_list_user } from "@/api/log";
export default {
  data() {
    return {
      searchForm: {
        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: "班级",
          key: "classname",
          align: "center",
          minWidth: 150
        },
        {
          title: "登录名",
          minWidth: 100,
          key: "loginname",
          align: "center"
        },
        {
          title: "姓名",
          minWidth: 80,
          key: "username",
          align: "center"
        },
        {
          title: "设备号",
          minWidth: 170,
          key: "sn",
          align: "center"
        },
        {
          title: "设备型号",
          width: 150,
          key: "deviceModel",
          align: "center"
        },
        {
          title: "Rom版本",
          minWidth: 200,
          key: "romVersion",
          align: "center"
        },
        {
          title: "违规",
          slot: "violated",
          align: "center",
          width: 70
        },
        {
          title: "更新时间",
          key: "createtime",
          align: "center",
          width: 180
        }
      ]
    };
  },
  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 _regionids = this.$store.state.userPower.regionids;
      _regionids = [..._regionids];
      logDeviceBind_list_school({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        regionid: _regionids.pop(),
        schoolid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size
      }).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() {
      logDeviceBind_list_user({
        schoolid: this.powerParams.objectid
      }).then((data) => {
        if (data.code === 0) {
          exportToExcel(
            [
              {
                list: data.obj.map((item) => {
                  return {
                    学校名称: item.schoolName,
                    班级: item.classname,
                    登录名: item.loginname,
                    姓名: item.username,
                    设备号: item.sn,
                    设备型号: item.deviceModel,
                    Rom版本: item.romVersion,
                    违规: item.violated === 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>
