<template>
  <div class="my-warn-ticket">
    <div class="breadcrumb">
      <bg-breadcrumb />
    </div>
    <div class="content">
      <bg-filter-group @search="changeSearch" v-model="filter.keyword" placeholder="请输入预警点/分类/指标">
        <template v-slot:filter_group>
          <div class="left-filter filter_list">
            <div class="filter_item">
              <span class="filter_title">风险等级</span>
              <el-select v-model="filter.risk_level" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(value, key, index) in riskLevels"
                  :key="'riskLevels' + index"
                  :label="value"
                  :value="key">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">状态</span>
              <el-select v-model="filter.status" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(value, key, index) in statusOptions"
                  :key="'stateOptions' + index"
                  :label="value"
                  :value="key">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">预警时段</span>
              <el-date-picker
                v-model="filter.time"
                style="width: 400px"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间">
              </el-date-picker>
            </div>
          </div>
          <div class="right-action apaas_button">
            <el-button type="primary" @click="filterAction"> 查询 </el-button>
            <el-button type="default" @click="filterClear"> 重置 </el-button>
          </div>
        </template>
      </bg-filter-group>
      <div class="table_container">
        <div class="table">
          <bg-table ref="listtable" :headers="headers" :rows="rows" height="100%" :isIndex="true" :stripe="true">
            <template v-slot:alert_point="{ row }">
              <span class="can_click_text" @click="goDetail(row)">
                {{ row.alert_point }}
              </span>
            </template>
            <template v-slot:risk_level="{ row }">
              {{ riskLevels[row.risk_level] }}
            </template>
            <template v-slot:current_value="{ row }">
              {{ row.current_value }}{{ ruleTypeOptions[row.alert_rule_type]?.unit || "" }}
            </template>
            <template v-slot:warn_threshold="{ row }">
              {{ row.alert_condition.thresholds_min }}{{ ruleTypeOptions[row.alert_rule_type]?.unit || "" }}
              -
              {{ row.alert_condition.thresholds_max }}{{ ruleTypeOptions[row.alert_rule_type]?.unit || "" }}
            </template>
            <template v-slot:alert_time="{ row }">
              {{ row.alert_time ? row.alert_time.split("+")[0].replace("T", " ").replace("Z", " ") : "-" }}
            </template>
            <template v-slot:last_push_time="{ row }">
              {{ row.last_push_time ? row.last_push_time.split("+")[0].replace("T", " ").replace("Z", " ") : "-" }}
            </template>
            <template #status="{ row }">
              <span :class="`circle status-${row.status}`"></span>
              {{ statusOptions[row.status] }}
            </template>
            <template #operation="{ row }">
              <el-button type="primary" :disabled="row.is_disposed == 1" link size="small" @click="operation(row)">
                处置反馈
              </el-button>
            </template>
          </bg-table>
          <div class="pagination_box">
            <bg-pagination
              :page="filter.page"
              :size="filter.page_size"
              :total="tableTotal"
              @change-page="changePage"
              @change-size="changeSize">
            </bg-pagination>
          </div>
        </div>
      </div>
    </div>
    <FeedBack v-model:visible="visible" :active_row="active_row" :isWarn="true" />
  </div>
</template>

<script setup>
import { reactive, ref, onBeforeMount, toRefs, computed, watch, nextTick, watchEffect } from "vue";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import FeedBack from "../modules/feedback.vue";
import { useRouter } from "vue-router";
import { GetRuleTypeOptions } from "@/components/env.js";
const router = useRouter();
const filter = reactive({
  risk_level: "", // 风险等级
  status: "", // 状态
  time: [],
  keyword: "",
  page: 1,
  page_size: 10,
});
const tableTotal = ref(0);
const ruleTypeOptions = ref({});
const filterClear = () => {
  filter.warning_index = "";
  filter.risk_level = "";
  filter.status = "";
  filter.time = [];
  filter.page_size = 10;
  filter.page = 1;
  changePage(1);
}; // 重置筛选项
const statusOptions = ["全部", "待处置", "已完成", "已关闭"];
// const disposedOptions = ["", "已处置", "已处置"];
const riskLevels = {
  "": "全部",
  1: "低风险",
  2: "一般风险",
  3: "较大风险",
  4: "重大风险",
};
let headers = reactive([
  {
    label: "预警点",
    prop: "alert_point",
    align: "left",
    width: 180,
  },
  {
    label: "预警时间",
    prop: "alert_time",
    align: "left",
    width: 160,
  },
  {
    label: "预警分类",
    prop: "class_parent_name",
    align: "left",
  },
  {
    label: "预警指标",
    prop: "class_name",
    align: "left",
  },
  {
    label: "风险等级",
    prop: "risk_level",
    align: "left",
  },
  {
    label: "当前报警值",
    prop: "current_value",
    align: "left",
  },
  {
    label: "预警阀值",
    prop: "warn_threshold",
    align: "left",
  },
  {
    label: "推送次数",
    prop: "push_count",
    align: "left",
  },
  {
    label: "最近推送时间",
    prop: "last_push_time",
    align: "left",
    width: 160,
  },
  {
    label: "状态",
    prop: "status",
    align: "left",
    width: 90,
  },
  {
    label: "操作",
    prop: "operation",
    align: "left",
    width: 80,
    fixed: "right",
  },
]);
let rows = ref([]);
const changeSize = (size) => {
  filter.page_size = size;
  changePage(1);
};

const changePage = (page) => {
  filter.page = page;
  getTableRows();
};
const changeSearch = (val) => {
  filter.keyword = val;
  changePage(1);
}; // 表格关键字筛选

const filterAction = () => {
  changePage(1);
}; // 查询按钮

const getTableRows = async () => {
  let [start_time = "", end_time = ""] = filter.time || [];
  let params = {
    ...filter,
    start_time,
    end_time,
  };
  Reflect.deleteProperty(params, "time");
  axios.get("/v1/api/alert_list/list", { params }).then((res) => {
    if (res.data.code == 200) {
      let { list, total_count } = res.data.data;
      rows.value = list || [];
      tableTotal.value = total_count;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
// 处置反馈
const visible = ref(false);
const active_row = ref(null);
const operation = (row) => {
  active_row.value = row;
  visible.value = true;
};
const goDetail = ({ id }) => {
  router.push({
    path: "/ticket/my-warn-ticket/detail",
    query: {
      id,
    },
  });
};
const getRuleTypeOptions = async () => {
  ruleTypeOptions.value = await GetRuleTypeOptions();
  getTableRows();
};
onBeforeMount(() => {
  getRuleTypeOptions();
});
</script>

<style lang="scss" scoped>
.my-warn-ticket {
  width: 100%;
  height: 100%;
  padding: 0 24px 16px;
  .breadcrumb {
    width: 100%;
    height: 46px;
  }
  .content {
    width: 100%;
    height: calc(100% - 46px);
    background-color: #ffffff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    padding: 16px;
    :deep(.bg-filter-group) {
      padding: 0;
    }
    .filter-group {
      .left-filter {
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
      }
      .right-action {
        white-space: nowrap;
      }
    }
    .table_container {
      flex: 1;
      display: flex;
      flex-direction: column;
      position: relative;
      .table {
        position: absolute;
        flex: 1;
        width: 100%;
        height: calc(100% - 48px);
        .href {
          font-size: 14px;
          color: #3759be;
          cursor: pointer;
        }
      }
    }
    :deep(.el-button.is-link) {
      line-height: 1;
    }
    .bg-pagination {
      bottom: unset;
    }
  }
}
</style>
