name: instance_distribute
is_enable: true
label: 分发
'on': record_only
type: amis_button
visible: true
amis_schema: |-
  {
    "type": "service",
    "body": [
      {
        "type": "button",
        "label": "分发",
        "id": "u:instance_distribute",
        "className": "  ",
        "onEvent": {
          "click": {
            "actions": [
              {
                "actionType": "dialog",
                "dialog": {
                  "type": "dialog",
                  "size": "lg",
                  "showLoading": true,
                  "title": {
                    "type": "tpl",
                    "id": "u:0bce3c33b6e5",
                    "tpl": "<p>分发<a href=\"https://www.steedos.cn/docs/user/workflow#%E5%A6%82%E4%BD%95%E5%88%86%E5%8F%91\" target=\"_blank\" rel=\"noopener\"> ?&nbsp;</a></p>"
                  },
                  "body": [
                    {
                      "type": "tpl",
                      "tpl": "<p>将当前选中的文件分发到新的流程，继续执行审批操作</p>",
                      "inline": false,
                      "id": "u:11bcfab80ce9",
                      "className": "m-b-sm"
                    },
                    {
                      "type": "form",
                      "title": "表单",
                      "debug": false,
                      "body": [
                        {
                          "type": "steedos-select-flow",
                          "label": "流程名称",
                          "name": "flow_id",
                          "id": "instanceForwardFlow",
                          "multiple": false,
                          "mode": "tree-select",
                          "required": true,
                          "options": [],
                          "searchable": true,
                          "amis": {
                            "onEvent":{
                                "change": {
                                "weight": 0,
                                "actions": [
                                  {
                                    "args": {
                                      "options": {},
                                      "api": {
                                        "url": "${context.rootUrl}/graphql",
                                        "method": "post",
                                        "messages": {},
                                        "dataType": "json",
                                        "headers": {
                                          "Authorization": "Bearer ${context.tenantId},${context.authToken}"
                                        },
                                        "data": {
                                          "eData": "${event.data}"
                                        },
                                        "requestAdaptor": "console.log('====', api);\nconst { flow_id } = api.data.eData\napi.data = {\n  query: \n  `{\n    flow: flows__findOne(id:\"${flow_id}\"){\n      distribute_optional_users,distribute_to_self\n    }\n  }`\n}\nreturn api;\n"
                                      }
                                    },
                                    "outputVar": "responseResult",
                                    "actionType": "ajax",
                                    "expression": "${!!event.data.flow_id}"
                                  },
                                  {
                                    "componentId": "u:742f9c0dc8a1",
                                    "args": {
                                      "value": {
                                        "distribute_optional_users": "${event.data.flow.distribute_optional_users|pick:id}",
                                        "distribute_to_self": "${event.data.flow.distribute_to_self}"
                                      }
                                    },
                                    "actionType": "setValue"
                                  },
                                  {
                                    "componentId": "u:c3ad733db365",
                                    "actionType": "visibility",
                                    "args": {
                                      "value": "${!event.data.flow.distribute_optional_users || event.data.flow.distribute_optional_users.length == 0}"
                                    }
                                  },
                                  {
                                    "componentId": "u:c3ad733db3602",
                                    "actionType": "visibility",
                                    "args": {
                                      "value": "${event.data.flow.distribute_optional_users.length > 0}"
                                    }
                                  },
                                  {
                                    "actionType": "custom",
                                    "script": "//先控制字段显隐后,再给赋值,存在值上不去的情况. \n setTimeout(function(){doAction({\"componentId\":\"u:c3ad733db3602\",\"args\":{\"value\":\"${event.data.flow.distribute_optional_users|pick:id}\"},\"actionType\":\"setValue\",\"expression\":\"${event.data.flow.distribute_optional_users.length == 1}\"})}, 300)"
                                  },
                                  {
                                    "componentId": "u:c3ad733db3602",
                                    "args": {
                                      "value": []
                                    },
                                    "actionType": "setValue",
                                    "expression": "${!event.data.flow.distribute_optional_users || event.data.flow.distribute_optional_users.length != 1}"
                                  }
                                ]
                              }
                            }
                          },
                          "onlyLeaf": true,
                          "action": "distribute",
                          "distributeInstanceId": "${record._id}",
                          "distributeStepId": "${distributeStepId}"
                        },
                        {
                          "type": "steedos-select-user",
                          "label": "分发对象",
                          "name": "users",
                          "id": "u:c3ad733db365",
                          "multiple": true,
                          "requiredOn":"${distribute_to_self != true || instance_distribute_to_self != true}",
                          "visible": false
                        },
                        {
                          "type": "steedos-select-user",
                          "label": "分发对象",
                          "name": "users",
                          "id": "u:c3ad733db360",
                          "multiple": true,
                          "requiredOn":"${distribute_to_self != true || instance_distribute_to_self != true}",
                          "idsDependOn": "distribute_optional_users",
                          "amis": {
                               "id": "u:c3ad733db3602",
                                "visible": false,
                               "source": {
                                  "method": "post",
                                  "url": "${context.rootUrl}/service/api/standard-ui/getUsersName",
                                  "requestAdaptor": "api.data={userIds: api.data.$self?.distribute_optional_users || [] }; return api;",
                                  "headers": {
                                    "Authorization": "Bearer ${context.tenantId},${context.authToken}"
                                  },
                                  "data": {
                                    "orderBy": "${orderBy}",
                                    "orderDir": "${orderDir}",
                                    "pageNo": "${page}",
                                    "pageSize": "${perPage}",
                                    "$self": "$$",
                                    "keywords": "${keywords}",
                                    "ids": "${ids}"
                                  }
                                }
                          }
                        },
                        {
                          "type": "textarea",
                          "label": "备注",
                          "name": "description",
                          "id": "u:1dc7e49e6e7d",
                          "minRows": 3,
                          "maxRows": 20
                        },
                        {
                          "type": "checkbox",
                          "name": "instance_distribute_to_self",
                          "id": "u:da17af0371f82",
                          "option": "同时分发给自己",
                          "hiddenOn": "${distribute_to_self != true}"
                        },
                        {
                          "type": "checkbox",
                          "name": "saveInstanceToAttachment",
                          "id": "u:da17af0371f8",
                          "option": "将原表单存储为附件",
                          "hiddenOn": "${enable_distribute_instance_to_attachment != true}"
                        },
                        {
                          "type": "checkbox",
                          "name": "instance_related",
                          "id": "u:da17af0371f9",
                          "option": "自动创建关联文件",
                          "hiddenOn": "${enable_distribute_instance_related != true}"
                        }
                      ],
                      "id": "u:742f9c0dc8a1",
                      "mode": "normal",
                      "initApi": {
                        "url": "${context.rootUrl}/graphql",
                        "method": "post",
                        "dataType": "json",
                        "data": {
                          "&": "$$",
                          "record": "${record}",
                          "context": "${context}"
                        },
                        "requestAdaptor": "api.data={query: '{flows__findOne(id: \"'+api.body.record.flow+'\"){enable_distribute_instance_to_attachment, enable_distribute_instance_related}}'}; return api;",
                        "adaptor": "console.log('payload', payload);payload.data=payload.data.flows__findOne; \nreturn payload;",
                        "headers": {
                          "Authorization": "Bearer ${context.tenantId},${context.authToken}"
                        }
                      },
                      "api": {
                        "url": "${context.rootUrl}/api/workflow/v2/instance/forward",
                        "method": "post",
                        "dataType": "json",
                        "data": {
                          "&": "$$",
                          "record": "${record}",
                          "context": "${context}"
                        },
                        "requestAdaptor": "console.log('====', api);\nlet { record, flow_id, description, context: contextData, users, saveInstanceToAttachment, instance_related, instance_distribute_to_self } = api.data;if(!users){users=[]}; if(instance_distribute_to_self){users.push(api.data.context.userId);} \nvar from_approve_id = null;\nif (record.approve && record.approve._id) {\n  from_approve_id = record.approve._id\n} else if (window.InstanceManager && window.InstanceManager.getCurrentApprove()?._id) {\n  from_approve_id = window.InstanceManager.getCurrentApprove()._id\n}; \napi.data = {\n  \"instance_id\": record._id,\n  \"space_id\": record.space,\n  \"flow_id\": flow_id,\n  \"hasSaveInstanceToAttachment\": saveInstanceToAttachment || false,\n  \"description\": description,\n  \"isForwardAttachments\": true,\n  \"selectedUsers\": _.uniq(users),\n  \"action_type\": \"distribute\",\n  \"related\": instance_related || false,\n  \"from_approve_id\": from_approve_id\n};\nreturn api;",
                        "adaptor": "console.log('payload', payload);\nreturn payload;",
                        "headers": {
                          "Authorization": "Bearer ${context.tenantId},${context.authToken}"
                        },
                        "messages": {
                          "success": "分发成功!",
                          "failed": "分发失败"
                        }
                      },
                      "onEvent": {
                        "submitSucc": {
                          "actions": [
                            {
                              "componentId": "",
                              "args": {},
                              "actionType": "closeDialog"
                            }
                          ]
                        },
                        "validateError": {
                          "actions": [
                            {
                              "componentId": "u:f7f767bed23f",
                              "actionType": "enabled"
                            }
                          ]
                        }
                      }
                    }
                  ],
                  "id": "u:519ca64b1934",
                  "actions": [
                    {
                      "type": "button",
                      "label": "取消",
                      "onEvent": {
                        "click": {
                          "actions": [
                            {
                              "componentId": "",
                              "args": {},
                              "actionType": "closeDialog"
                            }
                          ]
                        }
                      },
                      "id": "u:1d0d136fe2f0"
                    },
                    {
                      "type": "button",
                      "label": "确定",
                      "onEvent": {
                        "click": {
                          "actions": [
                            {
                              "args": {},
                              "actionType": "validate",
                              "componentId": "u:742f9c0dc8a1"
                            },
                            {
                              "componentId": "u:f7f767bed23f",
                              "actionType": "disabled"
                            },
                            {
                              "componentId": "u:742f9c0dc8a1",
                              "args": {},
                              "actionType": "submit"
                            }
                          ]
                        }
                      },
                      "id": "u:f7f767bed23f",
                      "level": "primary"
                    }
                  ]
                }
              }
            ],
            "weight": 0
          }
        }
      }
    ],
    "regions": [
      "body"
    ],
    "data": {},
    "bodyClassName": "p-0",
    "id": "u:c826d759cf96"
  }