<template>
  <el-col>
    <el-row>
      <el-col style="width: 200px;">
        <el-form ref="form" :model="form" label-width="60px" size="mini" class="row-style">
          <el-form-item :label="$t('dataset.showRow')">
            <el-input v-model="form.row">
              <el-button slot="append" icon="el-icon-search" @click="reSearch" />
            </el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <ux-grid
      ref="plxTable"
      size="mini"
      style="width: 100%;"
      :height="height"
      :checkbox-config="{highlight: true}"
      :width-resize="true"
    >
      <ux-table-column
        v-for="field in fields"
        :key="field.dataeaseName"
        min-width="200px"
        :field="field.dataeaseName"
        :resizable="true"
      >
        <template slot="header">
          <svg-icon v-if="field.deType === 0" icon-class="field_text" class="field-icon-text" />
          <svg-icon v-if="field.deType === 1" icon-class="field_time" class="field-icon-time" />
          <svg-icon v-if="field.deType === 2 || field.deType === 3" icon-class="field_value" class="field-icon-value" />
          <span>{{ field.name }}</span>
        </template>
      </ux-table-column>
    </ux-grid>
    <el-row style="margin-top: 4px;">
      <span class="table-count">
        <span v-if="page.total <= currentPage.show">
          {{ $t('dataset.preview_total') }}
          <span class="span-number">{{ page.total }}</span>
          {{ $t('dataset.preview_item') }}
        </span>
        <span v-if="page.total > currentPage.show">
          {{ $t('dataset.preview_show') }}
          <span class="span-number">{{ currentPage.show }}</span>
          {{ $t('dataset.preview_item') }}
          ，{{ $t('dataset.preview_total') }}
          <span class="span-number">{{ page.total }}</span>
          {{ $t('dataset.preview_item') }}
        </span>
      </span>
      <el-pagination
        :current-page="currentPage.page"
        :page-sizes="[100]"
        :page-size="currentPage.pageSize"
        :pager-count="5"
        layout="sizes, prev, pager, next"
        :total="currentPage.show"
        @current-change="pageChange"
      />
    </el-row>
  </el-col>
</template>

<script>
export default {
  name: 'TabDataPreview',
  props: {
    table: {
      type: Object,
      required: true
    },
    fields: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    },
    form: {
      type: Object,
      required: true
    },
    page: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      height: 500,
      currentPage: {
        page: 1,
        pageSize: 100,
        show: parseInt(this.form.row)
      }
    }
  },
  computed: {
  },
  watch: {
    data() {
      const datas = this.data
      this.$refs.plxTable.reloadData(datas)
    },
    page() {
      if (this.page.total < parseInt(this.form.row)) {
        this.currentPage.show = this.page.total
      } else {
        this.currentPage.show = parseInt(this.form.row)
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.calHeight()
    },
    calHeight() {
      const that = this
      setTimeout(function() {
        const currentHeight = document.documentElement.clientHeight
        that.height = currentHeight - 56 - 30 - 26 - 25 - 55 - 38 - 28 - 10
      }, 10)
    },
    reSearch() {
      if (!this.form.row || this.form.row === '' || this.form.row.length > 8 || isNaN(Number(this.form.row)) || String(this.form.row).includes('.')) {
        this.$message({
          message: this.$t('dataset.pls_input_less_9'),
          type: 'error',
          showClose: true
        })
        return
      }
      this.currentPage.show = parseInt(this.form.row)
      this.currentPage.page = 1
      this.$emit('reSearch', { form: this.form, page: this.currentPage })
    },
    pageChange(val) {
      this.currentPage.page = val
      // console.log(this.currentPage)
      this.$emit('reSearch', { form: this.form, page: this.currentPage })
    }
  }
}
</script>

<style scoped>
  .row-style>>>.el-form-item__label{
    font-size: 12px;
  }
  .row-style>>>.el-form-item--mini.el-form-item{
    margin-bottom: 10px;
  }
  .el-pagination{
    float: right;
  }
  span{
    font-size: 12px;
  }
  .span-number{
    color: #f18126;
  }
  .table-count{
    color: #606266;
  }
</style>
