<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" @click="addData">添加</el-button>
        <el-upload
          class="upload-demo"
          :action="import_type_url"
          :show-file-list="false"
          :data="import_type_data"
          :on-success="import_success"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">导入</el-button>
        </el-upload>
        <el-button type="primary" @click="exportModle">导入模板下载</el-button>
        <el-button type="primary" @click="exportAll">导出</el-button>
      </div>
      <el-form :inline="true">
        <el-form-item label="">
          <el-select
            v-model="firstForm.grade"
            placeholder="请选择年级"
            clearable
            @change="handleGradeChange"
          >
            <el-option
              v-for="item in gradeList"
              :key="item.grade_id"
              :label="item.grade_name"
              :value="item.grade_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="firstForm.class"
            placeholder="请选择班级"
            clearable
            @change="getListData"
          >
            <el-option
              v-for="item in classList"
              :key="item.class_id"
              :label="item.class_name"
              :value="item.class_num"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr10" prop="xtj_xsxm" label="">
          <el-input
            placeholder="请输入学生姓名"
            v-model="xtj_xsxm"
            clearable
            @clear="getListData"
          ></el-input
        ></el-form-item>
        <el-form-item class="mr10" prop="xtj_xsxh" label="">
          <el-input
            placeholder="请输入学籍号"
            v-model="xtj_xsxh"
            clearable
            @clear="getListData"
          ></el-input
        ></el-form-item>
        <el-form-item class="mr10" prop="xtj_jlrq" label="">
          <el-date-picker
            style="width: 100%"
            v-model="xtj_jlrq"
            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>
    </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="class_name"
        width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="记录日期" prop="xtj_jlrq" width="150">
      </el-table-column>
      <el-table-column
        label="学生姓名"
        prop="xtj_xsxm"
        align=""
        width=""
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="学籍号"
        prop="xtj_xsxh"
        align=""
        width="250"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="基础心率"
        prop="xtj_jcxl"
        align="center"
        width="120"
      >
      </el-table-column>
      <el-table-column label="BMI" prop="xtj_bmi" align="center" width="">
      </el-table-column>
      <el-table-column label="体脂率" prop="xtj_tzl" align="center" width="">
      </el-table-column>
      <el-table-column label="肺活量" prop="xtj_fhl" align="center" width="">
      </el-table-column>
      <el-table-column label="操作" width="auto" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(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>
    <el-dialog title="添加" :visible.sync="dialogFormVisible" width="680px">
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        label-width="120px"
        :rules="rules"
        inline
      >
        <el-form-item label="学生姓名" prop="xtj_xsxm">
          <el-input v-model="dialogForm.xtj_xsxm"></el-input>
        </el-form-item>

        <el-form-item label="学籍号" prop="xtj_xsxh">
          <el-input v-model="dialogForm.xtj_xsxh"></el-input>
        </el-form-item>

        <el-form-item label="年级" prop="grade_id">
          <el-select
            v-model="dialogForm.grade_id"
            placeholder="请选择年级"
            clearable
            filterable
            @change="handleGradeChange"
            style="width: 100%"
          >
            <el-option
              v-for="item in gradeList"
              :key="item.grade_id"
              :label="item.grade_name"
              :value="item.grade_id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班级" prop="class_id">
          <el-select
            v-model="dialogForm.class_id"
            placeholder="请选择班级"
            clearable
            filterable
            @change="handleClassChange"
            style="width: 100%"
          >
            <el-option
              v-for="item in classList"
              :key="item.class_id"
              :label="item.class_name"
              :value="item.class_num"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学期" prop="team_id">
          <el-select
            v-model="dialogForm.team_id"
            placeholder="请选择学期"
            clearable
            @change="handleTeamChange"
            style="width: 100%"
          >
            <el-option
              v-for="item in teamList"
              :key="item.team_id"
              :label="item.team_name"
              :value="item.team_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录日期" prop="xtj_jlrq">
          <el-date-picker
            v-model="dialogForm.xtj_jlrq"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="基础心率" prop="xtj_jcxl">
          <el-input v-model="dialogForm.xtj_jcxl"></el-input>
        </el-form-item>
        <el-form-item label="BMI" prop="xtj_bmi">
          <el-input v-model="dialogForm.xtj_bmi"></el-input>
        </el-form-item>
        <el-form-item label="体脂率" prop="xtj_tzl">
          <el-input v-model="dialogForm.xtj_tzl"></el-input>
        </el-form-item>
        <el-form-item label="肺活量" prop="xtj_fhl">
          <el-input v-model="dialogForm.xtj_fhl"></el-input>
        </el-form-item>
        <el-form-item label="视力左" prop="xtj_slz">
          <el-input v-model="dialogForm.xtj_slz"></el-input>
        </el-form-item>
        <el-form-item label="视力右" prop="xtj_sly">
          <el-input v-model="dialogForm.xtj_sly"></el-input>
        </el-form-item>
        <el-form-item label="视力" prop="xtj_sl">
          <el-input v-model="dialogForm.xtj_sl"></el-input>
        </el-form-item>
        <el-form-item label="往返跑" prop="xtj_wfp">
          <el-input v-model="dialogForm.xtj_wfp"></el-input>
        </el-form-item>
        <el-form-item label="耐力跑" prop="xtj_nlp">
          <el-input v-model="dialogForm.xtj_nlp"></el-input>
        </el-form-item>
        <el-form-item label="50米跑" prop="xtj_50mp">
          <el-input v-model="dialogForm.xtj_50mp"></el-input>
        </el-form-item>
        <el-form-item label="一分钟仰卧起坐" prop="xtj_1fzywqz">
          <el-input v-model="dialogForm.xtj_1fzywqz"></el-input>
        </el-form-item>
        <el-form-item label="立定跳远" prop="xtj_ldty">
          <el-input v-model="dialogForm.xtj_ldty"></el-input>
        </el-form-item>
        <el-form-item label="一分钟跳绳" prop="xtj_1fzts">
          <el-input v-model="dialogForm.xtj_1fzts"></el-input>
        </el-form-item>
        <el-form-item label="引体向上" prop="xtj_ytxs">
          <el-input v-model="dialogForm.xtj_ytxs"></el-input>
        </el-form-item>
        <el-form-item label="坐位体前屈" prop="xtj_zwtqq">
          <el-input v-model="dialogForm.xtj_zwtqq"></el-input>
        </el-form-item>
        <el-form-item label="实心球" prop="xtj_sxq">
          <el-input v-model="dialogForm.xtj_sxq"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit('dialogForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { xslb_list, class_search, grade_search, xqgl_list } from "./api";
import { useUserStore } from "@/stores/user";
import request from "@/utils/request";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      grade_name: "",
      class_name: "",
      xtj_xsxm: "",
      xtj_xsxh: "",
      xtj_jlrq: "",
      grade_id: "",
      class_id: "",
      tableData: [],
      gradeList: [],
      teamList: [],
      classList: [],
      dialogForm: {
        xtj_xsxm: "",
        xtj_xsxh: "",
        grade_id: "",
        grade_name: "",
        team_id: "",
        team_name: "",
        class_id: "",
        class_name: "",
        xtj_jlrq: "",
        xtj_jcxl: "",
        xtj_bmi: "",
        xtj_tzl: "",
        xtj_fhl: "",
        xtj_slz: "",
        xtj_sly: "",
        xtj_sl: "",
        xtj_wfp: "",
        xtj_nlp: "",
        xtj_50mp: "",
        xtj_1fzywqz: "",
        xtj_ldty: "",
        xtj_1fzts: "",
        xtj_ytxs: "",
        xtj_zwtqq: "",
        xtj_sxq: "",
      },
      firstForm: {
        grade: "",
        class: "",
      },
      multipleSelection: [],
      loading: false,
      // 导入导出
      import_type_url:
        window.globalVariables.api +
        "/xddy/xstzjk_xslb/import?issubmit=1&token=" +
        token,
      import_type_data: {},
      dialogFormVisible: false,
      rules: {
        xtj_xsxm: [{ required: true, message: "请输入学生姓名", trigger: "blur" }],
        xtj_xsxh: [{ required: true, message: "请输入学籍号", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        grade_id: this.firstForm.grade,
        class_id: this.firstForm.class,
        xtj_xsxm: this.xtj_xsxm,
        xtj_xsxh: this.xtj_xsxh,
        xtj_jlrq: this.xtj_jlrq,
      };

      xslb_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);
        }
      });
    },
    handleGradeChange(e) {
      this.firstForm.class = "";
      this.dialogForm.class_id = "";
      this.gradeList.map((val) => {
        if (val.grade_id == this.dialogForm.grade_id) {
          this.dialogForm.grade_name = val.grade_name;
        }
      });

      let data = { limit: "999", grade_id: e };
      console.log(data, 111111111111111);
      class_search(data).then((res) => {
        this.classList = res.data.page_data;
      });
      if (this.dialogFormVisible != true) {
        this.getListData();
      }
    },
    teamListData() {
      let data = {
        page: 1,
        limit: 9999,
      };
      xqgl_list(data).then((res) => {
        if (res.code == 1) {
          this.teamList = res.data.page_data;
        }
      });
    },
    handleTeamChange() {
      this.teamList.map((val) => {
        if (val.team_id == this.dialogForm.team_id) {
          this.dialogForm.team_name = val.team_name;
        }
      });
    },

    handleClassChange() {
      this.classList.map((item) => {
        if (item.class_num == this.dialogForm.class_id) {
          this.dialogForm.class_name = item.class_name;
        }
      });
    },
    import_success(res) {
      if (res.code == 1) {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "success",
        });
        this.getListData();
      } else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "error",
        });
      }
    },

    addData() {
      (this.dialogForm = {
        xtj_xsxm: "",
        xtj_xsxh: "",
        grade_id: "",
        grade_name: "",
        team_id: "",
        team_name: "",
        class_id: "",
        class_name: "",
        xtj_jlrq: "",
        xtj_jcxl: "",
        xtj_bmi: "",
        xtj_tzl: "",
        xtj_fhl: "",
        xtj_slz: "",
        xtj_sly: "",
        xtj_sl: "",
        xtj_wfp: "",
        xtj_nlp: "",
        xtj_50mp: "",
        xtj_1fzywqz: "",
        xtj_ldty: "",
        xtj_1fzts: "",
        xtj_ytxs: "",
        xtj_zwtqq: "",
        xtj_sxq: "",
      }),
        (this.dialogFormVisible = true);
    },

    formSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = {
            xddy_xstzjk_xslb: this.dialogForm,
          };
          console.log(data, "data");
          let res = {};
          res = await request({ url: "/xddy/xstzjk_xslb/add", data });

          if (res.code == 1) {
            this.$message({
              message: "保存成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          }
        }
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    exportModle() {
      let link =
        window.globalVariables.api +
        "/xddy/xstzjk_xslb/index?api=xls&page=1&limit=1&token=" +
        token;
      window.open(link, "_blank");
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xtj_id);
      }
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/xstzjk_xslb/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xtj_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
    },
    detailData(item) {
      this.$router.push({
        name: "xstzjk_xslb_ckxq",
        params: { xtj_xsxh: item.xtj_xsxh },
      });
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
      this.xtj_xsxm = "";
      this.xtj_xsxh = "";
      this.xtj_jlrq = "";
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
    this.teamListData();
  },
};
</script>

<style scoped>
::v-deep .el-input {
  width: 160px;
}
::v-deep .el-form--inline .el-form-item__content {
  width: 160px;
}
</style>