<script>
import {
  addPrecipitationApi,
  deletePrecipitation,
  findAllPrecipitation,
  findPrecipitationById,
  updatePrecipitationApi
} from "@@/apis/tables/diceke/Precipitation.ts"

export default {
  data() {
    return {
      precipitationQuerry: null, // 电铲平台数据
      updateprecipitationform: false,
      precipitationform: null
    }
  },
  created() {
    // 页面渲染之前执行，一般调用methods定义的方法
    this.getList()
  },
  methods: {
    getList() {
      findAllPrecipitation().then((Response) => {
        console.log(Response)
        this.precipitationQuerry = Response.data.precipitation
      })
    },

    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-button type="primary" @click="handleAdd" icon="el-icon-plus">
        新增降水量
      </el-button>
    </div>
    <el-table :data="precipitationQuerry" border style="width: 100%" :span-method="spanMethod">
      <!-- 表格列定义 -->
      <el-table-column prop="amount" label="降水量" />
      <el-table-column prop="unit" label="计量单位" />
      <el-table-column prop="monthDate" label="月份" :formatter="(row) => formatMonth(row.monthDate)" />
      <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" text bg size="small" plain @click="handleEdit(scope.row.precipID)">
              修改
            </el-button>
            <el-button
              type="danger"
              text bg 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>
