<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.scenes.moveScene')"
    width="600px"
    :minHeight="400"
    :centered="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div class="pt-20px">
      <div class="ScreahNmae">
        <a-input
          class="ScreahInput"
          v-model:value="selectData.title"
          :placeholder="t('routes.scenes.moveSceneTitle')"
        />
      </div>
      <div class="treeList">
        <Tree
          class="draggable-tree"
          v-model:selectedKeys="selectedKeys"
          :tree-data="treeData"
          show-icon
          default-expand-all
          >
          <template #icon>
            <!-- <i class="ph:folder-open-fill"></i> -->
            <img src="/resource/img/file.png" style="padding: 3px 6px 0 0" alt="" />
            <!-- <FolderOpenOutlined /> -->
          </template>
          </Tree>
      </div>
    </div>
    <template #centerFooter>
      <!-- <a-button>xxxx</a-button> -->
    </template>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { FolderOpenOutlined } from '@ant-design/icons-vue';
  import { Tree } from 'ant-design-vue';
  import { folderMoveScene, folderTree } from '/@/api/scene/list';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import type { TreeProps } from 'ant-design-vue';
  const { t } = useI18n();
  const { createMessage } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm, Tree, FolderOpenOutlined },
    props: {
      userData: { type: Object },
    },
    emits: ['register', 'success', 'cancel'],
    setup(_, { emit }) {

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      const selectData = ref({
        account: '',
        type: '',
        title: '',
        numList: [],
      });
      const expandedKeys = ref<string[]>(['0-0', '0-1']);
      const selectedKeys = ref<string[]>([]);
      const treeData: TreeProps['treeData'] = ref([]);
      const checked = ref<boolean>(false);
      const headerInfo = reactive<Recordable>({});
      const searchInfo = reactive<Recordable>({});

      async function onDataReceive(data: any) {
        let tree = await folderTree({ type: data.type });

        treeData.value = [{
          title: t('common.root'),
          key: null,
          children: getTreeData(tree)
        }]
        selectData.value.type = Number(data.type);
        selectData.value.numList = data.numList;
      }

      const handleSelect = async () => {
        // const keys = getSelectRowKeys();
        // console.log('key', key);
      };
      const handleChange = (val, row) => {
        console.log('val,row', val, row);
      };
      const handleSubmit = async () => {
        console.log('selectData', selectedKeys);
        if(selectedKeys.value && selectedKeys.value.length  == 0) {
          return createMessage.error(t('routes.scenes.pleaseFloder'));
        }
        try {
          await folderMoveScene({...selectData.value, folderId: selectedKeys.value[0] });
          createMessage.success(t('common.optSuccess'));
          closeModal();
          emit('success');

          setTimeout(() => {
            createMessage.destroy();
          }, 3000);
        } catch (error) {
          console.error('error', error);
        }
      };
      const getTreeData = (tree = []) => {
        return tree.map((item) => {
          return {
            ...item,
            title: item.name,
            key: item.id,
            children: getTreeData(item.childrenList),
          };
        });
      };
      const handleCancel = async () => {
        selectData.value.account = '';
        selectData.value.tableType = NaN;
        selectData.value.numList = [];
        removeSchemaByField('cameraId');
        appendSchemaByField(schemas[1], 'cameraId');
        resetFields();
        emit('cancel');
      };

      return {
        register,
        searchInfo,
        closeModal,
        handleSelect,
        headerInfo,
        handleChange,
        t,
        checked,
        selectData,
        handleSubmit,
        handleCancel,
        expandedKeys,
        selectedKeys,
        treeData,
      };
    },
  });
</script>
<style lang="less">
  .ScreahNmae {
    .ScreahInput {
      height: 40px;
      line-height: 40px;
    }
  }
  .treeList {
    min-height: 350px;
    padding: 5px 20px;
    max-height: 350px;
    overflow-y: scroll;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-top: none;
    ::-webkit-scrollbar {
      display: none; /* Chrome Safari */
    }
    .ant-tree .ant-tree-treenode{
      padding: 5px 0;
    }
  }
</style>
