<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" @click="importAll">导入</el-button>
        <!-- <el-upload
          action="#"
          :show-file-list="false"
          :http-request="httpRequest"
          :file-list="FILE_LIST"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">导入</el-button>
        </el-upload> -->
        <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="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-button type="primary" class="ml5 mb20" @click="getListData"
          >搜索
        </el-button>
      </el-form>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <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="xdxn_zrs" width="">
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(scope.row.grade_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>
    <el-dialog
      :visible.sync="dialogFormVisible"
      v-if="dialogFormVisible"
      width="580px"
    >
      <el-form label-width="80px">
        <el-form-item label="" prop="">
          <el-button type="primary" @click="exportModel"
            >导入模板下载</el-button
          >
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-upload
          action="#"
          :show-file-list="false"
          :http-request="httpRequest"
          :file-list="FILE_LIST"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">确 定</el-button>
        </el-upload>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { request } from "@/utils/request";
import { Message } from "element-ui";
import { nj_list, nj_export, grade_search, class_search } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 0,
      cur_page: 1,
      keyword: "",
      grade_id: "",
      class_name: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
      },
      loading: false,
      FILE_LIST: [],
      dialogFormVisible: false,
    };
  },
  methods: {
    formCancel() {
      this.dialogFormVisible = false;
    },
    importAll() {
      this.dialogFormVisible = true;
    },
    httpRequest(file) {
      const formData = new FormData();
      formData.append("token", token);
      formData.append("issubmit", "1");
      // formData.append("xdct_tjrq", this.dialogForm.xdct_tjrq);
      console.log(formData, 444444444444);
      formData.append("file", file.file);
      request({
        url: "/xddy/dygl_xsczda_xs/import_new",
        data: formData,
        timeout: 1000 * 60,
      }).then(({ data, code }) => {
        console.log(data, code, 4444444444);
        if (code === "1") {
          Message.success("上传成功");
          this.getListData();
          this.dialogFormVisible = false;
        } else {
          this.$message({
            message: data.error,
            type: "error",
          });
        }
      });
    },
    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,
        keyword: this.keyword,
        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);
        }
      });
    },
    exportModel() {
      const link = `${window.globalVariables.api}template/xls/xddy_dygl_xsczda_xs.xls`;
      window.open(link, "_blank");
      console.log(link);
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsczda_nj/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&grade_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
      console.log(link);
    },
    detailData(id) {
      this.$router.push({ name: "xsczda_xslb_bjlb", params: { id: id } });
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
  },
};
</script>

<style scoped>
</style>