<template>
  <BasicModal v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="50%" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from './config.data';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { queryFlowInfoPage, submitExamine } from '/@/api/resource/examine';
import { session } from '/@/utils/Memory';
import { message } from 'ant-design-vue';

export default defineComponent({
  name: 'MenuDrawer',
  components: { BasicForm, BasicModal },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    const isUpdate = ref(true);
    let menuId;

    const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
      labelWidth: 100,
      schemas: formSchema,
      showActionButtonGroup: false,
      baseColProps: { lg: 12, md: 24 },
    });

    var ids = null;
    //默认传递页面数据
    const [registerDrawer, { setModalProps, closeModal }] = useModalInner(async (data) => {
      resetFields();
      setModalProps({ confirmLoading: false });
      isUpdate.value = !!data?.isUpdate;
      var defValue = {
        sqzys: data.sqzys.length,
        sqr: session.getItem("userInfo").EMPLOYEE.NAME,
      }
      setFieldsValue({ ...defValue });
      ids = data.sqzys;
    });

    //得到页面标题
    const getTitle = "提交申请";

    //提交按钮
    async function handleSubmit() {
      const value = await validate();
      queryFlowInfoPage({
        page: 1,
        rows: 1000000
      }).then(flowRes => {
        if (flowRes && flowRes.length) {
          var flow = flowRes.filter(i => i.FLOWNAME === '组件资源授权');
          if (flow.length && ids && ids.length) {
            let params = {
              bussInfo: {
                bussname: '组件资源授权',//业务名称
                flowid: flow[0].id,//流程id
                serverids: ids.toString(),//资源id
                ...value
              }
            }
            console.log("submitApply:", params)
            submitExamine(params).then(res => {
              if (res.resp_code === 0 && res.resp_msg === '提交成功') {
                message.success('申请成功')
                closeModal();
                emit('success');
              } else {
                message.error('申请失败')
              }
            })
          }
        }
      })
    }

    return { registerDrawer, registerForm, getTitle, handleSubmit };
  },
});
</script>
