<template>
  <el-row>
    <el-col :span="6" style="border-right: 1px solid #000; padding-right: 20px">
      <!-- 顶部工具栏 -->
      <el-form :model="headerFormData" label-width="auto">
        <el-form-item label="数据资源名称">
          <el-input v-model="headerFormData.datasourceName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="节点IP">
          <el-input v-model="headerFormData.sixSftpIp" 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="leftTableData"
        border
        max-height="500"
        style="width: 100%; margin-right: 10px"
      >
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column prop="datasourceName" label="数据资源名称"></el-table-column>
        <el-table-column prop="sixSftpIp" label="节点IP"></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>六统一接入方式为sftp,必填此表</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">上传</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 label="序号" type="index" width="50"></el-table-column>

        <!-- 普通列 -->
        <el-table-column prop="datasourceName" 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.datasourceName"
                placeholder="请输入内容"
              ></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fileName" 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.fileName"
                placeholder="请输入内容"
              ></el-input>
            </div>
          </template>
        </el-table-column>

        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <div class="expanded-content">
              <el-form
                :model="apiFormData"
                label-width="auto"
                inline
                class="form-detail-inline"
                :label-position="right"
              >
                <el-form-item label="应用场景">
                  <el-input v-model="props.row.scenario" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="文件类型">
                  <el-input v-model="props.row.fileType" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="六统一SFTP IP">
                  <el-input v-model="props.row.sixSftpIp" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="六统一SFTP 端口">
                  <el-input v-model="props.row.sixSftpPort" placeholder="请输入"></el-input>
                </el-form-item>

                <el-form-item label="文件夹路径">
                  <el-input v-model="props.row.filePath" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="acks">
                  <el-input v-model="props.row.acks" placeholder="请输入"></el-input>
                </el-form-item>

                <el-form-item label="六统一用户名">
                  <el-input v-model="props.row.sixName" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="六统一密码">
                  <el-input v-model="props.row.sixPassword" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="文件大小">
                  <el-input v-model="props.row.fileSize" placeholder="请输入"></el-input>
                </el-form-item>
              </el-form>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
</template>
<script>
import {
  deleteUrlSun,
  deleteDataSun
} from "@/api/unified-access/access-feedback";
import MyForm from "@/components/MyForm";
export default {
  name: "",
  components: {
    MyForm
  },
  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: 161
      },
      leftTableData: [],
      rightTableData: [],
      headerFormData: {},
      apiFormData: {},
      // 表格选中的数据
      selectedRows: [],
      expandedRows: []
    };
  },
  methods: {
    onSearch () { },
    onReset () {
      this.headerFormData={}
     },
    onPageChange(page) {
      this.pagination.currentPage = page;
    },
    // 右侧列表新增按钮
    addClick() {
      const newRow = {
        id: Date.now(), // 使用当前时间戳生成唯一ID
        datasourceName: "",
        fileName: "",
        scenario: "",
        fileType: "",
        sixSftpIp: "",
        sixSftpPort: "",
        filePath: "",
        acks: "",
        sixName: "",
        sixPassword: "",
        fileSize: "",
      };
      this.rightTableData.push({ ...newRow });
      this.leftTableData = this.rightTableData;
      this.pagination.total = this.leftTableData.length;
    },
    // 处理选择框勾选变化
    handleSelectionChange(val) {
      this.selectedRows = val; // 将勾选的行保存到 selectedRows 中
    },
    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);
      }
    }
  },
  created() {},
  mounted() {}
};
</script>
<style lang="scss" scoped>
.expanded-content {
  padding: 20px;
  box-sizing: border-box;
}
.textarea-form-block {
  margin-top: 20px;
}
.title {
  width: 100%;
  text-align: left;
}
.table_title {
  width: 100%;
  text-align: left;
  margin: 10px 0;
}

.textarea-form {
  margin-top: 20px;
}
</style>
