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

          <template #alarmCount="{ row }">
            <span :style="{ color: getHealthScoreColor(row.alarmCount) }">
              {{ row.alarmCount }}
            </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>
  </div>
</template>

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

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

const currentPage = ref(1);
const pageSize = ref(10);

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

const tableColumns = ref([
  {
    prop: "index",
    label: "序号",
    width: "5%",
  },
  {
    prop: "deviceName",
    label: "除尘器名称",
    width: "15%",
  },
  {
    prop: "deviceName",
    label: "除尘器编号",
    width: "15%",
  },
  {
    prop: "process",
    label: "所属工序",
    width: "5%",
  },
  {
    prop: "healthScore",
    label: "仓室数量",
    width: "5%",
  },
  {
    prop: "health2Score",
    label: "电磁阀数量",
    width: "5%",
  },
  {
    prop: "status",
    label: "仓室健康状态",
    width: "25%",
  },
  {
    prop: "alarmCount",
    label: "健康度",
    width: "5%",
  },
  {
    prop: "lastAlarmTime",
    label: "合理压差范围",
    width: "10%",
  },
  {
    prop: "operation",
    label: "操作",
    width: "10%",
  },
]);

const tableData = [
  {
    deviceName: "1#除尘器",
    process: "转炉",
    healthScore: 24,
    health2Score: 120,
    status: "正常运行",
    alarmCount: "98%",
    lastAlarmTime: "-",
    location: "一号转炉东侧",
  },
  {
    deviceName: "2#除尘器",
    process: "转炉",
    healthScore: 24,
    health2Score: 120,
    status: "故障",
    alarmCount: "45%",
    lastAlarmTime: "100～3000",
    location: "二号转炉西侧",
  },
  {
    deviceName: "3#除尘器",
    process: "炼铁",
    healthScore: 24,
    health2Score: 120,
    status: "轻微异常",
    alarmCount: "78%",
    lastAlarmTime: "100～3000",
    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 getIndex = (index) => {
  return (currentPage.value - 1) * pageSize.value + index + 1;
};

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

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

const handlePaginationChange = (pagination) => {
  currentPage.value = pagination.currentPage;
  pageSize.value = pagination.pageSize;
  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;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 400px);
    }
  }

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