<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #leader>
        <a-input-search v-model:value="groupLeaderName" placeholder="选择组领导">
          <template #enterButton>
            <a-button @click="openSelectModal" type="primary">选择</a-button>
          </template>
        </a-input-search>
      </template>
    </BasicForm>
    <a-modal
      width="60%"
      v-model:open="accountOpen"
      wrap-class-name="full-modal-children"
      title="分配组领导"
      @ok="accountHandleOk"
      :destroyOnClose="true"
    >
      <SelectAccount ref="accountRef" />
    </a-modal>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, reactive } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './role.data';
  import { addGroup, updateRole } from '@/api/demo/system';
  import { useMessage } from '@/hooks/web/useMessage';
  import { SelectAccount } from '@/components/SelectAccount';

  const { createMessage } = useMessage();
  const leaderName = ref('');
  defineOptions({ name: 'DeptModal' });
  const accountOpen = ref(false);
  const emit = defineEmits(['success', 'register']);

  const isUpdate = ref(true);
  const groupLeaderName = ref('');

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  const groupId = ref('');
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    groupId.value = '';
    groupLeaderName.value = '';
    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
      groupId.value = data.record.id;
      groupLeaderName.value = data.record.groupLeaderName;
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增组' : '编辑组'));

  async function handleSubmit() {
    try {
      const values = await validate();
      let query = values;
      const data = await addGroup(groupId.value, query);
      if (data) {
        setModalProps({ confirmLoading: true });
        // TODO custom api
        closeModal();
        emit('success');
        return createMessage.success(!unref(isUpdate) ? '新增成功' : '编辑成功');
      } else {
        return createMessage.error(!unref(isUpdate) ? '新增失败' : '编辑失败');
      }
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  const accountRef = ref<any>();
  function openSelectModal() {
    accountOpen.value = true;
  }
  function accountHandleOk() {
    const list = accountRef.value.getRow();
    if (list.length == 1) {
      groupLeaderName.value = list[0].name;
      accountOpen.value = false;
      setFieldsValue({ groupLeaderName: list[0].name, groupLeaderId: list[0].id });
    } else {
      return createMessage.error('请选择一个作为领导');
    }
  }
</script>
<style lang="less">
  .full-modal-children {
    .ant-modal {
      max-width: 100%;
    }

    .ant-modal-content {
      height: calc(80vh);
    }

    .ant-modal-body {
      height: 80%;
    }
  }
</style>
