<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div><el-button type="primary" @click="exportData">导出</el-button></div>
      <el-form :inline="true" :model="firstForm">
        <el-form-item class="mr10" label="">
          <el-select
            v-model="firstForm.class"
            placeholder="请选择班级"
            @change="getListData"
          >
            <el-option
              v-for="item in classList"
              :label="item.class_name"
              :value="item.class_num"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr10" label="">
          <el-select
            v-model="firstForm.type"
            placeholder="请选择检查类别"
            @change="getListData"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr10" label="">
          <el-date-picker
            v-model="firstForm.time"
            @change="getTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-button type="primary" class="ml5 mb20" @click="getListData"
          >搜索</el-button
        >
      </el-form>
    </div>

    <el-table
      @selection-change="handleSelectionChange"
      :data="firstTable"
      v-loading="loading"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column
        label="晨午检"
        prop="xdcbj_tjlx"
        width="100"
      ></el-table-column>
      <el-table-column label="班级" prop="class_name" width="150">
      </el-table-column>
      <el-table-column label="体检日期" prop="xdcbj_tjrq" width="150">
      </el-table-column>
      <el-table-column
        label="应到人数"
        prop="xdcbj_ydrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="实到人数"
        prop="xdcbj_sdrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="因病缺勤人数"
        prop="xdcbj_ybqqrs"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="因事缺勤人数"
        align="center"
        prop="xdcbj_ysqqrs"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="异常学生人数"
        align="center"
        prop="xdcbj_ysqqrs"
        width=""
      >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="toClass(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 { cj_bj, class_list } from "./api";
import { useUserStore } from "@/stores/user";
const { token, real_name } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 0,
      cur_page: 1,
      loading: false,
      grade_id: "",
      firstForm: {
        class: "",
        type: "",
        time: ["", ""],
      },
      classList: [],
      typeList: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "晨检",
          value: 1,
        },
        {
          label: "午检",
          value: 2,
        },
      ],
      firstTable: [],
      curDate: "",
      multipleSelection: [],
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((item) => {
        this.multipleSelection.push(item.class_id);
      });
    },
    getTimeFormat() {
      var date = new Date();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
     this.firstForm.time = [
        date.getFullYear() + "-" + month + "-" + strDate,
        date.getFullYear() + "-" + month + "-" + strDate,
      ];
    },

    getClassList() {
      let data = {
        grade_id: this.grade_id,
        page: 1,
        limit: 999,
      };
      class_list(data).then((res) => {
        if (res.code == 1) {
          this.classList = res.data.page_data;
          // let obj = {
          //   class_id: "",
          //   class_name: "",
          // };
          // this.classList.unshift(obj);
        }
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        grade_id: this.grade_id,
        class_id: this.firstForm.class,
        xdcbj_tjlx: this.firstForm.type,
        xdcbj_tjrq: this.curDate,
      };
      if (this.firstForm.time.length > 1) {
        data.xdcbj_tjrq =
          this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        data.xdcbj_tjrq = "";
      }
      cj_bj(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          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();
    },
    getTime() {
      if (!this.firstForm.time) {
        this.firstForm.time = ["", ""];
      }
      this.cur_page = 1;
      this.getListData();
    },
    exportData() {
      let time = "";
      if (this.firstForm.time.length > 1) {
        time = this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        time = "";
      }
      let link =
        window.globalVariables.api +
        "/xddy/dygl_cjxt_bj/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&grade_id=" +
        this.grade_id +
        "&class_id=" +
        this.multipleSelection.join(",") +
        "&xdcbj_tjlx=" +
        this.firstForm.type +
        "&xdcbj_tjrq=" +
        time;
      window.open(link, "_self");
    },
    toClass(item) {
      this.$router.push({
        name: "cjxt_tjdj_bjck",
        params: { grade_id: item.grade_id, class_id: item.class_id },
      });
    },
  },
  mounted() {
    this.getTimeFormat();
    if (this.$route.params.id != "") {
      this.grade_id = this.$route.params.id;
      this.getClassList();
      this.getListData();
    }
  },
};
</script>

<style scoped>
::v-deep .el-input {
  width: 160px;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 300px;
}
::v-deep .el-date-editor .el-range-separator {
  width: 8%;
}
</style>