<template>
  <div class="table-box2">
    <div>
      <a-icon class="add-but" type="plus-circle" @click="handleAdd" />
      <a-icon class="del-but" type="minus-circle" @click="handleDel" />
    </div>
    <a-table bordered :data-source="dataSource" :columns="columns" :pagination="false">
      <template slot="name" slot-scope="text, record">
        <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />
      </template>
      <template slot="address" slot-scope="text, record">
        <j-upload v-model="text"></j-upload>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-popconfirm v-if="dataSource.length" title="Sure to delete?" @confirm="() => onDelete(record.key)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
      </template>
    </a-table>
  </div>
</template>
<script>
const EditableCell = {
  template: `
      <div class="editable-cell">
        <div class="editable-cell-input-wrapper">
          <a-input :value="value" @change="handleChange" @pressEnter="check" />
        </div>
      </div>
    `,
  props: {
    text: String,
  },
  data () {
    return {
      value: this.text,
      editable: false,
    };
  },
  methods: {
    handleChange (e) {
      const value = e.target.value;
      this.value = value;
    },
    check () {
      this.$emit('change', this.value);
    },
  },
};
import JUpload from '@/components/jeecg/JUpload'

export default {
  components: {
    EditableCell, JUpload
  },
  data () {
    return {
      dataSource: [
        {
          key: '0',
          name: 'Edward King 0',
          address: [],
        },
        {
          key: '1',
          name: 'Edward King 1',
          address: [],
        },
      ],
      count: 2,
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 80,
          align: 'center',
          customRender: (t, r, index) => index + 1
        },
        {
          title: '申报所需材料',
          dataIndex: 'name',
          width: '50%',
          align: 'center',
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '上传材料样例',
          dataIndex: 'address',
          align: 'center',
          scopedSlots: { customRender: 'address' },
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center',

        },
      ],
    };
  },
  methods: {
    onCellChange (key, dataIndex, value) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    onDelete (key) {
      const dataSource = [...this.dataSource];
      this.dataSource = dataSource.filter(item => item.key !== key);
    },
    handleAdd () {
      const { count, dataSource } = this;
      const newData = {
        key: count,
        name: '',
        address: [],
      };
      this.dataSource = [...dataSource, newData];
      this.count = count + 1;
    },
    handleDel () {
      this.dataSource.pop()
    }
  },
};
</script>
<style scoped>
.table-box2 {
  width: 95%;
  float: right;
}

.add-but {
  font-size: 25px;
  color: #16c4af;
  margin-bottom: 5px;
}

.del-but {
  font-size: 25px;
  color: #f95556;
  margin-left: 10px;
  margin-bottom: 5px;
}

.editable-cell {
  position: relative;
}

.editable-cell-input-wrapper,
.editable-cell-text-wrapper {
  padding-right: 24px;
}

.editable-cell-text-wrapper {
  padding: 5px 24px 5px 5px;
}

.editable-cell-icon,
.editable-cell-icon-check {
  position: absolute;
  right: 0;
  width: 20px;
  cursor: pointer;
}

.editable-cell-icon {
  line-height: 18px;
  display: none;
}

.editable-cell-icon-check {
  line-height: 28px;
}

.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
  color: #108ee9;
}

.editable-add-btn {
  margin-bottom: 8px;
}
</style>
