<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" @click="exportData">导出</el-button>
        <el-button
          type="primary"
          :disabled="multipleSelection.length == 0"
          @click="batchSign"
          >一键签到</el-button
        >
      </div>
      <el-form :model="searchForm" label-width="90px" :inline="true">
        <el-form-item label="" prop="xsxm">
          <el-input
            v-model="searchForm.xsxm"
            placeholder="请输入学生姓名"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="xsxh">
          <el-input
            v-model="searchForm.xsxh"
            placeholder="请输入学籍号"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="searchForm.tjjg"
            placeholder="请选择体检结果"
            @change="getListData"
          >
            <el-option
              v-for="item in jgList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="searchForm.tjlx"
            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 label="">
          <el-date-picker
            v-model="searchForm.tjrq"
            @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
      :data="tableData"
      v-loading="loading"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column
        label="晨午检"
        prop="xdct_xslx"
        width="100"
      ></el-table-column>
      <el-table-column label="班级" prop="class_name" width="150">
      </el-table-column>
      <el-table-column label="体检日期" prop="xdct_tjrq" width="150">
      </el-table-column>
      <el-table-column
        label="学生姓名"
        prop="xdct_xsxm"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="学籍号"
        prop="xdct_xsxh"
        align="center"
        width="200"
      >
      </el-table-column>
      <el-table-column label="性别" prop="xdct_xsxb" align="center" width="">
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="xdct_nl" width="">
      </el-table-column>
      <el-table-column label="体检结果" align="center" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.xdct_tjjg_option_k == 0">--</span>
          <span v-if="scope.row.xdct_tjjg_option_k == 1">良好</span>
          <span v-if="scope.row.xdct_tjjg_option_k == 2">及格</span>
          <span v-if="scope.row.xdct_tjjg_option_k == 3" class="color-red"
            >异常</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="checkPop(scope.row)"
            >体检登记
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.xdct_sfqd_option_k != 1"
            @click="checkSign(scope.row)"
            >签到
          </el-button>
          <el-button type="text" class="ml10" v-else disabled>已签</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
      title="体检登记"
      :visible.sync="checkDialogVisible"
      width="780px"
      v-loading="loading"
    >
      <el-form
        :model="checkForm"
        ref="checkForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="体检类型：" prop="xdct_xslx">
          <el-radio-group v-model="checkForm.xdct_xslx">
            <el-radio
              v-for="(item, index) in typeList"
              v-model="checkForm.xdct_xslx"
              v-if="index > 0"
              :label="item.value"
              >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="d-flex">
          <div class="flex-1">
            <el-form-item label="学生姓名：" prop="xdct_xsxm">
              <el-input v-model="checkForm.xdct_xsxm" readonly></el-input>
            </el-form-item>
          </div>
          <div class="flex-1">
            <el-form-item label="学籍号：" prop="xdct_xsxh">
              <el-input v-model="checkForm.xdct_xsxh" readonly></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="d-flex">
          <div class="flex-1">
            <el-form-item label="身高：" prop="xdct_tjxx_sg">
              <el-input v-model="checkForm.xdct_tjxx_sg"></el-input>
            </el-form-item>
          </div>
          <div class="flex-1">
            <el-form-item label="体重：" prop="xdct_tjxx_tz">
              <el-input v-model="checkForm.xdct_tjxx_tz"></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="d-flex">
          <div class="flex-1">
            <el-form-item label="胸围：" prop="xdct_tjxx_xw">
              <el-input v-model="checkForm.xdct_tjxx_xw"></el-input>
            </el-form-item>
          </div>
          <div class="flex-1">
            <el-form-item label="肺活量：" prop="xdct_tjxx_fhl">
              <el-input v-model="checkForm.xdct_tjxx_fhl"></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="d-flex">
          <div class="flex-1">
            <el-form-item label="血压：" prop="xdct_tjxx_xy">
              <el-input v-model="checkForm.xdct_tjxx_xy"></el-input>
            </el-form-item>
          </div>
          <div class="flex-1">
            <el-form-item label="脉搏：" prop="xdct_tjxx_mb">
              <el-input v-model="checkForm.xdct_tjxx_mb"></el-input>
            </el-form-item>
          </div>
        </div>
        <div class="d-flex">
          <div class="flex-1">
            <el-form-item label="视力：" prop="xdct_tjxx_sl">
              <el-input v-model="checkForm.xdct_tjxx_sl"></el-input>
            </el-form-item>
          </div>
          <div class="flex-1">
            <el-form-item label="听力：" prop="xdct_tjxx_tl">
              <el-input v-model="checkForm.xdct_tjxx_tl"></el-input>
            </el-form-item>
          </div>
        </div>
        <el-form-item label="是否发病：" prop="xdct_sffb">
          <el-radio v-model="checkForm.xdct_sffb" :label="2">否</el-radio>
          <el-radio v-model="checkForm.xdct_sffb" :label="1">是</el-radio>
          <el-input
            v-if="checkForm.xdct_sffb == 1"
            v-model="checkForm.xdct_qtzz"
            style="width: 270px"
            placeholder="请输入其他症状"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="checkDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkFormSubmit('checkForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tj_stu_list, tj_stu_edit, tj_stu_sign } from "./api";
import { useUserStore } from "@/stores/user";
const { token, real_name } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      grade_id: "",
      class_id: "",
      searchForm: {
        xsxm: "",
        xsxh: "",
        tjjg: "",
        tjlx: "",
        tjrq: ["", ""],
      },
      tableData: [],
      limit: 10,
      total: 0,
      loading: false,
      cur_page: 1,
      typeList: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "晨检",
          value: 1,
        },
        {
          label: "午检",
          value: 2,
        },
      ],
      jgList: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "良好",
          value: 1,
        },
        {
          label: "及格",
          value: 2,
        },
        {
          label: "异常",
          value: 3,
        },
      ],
      checkDialogVisible: false,
      checkForm: {
        xdct_id: "",
        xdct_xslx: 1,
        grade_id: "",
        grade_name: "",
        class_id: "",
        class_name: "",
        xdct_xsxm: "",
        xdct_xsxh: "",
        xdct_sffb: "",
        xdct_tjxx_tl: "",
        xdct_tjxx_sg: "",
        xdct_tjxx_tz: "",
        xdct_tjxx_xw: "",
        xdct_tjxx_fhl: "",
        xdct_tjxx_xy: "",
        xdct_tjxx_mb: "",
        xdct_tjxx_sl: "",
        xdct_qtzz: "",
      },
      rules: {
        xdct_sffb: [
          { required: true, message: "请选择状态", trigger: "change" },
        ],
        xdct_tjxx_tl: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_sg: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_tz: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_xw: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_fhl: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_xy: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_mb: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
        xdct_tjxx_sl: [
          { required: true, message: "请输入文字", trigger: "blur" },
        ],
      },
      multipleSelection: [],
      multipleSelection1: [],
    };
  },
  methods: {
    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.searchForm.tjrq = [
        date.getFullYear() + "-" + month + "-" + strDate,
        date.getFullYear() + "-" + month + "-" + strDate,
      ];
    },
    getTime() {
      if (!this.searchForm.tjrq) {
        this.searchForm.tjrq = ["", ""];
      }
      this.cur_page = 1;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        grade_id: this.grade_id,
        class_id: this.class_id,
        xdct_xsxm: this.searchForm.xsxm,
        xdct_xsxh: this.searchForm.xsxh,
        xdct_tjjg: this.searchForm.tjjg,
        xdct_xslx: this.searchForm.tjlx,
        page: this.cur_page,
        limit: this.limit,
      };
      if (this.searchForm.tjrq.length > 1) {
        data.xdct_tjrq =
          this.searchForm.tjrq[0] + " - " + this.searchForm.tjrq[1];
      } else {
        data.xdct_tjrq = "";
      }
      tj_stu_list(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          this.tableData = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
          console.log(this.tableData, 123);
        }
      });
    },

    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    checkPop(item) {
      this.checkDialogVisible = true;
      this.checkForm = {
        xdct_id: item.xdct_id,
        xdct_xslx: Number(item.xdct_xslx_option_k),
        grade_id: item.grade_id,
        grade_name: item.grade_name,
        class_id: item.class_id,
        class_name: item.class_name,
        xdct_xsxm: item.xdct_xsxm,
        xdct_xsxh: item.xdct_xsxh,
        xdct_sffb: Number(item.xdct_sffb_option_k),
        xdct_tjxx_tl: item.xdct_tjxx_tl,
        xdct_tjxx_sg: item.xdct_tjxx_sg,
        xdct_tjxx_tz: item.xdct_tjxx_tz,
        xdct_tjxx_xw: item.xdct_tjxx_xw,
        xdct_tjxx_fhl: item.xdct_tjxx_fhl,
        xdct_tjxx_xy: item.xdct_tjxx_xy,
        xdct_tjxx_mb: item.xdct_tjxx_mb,
        xdct_tjxx_sl: item.xdct_tjxx_sl,
        xdct_qtzz: item.xdct_qtzz,
      };
    },
    checkFormSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let data = {
            xdct_id: this.checkForm.xdct_id,
            xdct_xslx: this.checkForm.xdct_xslx,
            grade_id: this.checkForm.grade_id,
            grade_name: this.checkForm.grade_name,
            class_id: this.checkForm.class_id,
            class_name: this.checkForm.class_name,
            xdct_xsxm: this.checkForm.xdct_xsxm,
            xdct_xsxh: this.checkForm.xdct_xsxh,
            xdct_sffb: this.checkForm.xdct_sffb,
            xdct_tjxx_tl: this.checkForm.xdct_tjxx_tl,
            xdct_tjxx_sg: this.checkForm.xdct_tjxx_sg,
            xdct_tjxx_tz: this.checkForm.xdct_tjxx_tz,
            xdct_tjxx_xw: this.checkForm.xdct_tjxx_xw,
            xdct_tjxx_fhl: this.checkForm.xdct_tjxx_fhl,
            xdct_tjxx_xy: this.checkForm.xdct_tjxx_xy,
            xdct_tjxx_mb: this.checkForm.xdct_tjxx_mb,
            xdct_tjxx_sl: this.checkForm.xdct_tjxx_sl,
            xdct_qtzz: this.checkForm.xdct_qtzz,
          };
          tj_stu_edit(data).then((res) => {
            this.loading = false;
            if (res.code == 1) {
              this.$message({
                message: "体检登记成功！",
                type: "success",
              });
              this.checkDialogVisible = false;
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;

      this.multipleSelection1 = [];

      for (let i in val) {
        this.multipleSelection1.push(val[i].xdct_id);
        console.log(this.multipleSelection1, 123);
      }
    },
    exportData() {
      let time = "";
      if (this.searchForm.tjrq.length > 1) {
        time = this.searchForm.tjrq[0] + " - " + this.searchForm.tjrq[1];
      } else {
        time = "";
      }
      let link =
        window.globalVariables.api +
        "/xddy/dygl_cjxt_tjdj/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdct_xsxm=" +
        this.searchForm.xsxm +
        "&xdct_id=" +
        this.multipleSelection1.join(",") +
        "&xdct_xslx=" +
        this.searchForm.tjlx +
        "&xdct_tjrq=" +
        time;
      window.open(link, "_self");
    },
    checkSign(item) {
      this.loading = true;
      let data = {
        xdct_id: item.xdct_id,
        xdct_sfqd: "1",
      };
      tj_stu_sign(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          this.$message({
            message: "签到成功！",
            type: "success",
          });
          this.getListData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    batchSign() {
      this.loading = true;
      let sign_arr = [];
      for (let i in this.multipleSelection) {
        sign_arr.push(this.multipleSelection[i].xdct_id);
      }
      let data = {
        xdct_id: sign_arr,
        xdct_sfqd: "1",
      };
      tj_stu_sign(data).then((res) => {
        this.loading = false;
        if (res.code == 1) {
          this.$message({
            message: "签到成功！",
            type: "success",
          });
          this.getListData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
  },
  mounted() {
    this.getTimeFormat();
    if (this.$route.params.grade_id != "") {
      this.grade_id = this.$route.params.grade_id;
      this.class_id = this.$route.params.class_id;
      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>