<template>
  <div class="table-content">
    <el-table :data="tableData">
      <el-table-column
        v-for="(item, index) in head"
        :key="index"
        :prop="item.key"
        :label="item.title"
      >
        <template #default="scope">
          <el-input v-model="scope.row[item.key]" clearable @input="inputFun" />
        </template>
      </el-table-column>
      <el-table-column v-if="mode === 'input'" prop="actions" label="操作" width="60">
        <template #default="scope">
          <el-link type="danger" @click="delDataFun(scope.$index)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="mode === 'edit'" class="setting-btn" @click="diaShow = true">
      <el-icon :size="22"><Setting /></el-icon>
    </div>
    <div v-if="mode === 'input' && !disabled" class="add-column" @click="addDataFun">
      <el-icon :size="22"><Plus /></el-icon>
    </div>
    <Dialog v-model="diaShow" diaTitle="表格设置" :width="500" @close="closeFun">
      <div class="content">
        <div class="title-key">
          <div class="label">表格key</div>
          <el-input v-model="titleKey" placeholder="请输入" clearable />
        </div>
        <div class="table">
          <el-table :data="headData">
            <el-table-column prop="title" label="表头名称">
              <template #default="scope">
                <el-input v-model="scope.row.title" clearable />
              </template>
            </el-table-column>
            <el-table-column prop="key" label="表头key值">
              <template #default="scope">
                <el-input v-model="scope.row.key" clearable />
              </template>
            </el-table-column>
            <el-table-column prop="actions" label="操作" width="60">
              <template #default="scope">
                <el-link type="danger" @click="delHeadFun(scope.$index)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <template #footer>
        <div>
          <el-button @click="addFun">新增列头</el-button>
          <el-button type="primary" @click="saveFun">确定</el-button>
        </div>
      </template>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { Setting, Plus } from '@element-plus/icons-vue';
import Dialog from '@/components/common/dialog/index.vue';
import { cloneDeep } from 'lodash-es';

const emit = defineEmits(['update:keyValue', 'update:value', 'update:head']);

interface Props {
  keyValue: any;
  value: any;
  mode: string;
  head?: any;
  disabled?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  keyValue: '',
  value: [],
  mode: '',
  head: [],
  disabled: false,
});

const diaShow = ref(false);
const titleKey = ref(props.keyValue);
const tableData = ref<any>([]);
const headData = ref<any>(cloneDeep(props.head));

watch(
  () => titleKey.value,
  (val: any) => {
    emit('update:keyValue', val);
  },
  { deep: true }
);

watch(
  () => props.head,
  (val: any) => {
    headData.value = cloneDeep(val) || [];
  },
  { deep: true, immediate: true }
);

watch(
  () => props.value,
  (val: any) => {
    tableData.value = cloneDeep(val) || [];
  },
  { deep: true, immediate: true }
);

const inputFun = () => {
  emit('update:value', tableData.value);
};

const addFun = () => {
  headData.value.push({
    title: '',
    key: '',
  });
};

const addDataFun = () => {
  tableData.value.push({});
  emit('update:value', tableData.value);
};

const delDataFun = (index: any) => {
  tableData.value.splice(index, 1);
  emit('update:value', tableData.value);
};

const delHeadFun = (index: any) => {
  headData.value.splice(index, 1);
};

const saveFun = () => {
  emit('update:head', headData.value);
  closeFun();
};

const closeFun = () => {
  diaShow.value = false;
};
</script>

<style lang="scss" scoped>
.table-content {
  position: relative;
  &:hover {
    .setting-btn {
      opacity: 1;
    }
  }
  .setting-btn {
    opacity: 0;
    position: absolute;
    top: 40px;
    left: -29px;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--el-color-info);
    background-color: var(--el-bg-color);
    border-radius: 4px 0 0 4px;
    border: solid 1px var(--el-border-color);
    border-right: 0;
    z-index: 1;
    cursor: pointer;
  }
}
.add-column {
  width: 30px;
  height: 30px;
  margin: 5px auto 0;
  background-color: var(--el-color-primary);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.content {
  .title-key {
    display: flex;
    align-items: center;
    width: 300px;
    margin-bottom: 10px;
    .label {
      width: 80px;
    }
  }
}
</style>
