<template>
  <BasicModal v-bind="$attrs" @register="register" @ok="confirm" :title="title">
    <BasicForm @register="registerForm" :model="model" />
    <!-- <MenuTree /> -->
    <div style="display: flex;">
      <p class="tree-label">权限</p>
      <a-tree
      checkable
      :tree-data="fakeTree"
      v-model:expandedKeys="expandedKeys"
      v-model:selectedKeys="selectedKeys"
      v-model:checkedKeys="checkedKeys"
      >
        <!-- <template #title0010><span style="color: #1890ff;">sss</span></template> -->
      </a-tree>
    </div>
  </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 { fakeTree } 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[] = [
        {
            field: 'parent',
            label: '父级',
            component: 'TreeSelect',
            componentProps: {
            replaceFields: {
                title: 'title',
                key: 'id',
                value: 'id',
            },
            getPopupContainer: () => document.body,
            },
            labelWidth: adaptWidth.labelWidth,
            colProps: {
              span: adaptWidth.elContainer,
          }
        },
        {
          field: 'uname',
          component: 'Input',
          label: '名称',
          labelWidth: adaptWidth.labelWidth,
          colProps: {
            span: adaptWidth.elContainer,
          }
        },
        {
          field: 'status',
          label: '状态',
          component: 'RadioButtonGroup',
          defaultValue: 1,
          componentProps: {
            options: [
              { label: '启用', value: 1 },
              { label: '停用', value: 0 },
            ],
          },
          labelWidth: adaptWidth.labelWidth,
          colProps: {
            span: adaptWidth.elContainer,
          },
        },
      ];

      const [registerForm, { getFieldsValue, updateSchema, setFieldsValue, resetFields/* validate*/ }] = useForm({
        labelWidth: 100,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: { span: 24 },
      });
      const [register, { closeModal } ] = useModalInner(data => {
        console.log('-------------------data---------------')
        console.log(data)
        resetFields()
        // if (unref(isUpdate)) {
        //   setFieldsValue(data);
        // }
        setFieldsValue(data);
        updateSchema({
          field: 'parent',
          componentProps: { treeData: fakeTree },
        });
      });

      function confirm() {
        console.log('确定')
        const data = getFieldsValue()
        if(role.id) {
          data.id = role.id
        }
        // const data = {
        //   info,
        // }
        // console.log(data)
        // console.log(getFieldsValue())  // 表单数据
        // console.log('------ 菜单key ------')
        // console.log(modelData.checkedKeys)
        emit('saveData', data)
        closeModal()  // 关闭弹窗
      }
      return {
        register,
        schemas,
        fakeTree,
        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>
