<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">添加</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item>
          <el-select
            v-model="firstForm.grade"
            placeholder="请选择年级"
            clearable
            filterable
            @change="getListData"
            style="width: 180px"
          >
            <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>
          <el-select
            v-model="firstForm.team"
            placeholder="请选择学期"
            clearable
            filterable
            @change="getListData"
            style="width: 180px"
          >
            <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 class="mr10" prop="rxnf" label="">
          <el-input
            placeholder="请输入入学年份"
            v-model="rxnf"
            clearable
            @clear="getListData"
            style="width: 180px"
          ></el-input
        ></el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
            style="width: 180px"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
      </el-form>
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        align="center"
        width="55"
      ></el-table-column>
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="80"
      ></el-table-column>
      <el-table-column
        label="班级名称"
        prop="class_name"
        width="200"
      ></el-table-column>
      <el-table-column
        label="班级简称"
        align=""
        prop="class_name_short"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="班主任"
        align=""
        prop="bzr_name"
        width=""
      ></el-table-column>
      <!-- <el-table-column
        label="班号"
        align=""
        width=""
        prop="class_num"
      ></el-table-column>
      <el-table-column
        label="所属年级编号"
        align=""
        prop="grade_id"
        width=""
      ></el-table-column> -->
      <el-table-column
        label="所属年级名称"
        prop="grade_name"
        width=""
      ></el-table-column>
      <el-table-column
        label="所属学期编号"
        prop="team_id"
        width=""
      ></el-table-column>
      <el-table-column
        label="所属学期名称"
        prop="team_name"
        width="250"
      ></el-table-column>
      <el-table-column label="入学年份" prop="rxnf" align="" width="">
        <!-- <template slot-scope="scope">
          <span v-if="scope.row.rxnf == '1'">2019</span>
          <span v-if="scope.row.rxnf == '2'">2020</span>
          <span v-if="scope.row.rxnf == '3'">2021</span>
          <span v-if="scope.row.rxnf == '4'">2022</span>
        </template> -->
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <el-button type="text" @click="delData(scope.row.class_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="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="年级" prop="ssnjbh">
          <el-select
            v-model="dialogForm.ssnjbh"
            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="bh">
          <el-select
            v-model="dialogForm.bh"
            placeholder="请选择班号"
            clearable
            @change="handleChange"
            style="width: 100%"
          >
            <el-option
              v-for="i in 50"
              :key="i"
              :label="i"
              :value="i"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班级名称" prop="bjmc">
          <el-input
            disabled
            placeholder="请输入班级名称"
            v-model="dialogForm.bjmc"
          ></el-input>
        </el-form-item>
        <el-form-item label="班级简称" prop="bjjc">
          <el-input
            v-model="dialogForm.bjjc"
            placeholder="请输入班级简称"
          ></el-input>
        </el-form-item>

        <el-form-item label="学期" prop="ssxqbh">
          <el-select
            v-model="dialogForm.ssxqbh"
            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="rxnf">
          <el-input
            v-model="dialogForm.rxnf"
            placeholder="请输入入学年份"
          ></el-input>
        </el-form-item>
        <el-form-item label="班主任" prop="bzr_id">
          <el-select
            v-model="dialogForm.bzr_id"
            placeholder="请选择班主任"
            clearable
            filterable
            @change="handleTeacherChange"
            style="width: 100%"
          >
            <el-option
              v-for="item in teacherList"
              :key="item.user_id"
              :label="item.user_realname"
              :value="item.user_id"
            ></el-option>
          </el-select>
        </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 { useUserStore } from "@/stores/user";
const { token, user_org } = useUserStore();
import {
  bjgl_list,
  bjgl_add,
  bjgl_edit,
  bjgl_del,
  njgl_list,
  xqgl_list,
  bzr_list,
} from "./api";
export default {
  name: "index",
  data() {
    return {
      keyword: "",
      tableData: [],
      gradeList: [],
      grade_name: "",
      grade_id: "",
      team_id: "",
      team_name: "",
      teamList: [],
      rxnf: "",
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      firstForm: {
        grade: "",
        team: "",
      },
      dialogForm: {
        bjmc: "",
        bjjc: "",
        bh: "",
        ssnjbh: "",
        ssnjmc: "",
        ssxqbh: "",
        ssxqmc: "",
        rxnf: "",
        bzr_id: "",
        bzr_name: "",
      },
      rules: {
        bjmc: [{ required: true, message: "请输入班级名称", trigger: "blur" }],
        bh: [{ required: true, message: "请输入班号", trigger: "blur" }],
        ssnjbh: [{ required: true, message: "请选择年级", trigger: "blur" }],
        ssxqbh: [{ required: true, message: "请选择学期", trigger: "blur" }],
        rxnf: [{ required: true, message: "请输入入学年份", trigger: "blur" }],
        bzr_id: [{ required: true, message: "请选择班主任", trigger: "blur" }],
      },

      title: "",
      isEdit: false,
      id: "",
      loading: false,
      teacherList: [],
      dept_id: "",
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        grade_id: this.firstForm.grade,
        team_id: this.firstForm.team,
        rxnf: this.rxnf,
      };

      bjgl_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,
      };
      njgl_list(data).then((res) => {
        if (res.code == 1) {
          this.gradeList = res.data.page_data;
        }
      });
    },

    handleGradeChange() {
      this.gradeList.map((val) => {
        if (val.grade_id == this.dialogForm.ssnjbh) {
          this.dialogForm.ssnjmc = val.grade_name;
        }
      });
      if (this.dialogForm.ssnjmc && this.dialogForm.bh) {
        this.dialogForm.bjmc =
          this.dialogForm.ssnjmc + "(" + this.dialogForm.bh + ")班";
      }
    },
    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.ssxqbh) {
          this.dialogForm.ssxqmc = val.team_name;
        }
      });
    },
    teacherListData() {
      let data = {
        page: 1,
        limit: 9999,
        dept_id: this.dept_id,
        all: "1",
        org_type: "2",
      };
      bzr_list(data).then((res) => {
        if (res.code == 1) {
          this.teacherList = res.data.page_data;
        }
      });
    },
    handleTeacherChange() {
      this.teacherList.map((val) => {
        if (val.user_id == this.dialogForm.bzr_id) {
          this.dialogForm.bzr_name = val.user_realname;
        }
      });
    },
    handleChange() {
      if (this.dialogForm.ssnjmc && this.dialogForm.bh) {
        this.dialogForm.bjmc =
          this.dialogForm.ssnjmc + "(" + this.dialogForm.bh + ")班";
      }
    },

    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建";
      this.dialogForm = {
        bjmc: "",
        bjjc: "",
        bh: "",
        ssnjbh: "",
        ssnjmc: "",
        ssxqbh: "",
        ssxqmc: "",
        rxnf: "",
        bzr_id: "",
        bzr_name: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.class_id;
      this.dialogForm.bjmc = item.class_name;
      this.dialogForm.bjjc = item.class_name_short;
      this.dialogForm.bh = item.class_num;
      this.dialogForm.ssnjbh = item.grade_id;
      this.dialogForm.ssnjmc = item.grade_name;
      this.dialogForm.ssxqbh = item.team_id;
      this.dialogForm.ssxqmc = item.team_name;
      this.dialogForm.rxnf = item.rxnf;
      this.dialogForm.bzr_name = item.bzr_name;
      this.dialogForm.bzr_id = item.bzr_id;
    },
    formSubmit() {
      let data = {
        class_name: this.dialogForm.bjmc,
        class_name_short: this.dialogForm.bjjc,
        class_num: this.dialogForm.bh,
        grade_id: this.dialogForm.ssnjbh,
        grade_name: this.dialogForm.ssnjmc,
        team_id: this.dialogForm.ssxqbh,
        team_name: this.dialogForm.ssxqmc,
        rxnf: this.dialogForm.rxnf,
        bzr_name: this.dialogForm.bzr_name,
        bzr_id: this.dialogForm.bzr_id,
      };
      console.log(data, 1111);

      if (!this.isEdit) {
        bjgl_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "信息添加成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.class_id = this.id;
        bjgl_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "信息编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        class_id: id,
      };
      this.$confirm("确认删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          bjgl_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "信息删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    if (user_org) {
      user_org.map((item) => {
        if (item.uo_type === "2") {
          this.dept_id = item.uo_id;
        }
      });
    }
    this.getListData();
    this.gradeListData();
    this.teamListData();
    this.teacherListData();
  },
};
</script>

<style scoped>
</style>