<template>
  <div class="app-container">
    <el-button class="add-button" @click="openDialog">添加制度</el-button>

    <el-row>
      <el-col :span="24" v-loading="loading">
        <div class="card" v-for="institution in institutions" :key="institution.id">
          <div class="title">
            <span style="cursor: pointer;" @click="toDetail(institution.id)">{{institution.title}}</span>
            <span class="button" @click="toDetail(institution.id)">查看详情</span>
            <span
              class="button"
              @click="deleteInstitution(institution.id, institution.title)"
              style="margin-right: 15px"
            >删除制度</span>
          </div>
          <div class="content" @click="toDetail(institution.id)">{{institution.content}}</div>
        </div>
      </el-col>
    </el-row>

    <!-- 页码区域 -->
    <el-pagination
      v-if="total > 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      style="float: right"
    ></el-pagination>

    <!-- 添加的dialog -->
    <el-dialog title="添加制度" :visible.sync="dialogVisible" width="600px">
      <el-form ref="form1" :model="institutionForm" :rules="rules" label-width="110px">
        <el-form-item label="制度标题" prop="title">
          <el-input v-model="institutionForm.title" placeholder="请输入制度标题" clearable></el-input>
        </el-form-item>
        <el-form-item label="制度内容" prop="content">
          <el-input v-model="institutionForm.content" placeholder="请输入制度内容" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="addInstitution">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      institutions: [],
      loading: false,
      institutionForm: {
        title: "",
        content: ""
      },
      dialogVisible: false,
      rules: {
        title: [{ required: true, message: "请输入制度标题", trigger: "blur" }],
        content: [
          { required: true, message: "请输入制度内容", trigger: "blur" }
        ]
      },
      //分页相关
      pageIndex: 1,
      pageSize: 20,
      total: 0
    };
  },
  methods: {
    //页码规格变更
    handleSizeChange(val) {
      this.pageSize = val;
      //重新获取
      this.getInstitution();
    },

    //页码页数变更
    handleCurrentChange(val) {
      this.pageIndex = val;
      //重新获取
      this.getInstitution();
    },

    //增加制度
    addInstitution() {
      this.$refs.form1.validate(valid => {
        if (valid) {
          this.loading = true;
          request({
            url: "/institution/insertInstitution",
            method: "post",
            params: {
              title: this.institutionForm.title,
              content: this.institutionForm.content
            }
          })
            .then(response => {
              this.$message({
                message: "添加制度成功",
                type: "success"
              });
              this.loading = false;
              this.dialogVisible = false;
              this.getInstitution();
            })
            .catch(error => {
              this.loading = false;
            });
        }
      });
    },

    //删除制度
    deleteInstitution(id, title) {
      this.$confirm("是否删除" + title, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          request({
            url: "/institution/deleteInstitution",
            method: "post",
            params: {
              id: id
            }
          })
            .then(response => {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.loading = false;
              this.getInstitution();
            })
            .catch(error => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },

    //获取制度
    getInstitution() {
      this.loading = true;
      request({
        url: "/institution/getInstitutionsByPage",
        method: "post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      })
        .then(response => {
          this.institutions = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //打开添加的dialog
    openDialog() {
      this.institutionForm.title = "";
      this.institutionForm.content = "";
      this.dialogVisible = true;
    },

    //到详情
    toDetail(id) {
      if (id != null) {
        this.$router.push("/institution/" + id);
      } else {
        this.$message({
          message: "发生错误",
          type: "error"
        });
      }
    }
  },
  mounted() {
    this.getInstitution();
  }
};
</script>

<style scoped>
.card {
  margin-bottom: 20px;
}
.card .title {
  margin-bottom: 10px;
  font-size: 18px;
  cursor: pointer;
}
.card .title .button {
  float: right;
  color: #409eff;
  font-size: 16px;
  cursor: pointer;
}
.card .content {
  font-size: 15px;
  color: #aaa;
  cursor: pointer;
}
.add-button {
  margin-bottom: 15px;
}
</style>
