<template>
  <div class="list-box" v-if="showPanel">
    <div class="opr-box">
      <div class="button-box">
        <a-button type="primary" size="small" @click.stop="add" class="!rounded">{{
          t('添加事件')
        }}</a-button>
      </div>
    </div>
    <div :class="prefixCls" v-if="formInfo.paramConfigs">
      <a-table
        :dataSource="formInfo.paramConfigs"
        :columns="configColumns"
        :pagination="false"
        :key="tableKey"
        size="middle"
        :class="`table-box`"
        ><template #headerCell="{ column }">
          <div class="header-cell__custom-title">{{ column.title }}</div>
        </template>
        <template #bodyCell="{ column, record, index }">
          <!-- <template v-if="column.key === 'sort'">
            <svg
              :class="['icon', `draggable-icon`]"
              aria-hidden="true"
              style="cursor: move; display: inline-block"
            >
              <use xlink:href="#icon-paixu" />
            </svg>
          </template> -->
          <template v-if="column.key === 'formulaContent'">
            <SelectFormulaContent
              v-model:formulaContent="record.formulaContent"
              v-model:variableAssignmentConfigs="record.variableAssignmentConfigs"
            />
          </template>
          <template v-if="column.key === 'point'">
            <a-select
              v-model:value="record.point"
              :placeholder="t('请选择指向')"
              style="width: 120px"
            >
              <a-select-option
                style="font-size: 12px"
                v-for="item in pointOptions"
                :key="item.id"
                :value="item.id"
              >
                {{ item.label ? item.label : item.id }}
              </a-select-option>
            </a-select>
          </template>
          <template v-if="column.key === 'operation'">
            <div class="text-[#FF5461]" @click.stop="deleteItem(index)">删除</div>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, ref } from 'vue';
  // import { nextTick, onMounted, ref, watch } from 'vue';
  // import Sortable from 'sortablejs';
  import { randomNum } from '/@bpmn/util/random';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDesign } from '/@/hooks/web/useDesign';
  import useStateInfo from './../../hooks/useStateInfo';
  // import { isNullAndUnDef } from '/@/utils/is';
  import SelectFormulaContent from './SelectFormulaContent.vue';
  // import { cloneDeep } from 'lodash-es';
  import { SelectNodeResultsType } from '/@/model/ruleProcessConfig';
  import { Edge } from '@vue-flow/core';
  const { showPanel, formInfo, getNextEdges } = useStateInfo();
  const { prefixCls } = useDesign('basic-table');
  const { t } = useI18n();
  const configColumns = [
    // {
    //   title: t('排序'),
    //   dataIndex: 'sort',
    //   key: 'sort',
    //   align: 'center',
    //   width: 80,
    // },
    {
      title: t('条件公式'),
      dataIndex: 'formulaContent',
      key: 'formulaContent',
      width: 180,
      align: 'center',
    },
    {
      title: t('指向'),
      dataIndex: 'point',
      key: 'point',
      width: 120,
      align: 'center',
    },

    {
      title: t('操作'),
      dataIndex: 'operation',
      key: 'operation',
      width: 88,
      align: 'center',
    },
  ];
  const tableKey = ref(0);
  // watch(
  //   () => formInfo.value?.paramConfigs,
  //   (val) => {
  //     if (val && val.length) {
  //       nextTick(() => {
  //         const tbody: any = document.querySelector(`.table-box .ant-table-tbody`);
  //         Sortable.create(tbody, {
  //           handle: `.draggable-icon`,
  //           onEnd: ({ oldIndex, newIndex }) => {
  //             if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || newIndex === oldIndex) {
  //               return;
  //             }
  //             const columns = cloneDeep(formInfo.value.paramConfigs);
  //             const oldColumns = cloneDeep(formInfo.value.paramConfigs);
  //             columns[newIndex] = oldColumns[oldIndex];
  //             columns[oldIndex] = oldColumns[newIndex];
  //             formInfo.value.paramConfigs = cloneDeep(columns);
  //             tableKey.value++;
  //           },
  //         });
  //       });
  //     }
  //   },
  //   {
  //     deep: true,
  //     immediate: true,
  //   },
  // );
  let pointOptions = ref([] as Edge[]);

  onMounted(() => {
    pointOptions.value = getNextEdges();
  });
  // 新增
  function add() {
    formInfo.value.paramConfigs.push({
      id: randomNum(),
      type: SelectNodeResultsType.API,
      point: '',
      value: '',
    });
  }
  //删除
  function deleteItem(index: number) {
    formInfo.value.paramConfigs.splice(index, 1);
  }
</script>

<style lang="less" scoped>
  .list-box {
    .opr-box {
      display: flex;
      justify-content: flex-end;
      margin: 0 10px 10px;
    }
  }

  .delete-icon {
    color: @clear-color;
  }

  .pop-desc {
    font-size: 12px;
    color: rgb(0 0 0 / 40%);
  }

  .icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentcolor;
    overflow: hidden;
  }
</style>
