<template>
  <div>
    <!-- 项目评价配置 -->
    <div class="headerbox">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200">
        <el-row>
          <el-col :span="5">
            <el-form-item label="评价要素" prop="pjys" style="width: 100%;">
              <el-input v-model="queryParams.pjys" placeholder="请输入评价要素" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格内容区 -->
    <div class="tablebox">
      <!-- 标签行 -->
      <div class="tablehead">
        <div class="headbtn">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加规则</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{ (scope.$index + 1) + (queryParams.current - 1) * queryParams.size }}
          </template>
        </el-table-column>
        <el-table-column label="评价要素" align="center" prop="pjys" />
        <el-table-column label="评价规则" align="center" prop="pfgz" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" style="color: red;" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
        @pagination="getList" />
    </div>

    <!-- 添加规则的弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评价要素" prop="pjys">
          <el-input v-model="form.pjys" placeholder="请输入评价要素" />
        </el-form-item>
        <el-form-item label="评分规则" prop="pfgz">
          <el-input v-model="form.pfgz" placeholder="请输入评分规则" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectEvaluationPage, addProjectEvaluation, updateProjectEvaluation, deleteProjectEvaluation } from '@/api/ManageApi/index';

export default {
  data() {
    return {
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        pjys: '' // 修改为 pjys
      },
      // 表格数据
      postList: [],
      // 总条数
      total: 0, // 确保 total 有一个默认值
      // 是否显示搜索条件
      showSearch: true,
      // 是否显示加载中
      loading: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: undefined,
        pjys: undefined,
        pfgz: undefined
      },
      // 表单校验
      rules: {
        pjys: [
          { required: true, message: '评价要素不能为空', trigger: 'blur' }
        ],
        pfgz: [
          { required: true, message: '评分规则不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取项目评价配置列表
    getList() {
      this.loading = true;
      getProjectEvaluationPage(this.queryParams).then(response => {
        if (response && response.data && response.data.records !== undefined && response.data.total !== undefined) {
          this.postList = response.data.records;
          this.total = response.data.total;
        } else {
          this.postList = [];
          this.total = 0;
        }
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      });
    },
    // 查询操作
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    // 重置操作
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id); // 使用 id 字段
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 显示添加规则弹出层
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加规则';
    },
    // 显示编辑规则弹出层
    handleUpdate(row) {
      this.reset();
      const postId = row.id || this.ids; // 使用 id 字段
      this.form = { ...row };
      this.open = true;
      this.title = '编辑规则';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        pjys: undefined,
        pfgz: undefined
      };
      this.resetForm('form');
    },
    // 提交按钮
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateProjectEvaluation(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addProjectEvaluation(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除按钮操作
    handleDelete(row) {
      const postIds = row.id || this.ids;
      const pjys = row.pjys; // 获取 pjys 字段的内容
      this.$modal.confirm(`是否确认删除"${pjys}"的数据项？`).then(() => {
        return deleteProjectEvaluation(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => { });
    }
  }
};
</script>

<style scoped>
.headerbox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablebox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablehead {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: .5rem
}

.headbtn {
  display: flex;
}

.tablebtntwo {
  margin-top: 1rem;
  margin-bottom: 1rem;
}
</style>

<style scoped>
.headerbox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablebox {
  background-color: #fff;
  border-radius: .5rem;
  padding: 1rem;
  margin: .5rem;
  border: 1px solid #eee;
}

.tablehead {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: .5rem
}

.headbtn {
  display: flex;
}

.tablebtntwo {
  margin-top: 1rem;
  margin-bottom: 1rem;
}
</style>