<template>
  <DesignPropertiesBox v-if="data.show">
    <a-collapse v-model:activeKey="activeKey" ghost expandIconPosition="right">
      <a-collapse-panel key="1" :header="t('功能配置')">
        <PlusCircleFilled class="add-custom" @click="add" />
        <a-form-item
          v-if="data.info.config?.functions?.length > 0"
          :colon="false"
          labelAlign="left"
          :wrapperCol="{
            style: { paddingLeft: '10px', paddingRight: '10px' },
          }"
        >
          <div class="card-box" :key="data.renderKey">
            <div class="card-item" v-for="(item, index) in data.info.config.functions" :key="index">
              <a-form-item :label="t('绑定功能')" :label-col="{ style: { width: '112px' } }">
                <JumpMenu
                  v-model:value="item.id"
                  @name="
                    (val) => {
                      item.name = val;
                    }
                  "
                  @icon="
                    (val) => {
                      item.icon = val;
                    }
                  "
                  @path="
                    (val) => {
                      item.path = val;
                    }
                  "
                />
              </a-form-item>
              <a-form-item :label="t('绑定颜色')" :label-col="{ style: { width: '112px' } }">
                <SelectColor v-model:value="item.color" />
              </a-form-item>
              <div class="close-icon">
                <IconFontSymbol
                  icon="chongzhishibai"
                  style="fill: rgb(255 84 97 / 50%)"
                  :size="14"
                  @click="deleteFunction(index)"
                />
              </div>
            </div>
          </div>
        </a-form-item>
      </a-collapse-panel>
      <a-collapse-panel key="2" :header="t('大小定位')">
        <Location v-model:info="data.info" />
      </a-collapse-panel>
    </a-collapse>
  </DesignPropertiesBox>
</template>

<script setup lang="ts">
  import { onMounted, reactive, ref, watch } from 'vue';
  import { DesignPropertiesBox } from '/@/components/Desktop';
  import IconFontSymbol from '/@/components/IconFontSymbol/Index.vue';
  import { PlusCircleFilled } from '@ant-design/icons-vue';
  import { JumpMenu } from '/@/components/MenuSelect';
  import { modulesInfo } from '../config/info';
  import SelectColor from './SelectColor.vue';
  import Location from './collapse/Location.vue';
  import { ModulesInfo } from '/@/model/desktop/designer';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const props = withDefaults(
    defineProps<{
      info: ModulesInfo;
    }>(),
    {
      info: () => {
        return modulesInfo;
      },
    },
  );
  watch(
    () => props.info,
    (val) => {
      if (val) data.info = val;
    },
    {
      deep: true,
    },
  );
  const data: {
    show: boolean;
    info: ModulesInfo;
    renderKey: number;
  } = reactive({
    show: false,
    info: modulesInfo,
    renderKey: 0,
  });
  const activeKey = ref(['1', '2']);
  onMounted(() => {
    data.info = props.info;
    data.show = true;
  });
  function add() {
    data.info.config.functions.push({
      id: '',
      name: '',
      icon: '',
      path: '',
      color: '#5e95ff',
    });
  }
  function deleteFunction(index: number) {
    data.info.config.functions.splice(index, 1);
    data.renderKey++;
  }
</script>

<style lang="less" scoped></style>
