<template>
  <div class="scan-log-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="启用/禁用：" label-width="90px">
          <el-select
            v-model="filter.schoolCode"
            placeholder="选择学校"
            filterable
          >
            <el-option
              v-for="item in schools"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="filter.loginName"
            placeholder="扫描账号模糊查询"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" @click="toPage(1)">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column type="index" label="序号" width="70"></el-table-column>
        <el-table-column
          prop="schoolCode"
          label="学校Code"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="loginName"
          label="扫描账号"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="deviceCode"
          label="设备号"
          width="300"
        ></el-table-column>
        <el-table-column
          prop="fileName"
          label="文件名"
          min-width="200"
        ></el-table-column>
        <el-table-column prop="uploadTime" label="更新时间" width="170">
          <span slot-scope="scope">{{
            scope.row.uploadTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          fixed="right"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toView(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { download } from "@/plugins/utils";
import { schoolList, scanLogListPage } from "../api";

export default {
  name: "scan-log-manage",
  data() {
    return {
      filter: {
        schoolCode: null,
        loginName: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      schools: [],
    };
  },
  async mounted() {
    await this.getSchools();
    this.filter.schoolCode = this.schools[0]?.code;
    await this.toPage(1);
  },
  methods: {
    async getSchools() {
      const data = await schoolList();
      this.schools = data || [];
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await scanLogListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toView(row) {
      if (row.type === "NORMAL") {
        const res = await download({ url: row.pathUrl });
        const win = window.open("", "_blank");
        win.document.write(`<pre>${res}</pre>`);
        win.document.close();
      } else {
        window.open(row.pathUrl);
      }
    },
  },
};
</script>
