<script>
import {
  updateFangpaishuiPumpoperationrecordApi,
  deleteFangpaishuiPumpoperationrecordApi,
  findPumpoperationrecordByDetailApi,
  addFangpaishuiPumpoperationrecordApi,
  findFangpaishuiPumpoperationrecordByMonthApi,
} from "@@/apis/tables/drainageWorkshop/pumpOperationLog.ts";
import { getSpecificDate } from "@@/utils/datetime";
export default {
  data() {
    return {
      date: "",
      tableList: [],
    };
  },
  created() {
    // 页面渲染之前执行，一般调用methods定义的方法
    this.getList();
  },
  methods: {
    //获取列表
    async getList() {
      const res = await findAllMiningglossApi();
      console.log(res);
    },
    //按月查询
    async getMouthLoss() {
      let data = getSpecificDate(this.date);
      const res = await findTotalMiningByMonthApi(data);
      console.log(res);
    },
    removeDataById(id) {
      ElMessageBox.confirm("此操作将永久删除降水量记录，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePrecipitation(id).then(() => {
            // 删除成功
            ElMessage({
              type: "success",
              message: "删除成功!",
            });
            // 重新加载列表
            this.getList();
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.isEdit = false; // 设置为新增操作
      this.precipitationform = { plateRange: "" }; // 初始化为空数据
      this.updateprecipitationform = true; // 显示弹窗
    },
    handleEdit(id) {
      this.isEdit = true;
      findPrecipitationById(id)
        .then((Response) => {
          this.precipitationform = Response.data.precipitationById;
          console.log(this.precipitationform);
          this.updateprecipitationform = true;
        })
        .catch(() => {
          ElMessage.error("查询失败，请稍后重试");
        });
    },
    formatMonth(dateTime) {
      if (!dateTime) return "-";
      return dateTime.slice(0, 7); // 直接截取 "yyyy-MM"
      // 或用 dayjs（需安装）：
      // return dayjs(dateTime).format('YYYY-MM');
    },
    updatePrecipitation() {
      if (this.isEdit) {
        updatePrecipitationApi(this.precipitationform)
          .then(() => {
            ElMessage({
              type: "success",
              message: "修改成功!",
            });
            this.updateprecipitationform = false;
            this.getList();
          })
          .catch(() => {
            ElMessage.error("修改失败，请稍后重试");
          });
      } else {
        addPrecipitationApi(this.precipitationform)
          .then(() => {
            ElMessage({
              type: "success",
              message: "新增成功!",
            });
            this.updateprecipitationform = false;
            this.getList();
          })
          .catch(() => {
            ElMessage.error("新增失败，请稍后重试");
          });
      }
    },
  },
};
</script>

<template>
  <div class="app-container">
    <div style="margin-bottom: 20px">
      <el-date-picker v-model="date" type="month" placeholder="选择年月">
      </el-date-picker>
      <el-button
        type="primary"
        size="mini"
        style="margin-left: 20px"
        @click="getMouthLoss"
        >查询</el-button
      >
    </div>
    <el-table
      :data="tableList"
      border
      style="width: 100%"
      :span-method="spanMethod"
      :header-align="center"
      :cell-style="rowStyle"
    >
      <!-- 表格列定义 -->
      <el-table-column align="center" prop="amount" label="平盘(m)" />
      <el-table-column align="center" prop="amount" label="铲号(#)" />
      <el-table-column align="center" prop="amount" label="出矿量(t)" />
      <el-table-column align="center" prop="amount" label="剥离量(t)" />
      <el-table-column align="center" prop="amount" label="采剥总量(t)" />
      <el-table-column align="center" prop="amount" label="计量单位" />
      <el-table-column align="center" prop="amount" label="月份" />
      <el-table-column align="center" fixed="right" label="操作" width="300">
        <template #default="scope">
          <div style="display: flex; justify-content: center; gap: 8px">
            <el-button
              type="primary"
              size="small"
              @click="handleEdit(scope.row.precipID)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="removeDataById(scope.row.precipID)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="updateprecipitationform"
      title="修改降水量信息"
      width="500"
    >
      <el-form :model="precipitationform">
        <el-form-item label="降水量" :label-width="formLabelWidth">
          <el-input v-model="precipitationform.amount" autocomplete="off" />
        </el-form-item>
        <el-form-item label="计量单位" :label-width="formLabelWidth">
          <el-input v-model="precipitationform.unit" autocomplete="off" />
        </el-form-item>
        <el-form-item label="月份" :label-width="formLabelWidth">
          <el-date-picker
            v-model="precipitationform.monthDate"
            type="datetime"
            placeholder="选择月份"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="updateprecipitationform = false"> 取消 </el-button>
          <el-button type="primary" @click="updatePrecipitation">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
