<template>
  <div class="config-box">
    <div class="button-box">
      <div class="preview-btn"><PreviewTable :columns="formState.headerMergingData" /></div>
      <div class="add-btn" @click="addColumns">添加子列</div>
    </div>
    <div class="table-box">
      <div class="table-item head">
        <div
          class="item"
          :style="getWidthStyle(columnItem.width)"
          v-for="(columnItem, columnIndex) in formState.columns"
          :key="columnIndex"
          >{{ columnItem.title }}
        </div>
        <div class="icon-box" :style="getActionWidthStyle(100)">操作</div>
      </div>
      <template v-if="formState.headerMergingData && formState.headerMergingData.length > 0">
        <MergingDynamicTable
          :sourceData="sourceData"
          :data="formState.headerMergingData"
          :columns="formState.columns"
          @change-data-item="changeDataItem"
          @delete-data-item="deleteDataItem"
        />
      </template>
      <div class="kong-box" v-else> <EmptyBox /></div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { inject, onMounted, reactive } from 'vue';
  import { EmptyBox } from '/@/components/ModalPanel/index';
  import MergingDynamicTable from './MergingDynamicTable.vue';
  import PreviewTable from './Preview.vue';
  import { HeaderMergingColumnItem, ListHeaderMergingItem } from '/@/model/generator/listConfig';
  import { BasicColumn } from '/@/components/Table/src/types/table';
  import { cloneDeep } from 'lodash-es';
  import { GeneratorConfig } from '/@/model/generator/generatorConfig';
  const generatorConfig = inject<GeneratorConfig>('generatorConfig') as GeneratorConfig;

  const props = defineProps({
    sourceData: Array<BasicColumn>,
  });
  onMounted(() => {
    if (
      generatorConfig?.listConfig?.isHeaderMerging &&
      Array.isArray(generatorConfig?.listConfig?.headerMergingData)
    ) {
      formState.headerMergingData = generatorConfig?.listConfig?.headerMergingData;
    }
  });
  const formState = reactive({
    columns: [
      {
        title: '名称',
        dataIndex: 'title',
        key: 'title',
        width: 160,
      },
      {
        title: '字段',
        dataIndex: 'dataIndex',
        key: 'dataIndex',
        width: 120,
      },
    ] as Array<HeaderMergingColumnItem>,
    headerMergingData: [] as Array<ListHeaderMergingItem>,
  });
  function addColumns() {
    let key = formState.headerMergingData.length + '';
    formState.headerMergingData.push({
      key,
      title: '列名' + key,
      dataIndex: '',
      expand: true,
      children: [],
    });
  }

  function getWidthStyle(width) {
    return 'min-width:' + width + 'px;flex-basis:' + width + 'px;flex:1;';
  }
  function getActionWidthStyle(width: number) {
    return 'min-width:' + width + 'px;';
  }
  function changeDataItem(dataItem) {
    let key = dataItem.key + '-' + dataItem.children.length;
    let arr = dataItem.key.split('-'); //0 代表顶级  0-0 代表二级 0-0-0 代表三级 以此类推
    let index = arr[arr.length - 1];
    if (arr.length == 1) {
      formState.headerMergingData[index].children.push({
        key,
        title: '列名' + key,
        dataIndex: '',
        expand: true,
        children: [],
      });
    } else {
      const childArr = getChildArr(dataItem.key, arr);
      if (childArr[0].children.length > 0) {
        let lastKeyObj = childArr[0].children[childArr[0].children.length - 1];
        let arr = lastKeyObj.key.split('-');
        let numb = Number(arr[arr.length - 1]) + 1;
        let lastKey = dataItem.key + '-' + numb;
        childArr[0].children.push({
          key: lastKey,
          title: '列名' + lastKey,
          dataIndex: '',
          expand: true,
          children: [],
        });
      } else {
        childArr[0].children.push({
          key,
          title: '列名' + key,
          dataIndex: '',
          expand: true,
          children: [],
        });
      }
    }
  }
  function deleteDataItem(dataItem) {
    let arr = dataItem.key.split('-'); //0 代表顶级  0-0 代表二级 0-0-0 代表三级 以此类推
    let index = arr[arr.length - 1];
    if (arr.length == 1) {
      formState.headerMergingData.splice(index, 1);
    } else {
      const childArr = getChild(arr);
      childArr.splice(index, 1);
    }
  }

  function getChildArr(key, selectArray): Array<ListHeaderMergingItem> {
    let level = selectArray.length;
    let child: Array<ListHeaderMergingItem> = [];
    for (let i = 0; i < level - 1; i++) {
      if (child && child.length > 0) {
        child = child[selectArray[i]].children;
      } else {
        child = formState.headerMergingData[selectArray[i]].children;
      }
    }
    return child.filter((ele) => {
      return ele.key == key;
    });
  }
  function getChild(selectArray): Array<ListHeaderMergingItem> {
    let level = selectArray.length;
    let child: Array<ListHeaderMergingItem> = [];
    for (let i = 0; i < level - 1; i++) {
      if (child && child.length > 0) {
        child = child[selectArray[i]].children;
      } else {
        child = formState.headerMergingData[selectArray[i]].children;
      }
    }
    return child;
  }
  function getChildrenData(arr, mapData) {
    let val: BasicColumn[] = [];
    arr.forEach((element) => {
      if (element && element.children && element.children.length > 0) {
        element.children = getChildrenData(element.children, mapData);
      } else {
        element = { ...mapData.get(element.dataIndex), ...element, columnName: element.title };
      }
      val.push(element);
    });
    return val;
  }
  function handleSubmit() {
    if (generatorConfig?.listConfig?.isHeaderMerging) {
      let mapData = new Map();
      if (props.sourceData && Array.isArray(props.sourceData)) {
        props.sourceData.forEach((element) => {
          if (element && element.columnName) {
            mapData.set(element.columnName, {
              resizable: element.resizable,
              dataIndex: element.dataIndex,
              title: element.title,
              componentType: element.componentType,
              align: element.align,
              onFilter: element.onFilter,
              fixed: element.fixed,
              sorter: element.sorter,
              styleConfig: undefined,
              listStyle: '',
            });
          }
        });
      }
      let arr = getChildrenData(cloneDeep(formState.headerMergingData), mapData);
      return arr;
    } else {
      return [];
    }
  }
  defineExpose({
    handleSubmit,
  });
</script>

<style lang="less" scoped>
  .import-title {
    font-size: 16px;
    font-weight: bold;
    margin: 10px 0 20px;
  }

  .config-box {
    padding: 0 16px 0 0;
  }

  .button-box {
    display: flex;
    justify-content: flex-end;

    button {
      margin: 0 4px;
      font-size: 12px;
    }

    .add-btn {
      width: 94px;
      height: 32px;
      background: #1c8dff;
      border-radius: 4px;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-left: 16px;
    }
  }

  .table-box {
    width: 100%;
    margin-top: 20px;
    overflow-x: scroll;
    white-space: nowrap;
    height: 600px;

    .child1 {
      width: 100%;
      padding-left: 14px;
    }

    .table-item {
      width: 100%;
      display: flex;
      line-height: 50px;
      border-bottom: 1px solid #ececec;

      .item {
        height: 50px;
        color: #6e6a6e;
        font-size: 14px;
        margin: 0 4px;
        width: 120px;
        display: flex;
        // justify-content: center;
        padding-left: 20px;
        align-items: center;
      }

      .delete-icon {
        color: @clear-color;
        margin: 0 10px;
        font-size: 12px;
      }

      .jia-icon {
        color: @primary-color;
        font-weight: 700;
        margin: 0 10px;
        font-size: 12px;
      }
    }

    .head {
      background-color: #f9f9f9;
      border: none;
    }
  }

  .kong-box {
    margin: 10px 0;
    height: 200px;
  }

  .action-box {
    width: 100%;
    border: 1px dotted #d8d8d8;
    color: #65696b;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
  }

  .model-box {
    padding: 16px;
  }

  .tip-box {
    background: rgb(255 119 0 / 8%);
    border-radius: 4px;
    border: 1px solid #f70;
    font-size: 14px;
    color: #333;
    padding: 20px;
    margin-bottom: 120px;

    .tip-icon-box {
      display: flex;
      align-items: center;
      margin-bottom: 10px;

      .icon-box {
        width: 18px;
        height: 18px;
        background: #f70;
        border-radius: 50%;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .title-box {
        font-weight: bold;
        font-size: 16px;
        color: #333;
        margin-left: 10px;
      }
    }
  }

  .delete-box {
    font-size: 18px;
    font-weight: 700;
    color: #ee8d96;
    cursor: pointer;
  }

  .node-title {
    margin-bottom: 16px;
    padding-left: 0;
  }

  :deep(.table-box .head) {
    height: 50px;
    line-height: 50px;
    background: #f7fbff;
    border-radius: 0;
    border: 1px solid #ddd;
    font-weight: bold;
    font-size: 14px;
    color: #333;
  }
</style>
