<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" @click="importAll">导入</el-button>
        <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.grade"
            placeholder="请选择年级"
            @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" 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=""
      ></el-table-column>
      <el-table-column label="年级" prop="grade_name" width="">
      </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="toGrade(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
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="" prop="">
          <el-button type="primary" @click="exportModel"
            >导入模板下载</el-button
          >
        </el-form-item>
        <el-form-item label="日期" prop="xdct_tjrq">
          <el-date-picker
            type="date"
            v-model="dialogForm.xdct_tjrq"
            value-format="yyyy-MM-dd"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <!-- <el-upload
          class="upload-demo"
          :action="import_type_url"
          :data="{ ...dialogForm, token, issubmit: 1 }"
          :show-file-list="false"
          :on-success="import_success"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">确 定</el-button>
        </el-upload> -->
        <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 { cj_nj, grade_list } from "./api";
import { useUserStore } from "@/stores/user";
import { dealWithDate } from "@/utils/index";
const { token, real_name } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      activeName: "first",
      limit: 10,
      total: 0,
      cur_page: 1,
      loading: false,

      firstForm: {
        grade: "",
        type: "",
        // time: [dealWithDate(new Date()), dealWithDate(new Date())],
        time: ["", ""],
      },
      gradeList: [],
      typeList: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "晨检",
          value: 1,
        },
        {
          label: "午检",
          value: 2,
        },
      ],
      firstTable: [],
      curDate: "",
      multipleSelection: [],
      FILE_LIST: [],
      dialogFormVisible: false,
      dialogForm: {
        xdct_tjrq: "",
      },
      rules: {
        xdct_tjrq: [{ required: true, message: "请选择日期", trigger: "blur" }],
      },
    };
  },
  methods: {
    formCancel() {
      this.dialogFormVisible = false;
    },
    importAll() {
      this.dialogFormVisible = true;
      this.dialogForm = {
        xdct_tjrq: this.firstForm.time[0],
      };
    },
    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_cjxt_tjdj/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);
      });
      console.log(this.multipleSelection, 1111);
    },
    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,
      ];
      // this.firstForm.time[0] = date.getFullYear() + "-" + month + "-" + strDate;

      // this.firstForm.time[1] = date.getFullYear() + "-" + month + "-" + strDate;
      console.log(this.firstForm.time, " this.firstForm.time[0]");

      this.$forceUpdate();
    },
    getGradeList() {
      let data = {
        page: 1,
        limit: 999,
      };
      grade_list(data).then((res) => {
        if (res.code == 1) {
          this.gradeList = res.data.page_data;
          // let obj = {
          //   grade_id: "",
          //   grade_name: "全部",
          // };
          // this.gradeList.unshift(obj);
        }
      });
    },

    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        grade_id: this.firstForm.grade,
        xdcbj_tjlx: this.firstForm.type,
      };
      if (this.firstForm.time.length > 1) {
        data.xdcbj_tjrq =
          this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        data.xdcbj_tjrq = "";
      }
      console.log(data.xdcbj_tjrq, "data");
      cj_nj(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);
          console.log(this.firstTable, 1111);
        }
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getTime() {
      if (!this.firstForm.time) {
        this.firstForm.time = ["", ""];
      }
      this.cur_page = 1;
      this.getListData();
    },
    exportModel() {
      const link = `${window.globalVariables.api}template/xls/xddy_dygl_cjxt_tjdj.xls`;
      window.open(link, "_blank");
      console.log(link);
    },
    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_nj/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&grade_id=" +
        this.multipleSelection.join(",") +
        "&xdcbj_tjlx=" +
        this.firstForm.type +
        "&xdcbj_tjrq=" +
        time;
      window.open(link, "_self");
      console.log(link);
    },
    toGrade(id) {
      this.$router.push({ name: "cjxt_tjdj_njck", params: { id: id } });
    },
  },
  mounted() {
    this.getTimeFormat();
    this.getGradeList();
    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>