<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="年级：">
        <el-select
          v-model="firstForm.grade"
          placeholder="请选择年级"
          clearable
          @change="changeGrade"
        >
          <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_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="mr10" prop="xdfxx_xsxm" label="学生姓名">
        <el-input
          placeholder="输入学生姓名"
          v-model="xdfxx_xsxm"
          clearable
          @clear="getListData"
        ></el-input
      ></el-form-item>
      <el-form-item class="mr10" prop="xdfxx_xsxh" label="学籍号">
        <el-input
          placeholder="输入学籍号"
          v-model="xdfxx_xsxh"
          clearable
          @clear="getListData"
        ></el-input
      ></el-form-item>
      <el-form-item class="mr10" prop="xdfxx_fxrq" label="放学日期">
        <el-date-picker
          style="width: 100%"
          v-model="xdfxx_fxrq"
          type="date"
          placeholder="请选择日期"
          value-format="yyyy-MM-dd"
          clearable
          @change="getListData"
        >
        </el-date-picker>
      </el-form-item>
      <div class="mb20">
        <el-button type="primary" @click="getListData">搜索</el-button>
      </div></el-form
    >
    <el-button
      type="primary"
      @click="exportAll"
      :disabled="tableData.length == 0"
      >导出</el-button
    >

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      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="class_name" width="200">
      </el-table-column>
      <el-table-column label="放学日期" prop="xdfxx_fxrq" width="">
        <template slot-scope="scope">
          <span>{{ scope.row.xdfxx_fxrq.substr(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生姓名" prop="xdfxx_xsxm" width="">
      </el-table-column>
      <el-table-column label="学籍号" prop="xdfxx_xsxh" width="250">
      </el-table-column>
      <el-table-column label="性别" prop="xdfxx_xsxb" width="">
      </el-table-column>
      <el-table-column label="年龄" prop="xdfxx_xsnl" width="">
      </el-table-column>
      <el-table-column label="放学状态" prop="xdfxx_fxzt">
        <template slot-scope="scope">
          <span class="text-red">{{ scope.row.xdfxx_fxzt }}</span>
        </template>
      </el-table-column>
      <!--   <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="setData(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 { xs_list, xs_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: "",
      grade_id: "",
      class_id: "",
      class_name: "",
      xdfxx_fxrq: "",
      xdfxx_xsxm: "",
      xdfxx_xsxh: "",
      xdfxx_xszt: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
      },
      loading: false,
      multipleSelection: [],
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((item) => {
        this.multipleSelection.push(item.xdfxx_xsxh);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    changeGrade() {
      this.getListData();
      this.classListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        xdfxx_xsxm: this.xdfxx_xsxm,
        xdfxx_xsxh: this.xdfxx_xsxh,
        xdfxx_fxrq: this.xdfxx_fxrq,
        grade_id: this.firstForm.grade,
        class_id: this.firstForm.class,
        xdfxx_fxzt: "3",
      };

      xs_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 = {
        page: 1,
        grade_id: this.firstForm.grade,
        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_fxxt_xsfx_xs/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdfxx_fxzt=3&xdfxx_xsxh=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
    },
    // detailData() {
    //   this.$router.push({ name: "fxxt_xsfxgl_xslb_xslbxq" });
    // },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
      this.xdfxx_xsxm = "";
      this.xdfxx_xsxh = "";
      this.xdfxx_fxrq = "";
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
    this.classListData();
  },
};
</script>

<style scoped>
</style>