<style lang="scss">
.z-table-column__cell-editable {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .el-icon-edit {
    color: rgba(151, 151, 151, 0.5);
    &:hover {
      color: $primary;
    }
  }
  .el-icon-check {
    color: $green;
  }
  .el-icon-close {
    color: $red;
  }
  .el-icon-edit,
  .el-icon-check,
  .el-icon-close {
    cursor: pointer;
    margin-left: 5px;
    font-size: 14px;
  }
}
</style>

<template>
  <el-table :data="tableData | tableDataFilter" :size="tableSize" v-bind="_props" @header-click="onHeaderClick" @cell-click="onCellClick" @cell-dblclick="onCellDblclick">
    <template v-for="(item, index) in columns">
      <el-table-column v-bind="item" :key="index">
        <template #header>
          <span>{{ item.label }}</span>
        </template>
        <template #default="{ row, column }">
          <table-column-cell
            :disabled="item.editable === false"
            :editable="item.editable !== false && (row.$editable || (tableEditCell.index === row.$index && tableEditCell.prop === item.prop))"
            :type="item.type"
            :value="row[column.property]"
            @input="onCellInput"
            @edit-click="onCellDblclick(row, column)"
            @edit-confirm="onHeaderClick"
          >
            <template v-if="$scopedSlots[`column-editor-${item.prop}`]">
              <slot :name="`column-editor-${item.prop}`" :value="row[column.property]" :onInput="onCellInput"></slot>
            </template>
          </table-column-cell>
        </template>
      </el-table-column>
    </template>
  </el-table>
</template>

<script>
import TableNormal from './normal';
import tableProps from './props';
import { cloneDeep, get, set } from '../utils';

export default {
  name: 'TableEditable',
  extends: TableNormal,
  components: {
    tableColumnCell: {
      props: {
        value: [String, Number, Array, Object],
        type: { type: String, default: 'el-input' },
        editable: Boolean,
        disabled: Boolean,
        showHandler: {
          type: Boolean,
          default: true,
        },
      },
      watch: {
        editable(val) {
          if (val && this.type === 'el-input') {
            this.$nextTick(() => {
              this.$children[0] && this.$children[0].focus && this.$children[0].focus();
            });
          }
        },
      },
      render(h) {
        if (this.editable) {
          let editRender = [
            h(this.type, {
              props: { value: this.value, size: 'mini' },
              on: {
                input: value => {
                  this.$emit('input', value);
                },
              },
            }),
          ];
          if (this.$scopedSlots.default) {
            editRender = [this.$scopedSlots.default()];
          }
          if (this.showHandler !== false) {
            const handlerItems = [h('i', { attrs: { title: '确定', class: 'el-icon-check' }, on: { click: () => this.$emit('edit-confirm') } })];
            // handlerItems.push(h('i', { attrs: { title: '取消', class: 'el-icon-close' }, on: { click: () => this.$emit('edit-confirm') } }));
            const handler = h('span', handlerItems);
            editRender.push(handler);
          }
          return h('span', { class: 'z-table-column__cell-editable' }, editRender);
        }
        const defauleRender = [this.value];
        if (!this.disabled && this.showHandler !== false) {
          defauleRender.push(h('i', { attrs: { title: '编辑', class: 'el-icon-edit' }, on: { click: () => this.$emit('edit-click') } }));
        }
        return h('span', { class: 'z-table-column__cell-editable' }, defauleRender);
      },
    },
  },
  props: {
    value: {
      type: Array,
      default() {
        return [];
      },
    },
    columns: {
      type: Array,
      default() {
        return [];
      },
    },
    ...tableProps,
  },
  watch: {
    value(val) {
      this.tableData = val || [];
    },
  },
  data() {
    return {
      tableData: this.value,
      tableRowTemplate: { $editable: true }, // 行数据模板
      tableEditCell: {}, // 正在编辑的单元格
      tableSelection: [], // 表格已选中
    };
  },
  filters: {
    tableDataFilter(value) {
      return value.map((item, index) => ({ ...item, $index: index }));
    },
  },
  // mounted() {
  //   console.log(this.$scopedSlots);
  // },
  methods: {
    onHeaderClick() {
      this.tableEditCell = {};
    },
    onCellClick(row, column, cell, event) {
      if (row.$index !== this.tableEditCell.index || column.property !== this.tableEditCell.prop) {
        this.tableEditCell = {};
      }
    },
    onCellDblclick(row, column, cell, event) {
      this.tableEditCell = { index: row.$index, prop: column.property };
    },
    onCellInput(value) {
      const tableData = cloneDeep(this.tableData);
      const tableRow = tableData[this.tableEditCell.index];
      set(tableRow, this.tableEditCell.prop, value);
      tableData[this.tableEditCell.index] = tableRow;
      this.$set(this.tableData, this.tableEditCell.index, tableRow);
    },
  },
};
</script>
