<template>
  <div>
    <el-form :inline="true" :model="firstForm">
      <el-form-item label="年级：">
        <el-select
          v-model="firstForm.grade"
          placeholder="请选择年级"
          @change="switchGrade"
        >
          <el-option
            v-for="item in gradeList"
            :label="item.grade_name"
            :value="item.grade_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级：">
        <el-select
          v-model="firstForm.class"
          placeholder="请选择班级"
          clearable
          @change="getListData"
        >
          <el-option
            v-for="item in classList"
            :label="item.class_name"
            :value="item.class_num"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="mr10" label="学生姓名：">
        <el-input
          v-model="firstForm.xsxm"
          placeholder="输入学生姓名"
          @change="getListData"
        ></el-input>
      </el-form-item>
      <el-form-item class="mr10" label="学籍号：">
        <el-input
          v-model="firstForm.xsxh"
          placeholder="输入学籍号"
          @change="getListData"
        ></el-input>
      </el-form-item>
      <el-button type="primary" @change="getListData">搜索</el-button>
    </el-form>
    <el-button
      type="primary"
      @click="exportAll"
      :disabled="tableData.length === 0"
      >导出</el-button
    >
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      class="mt20"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>

      <el-table-column label="年级" prop="grade_name" width="100">
      </el-table-column>
      <el-table-column label="班级" prop="class_name" width="100">
      </el-table-column>
      <el-table-column label="学生姓名" prop="xdwx_xsxm" width="100">
      </el-table-column>
      <el-table-column
        label="学籍号"
        prop="xdwx_xsxh"
        align="center"
        width="150"
      >
      </el-table-column>
      <el-table-column label="性别" prop="xdwx_xsxb" width="100">
      </el-table-column>
      <el-table-column label="二维码">
        <template slot-scope="scope">
          <h3 class="qr-code hand" @click="showQr(scope.row)"></h3>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog title="二维码详情" :visible.sync="dialogVisible" width="300px">
      <div class="text-center">
        <vue-qr
          :size="200"
          :margin="0"
          :auto-color="true"
          :dot-scale="1"
          :text="appSrc"
          colorDark="white"
          colorLight="black"
        />
      </div>

      <div slot="footer" class="text-center">
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { xsewm_list, xsewm_export, grade_search, class_search } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
import VueQr from "vue-qr";
export default {
  name: "index",
  components: {
    VueQr,
  },
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      grade_name: "",
      class_name: "",
      grade_id: "",
      class_id: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
        xsxh: "",
        xsxm: "",
      },
      loading: false,
      logoSrc: "",
      appSrc: "",
      dialogVisible: false,
      multipleSelection: [],
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.firstForm.xsxm,
        grade_id: this.firstForm.grade,
        class_id: this.firstForm.class,
        xdwx_xsxh: this.firstForm.xsxh,
      };

      xsewm_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    gradeListData() {
      let data = {
        page: 1,
        limit: 9999,
      };
      grade_search(data).then((res) => {
        if (res.code == 1) {
          this.gradeList = res.data.page_data;
          let obj = {
            grade_id: "",
            grade_name: "全部",
          };
          this.gradeList.unshift(obj);
        }
      });
    },
    classListData() {
      let data = {
        grade_id: this.firstForm.grade,
        page: 1,
        limit: 9999,
      };
      class_search(data).then((res) => {
        if (res.code == 1) {
          this.classList = res.data.page_data;
          let obj = {
            class_id: "",
            class_name: "全部",
          };
          this.classList.unshift(obj);
        }
      });
    },
    exportAll() {
      let link = "";
      if (this.multipleSelection.length === 0) {
        link =
          window.globalVariables.api +
          "/xddy/dygl_wypj_xsewm/index?api=xls&page=1&limit=10000&token=" +
          token;
      } else {
        link =
          window.globalVariables.api +
          "/xddy/dygl_wypj_xsewm/index?api=xls&page=1&limit=10000&token=" +
          token +
          "&xdwx_id=" +
          this.multipleSelection.join(",");
      }

      window.open(link, "_self");
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
    },
    showQr(item) {
      this.appSrc =
        "http://60.188.226.44:8000/app/xddy/#/scanCode?xsxm=" +
        item.xdwx_xsxm +
        "&xsxh=" +
        item.xdwx_xsxh +
        "&sm_id=" +
        item.sm_id +
        "&token=" +
        token +
        "&xdwx_id=" +
        item.xdwx_id;

      this.dialogVisible = true;
    },
    switchGrade() {
      this.firstForm.class = "";
      this.classListData();
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdwx_id);
      }
    },
  },
  mounted() {
    this.gradeListData();
    this.getListData();
    // this.classListData();
  },
};
</script>

<style scoped lang="scss">
.qr-code {
  width: 28px;
  height: 28px;
  background: url("@/assets/images/a-2.png") center no-repeat;
  background-size: 28px 28px;
}
</style>