<template>
  <div class="page-container collectorList all-select-btn">
    <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.projectId" style="width: 200px">
              <el-option
                v-for="i in nameList.list"
                :key="i"
                :label="`${i.name}`"
                :value="i.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备类型">
            <el-select v-model="formInline.deviceType" style="width: 200px">
              <el-option
                v-for="i in typeList.list"
                :key="i"
                :label="`${i.desc}`"
                :value="i.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="原因分类">
            <el-select v-model="formInline.reasonCategory" style="width: 200px">
              <el-option
                v-for="i in reasonList.list"
                :key="i"
                :label="`${i.value}`"
                :value="i.key"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="default" class="reset-btn-balck-theme" @click="onReset"
              >重置</el-button
            >
            <el-button type="default"  class="search-btn-balck-theme" @click="onSubmit"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="table-box">
        <common-table
          ref="pageRef"
          :data="tableData.list"
          :columns="tableColumns"
          :default-page-size="20"
          :total="total"
          @pagination-change="handlePaginationChange"
          :pagination-texts="{
            total: '共',
            sizeChange: '条/页',
            prev: '前一页',
            next: '后一页',
            jumper: '跳至',
          }"
        >
          <template #index="{ $index }">
            {{ getIndex($index) }}
          </template>
          <template #operate="{ row }">
            <span class="health-score green-color" @click="linkTo(row)">挂起期间告警</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>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, computed, reactive } from "vue";
import CommonTable from "@/components/commonTable/index.vue";
import { getDataFun, postDataJSON } from "@/request/method.js";
import { useRoute, useRouter } from "vue-router";

const router = useRouter();
const formInline = ref({
  projectId: "",
  deviceType: "",
  reasonCategory: -1,
});

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

const tableColumns = ref([
  {
    prop: "index",
    label: "序号",
    width: "5%",
  },
  // {
  //   prop: "projectName",
  //   label: "项目名称",
  //   width: "7%",
  // },
  {
    prop: "deviceNo",
    label: "设备编号",
    width: "7%",
  },
  {
    prop: "deviceType",
    label: "设备类型",
    width: "7%",
  },
  {
    prop: "deviceName",
    label: "设备名称",
    width: "7%",
  },
  {
    prop: "signalNo",
    label: "设备信号编号",
    width: "7%",
  },
  {
    prop: "location",
    label: "设备所在位置",
    width: "7%",
  },
  {
    prop: "suspendStartTime",
    label: "挂起开始时间",
    width: "7%",
  },
  {
    prop: "suspendEndTime",
    label: "结束时间",
    width: "7%",
  },
  {
    prop: "reasonCategory",
    label: "原因分类",
    width: "7%",
  },
  {
    prop: "reasonDesc",
    label: "原因描述",
    width: "7%",
  },
  {
    prop: "createTime",
    label: "创建时间",
    width: "7%",
  },
  {
    prop: "createUser",
    label: "创建人",
    width: "7%",
  },
  {
    prop: "operate",
    label: "操作",
    width: "8%",
  },
]);

const tableData = reactive({
  list: [
    {
      deviceName: "1#除尘器",
      compart: "1234567890",
      position: "转炉",
      changeDateRemind: "xx",
      changeDate: "xx",
      changePeo: "xx",
      ownerdeviceName: "xx",
      lastAlarmTime: "xx",
      changeRound: "xx",
    },
  ],
});

const nameList = reactive({
  list: [],
});

const getNameList = () => {
  const url = "/alarm/getProjectEnumList";
  return getDataFun(url, "").then((res) => {
    if (res && res.data) {
      nameList.list = res.data || [];
    }
  });
};

const typeList = reactive({
  list: [],
});
const getTypeList = () => {
  const url = "/alarm/getDeviceType";
  return getDataFun(url, "").then((res) => {
    if (res && res.data) {
      typeList.list = res.data || [];
    }
  });
};

const reasonList = reactive({
  list: [
    { value: "全部", key: -1 },
  ],
});

const getReasonList = () => {
  const url = "/alarm/reasonCategoryEnumList";
  return getDataFun(url, "").then((res) => {
    if (res && res.data) {
      reasonList.list.push(...res.data);
    }
  }).catch(err => {
    reasonList.list = [{ value: "全部", key: null }];
    throw err;
  });
};

const getIndex = (index) => {
  return (currentPage.value - 1) * pageSize.value + index + 1;
};

const handlePaginationChange = (pagination) => {
  currentPage.value = pagination.currentPage;
  pageSize.value = pagination.pageSize;
  getManagementList();
};

const onSubmit = () => {
  currentPage.value = 1;
  pageRef.value.setCurrentPage(1);
  getManagementList();
};

const pageRef = ref(null);
const onReset = () => {
  formInline.value = {
    deviceType: "",
    reasonCategory: -1,
    projectId: "",
  };
  currentPage.value = 1;
  pageSize.value = 20;
  pageRef.value.setCurrentPage(currentPage.value);
  getManagementList();
};

const total = ref(0);
const getManagementList = () => {
  const url = "/alarm/getSuspendDeviceListPage";
  const params = {
    deviceType: formInline.value.deviceType,
    reasonCategory: formInline.value.reasonCategory == -1 ? null : formInline.value.reasonCategory,
    projectId: formInline.value.projectId,
    pageNo: currentPage.value,
    pageSize: pageSize.value,
  };
  tableData.list = [];
  return getDataFun(url, params).then((res) => {
    if (res && res.data) {
      tableData.list = res.data.list || [];
      total.value = res.data.total;
    }
  });
};

const linkTo = (item) => {
  router.push({
    path: "/alerts",
    query: {
      deviceName: item.deviceName,
      suspendStartTime: item.suspendStartTime,
      suspendEndTime: item.suspendEndTime,
      suspendFlag: "1",
    },
  });
};

onMounted(async () => {
  getNameList();
  getTypeList();
  getReasonList();
  getManagementList();
});

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

<style lang="scss" scoped>
.collectorList {
  width: 100%;
  height: calc(100% - 14px);
  box-sizing: border-box;
  :deep(.el-input--small) {
    width: 100%;
  }
  .content-box {
    display: flex;
    flex-direction: column;
    height: 100%;
    .search {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16px;
    }
    .table-box {
      flex: 1;
      width: 100%;
      height: calc(100vh - 600px);
    }
  }
  .default-btn {
    .search {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16px;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 230px);
    }
  }

  .default-btn {
    width: 85px;
  }
  .search-btn {
    width: 85px;
    background-color: var(--bg-primary);
    border: 1px solid var(--success);
  }
  .add-btn {
    width: 85px;
  }

  .health-score {
    color: var(--success);
    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: var(--success);
    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>
