<template>
  <el-row>
    <el-col :span="6" style="border-right: 1px solid #000; padding-right: 20px">
      <!-- 顶部工具栏 -->
      <el-form :model="headerFormData" :label-position="'right'" label-width="auto">
        <el-form-item label="数据集名称">
          <el-input v-model="headerFormData.datasetName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="数据类型">
          <el-input v-model="headerFormData.dataType" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="表名">
          <el-input v-model="headerFormData.tableName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="default" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 左侧表格 -->

      <el-table :data="currentLeftTableData" border style="width: 100%; margin-right: 10px">
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column prop="tableName" label="表单中文名"></el-table-column>
        <el-table-column prop="tableEnglishName" label="表单英文名"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="onEyeClick(scope.row)" icon="el-icon-view"></el-button>
            <el-button type="text" @click="onDeleteClick(scope.row)" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination background layout="prev, pager, next" :current-page="pagination.currentPage"
          :page-size="pagination.pageSize" :total="pagination.total" @current-change="onPageChange"></el-pagination>
      </div>
    </el-col>
    <el-col :span="18" style="padding-left: 20px">
      <div>六统一接入方式为ETL(DI),必填此表</div>
      <!-- 右侧表格 -->
      <div style="
          width: 100%;
          display: flex;
          justify-content: end;
          align-items: center;
        ">

        <el-button type="primary" @click="addClick">新增</el-button>
        <!-- <el-button type="primary" @click="deleteSelected">删除</el-button> -->
        <el-button type="primary">上传</el-button>
        <el-button type="primary">模版下载</el-button>
      </div>
      <el-table ref="rightTable" :data="rightTableData" border style="width: 100%" :expand-row-keys="expandedRows"
        row-key="id" @expand-change="onExpandChange" :show-header="false" max-height="800"
        @selection-change="handleSelectionChange">
        <!-- 勾选框列 -->
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- 序号列 -->
        <el-table-column label="序号" type="index" width="50"></el-table-column>

        <!-- 普通列 -->
        <el-table-column prop="tableName" label="表中文名">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center">
              <div>表中文名</div>
              <el-input style="width: 260px; margin-left: 20px" v-model="scope.row.tableName"
                placeholder="请输入内容"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="tableEnglishName" label="表英文名">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center">
              <span>表英文名</span>
              <el-input style="width: 260px; margin-left: 20px" v-model="scope.row.tableEnglishName"
                placeholder="请输入内容"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="tableDetail" label="表描述">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center">
              <span>表描述</span>
              <el-input style="width: 260px; margin-left: 20px" v-model="scope.row.tableDetail"
                placeholder="请输入内容"></el-input>
            </div>
          </template>
        </el-table-column>

        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <div class="expanded-content">
              <el-table ref="rightExpandTable" :data="props.row.tabDatasourceMsgs" border style="width: 100%">


                <!-- 序号列 -->
                <el-table-column label="序号" type="index" width="50"></el-table-column>

                <!-- 普通列 -->
                <el-table-column prop="code" label="所属序号/标识符">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.code" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="name" label="中文名称">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.name" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="nameEnglish" label="英文名称">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.nameEnglish" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="definition" label="定义说明">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.definition" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="dataType" label="数据类型">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dataType" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="dataLenght" label="数据长度">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dataLenght" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="dataPrecision" label="数据精度">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dataPrecision" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="numberOfDigits" label="小数位数">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.numberOfDigits" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="defaultValue" label="默认值">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.defaultValue" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="isRequired" label="是否必填">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.isRequired" placeholder="请选择">
                      <el-option v-for="item in whetherOptions" :key="item.value" :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column prop="isSecrecy" label="信息项是否为敏感数据">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.isSecrecy" placeholder="请选择">
                      <el-option v-for="item in whetherOptions" :key="item.value" :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column prop="status" label="状态">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.status" placeholder="请输入内容"></el-input>
                  </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="100">
                  <template slot-scope="scope">

                    <el-button type="text" size="small" @click="deleteTableRow(props.row, scope.$index,scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-button type="primary" plain class="mt-4" style="width: 100%" icon="el-icon-plus"
                @click="addTableRow(props.row)"></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
</template>
<script>
import {
  deleteUrlSun,
  deleteDataSun
} from "@/api/unified-access/access-feedback";
export default {
  name: "",
  props: {
    thirdStep: {
      type: Array,
      default: []
    },
    title: {
      type: String,
      default: "弹窗标题"
    }
  },
  watch: {
    thirdStep: {
      handler(newVal) {
        this.rightTableData = newVal;
        this.leftTableData = this.rightTableData;
        this.pagination.total = this.leftTableData.length;
      },
      deep: true, // 深入监听
      immediate: true
    },
    title: {
      handler(newVal) {
        this.dialogTitle = newVal;
      },
      deep: true, // 深入监听
      immediate: true
    }
  },
  data () {
    return {
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      // 表格选中的数据
      selectedRows: [],
      leftTableData: [],
      rightTableData: [],
      headerFormData: {},
      expandedRows: [],
      whetherOptions: [
        { label: "是", value: 1 },
        { label: "不是", value: 0 },
      ],
    };
  },
  computed: {
    // 计算当前页要显示的数据
    currentLeftTableData () {
      const start = (this.pagination.currentPage - 1) * this.pagination.pageSize;
      const end = start + this.pagination.pageSize;
      return this.leftTableData.slice(start, end);
    },
  },
  methods: {
 
    onExpandChange () { },
    onSearch () { },
    onReset () {
      this.headerFormData={}
     },
    onEyeClick (row) {
      // // 检查右侧表格是否已存在相同项
      // const existing = this.rightTableData.find(item => item.tableName === row.tableName && item.tableEnglishName === row.tableEnglishName);
      // 获取右侧表格对应的数据并展开
      if (!this.expandedRows.includes()) {
        // this.expandedRows.push(row.tableName); // 将左侧表格对应的行名称加入展开行
        this.expandedRows = [row.id]; // 将左侧表格对应的行名称加入展开行

        
      }
     
    },
    // 点击“删除”按钮时，删除左侧表格的项
    onDeleteClick(row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning"
      })
        .then(() => {
          deleteDataSun(row.id).then(res => {
            if (+res.code === 200) {
              this.$message({
                message: res.msg,
                type: "success"
              });
              const index = this.leftTableData.findIndex(
                item => item.id === row.id
              );
              if (index > -1) {
                this.leftTableData.splice(index, 1);
              }
              // 同步删除右侧表格的对应项
              this.onDeleteRightClick(row);
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 在右侧表格删除时，删除对应的数据
    onDeleteRightClick (row) {
      const index = this.rightTableData.findIndex(item => item.id === row.id );
      if (index > -1) {
        this.rightTableData.splice(index, 1);
      }
    },
    onPageChange (page) { 
      this.pagination.currentPage = page
     },
    addClick () {
      const newItem = {
        id: Date.now(),  // 使用当前时间戳作为唯一 ID
        tableName: "",
        tableEnglishName: "",
        tableDetail: "",
        tabDatasourceMsgs: [
          {
            id: Date.now(), // 使用时间戳作为唯一 ID
            code: '',
            name:'',
            nameEnglish: '',
            definition: '',
            dataType: '',
            dataLenght: '',
            dataPrecision: '',
            numberOfDigits: '',
            defaultValue: '',
            isRequired: '',
            isSecrecy: '',
            status: '',
          }
        ],

      };
      this.rightTableData.push({ ...newItem })
      this.leftTableData = this.rightTableData
      this.pagination.total = this.leftTableData.length
      // console.log(this.leftTableData.length,'this.leftTableData.lenght')
      // this.pagination.total = this.leftTableData.length
    },
    // 处理选择框勾选变化
    handleSelectionChange (val) {
      this.selectedRows = val; // 将勾选的行保存到 selectedRows 中
    },
    // 删除选中的行
    deleteSelected () {
      if (this.selectedRows.length === 0) {
        this.$message.warning('请先选择要删除的项');
        return;
      }

      this.$confirm('确定删除选中的数据吗?', '警告', {
        type: 'warning',
      }).then(() => {
        // 获取选中项的 id
        const selectedIds = this.selectedRows.map(row => row.id);

        // 删除右侧表格中选中的行
        this.rightTableData = this.rightTableData.filter(row => !selectedIds.includes(row.id));

        // 删除左侧表格中对应的行
        this.leftTableData = this.leftTableData.filter(row => !selectedIds.includes(row.id));

        // 清空选中行
        this.selectedRows = [];

        this.$message.success('删除成功');
      }).catch(() => { });
    },
 
    // 添加一个新项到展开的表格中
    addTableRow (row) {
      // 新增一项
      const newItem = {
        id: Date.now(), // 使用时间戳作为唯一 ID
        code: '',
        name: '',
        nameEnglish: '',
        definition: '',
        dataType: '',
        dataLenght: '',
        dataPrecision: '',
        numberOfDigits: '',
        defaultValue: '',
        isRequired: '',
        isSecrecy: '',
        status: '',
      };
      row.tabDatasourceMsgs.push(newItem);
    },
    // 删除当前行
    deleteTableRow (row, index,itemRow) {
      if (this.dialogTitle === "新增") {
       row.tabDatasourceMsgs.splice(index, 1);
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
              deleteUrlSun(itemRow.id).then(res => {
              if (+res.code === 200) {
                row.tabDatasourceMsgs.splice(index, 1);

                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
              }else{
                this.$message.error(res.msg);
              }
            });
 
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    }
  },
  created () { },
  mounted () { },
};
</script>
<style lang="scss" scoped></style>
