<template>
  <div class="page-container dust-container">
    <div class="header">
      <div class="item-box">
        <img src="@/assets/icons/warn.png" alt="dust" />
        <div class="title">
          <span>泄漏告警(条)</span>
          <div class="content">6</div>
        </div>
      </div>

      <div class="item-box">
        <img src="@/assets/icons/health.png" alt="dust" />
        <div class="title">
          <span>综合健康度</span>
          <div class="content">98%</div>
        </div>
      </div>

      <div class="item-box">
        <img src="@/assets/icons/close.png" alt="dust" />
        <div class="title">
          <span>闭环(条)</span>
          <div class="content">6</div>
        </div>
      </div>
    </div>

    <div class="content-box">
      <div class="search">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="工序">
            <el-select
              v-model="formInline.region"
              placeholder="请选择工序"
              style="width: 120px"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="除尘器名称">
            <el-input
              v-model="formInline.deviceName"
              placeholder="请输入除尘器名称"
              style="width: 200px"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button type="default" class="default-btn" @click="onSubmit"
              >重置</el-button
            >
            <el-button type="primary" class="search-btn" @click="onSubmit"
              >查询</el-button
            >
            <el-button type="success" class="add-btn" @click="onSubmit"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <common-table
        :data="tableData"
        :columns="tableColumns"
        :default-page-size="10"
        @pagination-change="handlePaginationChange"
        :pagination-texts="{
          total: '共',
          sizeChange: '条/页',
          prev: '前一页',
          next: '后一页',
          jumper: '跳至',
        }"
      >
        <template #status="{ row }">
          <el-tag :type="getStatusType(row.status)" effect="light">
            {{ row.status }}
          </el-tag>
        </template>

        <template #healthScore="{ row }">
          <span :style="{ color: getHealthScoreColor(row.healthScore) }">
            {{ row.healthScore }}
          </span>
        </template>

        <template #operation="{ row }">
          <el-button type="primary" link @click="handleView(row)">
            查看详情
          </el-button>
          <el-button type="primary" link @click="handleEdit(row)">
            编辑
          </el-button>
        </template>
      </common-table>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import CommonTable from "@/components/CommonTable/index.vue";

const formInline = ref({
  deviceName: "",
  region: 1,
});

const options = ref([
  {
    value: "1",
    label: "转炉",
  },
  {
    value: "2",
    label: "炼铁",
  },
]);

const tableColumns = ref([
  {
    prop: "deviceName",
    label: "除尘器名称",
    width: "15%",
  },
  {
    prop: "process",
    label: "工序",
    width: "10%",
  },
  {
    prop: "healthScore",
    label: "健康度",
    width: "10%",
  },
  {
    prop: "status",
    label: "运行状态",
    width: "10%",
  },
  {
    prop: "alarmCount",
    label: "告警数量",
    width: "10%",
  },
  {
    prop: "lastAlarmTime",
    label: "最近告警时间",
    width: "15%",
  },
  {
    prop: "location",
    label: "安装位置",
    width: "15%",
  },
  {
    prop: "operation",
    label: "操作",
    width: "15%",
  },
]);

const tableData = [
  {
    deviceName: "1#除尘器",
    process: "转炉",
    healthScore: "98%",
    status: "正常运行",
    alarmCount: 0,
    lastAlarmTime: "-",
    location: "一号转炉东侧",
  },
  {
    deviceName: "2#除尘器",
    process: "转炉",
    healthScore: "45%",
    status: "故障",
    alarmCount: 2,
    lastAlarmTime: "2024-03-19 14:30",
    location: "二号转炉西侧",
  },
  {
    deviceName: "3#除尘器",
    process: "炼铁",
    healthScore: "78%",
    status: "轻微异常",
    alarmCount: 1,
    lastAlarmTime: "2024-03-19 10:15",
    location: "炼铁区域北侧",
  },
  {
    deviceName: "4#除尘器",
    process: "炼铁",
    healthScore: "89%",
    status: "正常运行",
    alarmCount: 0,
    lastAlarmTime: "-",
    location: "炼铁区域南侧",
  },
  {
    deviceName: "5#除尘器",
    process: "转炉",
    healthScore: "92%",
    status: "正常运行",
    alarmCount: 0,
    lastAlarmTime: "-",
    location: "三号转炉东侧",
  },
  {
    deviceName: "6#除尘器",
    process: "炼铁",
    healthScore: "95%",
    status: "正常运行",
    alarmCount: 0,
    lastAlarmTime: "-",
    location: "炼铁区域东侧",
  },
  {
    deviceName: "7#除尘器",
    process: "转炉",
    healthScore: "87%",
    status: "正常运行",
    alarmCount: 0,
    lastAlarmTime: "-",
    location: "四号转炉西侧",
  },
];

const getStatusType = (status) => {
  const statusMap = {
    正常运行: "success",
    轻微异常: "warning",
    故障: "danger",
  };
  return statusMap[status] || "info";
};

const getHealthScoreColor = (score) => {
  const value = parseInt(score);
  if (value >= 90) return "#67C23A";
  if (value >= 70) return "#E6A23C";
  return "#F56C6C";
};

const handleView = (row) => {
  console.log("查看详情", row);
};

const handleEdit = (row) => {
  console.log("编辑", row);
};

const handlePaginationChange = (pagination) => {
  console.log("分页变化", pagination);
};

const onSubmit = () => {
  console.log("submit!", formInline.value);
};

onMounted(async () => {});

onBeforeUnmount(() => {});
</script>

<style lang="scss" scoped>
.dust-container {
  width: 100%;
  height: calc(100% - 14px);
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    gap: 16px;

    .item-box {
      flex: 1;
      max-width: 420px;
      height: 130px;
      background-color: #fff;
      border: 1.5px solid #e8e8e8;
      border-radius: 6px;
      box-shadow: 0px 1.33px 16px 0px rgba(83, 100, 170, 0.1);
      padding: 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;

      img {
        width: 72px;
        height: 72px;
        margin-right: 20px;
      }

      .title {
        width: 100%;

        span {
          color: rgba(36, 48, 71, 0.75);
          line-height: 22px;
          font-size: 14px;
          font-weight: 500;
        }
      }

      .content {
        color: #273849;
        line-height: 30px;
        font-size: 42px;
        font-weight: 600;
        margin-top: 10px;
      }
    }
  }
  .content-box {
    margin-top: 24px;
    .search {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16px;
    }
  }

  .default-btn {
    width: 85px;
  }
  .search-btn {
    width: 85px;
    background: #2182a0;
    border: 1px solid #2182a0;
  }
  .add-btn {
    width: 85px;
  }
}
</style>
