//-/////////////////////////////////////////////////////////////////////////////
//-                                                                           //
//-               This file is part of the Buildbotics firmware.              //
//-                                                                           //
//-      Copyright (c) 2015 - 2021, Buildbotics LLC, All rights reserved.     //
//-                                                                           //
//-       This Source describes Open Hardware and is licensed under the       //
//-                               CERN-OHL-S v2.                              //
//-                                                                           //
//-       You may redistribute and modify this Source and make products       //
//-  using it under the terms of the CERN-OHL-S v2 (https:/cern.ch/cern-ohl). //
//-         This Source is distributed WITHOUT ANY EXPRESS OR IMPLIED         //
//-  WARRANTY, INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS //
//-   FOR A PARTICULAR PURPOSE. Please see the CERN-OHL-S v2 for applicable   //
//-                                conditions.                                //
//-                                                                           //
//-              Source location: https://github.com/buildbotics              //
//-                                                                           //
//-    As per CERN-OHL-S v2 section 4, should You produce hardware based on   //
//-  these sources, You must maintain the Source Location clearly visible on  //
//-  the external case of the CNC Controller or other product you make using  //
//-                                this Source.                               //
//-                                                                           //
//-              For more information, email info@buildbotics.com             //
//-                                                                           //
//-/////////////////////////////////////////////////////////////////////////////

script#view-control-template(type="text/x-template")
  #control
    table.axes
      tr(:class="axes.klass")
        th.name Axis
        th.position Position
        th.absolute Absolute
        th.offset Offset
        th.state State
        th.actions
          bbutton(:disabled="!is_idle", text="&empty;",
            title="Zero all axis offsets.", @click="zero_all")

          bbutton(title="Home all axes.", @click="home_all", icon="home",
            :disabled="!is_idle")

      each axis in 'xyzabc'
        tr(is="axis-row", :axis=axis, v-if=`${axis}.enabled`)

    .macros
      bbutton.macro(v-for="macro in config.macros",
        v-if="macro.path", @click="run_macro($index + 1)",
        title="Run macro {{$index + 1}} {{macro.name}}\n{{macro.path}}",
        :style="{'background-color': macro.color}",
        :text="macro.name || ($index + 1)")

      bbutton.macro(@click="goto('#settings:macros')", icon="cog",
        text="Macros", title="Go to macro settings")

    .info-blocks
      table.info
        tr
          th State
          td(:class="{attention: highlight_state}") {{mach_state}}

        tr
          th Message
          td.message(:class="{attention: highlight_state}")
            | {{message.replace(/^#/, '')}}

        tr(title="Active machine units")
          th Units
          td.mach_units {{mach_units}}

        tr(title="Active tool")
          th Tool
          td {{state.tool || 0}}

      table.info
        tr(
          title="Velocity in {{metric ? 'meters' : 'inches'}} per minute")
          th Velocity
          td
            unit-value(:value="state.v", precision="2", unit="", iunit="",
              scale="0.0254")
            | {{metric ? ' m/min' : ' IPM'}}

        tr(title="Programmed feed rate.")
          th Feed
          td
            unit-value(:value="state.feed", precision="2", unit="", iunit="")
            | {{metric ? ' mm/min' : ' IPM'}}

        tr(title="Programed and actual speed.")
          th Speed
          td
            | {{state.speed || 0 | fixed 0}}
            span(v-if="!isNaN(state.s)") &nbsp;({{state.s | fixed 0}})
            = ' RPM'

      table.info
        tr
          th Remaining
          td(title="Total run time (days:hours:mins:secs)").
            #[span(v-if="remaining") {{remaining | time}} of]
            {{active.time | time}}
        tr
          th ETA
          td.eta {{eta}}
        tr
          th Line
          td {{0 <= state.line ? state.line : 0 | number}}
        tr
          th {{this.simulating ? 'Simulating' : 'Progress'}}
          td.progress
            label {{(progress || 0) | percent}}
            .bar(:style="'width:' + (progress || 0) * 100 + '%'")

    .tabs
      input#tab1(type="radio", name="tabs" checked, @click="tab = 'auto'")
      label(for="tab1", title="Run programs") Auto

      input#tab2(type="radio", name="tabs", @click="tab = 'mdi'")
      label(for="tab2", title="Manual command entry") MDI

      input#tab3(type="radio", name="tabs", @click="tab = 'jog'")
      label(for="tab3", "Jog the axes manually") Jog

      template(v-if="false")
        input#tab4(type="radio", name="tabs", @click="tab = 'overrides'")
        label(for="tab4", "Speed and feed overrides") Overrides

      input#tab5(type="radio", name="tabs", @click="tab = 'messages'")
      label(for="tab5") Messages

      input#tab6(type="radio", name="tabs", @click="tab = 'indicators'")
      label(for="tab6") I/O Pins

      input#tab7(type="radio", name="tabs", @click="tab = 'power'")
      label(for="tab7") Power

      section#content1.tab-content.pure-form
        .toolbar.pure-control-group
          bbutton(:class="{'attention': is_holding}",
            title="{{is_running ? 'Pause' : 'Start'}} program.",
            @click="start_pause", :icon="is_running ? 'pause' : 'play'",
            :disabled="!active.path || mach_state == 'JOGGING'",
            :text="is_running ? 'Pause' : 'Run'")

          bbutton(title="Stop program.", @click="stop", text="Stop")

          bbutton(title="Pause program at next optional stop (M1).",
            @click="optional_pause", v-if="false", icon="stop-circle-o",
            text="Optional Pause")

          bbutton(title="Execute one program step.", @click="step",
            :disabled="(!is_ready && !is_holding) || !active.path",
            v-if="false", icon="step-forward", text="Step")

          bbutton(title="Select a program.", @click="open",
            :disabled="!is_ready", text="Open")

          bbutton(title="Edit program.", @click="edit", text="Edit",
            :disabled="!active.path")

          bbutton(title="Open 3D view.", @click="view", icon="eye", text="View",
            v-if="$root.webGLSupported", :disabled="!active.path")

          .filename {{active.filename}}

        textarea.gcode-view(v-el:gcode-view)

      section#content2.tab-content
        .mdi.pure-form(title="Manual command entry.")
          bbutton(:disabled="!can_mdi", :class="{'attention': is_holding}",
            title="{{is_running ? 'Pause' : 'Start'}} command.",
            @click="mdi_start_pause", :icon="is_running ? 'pause' : 'play'")

          bbutton(title="Stop command.", @click="stop", icon="stop")

          input(v-model="mdi", :disabled="!can_mdi", @keyup.enter="submit_mdi")

        .history(:class="{placeholder: !history}")
          span(v-if="!history.length") MDI history displays here.
          ul
            li(v-for="item in history", @click="load_history($index)",
               track-by="$index")
              | {{item}}

      section#content3.tab-content
        .jog
          axis-control(axes="XY", :colors="['red', 'green']",
            :enabled="[x.enabled, y.enabled]",
            v-if="x.enabled || y.enabled", :adjust="jog_adjust",
            :step="jog_step")

          axis-control(axes="AZ", :colors="['orange', 'blue']",
            :enabled="[a.enabled, z.enabled]",
            v-if="a.enabled || z.enabled", :adjust="jog_adjust",
            :step="jog_step")

          axis-control(axes="BC", :colors="['cyan', 'purple']",
            :enabled="[b.enabled, c.enabled]",
            v-if="b.enabled || c.enabled", :adjust="jog_adjust",
            :step="jog_step")

          .jog-settings
            .jog-adjust
              | Fine adjust
              input(type="range", v-model="jog_adjust", min=0, max=2, step=1,
                list="jog-adjust-ticks")
              datalist#jog-adjust-ticks
                option(value="0")
                option(value="1")
                option(value="2")

            .jog-mode
              | Step mode
              input(type="checkbox", v-model="jog_step")

          .jog-instructions(v-if="jog_step")
            p Left click the axes above to jog by the specified amount.

          .jog-instructions(v-else)
            p.
              Left click the axes above holding down the mouse button to jog the
              machine.
            p Jogging speed is set by the ring that is clicked.

      section#content4.tab-content
        overrides(:state="state", :template="template")

      section#content5.tab-content
        console

      section#content6.tab-content
        indicators(:state="state", :template="template")

      section#content7.tab-content
        power(:state="state", :template="template")
