{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "athlete.plans-plan",
  "version": 1,
  "app": "athlete",
  "route": "/plaene/:id",
  "title": {
    "$bind": "plan.title"
  },
  "data": {
    "plan": {
      "source": "athlete.plans.get",
      "params": {
        "id": {
          "$bind": "params.id"
        }
      }
    }
  },
  "chrome": {
    "title": {
      "$bind": "plan.title"
    },
    "back": {
      "label": "Pläne",
      "href": "?screen=athlete.plans"
    }
  },
  "actions": {
    "done": {
      "handler": "athlete.plans.tickSet",
      "risk": "safe",
      "celebrate": {
        "kind": "small",
        "style": {
          "$if": {
            "$flag": "athlete.calm-celebrations"
          },
          "then": "calm",
          "else": "full"
        },
        "pref": {
          "$flag": "athlete.celebration-setting"
        },
        "points": 10
      },
      "next": {
        "$text": "?screen=athlete.plans-plan&id={params.id}&completed={params.completed}&{target.id}=done"
      }
    },
    "complete": {
      "handler": "athlete.plans.complete",
      "risk": "safe",
      "success": "Plan als fertig markiert.",
      "celebrate": {
        "kind": "huge",
        "style": {
          "$if": {
            "$flag": "athlete.calm-celebrations"
          },
          "then": "calm",
          "else": "full"
        },
        "pref": {
          "$flag": "athlete.celebration-setting"
        }
      },
      "next": {
        "$text": "?screen=athlete.plans-plan&id={params.id}&e8={params.e8}&completed=1"
      }
    },
    "pdf": {
      "handler": "athlete.plans.pdf",
      "risk": "safe",
      "success": "PDF gespeichert."
    }
  },
  "body": [
    {
      "component": "page-header",
      "data": {
        "title": {
          "$bind": "plan.title"
        }
      }
    },
    {
      "layout": "card",
      "children": [
        {
          "layout": "cluster",
          "grow": "last",
          "children": [
            {
              "component": "ring",
              "data": {
                "value": {
                  "$bind": "plan.pct"
                },
                "label": {
                  "$text": "Plan zu {plan.pct} Prozent erledigt"
                }
              }
            },
            {
              "layout": "cluster",
              "children": [
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$join": [
                        {
                          "$count": "plan.exercises",
                          "as": "x",
                          "where": {
                            "$bind": "x.done"
                          }
                        },
                        "/",
                        {
                          "$count": "plan.exercises"
                        }
                      ]
                    }
                  }
                },
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$text": "{plan.points} P."
                    },
                    "kind": "points"
                  }
                },
                {
                  "component": "badge",
                  "if": {
                    "$bind": "params.completed"
                  },
                  "data": {
                    "label": "fertig",
                    "tone": "success"
                  }
                },
                {
                  "component": "button",
                  "data": {
                    "label": "PDF",
                    "icon": "download",
                    "size": "sm",
                    "variant": "ghost",
                    "action": "pdf"
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "layout": "details",
      "title": {
        "$bind": "plan.setTitle"
      },
      "aside": {
        "$join": [
          {
            "$count": "plan.exercises"
          }
        ]
      },
      "open": true,
      "children": [
        {
          "component": "list",
          "data": {
            "label": {
              "$bind": "plan.setTitle"
            },
            "flush": true,
            "items": {
              "$each": "plan.exercises",
              "as": "ex",
              "map": {
                "id": {
                  "$bind": "ex.id"
                },
                "title": {
                  "$bind": "ex.title"
                },
                "meta": {
                  "$bind": "ex.scheme"
                },
                "href": {
                  "$text": "#sheet-{ex.id}"
                },
                "done": {
                  "$bind": "ex.done"
                },
                "quickAction": {
                  "action": "done",
                  "label": "Fertig",
                  "icon": "check"
                }
              }
            }
          }
        }
      ]
    },
    {
      "each": "plan.exercises",
      "as": "ex",
      "node": {
        "component": "sheet",
        "data": {
          "id": {
            "$text": "sheet-{ex.id}"
          },
          "title": {
            "$bind": "ex.title"
          },
          "meta": {
            "$bind": "ex.sheetMeta"
          },
          "paragraphs": [
            {
              "$bind": "ex.description"
            }
          ]
        }
      }
    },
    {
      "component": "button",
      "if": {
        "$not": {
          "$bind": "params.completed"
        }
      },
      "data": {
        "label": "Plan abschliessen",
        "variant": "primary",
        "block": "always",
        "action": "complete"
      }
    }
  ]
}
