<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button
          type="primary"
          @click="exportAll"
          :disabled="tableData.length == 0"
          >导出</el-button
        >
      </div>
      <el-form :inline="true">
        <el-form-item label="年级：">
          <el-select
            v-model="this.xdxn_id"
            placeholder="请选择年级"
            clearable
            @change="gradeChange"
          >
            <el-option
              v-for="item in gradeList"
              :key="item.id"
              :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"
              :key="item.class_id"
              :label="item.class_name"
              :value="item.class_num"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" class="ml5 mb20" @click="getListData"
          >搜索
        </el-button>
      </el-form>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      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="">
      </el-table-column>
      <el-table-column label="班级" prop="class_name" width="">
      </el-table-column>
      <el-table-column label="总人数" prop="xdxb_zrs" width="">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(scope.row.class_id)"
            >查看
          </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 { bj_list, bj_export, grade_search, class_search } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      grade_name: "",
      class_name: "",
      class_id: "",
      grade_id: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
      },
      xdxn_id: "",
      loading: false,
      multipleSelection: [],
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((item) => {
        this.multipleSelection.push(item.class_id);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        grade_id: this.xdxn_id,
        class_id: this.firstForm.class,
      };

      bj_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);
      });
    },
    gradeChange(e) {
      console.log(e, 4444);
      this.xdxn_id = e;
      this.getListData();
    },

    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 = {
        page: 1,
        grade_id: this.xdxn_id,
        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 =
        window.globalVariables.api +
        "/xddy/dygl_xsczda_bj/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&class_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
    },
    detailData(id) {
      this.$router.push({
        name: "xsczda_xslb_bjlb_xslbxq",
        params: { nj_id: this.xdxn_id, bj_id: id },
      });
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
    },
    // changeGrade() {
    //   this.getListData();
    //   this.classListData();
    // },
  },
  mounted() {
    if (this.$route.params.id != "") {
      this.xdxn_id = this.$route.params.id;
      this.getListData();
      this.gradeListData();
      this.classListData();
    }
  },
};
</script>

<style scoped>
</style>