<template>
  <div class="long-list1">
    <div class="list-title">
      <span>联动规则编辑器</span>
    </div>
    <div class="basic-box">
      <basicBorder title="触发条件设置">
        <div class="edit-wrap">
          <div class="edit-right">
            <div class="right-title">条件逻辑说明</div>
            <div class="right-content">
              <div v-for="(item, index) in formData" :key="index" class="right-item-row">
                <span class="right-item index-label">{{ index + 1 }}</span>
                <span class="right-item">设备名称: {{ item.alarmName }}</span>
                <span class="right-item">监测地点: {{ item.devicePos }}</span>
                <span class="right-item">IF {{ getConditionText(item) }}</span>
                <span class="right-item">AND 持续时间:{{ item.cxTime ?? '' }}s</span>
                <span class="right-item">AND 时间在生效范围内</span>
                <span class="right-item">THEN 执行联动动作</span>
              </div>
            </div>
          </div>
        </div>
      </basicBorder>
    </div>
    <div class="basic-box1">
      <basicBorder title="联动动作设置">
        <div class="basic-table">
          <div class="table-title">
            <div class="title-item" v-for="(item, index) in titleList" :key="index">{{ item.label }}</div>
          </div>
          <div class="table-content">
            <div class="content-item" v-for="(item, index) in tableData" :key="index">
              <div class="item-text">{{ item.indnex }}</div>
              <div class="item-text item-text1">
                <span>{{ item.actionType }}</span>
              </div>
              <div class="item-text">
                <span> {{ item.action }}</span>
              </div>
              <div class="item-text">
                <div class="switch" :class="{ active: item.qy === 1 }">
                  <div class="switch-dot"></div>
                </div>
              </div>
              <div class="item-text">{{ item.delayTime }}</div>
              <div class="item-text">{{ item.duration }}</div>
              <div class="item-text">
                <span class="text-look">查看</span>
              </div>
            </div>
          </div>
        </div>
      </basicBorder>
    </div>
    <div class="basic-box2">
      <basicBorder title="联动执行流程图">
        <div class="workflow-container">
          <div class="steps-wrapper">
            <div v-for="(step, index) in steps" :key="index" class="step-item">
              <div class="step-icon" :class="`icon-${step.id}`"></div>
              <div class="step-frame" :class="`frame-${step.id}`">
                <span class="step-text">{{ step.name }}</span>
              </div>
              <!-- 底部小圆点 -->
              <div class="step-dot">
                <div class="dot" :class="{ active: step.status === 1 }"></div>
              </div>
              <div class="steps-arrow"></div>
            </div>
          </div>
        </div>
      </basicBorder>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted, watch, computed } from 'vue';
import basicBorder from './basicBorder.vue';
import { useLinkRuleStore } from '/@/store/modules/linkRule';
import { getWarningDeviceList } from '../hsqHome.api.js';
const props = defineProps<{
  selectedRow?: any | null;
}>();

const linkRuleStore = useLinkRuleStore();
const currentSelectedRow = computed(() => linkRuleStore.getSelectedRow);

let titleList = ref<any[]>([
  { label: '序号', value: '1' },
  { label: '动作类型', value: '2' },
  { label: '动作说明', value: '3' },
  { label: '启用', value: '4' },
  { label: '延迟', value: '5' },
  { label: '持续', value: '6' },
  { label: '操作', value: '7' },
]);

let tableData = ref<any[]>([
  { indnex: '1', actionType: '声光报警器', action: '触发报警声+红色闪烁', qy: 1, delayTime: '0s', duration: '300s' },
  { indnex: '2', actionType: '声光报警器', action: '触发报警声', qy: 1, delayTime: '1s', duration: '200s' },
  { indnex: '3', actionType: '声光报警器', action: '红色闪烁', qy: 0, delayTime: '2s', duration: '180s' },
]);

// 流程图，添加 status 字段
const steps = ref([
  { id: 'alarm', name: '报警触发', status: 0 },
  { id: 'condition', name: '条件判断', status: 0 },
  { id: 'search', name: '查找规则', status: 0 },
  { id: 'action', name: '执行动作', status: 0 },
  { id: 'log', name: '记录日志', status: 0 },
]);

// 模拟接口请求获取步骤状态
const fetchStepStatus = async () => {
  const apiResponse = {
    alarmTrigger: 1, // 报警触发为 1
    conditionJudge: 0, // 条件判断为 1
    searchRule: 0, // 查找规则为 0
    executeAction: 0, // 执行动作为 0
    recordLog: 0, // 记录日志为 0
  };

  // 根据接口返回数据更新状态
  steps.value[0].status = apiResponse.alarmTrigger;
  steps.value[1].status = apiResponse.conditionJudge;
  steps.value[2].status = apiResponse.searchRule;
  steps.value[3].status = apiResponse.executeAction;
  steps.value[4].status = apiResponse.recordLog;
};
const formData = ref([]);
// 获取预警条目数据
const getWarningList = async (val) => {
  const params = {
    sysId: val.sysId,
    monitorType: 2,
    alarmId: val.id,
    pageSize: 1000,
  };
  const res = await getWarningDeviceList(params);
  formData.value = res.records;
};
// 获取单位
const getSensorUnit = (item: any) => {
  const type = item.deviceType;
  if (type === 'modelsensor_co' || type === 'modelsensor_hcl') return 'ppm';
  if (type === 'modelsensor_temperature') return '℃';
  return '%';
};
// 拼接IF条件文本
const getConditionText = (item: any) => {
  const { fmin, fmax, deviceType } = item;
  const unit = getSensorUnit(item);
  const minVal = fmin ?? '';
  const maxVal = fmax ?? '';

  // 同时存在最小值、最大值：区间 fmin~fmax
  if (minVal !== '' && maxVal !== '') {
    return `${minVal}~${maxVal} ${unit}`;
  }

  // 只有最大值，无最小值
  if (maxVal !== '' && minVal === '') {
    if (deviceType === 'modelsensor_co') {
      // CO：大于fmax
      return `> ${maxVal} ${unit}`;
    } else if (deviceType === 'modelsensor_hcl' || deviceType === 'modelsensor_temperature') {
      // HCl / 温度：大于等于fmax
      return `≥ ${maxVal} ${unit}`;
    } else {
      return `≥ ${maxVal} ${unit}`;
    }
  }
  // 只有最小值（无最大值），直接显示最小值+单位
  if (minVal !== '' && maxVal === '') {
    return `${minVal} ${unit}`;
  }
  // 两者都为空兜底
  return '';
};
onMounted(() => {
  fetchStepStatus();
  if (currentSelectedRow.value) {
    updateFormData(currentSelectedRow.value);
    getWarningList(currentSelectedRow.value);
  }
});

watch(
  currentSelectedRow,
  (newVal) => {
    console.log(newVal, 'newVal from store');
    if (newVal) {
      updateFormData(newVal);
      getWarningList(newVal);
    }
  },
  { deep: true }
);

const updateFormData = (row: any) => {
  console.log('更新表单数据:', row);
  // 在这里根据 row 数据更新表单或其他逻辑
  // 例如: formData.gzmc = row.alarmName;
};
</script>

<style lang="less" scoped>
@import '/@/design/theme.less';

.long-list1 {
  --image-box-bg: url('@/assets/images/home-container/configurable/hsq/2-5.png');
  --image-box-bg1: url('@/assets/images/home-container/configurable/hsq/2-13.png');
  --image-box-bg2: url('@/assets/images/home-container/configurable/hsq/2-14.png');
  --image-box-bg3: url('@/assets/images/home-container/configurable/hsq/2-15.png');
  --image-box-bg4: url('@/assets/images/home-container/configurable/hsq/2-16.png');
  --image-box-bg5: url('@/assets/images/home-container/configurable/hsq/2-17.png');
  --image-box-bg6: url('@/assets/images/home-container/configurable/hsq/2-24.png');

  position: relative;
  width: 100%;
  height: 100%;
  padding: 15px 30px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .basic-box {
    height: 30%;
    margin-top: 10px;
  }
  .basic-box1 {
    flex: 1;

    margin-top: 10px;
  }
  .basic-box2 {
    margin-top: 10px;
  }
  .edit-wrap {
    display: flex;
    width: 100%;
    height: 100%;
    gap: 10px;
    box-sizing: border-box;
  }

  .item-label {
    width: 80px;
    text-align: right;
    margin-right: 5px;
  }
  .edit-right {
    background: #0e395d;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    margin-right: 20px;
  }

  .right-title {
    display: flex;
    align-items: center;
    width: 146px;
    height: 30px;
    color: #fff;
    padding-left: 15px;
    background: var(--image-box-bg) no-repeat;
    background-size: 100% 100%;
    font-size: 15px;
    font-weight: bold;
    line-height: 24px;
  }

  .right-content {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 300px;
    overflow-y: auto;
  }

  .right-item-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 6px 20px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.06);
  }

  .right-item {
    font-size: 14px;
    color: #e0f2fe;
    white-space: nowrap;
  }

  /* 方案二：序号样式 */
  .index-label {
    color: #7dd3fc;
    font-weight: 600;
    min-width: 30px;
  }

  /* 方案三：内联样式 */
  .right-item.inline {
    display: inline-block;
  }
  .right-item {
    color: #29a1d4;
    font-size: 12px;
    line-height: 25px;
    font-weight: bold;
    padding: 0 5px;
  }
  .right-item:nth-child(4) {
    color: #fc002a;
  }
  .right-item:first-child {
    color: #fff;
  }
  .right-item:last-child {
    color: #46e8a3;
  }
  .list-title {
    width: 177px;
    height: 30px;
    line-height: 22px;
    padding-left: 5px;
    padding-bottom: 5px;
    color: #01fefc;
    font-weight: bolder;
    background: var(--image-box-bg) no-repeat;
    background-size: 80% 100%;
    margin-bottom: 5px;
  }

  .basic-table {
    height: 100%;
  }

  .item-label1 {
    width: 105px;
    text-align: right;
    margin-right: 5px;
  }
  .table-title {
    display: flex;
    align-items: center;
    height: 34px;
    background: var(--image-box-bg6) no-repeat;
    background-size: 100% 100%;
    margin-bottom: 6px;
  }

  .title-item {
    flex: 1;
    min-width: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #01fefc;
    white-space: nowrap;
    padding: 0 6px;
  }

  .table-content {
    height: calc(100% - 40px);
    overflow-y: auto;
  }

  .content-item {
    display: flex;
    align-items: center;
    height: 36px;
    &:nth-child(odd) {
      background-color: #0e3455;
    }
    &:nth-child(even) {
      background-color: #114268;
    }
  }

  .item-text {
    flex: 1;
    min-width: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    white-space: nowrap;
    padding: 0 6px;
  }

  .item-text1 {
    color: #fc002a;
  }
  .item-text2 {
    color: orange;
  }

  .switch {
    width: 36px;
    height: 18px;
    border-radius: 9px;
    background: none;
    position: relative;
    transition: all 0.2s ease;
    cursor: pointer;
    border: 1px solid #35d9e5;
    .switch-dot {
      width: 14px;
      height: 14px;
      border-radius: 50%;
      background: #fff;
      position: absolute;
      top: 1px;
      left: 1px;
      transition: all 0.2s ease;
    }
  }
  .switch.active .switch-dot {
    left: 20px;
  }

  .text-look {
    color: #01fefc;
    cursor: pointer;
  }
}

/*流程图*/
.workflow-container {
  width: 100%;
  padding: 20px 0;
  position: relative;
}

.steps-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 20px;
  position: relative;
  z-index: 2;
  &::before {
    content: '';
    position: absolute;
    left: 12%;
    right: calc(50% - 12px);
    top: 95%;
    transform: translateY(-50%);
    height: 2px;
    background: #4a9eff;
    z-index: 1;
  }
  &::after {
    content: '';
    position: absolute;
    left: calc(50% + 12px);
    right: 12%;
    top: 95%;
    transform: translateY(-50%);
    height: 2px;
    background: #4a9eff;
    z-index: 1;
  }
  .steps-arrow {
    position: absolute;
    left: 5%;
    top: 95%;
    transform: translate(-50%, -50%);
    width: 24px;
    height: 24px;
    background: url('@/assets/images/home-container/configurable/hsq/arrow.svg') center / contain no-repeat;
    z-index: 2;
  }
  .step-item:first-child .steps-arrow {
    display: none;
  }
}
.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  position: relative;
}

.step-icon {
  width: 56px;
  height: 56px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  margin-bottom: -6px;
}

.icon-alarm {
  background-image: url('@/assets/images/home-container/configurable/hsq/bjcf.png');
}
.icon-condition {
  background-image: url('@/assets/images/home-container/configurable/hsq/tjpd.png');
}
.icon-search {
  background-image: url('@/assets/images/home-container/configurable/hsq/czgz.png');
}
.icon-action {
  background-image: url('@/assets/images/home-container/configurable/hsq/zxdz.png');
}
.icon-log {
  background-image: url('@/assets/images/home-container/configurable/hsq/jlrz.png');
}

.step-frame {
  position: relative;
  width: 100%;
  max-width: 120px;
  padding: 2px;
  background-image: url('@/assets/images/home-container/configurable/hsq/3-6.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 5px;
}

.step-text {
  font-size: 14px;
  font-weight: 500;
  color: #fff;
  text-align: center;
  position: relative;
  z-index: 1;
}

.step-dot {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 3;
  background: transparent;
  width: 12px;
  height: 12px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #2c5a8c;
  border: 1px solid #4a9eff;
  transition: all 0.3s ease;
}
.dot.active {
  background: #4facfe;
  border-color: #4facfe;
  box-shadow: 0 0 6px rgba(79, 172, 254, 0.6);
}
</style>
