<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 #healthScore="{ row }">
            <span class="health-score" @click="handleHealthScoreClick(row)">{{
              row.healthScore
            }}</span>
          </template>
          <template #health2Score="{ row }">
            <span class="health-score" @click="handleHealth2ScoreClick(row)">{{
              row.health2Score
            }}</span>
          </template>
          <template #status="{ row }">
            <div class="status-matrix">
              <div
                v-for="(row, rowIndex) in row.status"
                :key="rowIndex"
                class="matrix-row"
              >
                <div
                  v-for="(status, colIndex) in row"
                  :key="colIndex"
                  class="status-dot"
                  :class="{
                    'status-normal': status.status === 1,
                    'status-warning': status.status === 2,
                    'status-error': status.status === 3,
                  }"
                ></div>
              </div>
            </div>
          </template>

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

          <template #operation="{ row }">
            <span class="view-btn" @click="handleView(row)">详情</span>
            <span class="edit-btn" @click="handleEdit(row)">编辑</span>
          </template>
        </common-table>
      </div>
    </div>

    <!-- 仓室数量设置弹窗 -->
    <el-dialog
      v-model="roomSettingVisible"
      title="仓室数量设置"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="roomForm" label-width="120px">
        <el-form-item label="仓室数量">
          <!-- <span class="readonly-value">{{ roomForm.totalRooms }}</span> -->
          <el-input
            v-model="roomForm.totalRooms"
            controls-position="right"
            style="width: 150px"
            disabled
          />
        </el-form-item>
        <el-form-item label="仓室分几排">
          <el-input-number
            v-model="roomForm.rows"
            :min="1"
            :max="20"
            controls-position="right"
            @change="handleRowsChange"
          />
        </el-form-item>
      </el-form>

      <!-- 分布表格 -->
      <div class="distribution-table">
        <div class="table-title">
          仓室数量分布
          <div
            v-if="distributionDiff !== 0"
            :class="[
              'distribution-warning',
              distributionDiff > 0 ? 'warning-less' : 'warning-less',
            ]"
          >
            当前仓室总数{{ distributionDiff > 0 ? "大于" : "小于" }}默认仓室数量
            {{ Math.abs(distributionDiff) }} 个，请修改。
          </div>
        </div>
        <el-table
          :data="distributionData"
          style="width: 100%"
          size="small"
          border
        >
          <el-table-column prop="row" label="排" width="180" />
          <el-table-column prop="count" label="仓数量">
            <template #default="{ row }">
              <el-input-number
                v-model="row.count"
                :min="1"
                :max="50"
                controls-position="right"
                size="small"
                @change="handleDistributionChange"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="roomSettingVisible = false">取消</el-button>
          <el-button type="primary" @click="handleRoomSettingConfirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 电磁阀数量设置弹窗 -->
    <el-dialog
      v-model="valveSettingVisible"
      title="电磁阀数量设置"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="valve-setting">
        <div class="setting-row">
          <div class="input-group">
            <span>电磁阀总数量：</span>
            <el-input v-model="valveForm.total" style="width: 120px" disabled />
          </div>
          <el-button type="primary" @click="handleAverageDistribute"
            >平均分布生成</el-button
          >
        </div>
        <div class="table-title">仓室脉冲阀数量分布</div>
        <div class="setting-row">
          <div class="input-group">
            <span>仓室编号：</span>
            <el-select v-model="valveForm.roomNo" style="width: 120px">
              <el-option
                v-for="i in roomForm.rows"
                :key="i"
                :label="`${i}`"
                :value="i"
              />
            </el-select>
          </div>
          <div class="input-group">
            <span>电磁阀分布数量：</span>
            <el-input-number
              v-model="valveForm.valveCount"
              :min="1"
              controls-position="right"
              style="width: 120px"
            />
          </div>
          <div class="input-group">
            <span>布阀数量：</span>
            <el-input-number
              v-model="valveForm.distribution"
              :min="1"
              controls-position="right"
              style="width: 120px"
            />
          </div>
        </div>
      </div>

      <!-- 分布表格 -->
      <div class="distribution-table">
        <div class="table-title">仓室脉冲阀数量分布</div>
        <div class="valve-distribution-grid">
          <div class="valve-row">
            <div
              v-for="(cell, colIndex) in currentEditingRow?.status"
              :key="colIndex"
              class="valve-cell"
            >
              <div class="cell-index">{{ cell.index }}仓</div>
              <div class="cell-value">
                {{ currentEditingRow.status[colIndex].count }}
              </div>
            </div>
          </div>
        </div>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="valveSettingVisible = false">取消</el-button>
          <el-button type="primary" @click="handleValveSettingConfirm">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, computed } 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: "12%",
  },
  {
    prop: "deviceNo",
    label: "除尘器编号",
    width: "12%",
  },
  {
    prop: "process",
    label: "所属工序",
    width: "5%",
  },
  {
    prop: "healthScore",
    label: "仓室数量",
    width: "8%",
  },
  {
    prop: "health2Score",
    label: "电磁阀数量",
    width: "8%",
  },
  {
    prop: "status",
    label: "仓室健康状态",
    width: "25%",
  },
  {
    prop: "alarmCount",
    label: "健康度",
    width: "5%",
  },
  {
    prop: "lastAlarmTime",
    label: "合理压差范围",
    width: "10%",
  },
  {
    prop: "operation",
    label: "操作",
    width: "10%",
  },
]);

const tableData = [
  {
    deviceName: "1#除尘器",
    deviceNo: "1234567890",
    process: "转炉",
    healthScore: 10,
    health2Score: 11,
    status: [
      [
        {
          index: 1,
          status: 1,
          count: 1,
        },
        {
          index: 2,
          status: 2,
          count: 2,
        },
        {
          index: 3,
          status: 1,
          count: 1,
        },
        {
          index: 4,
          status: 1,
          count: 1,
        },
        {
          index: 5,
          status: 3,
          count: 1,
        },
      ],
      [
        {
          index: 6,
          status: 1,
          count: 1,
        },
        {
          index: 7,
          status: 1,
          count: 1,
        },
        {
          index: 8,
          status: 1,
          count: 1,
        },
        {
          index: 9,
          status: 1,
          count: 1,
        },
        {
          index: 10,
          status: 2,
          count: 1,
        },
      ],
    ],
    alarmCount: "98%",
    lastAlarmTime: "-",
    location: "一号转炉东侧",
  },
  {
    deviceName: "2#除尘器",
    deviceNo: "1234567890",
    process: "转炉",
    healthScore: 10,
    health2Score: 11,
    status: [
      [
        {
          index: 1,
          status: 1,
          count: 1,
        },
        {
          index: 2,
          status: 2,
          count: 2,
        },
        {
          index: 3,
          status: 1,
          count: 1,
        },
        {
          index: 4,
          status: 1,
          count: 1,
        },
        {
          index: 5,
          status: 3,
          count: 1,
        },
      ],
      [
        {
          index: 6,
          status: 1,
          count: 1,
        },
        {
          index: 7,
          status: 1,
          count: 1,
        },
        {
          index: 8,
          status: 1,
          count: 1,
        },
        {
          index: 9,
          status: 1,
          count: 1,
        },
        {
          index: 10,
          status: 2,
          count: 1,
        },
      ],
    ],
    alarmCount: "45%",
    lastAlarmTime: "100～3000",
    location: "二号转炉西侧",
  },
  {
    deviceName: "3#除尘器",
    deviceNo: "1234567890",
    process: "炼铁",
    healthScore: 12,
    health2Score: 13,
    status: [
      [
        {
          index: 1,
          status: 1,
          count: 1,
        },
        {
          index: 2,
          status: 2,
          count: 2,
        },
        {
          index: 3,
          status: 1,
          count: 1,
        },
        {
          index: 4,
          status: 1,
          count: 1,
        },
        {
          index: 5,
          status: 3,
          count: 1,
        },
      ],
      [
        {
          index: 6,
          status: 1,
          count: 1,
        },
        {
          index: 7,
          status: 1,
          count: 1,
        },
        {
          index: 8,
          status: 1,
          count: 1,
        },
        {
          index: 9,
          status: 1,
          count: 1,
        },
        {
          index: 10,
          status: 2,
          count: 1,
        },
        {
          index: 11,
          status: 1,
          count: 1,
        },
        {
          index: 12,
          status: 2,
          count: 1,
        },
      ],
    ],
    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);
};

// 仓室设置相关
const roomSettingVisible = ref(false);
const roomForm = ref({
  totalRooms: 0,
  rows: 0,
});
const currentEditingRow = ref(null);

// 分布数据
const distributionData = ref([]);

// 计算分布差异
const distributionDiff = computed(() => {
  const total = distributionData.value.reduce(
    (sum, item) => sum + item.count,
    0
  );
  return total - roomForm.value.totalRooms;
});

// 处理行数变化
const handleRowsChange = (value) => {
  const total = roomForm.value.totalRooms;
  if (!total || !value) return;

  // 计算每行的基础数量（向下取整）和余数
  const baseValue = Math.floor(total / value);
  const remainder = total % value;

  // 生成分布数据
  distributionData.value = Array.from({ length: value }, (_, index) => {
    const isLast = index === value - 1;
    const count = isLast
      ? baseValue + remainder // 最后一项加上余数
      : baseValue;

    return {
      row: `第${index + 1}排`,
      count,
    };
  });
};

// 处理分布数量变化
const handleDistributionChange = () => {
  // 移除自动更新总数的逻辑，保持总数不变
  // const total = distributionData.value.reduce(
  //   (sum, item) => sum + item.count,
  //   0
  // );
  // roomForm.value.totalRooms = total;
};

// 修改打开弹窗的处理函数
const handleHealthScoreClick = (row) => {
  currentEditingRow.value = row;
  console.log(currentEditingRow.value.status);
  roomForm.value.totalRooms = row.healthScore;
  roomForm.value.rows = row.status.length;
  // 初始化分布数据
  distributionData.value = row.status.map((item, index) => {
    return {
      row: `第${index + 1}排`,
      count: item.length,
    };
  });
  roomSettingVisible.value = true;
};

// 确认设置
const handleRoomSettingConfirm = () => {
  if (currentEditingRow.value) {
    currentEditingRow.value.healthScore = roomForm.value.totalRooms;
    // 这里可以添加更新状态矩阵的逻辑
  }
  roomSettingVisible.value = false;
};

// 电磁阀设置相关
const valveSettingVisible = ref(false);
const valveForm = ref({
  total: 120,
  roomNo: 1,
  valveCount: 5,
  distribution: 30,
});

// 点击电磁阀数量时打开弹窗
const handleHealth2ScoreClick = (row) => {
  currentEditingRow.value = JSON.parse(JSON.stringify(row));
  currentEditingRow.value.status = currentEditingRow.value.status.flat();
  valveForm.value.total = row.health2Score;
  valveSettingVisible.value = true;
};

// 平均分布生成
const handleAverageDistribute = () => {
  if (!currentEditingRow.value?.status) return;

  const status = currentEditingRow.value.status;
  const total = Number(valveForm.value.total) || 0;

  // 计算总仓室数
  const totalRooms = status.reduce((sum, row) => sum + row.length, 0);
  if (totalRooms === 0 || total === 0) return;

  // 计算基础平均值（向下取整）和余数
  const baseValue = Math.floor(total / totalRooms);
  const remainder = total % totalRooms;

  // 先将所有仓室设置为基础值
  status.forEach((row, rowIndex) => {
    row.forEach((_, colIndex) => {
      currentEditingRow.value.status[rowIndex][colIndex] = baseValue;
    });
  });

  // 如果有余数，将其加到最后一个仓室
  if (remainder > 0) {
    const lastRow = status.length - 1;
    const lastCol = status[lastRow].length - 1;
    currentEditingRow.value.status[lastRow][lastCol] += remainder;
  }
};

// 确认设置
const handleValveSettingConfirm = () => {
  if (currentEditingRow.value) {
    currentEditingRow.value.health2Score = valveForm.value.total;
  }
  valveSettingVisible.value = false;
};

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;
  }

  .health-score {
    color: #027ffc;
    cursor: pointer;
    display: block;
    width: 100%;
  }

  .status-matrix {
    width: fit-content;
    display: flex;
    padding: 10px;
    box-sizing: border-box;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
    background-color: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);

    .matrix-row {
      display: flex;
      gap: 6px;
      justify-content: flex-start;
    }

    .status-dot {
      width: 14px;
      height: 14px;
      border-radius: 50%;
      &:hover {
        cursor: pointer;
        transform: scale(1.2);
      }
    }

    .status-normal {
      background-color: #67c23a; // 绿色
    }

    .status-warning {
      background-color: #e6a23c; // 橙色
    }

    .status-error {
      background-color: #f56c6c; // 红色
    }
  }

  .view-btn {
    margin-right: 8px;
  }

  .dialog-footer {
    display: flex;
    justify-content: center;
    gap: 12px;
  }

  :deep(.el-dialog__body) {
    padding: 20px;
  }

  .distribution-table {
    margin-top: 20px;
    border-top: 1px solid #ebeef5;
    padding-top: 20px;
  }

  .table-title {
    font-size: 14px;
    color: #2182a0;
    margin-bottom: 16px;
    font-weight: 500;
  }

  :deep(.el-input-number.el-input-number--small) {
    width: 130px;
  }
  :deep(.distribution-table .el-table--border .el-table__cell) {
    border-right: 1px solid #ebeef5 !important;
  }

  .valve-setting {
    .setting-row {
      display: flex;
      align-items: center;
      gap: 20px;
      margin-bottom: 20px;

      .input-group {
        display: flex;
        align-items: center;
        gap: 8px;

        span {
          white-space: nowrap;
          color: #606266;
        }
      }
    }
  }

  .valve-distribution-grid {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px;
    background: #f5f7fa;
    border-radius: 4px;

    .valve-row {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .valve-cell {
      width: 41px;
      background: white;
      padding: 4px;
      border-radius: 4px;
      text-align: center;
      border: 1px solid #dcdfe6;

      .cell-index {
        font-size: 12px;
        color: #909399;
        margin-bottom: 8px;
      }

      .cell-value {
        :deep(.el-input-number) {
          width: 100px;
        }
      }
    }
  }

  .readonly-value {
    font-size: 14px;
    color: #606266;
    line-height: 32px;
    padding: 0 12px;
  }

  .distribution-warning {
    display: inline-block;
    font-size: 13px;
    margin-left: 12px;
    padding: 2px 8px;
    border-radius: 4px;

    &.warning-more {
      color: #e6a23c;
      background-color: #fdf6ec;
    }

    &.warning-less {
      color: #f56c6c;
      background-color: #fef0f0;
    }
  }
}
</style>
