<template>
  <BasicModal v-bind="$attrs" @register="register" @ok="confirm" :title="title">
    <BasicForm @register="registerForm" :model="model" />
    <!-- <MenuTree /> -->
  </BasicModal>
</template>
<script lang="ts">

  import { Tree } from 'ant-design-vue';
  import { defineComponent, PropType, reactive, ref, toRefs, watch } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { adapt } from '/@/utils/adapt'
  import { formSchema,  } from './data'


  interface ModelData {
    title: string,
    treeData: object[],
    checkedKeys: string[],
    selectedKeys: string[],
    expandedKeys: string[]
  }
  interface Role {
    id: string|number
  }

  export default defineComponent({
    components: { BasicModal, BasicForm, [Tree.name]: Tree },
    props: {
      modelData: {
        type: Object as PropType<ModelData>,
        default: {},
      },
    },
    setup(props, { emit }) {
      const role = reactive<Role>({
        id:0
      })
      const modelData:ModelData = props.modelData
      console.log(modelData)
      const modelRef = ref({});
      const adaptWidth = adapt()
      watch(modelData, () => {
        console.log('expandedKeys', modelData.expandedKeys);
        console.log('selectedKeys', modelData.selectedKeys);
        console.log('checkedKeys', modelData.checkedKeys);
      });

      const schemas: FormSchema[] = formSchema

      const [
        registerForm,
        {
          getFieldsValue,
          // setProps
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const [register, { closeModal } ] = useModalInner((data) => {
        // 方式1
        // setFieldsValue({
        //   field2: data.data,
        //   field1: data.info,
        // });

        // 方式2
        modelRef.value = {
          username: data.username,
          nickname: data.nickname,
          status: data.status,
          menus: data.menus
          };

        // setProps({
        //   model:{ field2: data.data, field1: data.info }
        // })

        role.id = data.id
      });

      function confirm() {
        console.log('确定')
        const data = getFieldsValue()
        if(role.id) {
          data.id = role.id
        }
        // const data = {
        //   info,
          data.menus = [...modelData.checkedKeys]
        // }
        // console.log(data)
        // console.log(getFieldsValue())  // 表单数据
        // console.log('------ 菜单key ------')
        // console.log(modelData.checkedKeys)
        emit('saveData', data)
        closeModal()  // 关闭弹窗
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        confirm,
        adaptWidth,
        ...toRefs(modelData)
      };
    },
  });
</script>
<style lang='less'>
.ant-form-item-label {
  text-align: center !important;
}

.tree-label {
  width: 20.6%;
  margin-top: 8px;
  margin-bottom: 1em;
  text-align: center;
}

@media (max-width: 639px) {
  .ant-form-item-label {
  line-height: 2.5715 !important;
  text-align: center !important;
 }

  .tree-label {
  width: 33%;
  margin-top: 8px;
  margin-bottom: 1em;
  text-align: center;
}
}
</style>
