<template>
  <div class="p-4 k-search-table">
    <!--  -->
    <search ref="$form"
            v-bind="$attrs"
            @search="onSearch"
            @reset="onReset">
      <slot name="searchs"></slot>
    </search>

    <!-- tab -->
    <tab v-bind="$attrs"
         @change="onTabChange"></tab>

    <!-- 按钮 -->
    <!-- <el-row v-if="$slots['buttons'] || urlExport"
            class="mb-4 button-container">
      <el-col :span="24">
        <slot name="buttons"></slot>
        <el-button v-if="urlExport"
                   type="success"
                   @click="onExport">导出数据</el-button>
      </el-col>
    </el-row> -->

    <!--  -->
    <el-table ref="$table"
              class="w-full"
              :default-expand-all="expand"
              @sort-change="onSort"
              v-if="$slots['columns']"
              :data="dataList"
              :max-height="tableHeight"
              border
              stripe
              :row-key="rowKey"
              :show-summary="false"
              :summary-method="getSummaries"
              :row-class-name="onTableRowClassName">
      <!-- 不用element的, 不好控制要调用方法 -->
      <el-table-column v-if="select"
                       width="50px">
        <template slot="header"
                  slot-scope="scope">
          <el-checkbox v-show="showSelectAll"
                       v-model="selectAll">{{
                        scope.o
                    }}</el-checkbox>
        </template>
        <template slot-scope="r">
          <el-checkbox v-model="r.row[selectField]"
                       @change="onSelect(r.row)"></el-checkbox>
        </template>
      </el-table-column>
      <!--  -->
      <slot name="columns"></slot>
    </el-table>

    <!--  -->
    <el-pagination v-if="pageable && $slots['columns']"
                   class="mt-4 pagination"
                   @current-change="onPageChange"
                   @size-change="onSizeChange"
                   :page-size.sync="pageSize"
                   :current-page.sync="pageIndex"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="parseInt(total)"></el-pagination>

    <div class="clearfix"></div>
  </div>
</template>

<script>
import Search from './components/search'
import Tab from './components/tab'
import Sortable from 'sortablejs'

import MixSearch from './mix.search'
import MixTableSelect from './mix.table.select'

const pageCache = {}

export default {
  inheritAttrs: false,
  mixins: [MixSearch, MixTableSelect],
  components: {
    Tab,
    Search,
  },
  data() {
    return {}
  },
  props: {
    expand: {},
    drag: {
      default: false,
    },
    rowKey: {
      default: 'id',
    },
    showSummary: {
      default: false,
    },
    searchData: {
      default: false,
    },
    data: {
      type: Array,
      default: function () {
        return []
      },
    },
    tableHeight:{
      type:Number,
      default:400
    }
  },
  model: {
    prop: 'data',
    event: 'cc',
  },
  computed: {},
  watch: {
    dataList() {
      this.$emit('cc', this.dataList)
    },
  },
  methods: {
    redrag() {
      if (!this.drag) {
        return
      }
      const el = this.$el.querySelectorAll(
        '.el-table__body-wrapper > table > tbody'
      )[0]
      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost',
        setData: function (dataTransfer) {
          dataTransfer.setData('Text', '')
        },
        onEnd: (evt) => {
          this.$emit('drag', {
            newIndex: evt.newIndex,
            oldIndex: evt.oldIndex,
          })
        },
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map((item) =>
          // Number(item[column.property])

          {
            return item[column.property]
          }
        )
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ''
        } else {
          sums[index] = 'N/A'
        }
      })

      return sums
    },
    onTabChange({ name, value }) {
      this.$set(this.searchData, name, value)
    },
    onFilter(kv) {
      this.$emit('filter', kv)
    },
    onReset() {
      this.$refs.$form.resetFields()
      this.$emit('reset')
    },
    onSearch() {
      this.search()
    },
    onTableRowClassName(row, index) {
      if (index === 1) {
        return 'info-row'
      } else if (index === 3) {
        return 'positive-row'
      }
      return ''
    },
  },
  created() {},
  mounted() {
    //console.log("gx mounted",this.list,this.manual)
    // this.list &&
    if (!this.manual) {
      this.load(this.searchData)
    }
  },
}
</script>
<style lang="scss">
.k-search-table {
  width: calc(100% - 1px);

  //   height: 837px;
  overflow: auto;
  //
  .el-table .info-row {
    background: #c9e5f5;
  }
  .el-table .positive-row {
    background: #e2f0e4;
  }

  //
  .el-pagination {
    float: right;
  }
  .el-pagination__sizes {
    .el-input__inner {
      height: 28px;
    }
  }

  //
  .sortable-ghost {
    opacity: 0.8;
    color: #fff !important;
    background: #42b983 !important;
  }
}
</style>
