<template>
  <div>
    <el-table ref="tableRef" :data="tableData" class="table-default" max-height="1200" border fit
      @selection-change="handleSelectionChange" :row-key="rowKey">
      <el-table-column type="selection" width="55" v-if="selectionShow" :reserve-selection="true"/>
      <el-table-column type="index" label="序号" width="70" align="center" />

      <!-- 遍历动态列 -->
      <el-table-column v-for="(item, index) in formData.filter((i) => i.showInTable)" :label="item.label"
        :prop="item.fieldName" :key="item.label" :min-width="item.width" :align="item.align || ''">
        <template slot-scope="scope">
          <div v-if="item.fieldName === 'isApply'||item.fieldName === 'status'">
            <!-- <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
            <el-tag type="danger" v-if="scope.row.status === 0">停用</el-tag> -->
            <el-tag type="success" v-if="+scope.row[item.fieldName] === 1">启用</el-tag>
            <el-tag type="danger" v-if="+scope.row[item.fieldName] === 0">停用</el-tag>
          </div>
          <div v-else-if="item.fieldName === 'state'">
            <el-tag type="success" v-if="+scope.row[item.fieldName] === 1">正常</el-tag>
            <el-tag type="danger" v-if="+scope.row[item.fieldName] === 0">不正常</el-tag>
          </div>
          
          <el-tag type="success" v-else-if="item.fieldName ==='defaultSuccess'">正常</el-tag>
          <span v-else>{{ scope.row[item.fieldName] }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" :min-width="minWidth" v-if="isOperate">
        <template #default="scope">
          <slot name="operation" :row="tableData[scope.$index]" :index="scope.$index"></slot>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    formData: {
      type: Array,
      default: () => [],
    },
    tableData: {
      type: Array,
      default: () => [],
    },
    minWidth: {
      type: Number,
      default: 60,
    },
    isOperate: {
      type: Boolean,
      default: true,
    },
    isEdit: {
      type: Boolean,
      default: true,
    },
    selectionShow: {
      type: Boolean,
      default: true,
    },
    rowKey: {
      type: String,
      default: 'id',
    }
  },
  data() {
    return {
      editingRowIndex: -1,
      selectedRows: [],
      key: '',
    };
  },
  mounted() {
    // 执行初始化逻辑
  },
  methods: {
    handleEditRow(index) {
      this.dialogVisible = true;
      this.editingRowIndex = index;
      this.formData.forEach((i) => {
        i.value = this.tableData[index][i.fieldName];
      });
    },
    handleDeleteRow(index) {
      this.tableData.splice(index, 1);
    },
    handleDialogSave() {
      this.$refs.myForm.validate((valid) => {
        if (valid) {
          let tableRow = {};
          this.formData.forEach((i) => {
            tableRow[i.fieldName] = i.value;
          });
          if (this.editingRowIndex !== -1) {
            this.tableData[this.editingRowIndex] = tableRow;
          } else {
            this.tableData.push(tableRow);
          }
          this.handleDialogClose();
        }
      });
    },
    handleDialogClose() {
      this.dialogVisible = false;
      this.editingRowIndex = -1;
      this.formData.forEach((i) => {
        i.value = "";
      });
    },

    // 监听勾选项变化
    handleSelectionChange(selectedRows) {
      this.$emit("selection-change", selectedRows); // 将选中的数据传递给父组件
    },


  },
};
</script>

<style lang="scss" scoped>
.table-default {
  width: 100%;
  /* Add any other necessary styles */
}
</style>
