{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "comms.inquiries",
  "version": 3,
  "app": "anfragen",
  "route": "/anfragen",
  "title": "Posteingang",
  "requires": [
    "comms.inquiries"
  ],
  "data": {
    "inquiries": {
      "source": "comms.inquiries.list"
    }
  },
  "actions": {
    "sendAll": {
      "handler": "comms.sendAll",
      "risk": "irreversible",
      "confirm": {
        "title": {
          "$join": [
            "Alle ",
            {
              "$count": "inquiries.items",
              "as": "r",
              "where": {
                "$and": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "pending"
                    ]
                  },
                  {
                    "$not": {
                      "$bind": "r.done"
                    }
                  }
                ]
              }
            },
            " vorbereiteten Antworten jetzt absenden?"
          ]
        },
        "body": "Bitte stelle sicher, dass du alle Antworten geprüft hast.",
        "consequences": {
          "$each": "inquiries.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          },
          "map": {
            "$text": "An {r.name} <{r.fromEmail}>"
          }
        },
        "confirmLabel": "Alle senden",
        "tone": "primary"
      },
      "success": "Alle Antworten gesendet",
      "next": {
        "$join": [
          "?screen=comms.inquiries&sent=",
          {
            "$each": "inquiries.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            },
            "map": {
              "$bind": "r.id"
            }
          },
          ",",
          {
            "$bind": "params.sent"
          },
          "&moved=",
          {
            "$bind": "params.moved"
          }
        ]
      }
    },
    "assign": {
      "handler": "comms.assignInquiry",
      "risk": "safe",
      "success": "Du bearbeitest diese Anfrage – das Team sieht es."
    },
    "close": {
      "handler": "comms.closeInquiry",
      "risk": "reversible",
      "success": "Ohne Antwort erledigt"
    }
  },
  "body": [
    {
      "component": "page-header",
      "media": "phone",
      "data": {
        "title": "Posteingang",
        "meta": {
          "$if": {
            "$count": "inquiries.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            }
          },
          "then": {
            "$join": [
              {
                "$count": "inquiries.items",
                "as": "r",
                "where": {
                  "$and": [
                    {
                      "$eq": [
                        {
                          "$bind": "r.status"
                        },
                        "pending"
                      ]
                    },
                    {
                      "$not": {
                        "$bind": "r.done"
                      }
                    }
                  ]
                }
              },
              " offen"
            ]
          },
          "else": "Alles beantwortet"
        }
      }
    },
    {
      "component": "alert",
      "if": {
        "$and": [
          {
            "$flag": "anfragen.overdue-warning"
          },
          {
            "$bind": "inquiries.overdue"
          }
        ]
      },
      "data": {
        "tone": "warning",
        "title": {
          "$bind": "inquiries.overdue.title"
        },
        "body": "Familien entscheiden sich oft für den Verein, der zuerst antwortet.",
        "link": {
          "label": "Jetzt beantworten",
          "href": {
            "$text": "?screen=comms.inquiry-reply&id={inquiries.overdue.id}&sent={params.sent}&moved={params.moved}"
          }
        }
      }
    },
    {
      "component": "page-header",
      "media": "wide",
      "data": {
        "title": "Posteingang",
        "meta": {
          "$join": [
            {
              "$count": "inquiries.items",
              "as": "r",
              "where": {
                "$and": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "pending"
                    ]
                  },
                  {
                    "$not": {
                      "$bind": "r.done"
                    }
                  }
                ]
              }
            },
            " ausstehend · ",
            {
              "$count": "inquiries.items",
              "as": "r",
              "where": {
                "$or": [
                  {
                    "$eq": [
                      {
                        "$bind": "r.status"
                      },
                      "sent"
                    ]
                  },
                  {
                    "$bind": "r.sent"
                  }
                ]
              }
            },
            " beantwortet"
          ]
        },
        "actions": {
          "$if": {
            "$gte": [
              {
                "$count": "inquiries.items",
                "as": "r",
                "where": {
                  "$and": [
                    {
                      "$eq": [
                        {
                          "$bind": "r.status"
                        },
                        "pending"
                      ]
                    },
                    {
                      "$not": {
                        "$bind": "r.done"
                      }
                    }
                  ]
                }
              },
              2
            ]
          },
          "then": [
            {
              "label": {
                "$join": [
                  "Alle senden (",
                  {
                    "$count": "inquiries.items",
                    "as": "r",
                    "where": {
                      "$and": [
                        {
                          "$eq": [
                            {
                              "$bind": "r.status"
                            },
                            "pending"
                          ]
                        },
                        {
                          "$not": {
                            "$bind": "r.done"
                          }
                        }
                      ]
                    }
                  },
                  ")"
                ]
              },
              "icon": "mail",
              "action": "sendAll"
            }
          ],
          "else": []
        }
      }
    },
    {
      "component": "alert",
      "media": "wide",
      "if": {
        "$gte": [
          {
            "$count": "inquiries.items",
            "as": "r",
            "where": {
              "$and": [
                {
                  "$eq": [
                    {
                      "$bind": "r.status"
                    },
                    "pending"
                  ]
                },
                {
                  "$not": {
                    "$bind": "r.done"
                  }
                }
              ]
            }
          },
          1
        ]
      },
      "data": {
        "tone": "neutral",
        "title": "Klicke auf eine Anfrage, um die Antwort zu prüfen und zu bearbeiten."
      }
    },
    {
      "component": "list",
      "if": {
        "$count": "inquiries.items",
        "as": "r",
        "where": {
          "$and": [
            {
              "$eq": [
                {
                  "$bind": "r.status"
                },
                "pending"
              ]
            },
            {
              "$not": {
                "$bind": "r.done"
              }
            }
          ]
        }
      },
      "data": {
        "label": "Ausstehende Anfragen",
        "items": {
          "$each": "inquiries.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          },
          "map": {
            "id": {
              "$bind": "r.id"
            },
            "title": {
              "$bind": "r.name"
            },
            "time": {
              "$bind": "r.time"
            },
            "meta": {
              "$bind": "r.subject"
            },
            "excerpt": {
              "$bind": "r.preview"
            },
            "avatar": {
              "$bind": "r.initials"
            },
            "tags": {
              "$bind": "r.tags"
            },
            "href": {
              "$text": "?screen=comms.inquiry-reply&id={r.id}&sent={params.sent}&moved={params.moved}"
            },
            "actions": {
              "$if": {
                "$flag": "anfragen.swipe"
              },
              "then": [
                {
                  "action": "assign",
                  "label": "Ich übernehme das",
                  "icon": "user"
                },
                {
                  "action": "close",
                  "label": "Ohne Antwort erledigen",
                  "icon": "check"
                }
              ]
            },
            "swipe": {
              "$if": {
                "$flag": "anfragen.swipe"
              },
              "then": {
                "start": {
                  "action": "assign",
                  "label": "Übernehmen",
                  "icon": "user",
                  "tone": "accent"
                },
                "end": {
                  "action": "close",
                  "label": "Erledigt",
                  "icon": "check",
                  "tone": "neutral"
                }
              }
            }
          }
        }
      }
    },
    {
      "component": "empty-state",
      "if": {
        "$not": {
          "$count": "inquiries.items",
          "as": "r",
          "where": {
            "$and": [
              {
                "$eq": [
                  {
                    "$bind": "r.status"
                  },
                  "pending"
                ]
              },
              {
                "$not": {
                  "$bind": "r.done"
                }
              }
            ]
          }
        }
      },
      "data": {
        "icon": "check-circle",
        "title": "Alles erledigt!",
        "text": "Keine neuen Schnuppertraining-Anfragen vorhanden. Sobald neue Anfragen eintreffen, erscheinen sie hier automatisch.",
        "action": {
          "label": "Gesendet",
          "href": "?screen=comms.sent"
        }
      }
    }
  ]
}
