<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-upload
          class="upload-demo"
          :action="import_type_url"
          :show-file-list="false"
          :data="import_type_data"
          :on-success="import_success"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">导入</el-button>
        </el-upload>
        <el-button type="primary" @click="exportType">导入模板下载</el-button>
        <el-button type="primary" @click="exportType">导出</el-button>
        <!--      <el-button type="danger"  :disabled="multipleSelection.length == 0" @click="delStu">删除</el-button>-->
      </div>
      <el-form :inline="true" :model="firstForm">
        <el-form-item class="mr10" label="">
          <el-input
            v-model="firstForm.xsxm"
            placeholder="请输入学生姓名"
            clearable
            @clear="getListData"
          ></el-input>
        </el-form-item>
        <el-form-item class="mr10" label="">
          <el-input
            v-model="firstForm.xsxh"
            placeholder="请输入学籍号"
            clearable
            @clear="getListData"
          ></el-input>
        </el-form-item>
        <el-form-item class="mr10" label="">
          <el-input
            v-model="firstForm.sj"
            placeholder="请输入手机号码"
            clearable
            @clear="getListData"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="getListData">搜索</el-button>
      </el-form>
    </div>
    <el-table
      :data="firstTable"
      v-loading="loading"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column
        label="班级"
        prop="class_name"
        width=""
      ></el-table-column>
      <el-table-column label="学生姓名" prop="xdx_xsxm" width="150">
      </el-table-column>
      <el-table-column label="学籍号" prop="xdx_xsxh" align="center" width="">
      </el-table-column>
      <el-table-column label="手机号码" prop="xdx_sjhm" align="center">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="toList(scope.row)"
            >荣誉管理
          </el-button>
        </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>
  </div>
</template>

<script>
import { jl_list, jl_del } from "./api";
import { useUserStore } from "@/stores/user";

const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 0,
      cur_page: 1,
      firstForm: {
        xsxm: "",
        xsxh: "",
        sj: "",
      },
      loading: false,
      firstTable: [],
      import_type_url:
        window.globalVariables.api +
        "/xddy/dygl_xshjgl/import?issubmit=1&token=" +
        token,
      import_type_data: {},
      multipleSelection: [],
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdx_id);
      }
    },
    exportType() {
      let link =
        window.globalVariables.api +
        "xddy/dygl_xshjgl/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdx_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.firstForm.xsxm,
        xdx_xsxh: this.firstForm.xsxh,
        xdx_sjhm: this.firstForm.sj,
      };
      jl_list(data).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.firstTable = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
        }
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    import_success(res) {
      if (res.code == 1) {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "success",
        });
        this.getListData();
      } else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "error",
        });
      }
    },
    delStu() {
      let del_arr = [];
      for (let i in this.multipleSelection) {
        del_arr.push(this.multipleSelection[i].xdx_id);
      }
      this.$confirm("确定要删除这些学生?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdx_id: del_arr,
          };
          jl_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "学生删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    toList(item) {
      this.$router.push({
        name: "xshjgl_jlgl_rygl",
        params: { no: item.xdx_xsxh, xm: item.xdx_xsxm },
      });
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>