<template>
  <div class="th-source-table">
    <div class="head">
      <div class="row th">
        <div class="cell float" v-for="(col,index) in cols" :key="index" :title="col.name" :style="`width:${colWidths[col.name]||150}px;min-width:80px;`">
          <div class="field inline">
            <input type="text" :value="col.name" disabled>
            <div class="meta">{{col.type}}</div>
          </div>
          <div class="resize" @mousedown="resizeDown($event, col.name)"></div>
        </div>
      </div>
    </div>
    <div class="table" @scroll="scroll" v-if="data">
      <div class="row td" v-for="(d, index) in data" :key="index">
        <div class="cell float" v-for="(col,index) in cols" :key="index" :title="d[col.name]" :style="`width:${colWidths[col.name]||150}px;min-width:80px;`">
          <span v-if="(col.type==='geometry'||col.type==='object')&&d[col.name]">{{d[col.name].type}}</span>
          <span v-else>{{d[col.name]}}</span>
        </div>
      </div>
    </div>
    <div class="resize-line" v-show="resizeLineLeft>0" :style="`left:${resizeLineLeft}px;`"></div>
  </div>
</template>

<script>
export default {
  props: {
    columns: {
      type: Array,
      default: []
    },
    data: {
      type: Array,
      default: []
    }
  },
  methods: {
    resizeDown(ev, name) {
      this.resizeData.initWidth = this.colWidths[name] || 150
      this.resizeData.field = name
      this.resizeData.startX = ev.x
      this.resizeData.target = ev.target
      document.addEventListener('mousemove', this.resizeMove, false)
      document.addEventListener('mouseup', this.resizeUp, false)
    },
    resizeMove(ev) {
      this.resizeLineLeft = this.$el.scrollLeft + ev.x - this.$el.getBoundingClientRect().left
    },
    resizeUp(ev) {
      this.resizeLineLeft = -1
      let deltaX = ev.x - this.resizeData.startX
      let newWidth = this.resizeData.initWidth + deltaX
      if (newWidth < 80) {
        newWidth = 80
      }
      this.colWidths[this.resizeData.field] = newWidth
      document.removeEventListener('mousemove', this.resizeMove, false)
      document.removeEventListener('mouseup', this.resizeUp, false)
    },
    getFields() {
      if (!this.data.length) {
        this.cols = []
        return
      }else if (this.columns.length) {
        let array=[]
        this.columns.forEach(f => {
          let name=f.name
          let type=f.type
          array.push({name,type})
        })
        this.cols = array
      } else{
        let array = []
        this.data.forEach(dataItem => {
          for(let key in dataItem) {
            let index=array.findIndex(o=>o.name===key)
            if (index===-1) {
              array.push({
                name: key,
                type: typeof(dataItem[key])
              })
            }
          }
        })
        this.cols = array
      }
    },
    scroll(e) {
      this.headEl.scrollLeft = e.target.scrollLeft
    }
  },
  mounted() {
    this.getFields()
    this.headEl = this.$el.querySelector('.head')
  },
  data() {
    return {
      resizeData: {  // 列宽缩放事件的初始状态，记录初始列宽和鼠标位置
        initWidth: 150,
        startX: 0,
        field: ''
      },
      resizeLineLeft: -1,
      cols:[],
      colWidths: {}, // {col:150}
      headEl: null
    }
  },
  watch: {
    data() {
      this.getFields()
    },
    columns() {
      this.getFields()
    }
  }
}  
</script>