<!-- 赋值节点 -->

<template>
  <div class="p-2">
    <div v-loading="loading">
      <el-button type="primary" size="small" @click="addFormItem"> 增加赋值项 </el-button>
      <el-scrollbar max-height="400px">
        <div v-for="(card, cardIndex) in cards" :key="cardIndex" style="display: inline-block; margin-right: 15px">
          <el-scrollbar wrap-class="scrollbar-wrapper" :view-style="{ width: 'max-content' }">
            <div class="content" style="white-space: nowrap">
              <el-card>
                <template #header>
                  <div class="card-header">
                    <el-button circle size="small" type="primary">{{ cardIndex + 1 }}</el-button>
                    <el-text size="small">设置赋值结果</el-text>
                  </div>
                </template>
                <!-- 表单循环 -->
                <el-form ref="queryFormRef" :inline="true" label-width="10px">
                  <div v-for="(item, index) in card.formItems" :key="index" style="margin-top: 15px">
                    <el-row>
                      <el-form-item prop="rulesInputParams">
                        <el-select v-model="item.inputParams" size="small" placeholder="请选择变量" style="width: 140px">
                          <el-option
                            v-for="option in paramsList"
                            :key="option.fieldName"
                            :label="option.fieldDescription"
                            :value="option.fieldName"
                            style="width: 180px"
                          >
                            <span style="float: left">{{ option.fieldDescription }}</span>
                            <span style="float: right; font-size: 0px">
                              <el-tag>{{ option.parmsType }}</el-tag>
                            </span>
                          </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item prop="rulesComOperator">
                        <el-select v-model="item.comOperator" size="small" placeholder="判断符" disabled style="width: 140px">
                          <el-option
                            v-for="option in OperatorOptions"
                            :key="option.value"
                            :label="option.label"
                            :value="option.value"
                            style="width: 230px"
                          >
                            <span style="float: left">{{ option.label }}</span>
                            <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px"> {{ option.tag }} </span>
                          </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item prop="rulesDataType">
                        <el-select v-model="item.dataType" size="small" placeholder="类型" style="width: 140px">
                          <el-option v-for="option in DataTypeOptions" :key="option.value" :label="option.label" :value="option.value" />
                        </el-select>
                      </el-form-item>
                      <el-form-item v-if="item.dataType === 'variable'" prop="rulesOutSelect">
                        <el-select
                          v-model="item.outSelect"
                          size="small"
                          :multiple="item.comOperator === 'in' || item.comOperator === 'notIn'"
                          placeholder="请选择变量"
                          style="width: 140px"
                        >
                          <el-option
                            v-for="option in paramsList"
                            :key="option.fieldName"
                            :label="option.fieldDescription"
                            :value="option.fieldName"
                            style="width: 180px"
                          >
                            <span style="float: left">{{ option.fieldDescription }}</span>
                            <span style="float: right; font-size: 0px">
                            <el-tag>{{ option.parmsType }}</el-tag>
                          </span>
                          </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item v-if="item.dataType === 'fixed'" prop="rulesOutEntry">
                        <el-input v-model="item.outEntry" size="small" placeholder="请输入值" style="width: 140px" />
                      </el-form-item>
                      <el-button size="small" icon="Minus" circle @click="minusItem(cardIndex, index)"></el-button>
                    </el-row>
                    <el-row v-if="index !== card.formItems.length - 1">
                      <el-select v-model="card.connectors[index]" size="small" placeholder="连接符" style="width: 140px">
                        <el-option v-for="option in connectorOptions" :key="option.value" :label="option.label" :value="option.value" />
                      </el-select>
                    </el-row>
                  </div>
                </el-form>
              </el-card>
            </div>
          </el-scrollbar>
        </div>
      </el-scrollbar>
      <el-button icon="Check" v-if="showSaveFlag" type="primary" size="small" style="margin-left: 20px; margin-top: 10px" @click="saveCards">保存</el-button>
    </div>
    <el-divider></el-divider>
    <el-form :inline="true">
      <el-button size="small" circle type="primary" icon="Connection" />
      <el-text size="small">设置参数 </el-text>
      <el-button size="small" type="primary" icon="CirclePlusFilled">添加参数</el-button>
    </el-form>

    <el-table :data="paramsList">
      <el-table-column label="变量解释" align="center" prop="fieldDescription" />
      <el-table-column label="变量" align="center" prop="fieldName" />
      <el-table-column label="数据类型" align="center" prop="fieldType" />
      <el-table-column label="变量类型" align="center" prop="parmsType" />
    </el-table>
  </div>
</template>
<script setup name="EditsFiltersNodeForm" lang="ts">
import { Graph } from '@antv/x6';
import { connectorOptions, DataTypeOptions, nodeResualtOptions, OperatorOptions } from '@/api/rule/definition/Options';
import {
  getEditsRulesDecisionTreeDetail,
  getFiltersRulesDecisionTreeDetail,
  quryRulesParms,
  saveRulesDecisionTreeDetail
} from '@/api/rule/definition';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  nodeData: Object,
  graphData: Graph,
  rulesId: Object,
  showSave: Boolean
});

// 定义响应式数据
const formData = ref({});
const nodeGraph = ref(Graph);
const fromRulesId = ref(null);
const paramsList = ref([]);
const nodeResualt = ref('true');
const loading = ref(false);
const showSaveFlag = ref(true);
// 定义响应式数据
const cards = ref([
  {
    formItems: [
      {
        inputParams: '',
        comOperator: '=',
        dataType: '',
        outSelect: '',
        outEntry: ''
      }
    ]
  }
]);

async function updateFromProps() {
  loading.value = true;
  const { nodeData, graphData, rulesId, showSave } = props;
  formData.value = nodeData || {};
  (nodeGraph.value as unknown as Graph) = graphData;
  fromRulesId.value = rulesId;
  showSaveFlag.value = showSave;
  const res = await quryRulesParms(rulesId);
  paramsList.value = res.data;
  cards.value = [
    {
      formItems: [
        {
          inputParams: '',
          comOperator: '=',
          dataType: '',
          outSelect: '',
          outEntry: ''
        }
      ]
    }
  ];

  await getNodeParms();
}

async function getNodeParms() {
  const res = await getEditsRulesDecisionTreeDetail(fromRulesId.value, formData.value.id);
  if (res.code === 200 && res.data.nodeData) {
    cards.value = res.data.nodeData;
  }
  loading.value = false;
}

function addFormItem() {
  const newFormItem = {
    formItems: [
      {
        inputParams: '',
        comOperator: '=',
        dataType: '',
        outSelect: '',
        outEntry: ''
      }
    ]
  };
  cards.value.push(newFormItem);
}
async function saveCards() {
  const inEdges = (nodeGraph.value as unknown as Graph).getIncomingEdges(formData.value.id);
  const outEdges = (nodeGraph.value as unknown as Graph).getOutgoingEdges(formData.value.id);
  if (inEdges.length == 0 || outEdges.length == 0) {
    proxy?.$modal.msgError('请设置连接线');
    return;
  }
  const savedCards = JSON.stringify(cards.value);
  const res = await saveRulesDecisionTreeDetail(fromRulesId.value, 'assignment', formData.value.id, savedCards);
  if (res.code === 200) {
    proxy?.$modal.msgSuccess('保存成功');
  } else {
    proxy?.$modal.msgError(res.msg);
  }
}

function minusItem(cardIndex) {
  cards.value.splice(cardIndex, 1);
}

// 使用深度监听来捕捉 props 内部属性的变化
watch(
  () => ({ nodeData: props.nodeData, graph: props.graphData, rulesId: props.rulesId, showSave: props.showSave }),
  ({ nodeData, graph, rulesId, showSave }) => {
    updateFromProps();
  },
  { deep: true, immediate: true } // 添加 immediate 选项以确保首次渲染时也执行监听器
);
</script>

<style scoped lang="scss">
.scrollbar-wrapper {
  white-space: nowrap; /* 防止内容换行 */
}

.content {
  display: inline-block; /* 让内容在同一行显示 */
  padding: 10px;
}
</style>
