<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="6">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['metrics:detail:add']">新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="detailList" border>
      <el-table-column label="指标ID" align="center" prop="metricsId" width="80" />
      <el-table-column label="指标编码" align="left" prop="metricsCode" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['metrics:detail:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['metrics:detail:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改模板指标明细对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="detailRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="指标编码" prop="metricsCode">
          <el-input v-model="form.metricsCode" placeholder="请输入指标编码" />
        </el-form-item>
        <el-form-item label="告警规则" prop="alarmRule">
          <el-input v-model="form.alarmRule" placeholder="请输入告警规则" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" show-word-limit :maxlength="300" :autosize="{minRows: 4, maxRows: 6}"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Detail">
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/metrics/detail";

const { proxy } = getCurrentInstance();

const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 50,
  },
  rules: {
    alarmRule: [
      { required: true, message: "告警规则不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询模板指标明细列表 */
function getList() {
  loading.value = true;
  listDetail(queryParams.value).then(response => {
    detailList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    tplDetailId: null,
    tplId: null,
    metricsId: null,
    metricsCode: null,
    alarmRule: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("detailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  // queryParams.value.pageNum = 1;
  getList();
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加模板指标明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _tplDetailId = row.tplDetailId || ids.value
  getDetail(_tplDetailId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改模板指标明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["detailRef"].validate(valid => {
    if (valid) {
      if (form.value.tplDetailId != null) {
        updateDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _tplDetailIds = row.tplDetailId || ids.value;
  proxy.$modal.confirm('是否确认删除模板指标明细编号为"' + _tplDetailIds + '"的数据项？').then(function() {
    return delDetail(_tplDetailIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


getList();
</script>
