<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="xdbsp_pfx"> </el-table-column>
      <el-table-column label="类型" prop="xdbsp_lx"> </el-table-column>
      <el-table-column label="分值" prop="xdbsp_fz"> </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">
        <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-item class="mr75" label="类型" prop="xdbsp_lx">
          <el-radio v-model="dialogForm.xdbsp_lx" label="1">加分</el-radio>
          <el-radio v-model="dialogForm.xdbsp_lx" label="2">减分</el-radio>
        </el-form-item>

        <el-form-item class="mr75" label="分值" prop="fz">
          <el-input v-model="dialogForm.fz"></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_pfx_list,
  szwspb_pfx_add,
  szwspb_pfx_edit,
  szwspb_pfx_del,
} from "./api";
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      tableData: [],
      title: "",
      activeName: "first",
      id: "",
      xdbs_id: "",
      loading: false,
      isEdit: false,
      dialogFormVisible: false,
      dialogForm: {
        pfx: "",
        xdbsp_lx: "1",
        fz: "",
        xdbs_id: "",
      },

      rules: {
        pfx: [{ required: true, message: "请输入评分项", trigger: "blur" }],
        xdbsp_lx: [{ required: true, message: "请选择类型", trigger: "blur" }],
        fz: [{ 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_id: this.xdbs_id,
      };

      szwspb_pfx_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);
        // this.tableData.map((item) => {
        //   if (item.xdbsp_lx == "加分") {
        //     item.xdbsp_lx1 = "1";
        //   } else {
        //     item.xdbsp_lx1 = "2";
        //   }
        // })
      });
    },
    addData() {
      this.title = "添加";
      this.dialogForm = {
        pfx: "",
        xdbsp_lx: "",
        fz: "",
        xdbs_id: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    editData(item) {
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdbsp_id;
      this.xdbs_id = item.xdbs_id;
      this.dialogForm.pfx = item.xdbsp_pfx;
      // item为读取出的行数据，行数据中的这个类型是字符串“加、减分”，转为1和2即可
      this.dialogForm.xdbsp_lx = item.xdbsp_lx == "加分" ? "1" : "2";
      this.dialogForm.fz = item.xdbsp_fz;
      console.log(this.dialogForm.xdbsp_lx, 333);
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdbsp_pfx: this.dialogForm.pfx,
            xdbsp_lx: this.dialogForm.xdbsp_lx,
            xdbsp_fz: this.dialogForm.fz,
            xdbs_id: this.xdbs_id,
          };
          if (!this.isEdit) {
            szwspb_pfx_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.xdbsp_id = this.id;
            data.xdbs_id = this.xdbs_id;
            szwspb_pfx_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 = {
            xdbsp_id: this.multipleSelection,
          };

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