import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  ApprovalTemplateNodesNodeTypeEnum,
  ApprovalTemplatesTypeEnum,
} from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Approval.ApprovalTemplates.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="700px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`${props.title}`} />}
      onFinish={async (values) =>
        Apis.Approval.ApprovalTemplates.Store({
          ...values,
          is_enabled: true,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '业务类型',
          valueEnum: ApprovalTemplatesTypeEnum,
          required: true,
          colProps: { span: 24 },
        }),
        {
          key: 'name',
          title: '模板名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          key: 'code',
          title: '模板编码',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          valueType: 'formList',
          dataIndex: 'nodes',
          title: '设置审批节点',
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          formItemProps: {
            ...rulesHelper.array,
            wrapperCol: { span: 24 },
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                MyFormItems.EnumSelect({
                  key: 'node_type',
                  title: `类型`,
                  valueEnum: ApprovalTemplateNodesNodeTypeEnum,
                  colProps: { span: 5 },
                  formItemProps: {
                    ...rulesHelper.text,
                  },
                }),
                {
                  title: '节点名称',
                  key: 'name',
                  colProps: { span: 6 },
                  formItemProps: {
                    ...rulesHelper.text,
                  },
                },

                // MyFormItems.EnumSelect({
                //   key: 'approve_type',
                //   title: `审批方式`,
                //   valueEnum: ApprovalTemplateNodesApproveTypeEnum,
                //   colProps: { span: 5 },
                // }),

                {
                  valueType: 'dependency',
                  name: ['node_type'],
                  columns: ({ node_type }) => {
                    return [
                      Selects.Employees({
                        key: 'members',
                        title: `审批人员`,
                        colProps: { span: 13 },
                        fieldProps: {
                          mode: 'multiple',
                          maxCount:
                            node_type ===
                            ApprovalTemplateNodesNodeTypeEnum.Approver.value
                              ? 1
                              : 9,
                        },
                      }),
                    ];
                  },
                },
              ],
            },
          ],
        },
        {
          title: '备注',
          key: 'description',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
