<template>
  <CollapseItem name="数据设置" :expanded="true">
    <div style="overflow-x: auto">
      <n-table
        class="go-request-header-table-box"
        style="margin-top: 12px"
        :single-line="false"
        size="small"
      >
        <thead>
          <tr>
            <th>标题名称</th>
            <th>标题内容</th>
            <th>
              <n-button
                class="go-ml-2"
                type="primary"
                size="small"
                ghost
                @click="add(props.optionData.header.length - 1)"
              >
                +
              </n-button></th
            >
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in props.optionData.header" :key="index">
            <td>
              <n-input v-model:value="item.label" type="text" size="small" />
            </td>
            <td>
              <n-select
                v-model:value="item.value"
                multiple
                :options="datasetOptions"
                size="small"
              />
            </td>
            <td>
              <div style="width: 40px">
                <n-button
                  class="go-ml-2"
                  type="warning"
                  size="small"
                  ghost
                  :disabled="props.optionData.header.length === 1"
                  @click="remove(index)"
                >
                  -
                </n-button>
              </div>
            </td>
          </tr>
        </tbody>
      </n-table>
    </div>
  </CollapseItem>
  <CollapseItem name="样式设置" :expanded="true">
    <SettingItemBox name="整体设置">
      <SettingItem name="上方距离">
        <n-input-number
          v-model:value="optionData.dataStyle.marginTop"
          :min="0"
          size="small"
          placeholder="上方距离"
        />
      </SettingItem>
      <SettingItem name="左侧距离">
        <n-input-number
          v-model:value="optionData.dataStyle.marginLeft"
          :min="0"
          size="small"
          placeholder="左侧距离"
        />
      </SettingItem>
      <SettingItem name="单项上方距离">
        <n-input-number
          v-model:value="optionData.dataStyle.listMarginTop"
          :min="0"
          size="small"
          placeholder="单项上方距离"
        />
      </SettingItem>
      <SettingItem name="单项左侧距离">
        <n-input-number
          v-model:value="optionData.dataStyle.listMarginLeft"
          :min="0"
          size="small"
          placeholder="单项左侧距离"
        />
      </SettingItem>
      <SettingItem name="背景颜色">
        <n-color-picker
          size="small"
          :modes="['rgb']"
          v-model:value="optionData.dataStyle.backgroud"
        ></n-color-picker>
      </SettingItem>
    </SettingItemBox>

    <SettingItemBox name="标题设置">
      <SettingItem name="标题点颜色">
        <n-color-picker
          size="small"
          :modes="['rgb']"
          v-model:value="optionData.dataStyle.titleDotColor"
        ></n-color-picker>
      </SettingItem>
      <SettingItem name="标题点大小">
        <n-input-number
          v-model:value="optionData.dataStyle.titleDotSize"
          :min="0"
          size="small"
          placeholder="标题宽度"
        />
      </SettingItem>
      <SettingItem name="标题点与文字间的距离">
        <n-input-number
          v-model:value="optionData.dataStyle.titleDotMarginRight"
          :min="0"
          size="small"
          placeholder="标题宽度"
        />
      </SettingItem>
      <SettingItem name="标题宽度">
        <n-input-number
          v-model:value="optionData.dataStyle.titleWidth"
          :min="0"
          size="small"
          placeholder="标题宽度"
        />
      </SettingItem>
      <SettingItem name="标题高度">
        <n-input-number
          v-model:value="optionData.dataStyle.titleHeight"
          :min="0"
          size="small"
          placeholder="标题高度"
        />
      </SettingItem>
      <SettingItem name="标题字体颜色">
        <n-color-picker
          size="small"
          :modes="['hex']"
          v-model:value="optionData.dataStyle.titleFontcolor"
        ></n-color-picker>
      </SettingItem>
      <SettingItem name="标题字体大小">
        <n-input-number v-model:value="optionData.dataStyle.titleFontsize" :min="0" size="small" />
      </SettingItem>
      <SettingItem name="标题排列方式">
        <n-select
          v-model:value="optionData.dataStyle.titleAlign"
          size="small"
          :options="[
            { label: '左侧', value: 'left' },
            { label: '中间', value: 'center' },
            { label: '右侧', value: 'right' },
          ]"
        />
      </SettingItem>
    </SettingItemBox>

    <SettingItemBox name="内容设置">
      <SettingItem name="内容宽度">
        <n-input-number
          v-model:value="optionData.dataStyle.contentWidth"
          :min="0"
          size="small"
          placeholder="内容宽度"
        />
      </SettingItem>
      <SettingItem name="内容字体颜色">
        <n-color-picker
          size="small"
          :modes="['hex']"
          v-model:value="optionData.dataStyle.contentFontcolor"
        ></n-color-picker>
      </SettingItem>
      <SettingItem name="内容字体大小">
        <n-input-number
          v-model:value="optionData.dataStyle.contentFontsize"
          :min="0"
          size="small"
        />
      </SettingItem>
      <SettingItem name="内容排列方式">
        <n-select
          v-model:value="optionData.dataStyle.contentAlign"
          size="small"
          :options="[
            { label: '左侧', value: 'left' },
            { label: '中间', value: 'center' },
            { label: '右侧', value: 'right' },
          ]"
        />
      </SettingItem>
    </SettingItemBox>
  </CollapseItem>
</template>

<script setup lang="ts">
  import { PropType, watch, ref, computed } from 'vue';
  import { option } from './config';
  import { cloneDeep } from 'lodash-es';
  import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting';

  const props = defineProps({
    optionData: {
      type: Object as PropType<typeof option>,
      required: true,
    },
  });

  const datasetOptions = computed(() => {
    const result = Object.entries(option.dataset[0]).map(([key, value]) => ({
      label: key,
      value: key,
    }));
    return result;
  });

  // 新增
  const add = (index: number) => {
    props.optionData.header.splice(index + 1, 0, {
      label: '',
      value: [],
    });
  };

  // 减少
  const remove = (index: number) => {
    if (props.optionData.header.length !== 1) {
      props.optionData.header.splice(index, 1);
    }
  };
</script>
