<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_zzjs">

      </el-table-column>
      <el-table-column label="时间" prop="xdbz_sj"> </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 class="mr75" label="值周老师" prop="zzls">
          <!-- <el-select v-model="dialogForm.zzls">
            <el-option v-for="item in teacher_List" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select> -->
          <el-input v-model="dialogForm.zzls"></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="xxlx">
          <el-select v-model="dialogForm.zzlx">
            <el-option v-for="item in type_List" :key="item.value" :label="item.label" :value="item.value">
            </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 {
  zzls_list, zzls_add, zzls_edit, zzls_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: {
        zzls: "",
        grade_name: "",
        class_name: "",
        zzlx: "",
        sj: "",
      },
      rules: {
        zzls: [{ required: true, message: "请选择老师", trigger: "blur" }],
        grade_name: [{ required: true, message: "请选择年级", trigger: "blur" }],
        class_name: [{ required: true, message: "请选择班级", trigger: "blur" }],
        zzlx: [{ required: true, message: "请选择值周类型", trigger: "blur" }],
        sj: [{ required: true, message: "请选择时间", trigger: "blur" }],
      },
      multipleSelection: [],
      teacher_List: [
        {
          value: "1",
          label: "张老师",
        },
        {
          value: "2",
          label: "李老师",
        },
        {
          value: "3",
          label: "王老师",
        },
        {
          value: "4",
          label: "周老师",
        },
      ],
   
      gradeList: [],
      classList: [],
      type_List: [
        {
          value: "1",
          label: "卫生",
        },
        {
          value: "2",
          label: "常规",
        },
      ],
    };
  },
  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,
      };

      zzls_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() {
      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;
        }
      })
    },
    formCancel() {
      this.dialogFormVisible = false;
    },

    //值周老师
    addData() {
      this.title = "添加";
      this.dialogForm = {
        zzls: "",
        grade_name: "",
        class_name: "",
        zzlx: "",
        sj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

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

      this.id = item.xdbz_id;
      this.dialogForm.zzls = item.xdbz_zzjs;
      this.dialogForm.zzlx = item.xdbz_zzlx;
      this.dialogForm.grade_name = item.grade_name;
      this.dialogForm.class_name = item.class_name;
      this.dialogForm.sj = item.xdbz_sj;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdbz_zzjs: this.dialogForm.zzls,
            xdbz_zzlx: this.dialogForm.zzlx,
            grade_name: this.dialogForm.grade_name,
            class_name: this.dialogForm.class_name,
            xdbz_sj: this.dialogForm.sj,
          };
          if (!this.isEdit) {
            zzls_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;
            zzls_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",
                });
              }
            });
          }
        }
      });
    },

    delData() {
      this.$confirm("确认删除这些信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdbz_id: this.multipleSelection,
          };

          zzls_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);
      }
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
    this.classListData();
  },
};
</script>
<style scoped></style>;
