<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';
import eventBus from '/@/utils/eventBus';

export default defineComponent({
  name: 'MenuDrawer',
  components: { BasicForm, BasicModal },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    const isUpdate = ref(true);
    const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
      labelWidth: 100,
      schemas: formSchema,
      showActionButtonGroup: false,
      baseColProps: { lg: 12, md: 24 },
    });

    var ids = null;
    var idDelete = 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,
        sqdw: session.getItem("userInfo").ROLES[0].COMPANY_ID,
        // sqdw: '44a67bb2-9f4b-4eb2-b297-c53775dc7fd2',
      }
      setFieldsValue({ ...defValue });
      ids = data.sqzys;
      idDelete = data.type;
      console.log("ids:", ids);
    });

    function judgeType(id) {
      var id = JSON.stringify(id);
      return id.indexOf('MR') > -1 ? '地图资源授权' : id.indexOf('ER') > -1 ? '场景资源授权' : '文件资源授权';
    }
    function getType(id) {
      var str = JSON.stringify(id);
      return str.indexOf('MR') > -1 ? 'MAP' : str.indexOf('ER') > -1 ? 'SCENE' : 'DR';
    }
    //得到页面标题
    const getTitle = "提交申请";
    //提交按钮
    async function handleSubmit() {
      const value = await validate();
      queryFlowInfoPage({
        page: 1,
        rows: 1000000
      }).then(flowRes => {
        if (flowRes && flowRes.length) {
          var type = judgeType(ids)
          const flow = flowRes.find(i => i.FLOWNAME === type);
          if (flow && ids && ids.length) {
            let params = {
              bussInfo: {
                bussname: type,//业务名称
                flowid: flow?.id,//流程id
                serverids: ids.toString(),//资源id
                ...value
              }
            }
            console.log("submitApply:", params)
            submitExamine(params, idDelete).then(res => {
              if (res.resp_code === 0 && res.resp_msg === '提交成功') {
                message.success('申请成功')
                eventBus.emit('platListCenter', { type: getType(ids) })
                closeModal();
                emit('success');
              } else {
                message.error('申请失败')
              }
            })
          } else {
            message.error('流程未配置，请联系管理员')
          }
        }
      })
    }

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