<template>
  <BasicModal v-bind="$attrs" @register="register" @ok="confirm" :title="title">
    <BasicForm @register="registerForm" :model="model" />
    <!-- <MenuTree /> -->
    <div class="tree-wrap">
      <p class="tree-label">权限</p>
      <a-tree
        checkable
        :tree-data="fakeTree"
        showLine
        defaultExpandAll
        :replace-fields="replaceFields"
      />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { Tree } from 'ant-design-vue';
  import { defineComponent, PropType, reactive, ref, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { schemas, fakeTree } from './data';

  interface PopupData {
    title: string;
    treeData: object[];
  }
  interface Role {
    id: string | number;
  }

  export default defineComponent({
    components: { BasicModal, BasicForm, [Tree.name]: Tree },
    props: {
      popupData: {
        type: Object as PropType<PopupData>,
        default: () => {},
      },
    },
    emits: ['saveData'],
    setup(props, { emit }) {
      const role = reactive<Role>({
        id: 0,
      });
      const popupData = props.popupData as PopupData;
      console.log(popupData);
      const modelRef = ref({});
      const replaceFields = {
        key: 'id',
      };

      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);
        // }
        if (data.id) {
          role.id = data.id;
        }
        setFieldsValue(data);
        updateSchema({
          field: 'pid',
          componentProps: { treeData: popupData.treeData },
        });
      });

      function confirm() {
        console.log('确定');
        const data = getFieldsValue();
        if (role.id) {
          data.id = role.id;
        }
        // delete data.pname;
        console.log(`data`, data);
        // const data = {
        //   info,
        // }
        // console.log(data)
        // console.log(getFieldsValue())  // 表单数据
        // console.log('------ 菜单key ------')
        // console.log(popupData.checkedKeys)
        const popupData = { closeModal, data };
        emit('saveData', popupData);
      }
      return {
        register,
        schemas,
        registerForm,
        replaceFields,
        model: modelRef,
        confirm,
        fakeTree,
        ...toRefs(popupData),
      };
    },
  });
</script>
<style lang="less">
  .ant-form-item-label {
    text-align: center !important;
  }

  .tree-wrap {
    display: flex;
  }

  .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>
