<template>
  <div>
    <div class="mt20">
      <el-button type="primary" @click="addData">添加</el-button>
      <el-button type="danger" @click="delData" :disabled="multipleSelection.length == 0">删除</el-button>
    </div>
    <el-table :data="tableData" tooltip-effect="dark" v-loading="loading" ref="multipleTable" @selection-change="handleSelectionChange" class="mt20" style="width: 100%">
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="值周学生" prop="xdbz_zrxs">

      </el-table-column>
      <el-table-column label="时间" prop="xdbz_zrrq"> </el-table-column>
      <el-table-column label="年级" prop="grade_name">

      </el-table-column>
      <el-table-column label="班级" prop="class_name">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope" >
          <el-button type="text" @click="editData(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="title" :visible.sync="dialogFormVisible" append-to-body width="700px">
      <el-form :inline="true" :model="dialogForm" ref="dialogForm" :rules="rules" label-width="100px">
        <el-form-item label="值日学生" prop="zrxs">
          <!-- <el-select v-model="dialogForm.zrxs" style="width: 100%" placeholder="请选择值日学生">
            <el-option v-for="item in zrxsList" :key="item.xdx_id" :label="item.ddrw_name" :value="{ value: item.ddrw_id, label: item.xdbz_zrxs }"></el-option>
          </el-select> -->
          <el-input v-model="dialogForm.zrxs"></el-input>
        </el-form-item>

        <el-form-item class="mr75" label="负责年级" prop="grade_name">
          <el-select v-model="dialogForm.grade_name" @change="handleGradeChange">
            <el-option v-for="item in gradeList" :key="item.grade_id" :label="item.class_name" :value="item.grade_name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr75" label="负责班级" prop="class_name">
          <el-select v-model="dialogForm.class_name" @change="handleClassChange">
            <el-option v-for="item in classList" :key="item.class_id" :label="item.class_name" :value="item.class_name">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item class="mr75" label="时间" prop="sj">
          <el-date-picker v-model="dialogForm.sj" placeholder="选择日期" type="date" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="text-center">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  zrxs_list, zrxs_add, zrxs_edit, zrxs_del, grade_search,
  class_search,
} from "./api";
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      tableData: [],
      title: "",
      id: "",
      loading: false,
      isEdit: false,
      dialogFormVisible: false,
      dialogForm: {
        zrxs: "",
        grade_name: "",
        class_name: "",
        sj: "",
      },
      rules: {
        zrxs: [{ required: true, message: "请选择学生", trigger: "blur" }],
        grade_name: [{ required: true, message: "请选择年级", trigger: "blur" }],
        class_name: [{ required: true, message: "请选择班级", trigger: "blur" }],
        sj: [{ required: true, message: "请选择时间", trigger: "blur" }],
      },
      multipleSelection: [],

      gradeList: [],
      classList: [],
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },

    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };
      zrxs_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);
      });
    },

    addData() {
      this.title = "添加";
      this.dialogForm = {
        zrxs: "",
        grade_name: "",
        class_name: "",
        sj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    editData(item) {
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdbz_id;

      this.dialogForm.zrxs = item.xdbz_zrxs;
      this.dialogForm.sj = item.xdbz_zrrq;
      this.dialogForm.grade_name = item.grade_name;
      this.dialogForm.class_name = item.class_name;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {

            xdbz_zrxs: this.dialogForm.zrxs,
            xdbz_zrrq: this.dialogForm.sj,
            grade_name: this.dialogForm.grade_name,
            class_name: this.dialogForm.class_name,
          };
          if (!this.isEdit) {
            zrxs_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.xdbz_id = this.id;
            zrxs_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() {
      this.$confirm("确认删除这些信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdbz_id: this.multipleSelection,
          };

          zrxs_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "信息删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdbz_id);
      }
    },
    gradeListData() {
      this.loading = true;
      let data = {
      };
      grade_search(data).then((res) => {
        this.loading = false;
        this.gradeList = res.data.page_data;
        console.log(this.ryData, 1344);
      });
    },
    handleGradeChange() {
      this.gradeList.map((item) => {
        if (item.grade_id == this.dialogForm.grade_id) {
          this.dialogForm.grade_name = item.grade_name;
        }
      })
    },
    classListData() {
      this.loading = true;
      let data = {
      };
      class_search(data).then((res) => {
        this.loading = false;
        this.classList = res.data.page_data;
        console.log(this.ryData, 1344);
      });
    },
    handleClassChange() {
      this.classList.map((item) => {
        if (item.class_id == this.dialogForm.class_id) {
          this.dialogForm.class_name = item.class_name;
        }
      })
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
    this.classListData();
  },
};
</script>
<style scoped></style>;
