<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Select
          v-model="searchForm.regionid"
          :transfer="true"
          @on-change="searchList()"
          style="width: 150px"
        >
          <Option :value="0">所有区域</Option>
          <Option
            v-for="regionItem in searchForm.regionList"
            :value="regionItem.regionid"
            :key="regionItem.regionid"
            >{{ regionItem.regionName }}</Option
          >
        </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="lockStateSlot">
          <!-- 锁定状态0未锁定1已锁定 -->
          <div class="action_list">
            <div class="action_success" v-if="row.lockState === 0">正常</div>
            <div class="action_error" v-else-if="row.lockState === 1">锁定</div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="actionSlot">
          <div class="action_list">
            <div v-if="row.sn" @click="toView(row.sn)">查看</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>
    <Modal
      class="modal2"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div class="table_wrap_modal">
        <Table :columns="viewInfo.columns" :data="viewInfo.list"></Table>
      </div>
      <div slot="footer" style="text-align: center">
        <Button @click="viewInfo.show = false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { region_list_sel_pt } from "@/api/region";
import { exportToExcel } from "@/utils/exportToExcel";
import {
  deviceAuth_list_export,
  deviceAuth_export,
  deviceAuth_edetail
} from "@/api/device_manager";
export default {
  data() {
    return {
      searchForm: {
        regionid: 0,
        regionList: [],
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      viewInfo: {
        show: false,
        list: [],
        columns: [
          {
            title: "序号",
            type: "index",
            align: "center",
            width: 70
          },
          {
            title: "所属区域",
            minWidth: 100,
            key: "regionName",
            align: "center"
          },
          {
            title: "学校名称",
            minWidth: 150,
            key: "schoolName",
            align: "center"
          },
          {
            title: "班级",
            minWidth: 150,
            key: "classname",
            align: "center"
          },
          {
            title: "姓名",
            minWidth: 80,
            key: "username",
            align: "center"
          },
          {
            title: "绑定时间",
            key: "createtime",
            width: 190,
            align: "center"
          },
          {
            title: "解绑时间",
            key: "unbindtime",
            width: 190,
            align: "center"
          }
        ]
      },
      columns: [
        {
          title: "序号",
          fixed: "left",
          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: 80,
          key: "username",
          align: "center"
        },
        {
          title: "设备号",
          minWidth: 170,
          key: "sn",
          align: "center"
        },
        {
          title: "设备型号",
          width: 150,
          key: "deviceModel",
          align: "center"
        },
        {
          title: "状态",
          slot: "lockStateSlot",
          width: 90,
          align: "center"
        },
        {
          title: "首次激活时间",
          key: "firsttime",
          width: 190,
          align: "center"
        },
        {
          title: "末次解绑时间",
          key: "lasttime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          fixed: "right",
          slot: "actionSlot",
          width: 90,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.getRegionList();
    this.searchList();
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  methods: {
    // 获取搜索区域列表
    getRegionList() {
      region_list_sel_pt({}).then((data) => {
        if (data.code === 0) {
          this.searchForm.regionList = data.obj;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 搜索
    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() {
      deviceAuth_list_export({
        rtype: this.powerParams.rtype,
        regionid: this.searchForm.regionid,
        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);
        }
      });
    },
    toView(sn) {
      deviceAuth_edetail({ sn }).then((data) => {
        if (data.code === 0) {
          this.viewInfo.list = data.obj;
        } else {
          this.$Message.error(data.msg);
        }
      });
      this.viewInfo.show = true;
    },
    toExport() {
      deviceAuth_export({
        rtype: this.powerParams.rtype,
        regionid: this.searchForm.regionid
      }).then((data) => {
        if (data.code === 0) {
          exportToExcel(
            [
              {
                list: data.obj.map((item) => {
                  return {
                    学校名称: item.schoolName,
                    班级: item.classname,
                    姓名: item.username,
                    设备号: item.sn,
                    设备型号: item.deviceModel,
                    状态:
                      item.lockState === 1
                        ? "锁定"
                        : item.lockState === 0
                        ? "正常"
                        : "",
                    首次激活时间: item.firsttime,
                    末次解绑时间: item.lasttime
                  };
                }),
                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>
