<template>
  <div>
    <el-form :model="form" label-width="80px">
      <el-form-item>
        <input v-model="form.userName" style="padding: 10px 10px;float: initial;" placeholder=请输入要查询的用户名>
        <input v-model="form.connectorName" style="padding: 10px 10px;float: initial;margin-left: 20px" placeholder=请输入要查询的接口名称>
        <el-button @click="findConnectorUserList()" style="padding: 10px 20px;margin-left: 40px">查询</el-button>
      </el-form-item>
    </el-form>
    <p style="font-weight: bold;font-size: 20px">余额：{{ userBalanceData.userBalance || '加载中...' }}</p>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        label="编号"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.connectorUserId }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.userName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="接口"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.connectorName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="剩余次数"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.connectorResidueDegree }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="购买次数"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.connectorFrequency }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {findConnectorUserList} from "@/api/port/port";
import {userBalance} from "@/api/system/user";
export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    //这里存放数据"

    return {
      userBalanceData: {
        userBalance: '加载中...'
      },
      tableData:[],
      form:{},
    };
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    findConnectorUserList(){
      findConnectorUserList(this.form).then((res)=>{
        console.log(res.data);
        this.tableData=res.data;
      })
    },
    async fetchUserBalance() {
      try {
        const userId = localStorage.getItem('userId');
        console.log(userId)// 登录后把userId存到了localStorage
        if (!userId) {
          this.userBalanceData = {userBalance: '未登录'};
          return;
        }
        const response = await userBalance(userId);
        if (response.data) {
          console.log(response.data)
          this.userBalanceData.userBalance = response.data;
        } else {
          this.userBalanceData.userBalance = {userBalance: '获取失败'};
        }
      } catch (error) {
        this.userBalanceData = {userBalance: '获取失败'};
        console.error('Error fetching user balance:', error);
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.fetchUserBalance();
    this.findConnectorUserList();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, //生命周期 - 更新之后",
  beforeDestroy() {
  }, //生命周期 - 销毁之前",
  destroyed() {
  }, //生命周期 - 销毁完成",
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

</style>
