<template>
  <div class="monitor-table">
    <p v-if="title" class="title">{{title}}</p>
    <Row class="row row-title" >
      <i-col class="col" v-for="col in layout" :key="col.key" :span="col.width" :offset="col.offset || 0">
        <div :style="`text-align: ${col.align || 'left'}; `">{{col.title}}</div>
      </i-col>
    </Row>
    <template v-if="model && model.length > 0">
      <div>
        <Row class="row" v-for="(row, rowIndex) in model" :key="rowIndex">
          <i-col class="col" v-for="col in layout" :key="col.key" :span="col.width" :offset="col.offset || 0">
            <template v-if="col.type == 'img'">
              <template v-if="row[col.key] && row[col.key].length > 0">
                <img
                  v-for="(img, i) in row[col.key]"
                  :key="img + i"
                  :src="img"
                  @click="handleView(img)" />
              </template>
            </template>
            <div v-else :style="`text-align: ${col.align || 'left'};color: ${col.color} `">{{row[col.key]}}</div>
          </i-col>
        </Row>
      </div>
    </template>
    <m-modal v-model="showImg">
      <img v-if="showImg" style="width: 100%;" :src="require(`@/assets/images/${curSrc}`)"/>
    </m-modal>
  </div>
</template>

<script>
export default {
  name: 'MonitorTable',
  props: {
    title: {
      type: String,
      default: '',
    },
    layout: {
      type: Array,
      default() {
        return []
      }
    },
    model: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      curSrc: null,
      showImg: false,
    }
  },
  methods: {
    handleView(src) {
      this.curSrc = src
      this.showImg = true
    }
  }
}
</script>

<style lang="stylus" scoped>
.monitor-table
  font-size 1rem
  color #fff
  margin-bottom 1rem
  .title
    font-size 1.2rem
    font-weight bold
    color $edgeColor
    padding-bottom 0.5rem
    margin-bottom 1rem
    border-bottom 0.1rem solid $color-map()
  .row
    display flex
    align-items center
    line-height 2.4rem
    &.row-title
      font-weight bold
      font-size 1.1rem
      background-color $color-map()
    &:nth-child(2n)
      background-color $color-map(0.15)
  .col
    padding 0 1rem
    display flex
    align-items center
    flex-wrap wrap
    img
      width 50%
      margin 0.5rem 0
      cursor pointer
</style>
