<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <div class="select">
          <div class="title" @click.stop="headerTree">
            {{ searchForm.title }}
            <Icon type="ios-arrow-down" />
          </div>
          <div class="regintree" v-if="isshowtree">
            <Tree
              @on-select-change="getSelectedNodes"
              :data="region_list"
            ></Tree>
          </div>
        </div>
        <Select
          :transfer="true"
          v-model="searchForm.schoolid"
          @on-change="searchList()"
          style="width: 180px; margin-left: 10px"
        >
          <template v-if="school_list.length > 0">
            <Option :value="0">所有学校</Option>
            <Option
              v-for="item in school_list"
              :value="item.schoolid"
              :key="item.schoolid"
              >{{ item.schoolName }}</Option
            >
          </template>
        </Select>
      </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">
            <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>
          </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>
    <Spin fix v-if="showLoading" style="background-color: transparent">
      <Icon type="ios-loading" size="18" class="demo-spin-icon-load"></Icon>
      <div>加载中</div>
    </Spin>
  </div>
</template>

<script>
import { logDeviceBind_list_region, logDeviceBind_list_user } from "@/api/log";
import { region_list_sel_tree } from "@/api/region";
import { exportToExcel } from "@/utils/exportToExcel";
import { school_list_rs } from "@/api/school";
export default {
  data() {
    return {
      showLoading: false,
      searchForm: {
        regionid: 0,
        schoolid: 0,
        title: "所有区域",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      isshowtree: false,
      region_list: [],
      school_list: [],
      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",
          width: 110
        },
        {
          title: "登录名",
          key: "loginname",
          align: "center"
        },
        {
          title: "姓名",
          key: "username",
          align: "center"
        },
        {
          title: "设备号",
          key: "sn",
          align: "center"
        },
        {
          title: "设备型号",
          key: "deviceModel",
          align: "center"
        },
        {
          title: "Rom版本",
          key: "romVersion",
          align: "center"
        },
        {
          title: "违规",
          slot: "violated",
          align: "center",
          width: 70
        },
        {
          title: "更新时间",
          key: "createtime",
          align: "center",
          width: 180
        }
      ]
    };
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  created() {
    this.userInfo = JSON.parse(
      localStorage.getItem("xh_control_userInfo")
    ).content;
    this.init();
  },
  mounted() {
    window.addEventListener("click", () => {
      this.isshowtree = false;
    });
  },
  methods: {
    headerTree() {
      //打开树形区域列表
      this.isshowtree = true;
    },
    async getSelectedNodes(data) {
      this.searchForm.title = data[0].title;
      this.searchForm.regionid = data[0].regionid;
      this.searchForm.schoolid = 0;
      await this.getSchoolList();
      this.searchList();
    },
    async init() {
      this.showLoading = true;
      let res = await region_list_sel_tree({
        regionid: this.powerParams.objectid
      });
      this.showLoading = false;
      if (res.code === 0) {
        this.setRegionList(res.obj[0]);
        this.region_list = res.obj;
        this.searchForm.regionid = this.region_list[0].regionid;
        this.searchForm.title = this.region_list[0].title;
      } else {
        this.$Message.error(res.msg);
      }
      this.searchForm.schoolid = 0;
      await this.getSchoolList();
      this.searchList();
    },
    async getSchoolList() {
      this.showLoading = true;
      let res = await school_list_rs({ regionid: this.searchForm.regionid });
      this.showLoading = false;
      if (res.code === 0) {
        this.school_list = res.obj;
      } else {
        this.$Message.error(res.msg);
      }
    },
    setRegionList(node) {
      node.title = node.regionName;
      node.expand = true;
      if (node.children && node.children.length > 0) {
        for (let child of node.children) {
          this.setRegionList(child);
        }
      }
    },
    // 搜索
    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() {
      this.showLoading = true;
      logDeviceBind_list_region({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        regionid: this.searchForm.regionid,
        schoolid: this.searchForm.schoolid
      }).then((data) => {
        this.showLoading = false;
        if (data.code === 0) {
          this.searchForm.list = data.obj.data;
          this.searchForm.total = data.obj.total;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    toExport() {
      this.showLoading = true;
      logDeviceBind_list_user({
        regionid: this.searchForm.regionid,
        schoolid: this.searchForm.schoolid
      }).then((res) => {
        this.showLoading = false;
        if (res.code === 0) {
          let name = "";
          exportToExcel(
            [
              {
                list: res.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(res.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;
    .select {
      width: 200px;
      height: 32px;
      border-radius: 8px;
      border: 1px solid #d8dce9;
      line-height: 30px;
      position: relative;
      padding-left: 10px;
      cursor: pointer;
      .ivu-icon {
        position: absolute;
        top: 6px;
        right: 10px;
        font-size: 18px;
      }
    }
    .regintree {
      min-width: 200px;
      height: 320px;
      overflow-y: auto;
      border-radius: 8px;
      border: 1px solid #d8dce9;
      position: absolute;
      z-index: 10;
      background-color: white;
      top: 36px;
      left: 0;
      padding-left: 10px;
    }
  }
}
</style>
