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';
let showInfo: any = [];
export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Approval.ApprovalTemplates.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      key={new Date().getTime()}
      wrapperCol={{ span: 24 }}
      width="700px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          Apis.Approval.ApprovalTemplates.Show({
            id: props.item?.id ?? 0,
          }).then((res: any) => {
            showInfo = res?.data?.approval_template_nodes;
            form.setFieldsValue({
              ...res?.data,
              nodes: res?.data?.approval_template_nodes?.map((item: any) => ({
                ...item,
                members: item?.approval_template_node_members?.map(
                  (member: any) => member?.company_employees_id,
                ),
              })),
            });
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Approval.ApprovalTemplates.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .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,
                  },
                },
                {
                  valueType: 'dependency',
                  name: ['node_type', 'name'],
                  columns: ({ node_type, name }) => {
                    return [
                      Selects.Employees({
                        key: 'members',
                        title: `审批人员`,
                        colProps: { span: 13 },
                        fieldProps: {
                          mode: 'multiple',
                          maxCount:
                            node_type ===
                            ApprovalTemplateNodesNodeTypeEnum.Approver.value
                              ? 1
                              : 9,
                          maxTagTextLength: 3,
                          labelRender: (res: any) => {
                            console.log(res, showInfo, name, '222');
                            if (res?.label) {
                              return res?.label;
                            } else {
                              return showInfo?.map((i: any) => {
                                if (i?.name === name) {
                                  return i?.approval_template_node_members?.map(
                                    (k: any) => {
                                      if (
                                        k?.company_employees_id === res?.value
                                      ) {
                                        return k?.company_employee?.name || '-';
                                      }
                                    },
                                  );
                                }
                              });
                            }
                          },
                        },
                      }),
                    ];
                  },
                },
              ],
            },
          ],
        },
        {
          title: '备注',
          key: 'description',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
