<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="xdbs_jcxm"> </el-table-column>

      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="pfxData(scope.row.xdbs_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.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      append-to-body
      width="580px"
    >
      <el-form
        :inline="true"
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="120px"
      >
        <h4 class="mb20 ml30 font-size-14" style="color: #265cd4">
          每个班级总分为100分(卫生50分+常规50分)
        </h4>
        <el-form-item class="mr75" label="检查项目名称" prop="jcxm">
          <el-input
            placeholder="请输入检查项目名称"
            v-model="dialogForm.jcxm"
          ></el-input>
        </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 { szwspb_list, szwspb_add, szwspb_edit, szwspb_del } 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: {
        jcxm: "",
      },
      rules: {
        jcxm: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
      },
      multipleSelection: [],
    };
  },
  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,
        xdbs_jclx: "1",
      };

      szwspb_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 = {
        jcxm: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    editData(item) {
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdbs_id;
      this.dialogForm.jcxm = item.xdbs_jcxm;
      this.dialogForm.jclx = item.xdbs_jclx;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdbs_jcxm: this.dialogForm.jcxm,
            xdbs_jclx: "1",
          };
          if (!this.isEdit) {
            szwspb_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.xdbs_id = this.id;
            szwspb_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 = {
            xdbs_id: this.multipleSelection,
          };

          szwspb_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].xdbs_id);
      }
    },
    pfxData(id) {
      this.$router.push({ name: "bjzzpb_sz_wspb_wspfx", params: { id: id } });
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>
<style scoped></style>;
