<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 prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          ></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="学期名称"
        show-overflow-tooltip
        prop="team_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="学期开始时间"
        align="center"
        prop="team_start_date"
        width="180"
      >
      </el-table-column>

      <el-table-column
        label="学期结束时间"
        align="center"
        width="180"
        prop="team_end_date"
      ></el-table-column>
      <el-table-column
        label="编号"
        align="center"
        prop="team_id"
        width="150"
      ></el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <el-button type="text" @click="delData(scope.row.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="xqmc">
          <el-input
            v-model="dialogForm.xqmc"
            placeholder="请输入学期名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="学期开始时间" prop="xqkssj">
          <el-date-picker
            v-model="dialogForm.xqkssj"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="学期结束时间" prop="xqjssj">
          <el-date-picker
            v-model="dialogForm.xqjssj"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="编号" prop="bh">
          <el-input v-model="dialogForm.bh" placeholder="请输入编号"></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 { xqgl_list, xqgl_add, xqgl_edit, xqgl_del } from "./api";
export default {
  name: "index",
  data() {
    return {
      keyword: "",
      tableData: [],
      team_id: "",
      team_name: "",
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,

      dialogForm: {
        xqmc: "",
        xqkssj: "",
        xqjssj: "",
        bh: "",
      },
      rules: {
        xqmc: [{ required: true, message: "请输入学期名称", trigger: "blur" }],
        xqkssj: [{ required: true, message: "请选择日期", trigger: "blur" }],
        xqjssj: [{ required: true, message: "请选择日期", trigger: "blur" }],
        bh: [{ required: true, message: "请输入编号", trigger: "blur" }],
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
    };
  },
  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      xqgl_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);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建";
      this.dialogForm = {
        xqmc: "",
        xqkssj: "",
        xqjssj: "",
        bh: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.id;
      this.dialogForm.xqmc = item.team_name;
      this.dialogForm.xqkssj = item.team_start_date;
      this.dialogForm.xqjssj = item.team_end_date;
      this.dialogForm.bh = item.team_id;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            team_name: this.dialogForm.xqmc,
            team_start_date: this.dialogForm.xqkssj,
            team_end_date: this.dialogForm.xqjssj,
            team_id: this.dialogForm.bh,
          };
          if (!this.isEdit) {
            xqgl_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.id = this.id;
            xqgl_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 = {
        id: id,
      };
      this.$confirm("确认删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          xqgl_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() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>