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

      <el-table-column label="操作">
        <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"
      >
        <h4 class="mb20 ml20 font-size-12 color-2">
          每个班级总分为100分(卫生50分+常规50分)
        </h4>
        <el-form-item class="mr75" label="评分项" prop="pfx">
          <el-input v-model="dialogForm.pfx"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="text-center">
        <el-button @click="formCancel">取 消</el-button>
        <el-button :disabled="isButton" type="primary" @click="formSubmit('dialogForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { wdpfx_list, wdpfx_add, wdpfx_edit, wdpfx_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: {
        pfx: "",
        xdww_id: "",
      },
      rules: {
        pfx: [{ required: true, message: "请输入评分项", trigger: "blur" }],
      },
      multipleSelection: [],
      isButton: false,
    };
  },
  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,
        xdww_id: this.xdww_id,
      };

      wdpfx_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.isButton = false;
      this.title = "添加";
      this.dialogForm = {
        pfx: "",
        xdww_id: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    editData(item) {
      this.isButton = false;
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdwwp_id;
      this.xdww_id = item.xdww_id;
      this.dialogForm.pfx = item.xdwwp_name;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            xdwwp_name: this.dialogForm.pfx,
            xdww_id: this.xdww_id,
          };
          if (!this.isEdit) {
            wdpfx_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "信息添加成功！",
                  type: "success",
                });
                this.isButton = true;
                this.dialogFormVisible = false;
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            data.xdwwp_id = this.id;
            data.xdww_id = this.xdww_id;
            wdpfx_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",
                });
              }
            });
          }
        }
        else {
          this.isButton = false
        }
      });
    },

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

          wdpfx_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].xdwwp_id);
      }
    },
  },
  mounted() {
    if (this.$route.params.id != "") {
      this.xdww_id = this.$route.params.id;
      this.getListData();
    }
  },
};
</script>
<style scoped></style>;