<script>
import {
  addPlateAreaApi,
  deletePlateArea,
  findAllPlateArea,
  findPlateAreaById,
  updatePlateAreaApi,
} from "@@/apis/tables/diceke/plateArea.js";

export default {
  data() {
    return {
      plateAreaQuerry: null, // 电铲平台数据
      updatePlateAreaform: false,
      plateAreaform: null,
    };
  },
  created() {
    // 页面渲染之前执行，一般调用methods定义的方法
    this.getList();
  },
  methods: {
    getList() {
      findAllPlateArea().then((Response) => {
        console.log(Response);
        this.plateAreaQuerry = Response.data.plateArea;
      });
    },
    // 删除平盘
    removeDataById(id) {
      ElMessageBox.confirm("此操作将永久删除平盘记录，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePlateArea(id).then(() => {
            // 删除成功
            ElMessage({
              type: "success",
              message: "删除成功!",
            });
            // 重新加载列表
            this.getList();
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.isEdit = false; // 设置为新增操作
      this.plateAreaform = { plateRange: "" }; // 初始化为空数据
      this.updatePlateAreaform = true; // 显示弹窗
    },
    /** 修改平盘信息 */
    handleEdit(id) {
      this.isEdit = true;
      findPlateAreaById(id)
        .then((Response) => {
          this.plateAreaform = Response.data.dicekePlatearea;
          this.updatePlateAreaform = true;
        })
        .catch(() => {
          ElMessage.error("查询失败，请稍后重试");
        });
    },
    updatePlateArea() {
      if (this.isEdit) {
        updatePlateAreaApi(this.plateAreaform)
          .then(() => {
            ElMessage({
              type: "success",
              message: "修改成功!",
            });
            this.updatePlateAreaform = false;
            this.getList();
          })
          .catch(() => {
            ElMessage.error("修改失败，请稍后重试");
          });
      } else {
        addPlateAreaApi(this.plateAreaform)
          .then(() => {
            ElMessage({
              type: "success",
              message: "新增成功!",
            });
            this.updatePlateAreaform = false;
            this.getList();
          })
          .catch(() => {
            ElMessage.error("新增失败，请稍后重试");
          });
      }
    },
  },
};
</script>

<template>
  <div class="app-container">
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="handleAdd">新增平盘</el-button>
    </div>
    <el-table
      :data="plateAreaQuerry"
      border
      style="width: 100%"
      :span-method="spanMethod"
    >
      <!-- 表格列定义 -->
      <el-table-column prop="plateRange" label="平盘（#）" />
      <el-table-column fixed="right" label="操作" width="300" align="center">
        <template #default="scope">
          <div style="display: flex; justify-content: center; gap: 8px">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              @click="handleEdit(scope.row.plateID)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              size="small"
              icon="Delete"
              @click="removeDataById(scope.row.plateID)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="updatePlateAreaform" title="修改平盘信息" width="500">
      <el-form :model="plateAreaform">
        <el-form-item label="平盘名称" :label-width="formLabelWidth">
          <el-input v-model="plateAreaform.plateRange" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="updatePlateAreaform = false"> 取消 </el-button>
          <el-button type="primary" @click="updatePlateArea"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
