<template>
  <b-modal ref="duplicateWidgetModal" hide-footer @hidden="onCancel()">
    <div class="name-wrap">
      <h4>Duplicate Widget</h4>
      <b-form>
        <b-form-group>
          <label class="b-lable text-common-style mb-0" for="dashboardId"
            >Select Dashboard</label
          >
          <multiselect
          v-model="selectDashboard"
          :options="allDashboards"
          @select="onDashboardChange"
          :select-label="''"
          :selected-label="''"
          :deselect-label="''"
          track-by="name"
          label="name"
          :searchable="true"
          :allow-empty="false"
          class="b-control b-select">
            <template slot="singleLabel" slot-scope="{ option }">
              <strong>{{ option.name }}</strong>
            </template>
          </multiselect>
        </b-form-group>
        <b-form-group>
          <label class="b-lable text-common-style mb-0" for="penalId"
            >Select Panel</label
          >
          <multiselect
          v-model="selectPanel"
          :options="panelList"
          @select="onPanelChange()"
          :select-label="''"
          :selected-label="''"
          :deselect-label="''"
          track-by="name"
          label="name"
          :searchable="true"
          :allow-empty="false"
          class="b-control b-select">
            <template slot="singleLabel" slot-scope="{ option }">
              <strong>{{ option.name }}</strong>
            </template>
          </multiselect>
        </b-form-group>
        <b-form-group>
          <label class="b-lable text-common-style mb-0" for="subpanelId"
            >Select Subpanel</label
          >
          <multiselect
          v-model="selectSubpanel"
          :options="listOfSubpanels"
          :select-label="''"
          :selected-label="''"
          :deselect-label="''"
          track-by="name"
          label="name"
          :searchable="true"
          :allow-empty="false"
          class="b-control b-select">
            <template slot="singleLabel" slot-scope="{ option }">
              <strong>{{ option.name }}</strong>
            </template>
          </multiselect>
        </b-form-group>
      </b-form>
      <div class="mads-footer">
        <b-button
          variant="outline-primary"
          @click="onCancel()"
          class="cancel-btn"
          >Cancel</b-button
        >
        <b-button variant="primary" @click="onSave()" class="save-btn"
          >Save</b-button
        >
      </div>
    </div>
  </b-modal>
</template>

<script>
export default {
  props: {
    currentUser: {},
    selectedDashboard: {},
    selectedPanel: {},
    dashboardService: {}
  },
  watch: {
    selectDashboard: function () {
      this.getDashboardData()
    }
  },
  data () {
    return {
      selectDashboard: {},
      selectPanel: {},
      selectSubpanel: {},
      allDashboards: [],
      panelList: [],
      subpanelList: []
    }
  },
  mounted: function () {
    this.$emit('on-mounted', true)
    this.selectDashboard = this.selectedDashboard
    this.selectPanel = this.selectedPanel

    this.loadAllDashboards()
    this.getDashboardData()
  },
  methods: {
    onDashboardChange () {
      this.selectPanel = { }
      this.selectSubpanel = { }
    },
    onPanelChange () {
      this.selectSubpanel = { }
    },
    getDashboardData () {
      let dashboardId
      if (this.selectDashboard) {
        dashboardId = this.selectDashboard.id
      } else {
        dashboardId = this.selectedDashboard.id
      }
      let config = { orgId: this.currentUser.org.id, id: dashboardId }
      this.dashboardService.readId(config)
        .then((response) => {
          this.panelList = response.panels
          if (this.selectPanel.panel_id > -1) {
            this.selectPanel = this.panelList.find(i => i.id === this.selectPanel.panel_id)
          }
        })
    },
    show () {
      this.$refs.duplicateWidgetModal.show()
    },
    onSave () {
      if (this.selectSubpanel.id) {
        this.$emit('on-save', this.selectSubpanel.id)
      } else {
        this.$emit('on-save', this.selectPanel.id)
      }
    },
    onCancel () {
      this.$refs.duplicateWidgetModal.hide()
    },
    loadAllDashboards () {
      let config = { orgId: this.currentUser.org.id }
      const params = {
        page_size: 100,
        page_number: 0
      }
      this.dashboardService.read(config, params).then((response) => {
        this.allDashboards = response.dashboards
      })
    }
  },
  computed: {
    listOfSubpanels () {
      return this.selectPanel.subpanels ? this.selectPanel.subpanels : []
    }
  }
}
</script>

<style lang="scss" scoped>
.name-wrap {
  width: 100%;
  margin-top: 10px;
  padding: 10px 40px;
  .mads-footer {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 20px;
    .btn {
      min-width: 120px;
      margin-right: 5px;
    }
  }
  .text-common-style {
    font-size: 12px;
    line-height: 24px;
  }
  .b-lable {
    color: #acb5bd;
  }
  .b-control {
    background: #ffffff;
    border: 1px solid #acb5bd;
    border-radius: 8px;
  }
  .b-input {
    height: 44px;
  }
  .b-select {
    height: 52px !important;
  }
  .v-text-field.v-text-field--solo .v-input__control {
    min-height: 80px;
  }
  .cancel-btn {
    border: none;
    &:hover,
    &:active,
    &:focus {
      background-color: #ffffff;
      border: none;
      color: #000000;
    }
  }
  .save-btn {
    background: #0d85da;
    border-radius: 8px;
    border: 1px solid #0d85da;
  }
}
</style>

<style lang="scss">
.modal {
  .modal-content {
    .modal-footer {
      border-top: none;
      .btn {
        min-width: 120px;
      }
    }
  }
}
</style>
