window.InstanceDetailEnabledAmisRender = true;

if(!window.SteedosWorkflow){
    window.SteedosWorkflow = {}
}


window.SteedosWorkflow.InstanceTask = {
    getListViewSchema: (flow)=>{

        let crudDataFilter = "";

        let fields = flow?.form__expand?.current?.fields;

        if(fields){
            fields.forEach(function (field) {
                if (field.is_list_display) {
                    crudDataFilter = crudDataFilter + `
                        if (data.display !== 'split'){
                        crud.columns.push({
                            "name": "instance__expand.values.${field.code}",
                            "label": "${field.name || field.code}",
                            "width": "unset",
                            "type": "text",
                            "className": " whitespace-nowrap",
                            "static": true,
                            "options": null
                        });\r\n
                        }\r\n
                    `;
                }
            })
        }

        if(crudDataFilter){
            crudDataFilter = crudDataFilter + `
                return crud
            `;
        }


        return {
            "type": "steedos-object-listview",
            "objectApiName": "instance_tasks",
            "columnsTogglable": false,
            "filterVisible": true,
            "showHeader": true,
            "id": "u:d29cef0d1007",
            "showDisplayAs": true,
            "className": "h-full w-full instance-tasks-list",
            "hiddenColumnOperation": true,
            "formFactor": "LARGE",
            "headerToolbarItems": [
            {
                "type": "button",
                "label": "",
                "icon": "fa fa-bars",
                "className": "bg-white p-2 rounded border-gray-300 text-gray-500",
                "align": "left",
                "onEvent": {
                "click": {
                    "actions": [
                    {
                        "actionType": "custom",
                        "script": "document.querySelector('.instances-customPage').classList.toggle('instances-sidebar-open');"
                    }
                    ]
                }
                },
                "id": "u:115e270cae4d"
            }
            ],
            "listName": "inbox",
            "rowClassNameExpr": "<%=(data.is_read === false && (!data.clientIds || data.clientIds.length == 0 || !data.clientIds.includes(data._id))) ? 'unread-instance steedos-record-tr steedos-record-tr-'+data._id : 'steedos-record-tr steedos-record-tr-'+data._id %>",
            "crudDataFilter": crudDataFilter
        }
    }
}

window.SteedosWorkflow.Instance = {
    getListViewSchema: (flow)=>{
        let crudDataFilter = "";

        let fields = flow?.form__expand?.current?.fields;

        if(fields){
            fields.forEach(function (field) {
                if (field.is_list_display) {
                    crudDataFilter = crudDataFilter + `
                        if (data.display !== 'split'){
                        crud.columns.push({
                            "name": "instance__expand.values.${field.code}",
                            "label": "${field.name || field.code}",
                            "width": "unset",
                            "type": "text",
                            "className": " whitespace-nowrap",
                            "static": true,
                            "options": null
                        });\r\n
                        }\r\n
                    `;
                }
            })
        }

        if(crudDataFilter){
            crudDataFilter = crudDataFilter + `
                return crud
            `;
        };

        return {
          "type": "steedos-object-listview",
          "objectApiName": "instances",
          "columnsTogglable": false,
          "filterVisible": true,
          "showHeader": true,
          "id": "u:d29cef0d1007",
          "showDisplayAs": true,
          "className": "h-full w-full",
          "hiddenColumnOperation": true,
          "formFactor": "LARGE",
          "headerToolbarItems": [
            {
              "type": "button",
              "label": "",
              "icon": "fa fa-bars",
              "className": "bg-white p-2 rounded border-gray-300 text-gray-500",
              "align": "left",
              "onEvent": {
                "click": {
                  "actions": [
                    {
                      "actionType": "custom",
                      "script": "document.querySelector('.instances-customPage').classList.toggle('instances-sidebar-open');"
                    }
                  ]
                }
              },
              "id": "u:115e270cae4d"
            }
          ],
          "listName": "completed",
          "rebuildOn": "${flowId || categoryId}",
          "adaptor": "//在列表上加流程表单字段时，group和user需要特殊处理\nif (api.body.self.listName == \"monitor\") {\n  _.each(payload.data.rows, function (item, index) {\n    if (item.values) {\n      const form_fields = api.body.self.form_fields;\n      _.each(item.values, function (field, field_code) {\n        const form_field = _.find(form_fields, { code: field_code });\n        if (form_field && form_field.type == \"group\") {\n          if (form_field.is_multiselect) {\n            item.values[field_code] = field && field.length > 0 ? _.map(field, 'fullname').toString() : \"\";\n          } else {\n            item.values[field_code] = field ? field.fullname : \"\";\n          }\n        } else if (form_field && form_field.type == \"user\") {\n          if (form_field.is_multiselect) {\n            item.values[field_code] = field && field.length > 0 ? _.map(field, 'name').toString() : \"\";\n          } else {\n            item.values[field_code] = field ? field.name : \"\";\n          }\n        }\n      })\n    }\n  })\n}\n",
          "crudDataFilter": crudDataFilter
        }
    }
}