<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="年级：">
        <el-select
          v-model="firstForm.grade"
          placeholder="请选择年级"
          clearable
          @change="getListData"
        >
          <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 class="mr10" prop="xdfxn_fxrq" label="放学日期">
        <el-date-picker
          style="width: 100%"
          v-model="xdfxn_fxrq"
          type="date"
          placeholder="请选择日期"
          value-format="yyyy-MM-dd"
          clearable
          @change="getListData"
        >
        </el-date-picker>
      </el-form-item>

      <el-button type="primary" class="ml5" @click="getListData"
        >搜索</el-button
      >
      <!-- <el-button type="primary" class="ml5" @click="clearData">清空</el-button> -->
    </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="grade_name" width="">
      </el-table-column>
      <el-table-column label="放学日期" prop="xdfxn_fxrq" width="150">
      </el-table-column>
      <el-table-column
        label="应放人数"
        prop="xdfxn_yfrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="实放人数"
        prop="xdfxn_sfrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="滞留生人数"
        prop="xdfxn_zlsrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="留堂学生人数"
        prop="xdfxn_ltsrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column label="操作"    width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(scope.row.xdfxn_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 { nj_list, nj_export, grade_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: "",
      xdfxn_fxrq: "",
      grade_id: "",
      class_id: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
      },
      multipleSelection: [],
      loading: false,
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach(item => {
        this.multipleSelection.push(item.grade_id)
      })
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        xdfxn_fxrq: this.xdfxn_fxrq,
        grade_id: this.firstForm.grade,
      };

      nj_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);
        }
      });
    },

    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_fxxt_xsfx_nj/index?api=xls&page=1&limit=10000&token=" +
        token+'&grade_id='+ this.multipleSelection.join(',');
      window.open(link, "_blank");
    },
    detailData(id) {
      this.$router.push({ name: "fxxt_xsfxgl_bjlb", params: { id: id } });
    },
    clearData() {
      this.firstForm.grade = "";
      this.xdfxn_fxrq = "";
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
  },
};
</script>

<style scoped>
</style>