<template>
  <div class="app-container">
    <!-- 顶部搜索和操作区域 -->
    <div class="filter-container">
      <el-input 
        v-model="listQuery.odd_name" 
        placeholder="请输入玩法名称" 
        style="width: 250px;" 
        class="filter-item" 
        clearable 
        @keyup.enter.native="handleFilter" 
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button 
        class="filter-item" 
        type="success" 
        icon="el-icon-setting" 
        :disabled="multipleSelection.length === 0" 
        @click="handleBatchSet"
      >
        批量设置
      </el-button>
    </div>

    <!-- 列表数据表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      height="calc(100% - 120px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" prop="id" width="80" align="center" />
      <el-table-column label="玩法ID" prop="odd_id" width="80" align="center" /> -->
       <el-table-column label="玩法名称" prop="odd_name_en" min-width="180" align="center" />
      <el-table-column label="中文名称" prop="odd_name" min-width="150" align="center" />
     
      <!-- <el-table-column label="方法名称" prop="function_name" min-width="180" align="center" /> -->
        <el-table-column label="最小值" prop="mininum" width="150" align="center" />
      <el-table-column label="最大值" prop="maxinum" width="150" align="center" />
     
      
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleSet(row)">
            设置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 设置弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="450px" @close="resetForm">
      <el-form ref="dataForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="最小值" prop="mininum">
          <el-input-number v-model="form.mininum" :min="0" :precision="0" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="最大值" prop="maxinum">
          <el-input-number v-model="form.maxinum" :min="0" :precision="0" style="width: 200px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="submitLoading" @click="submitSet">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sportOddsList, sportSetOdds } from '@/api/data'

export default {
  name: 'FootballOddsManage',
  data() {
    return {
      // 列表数据
      list: [],
      total: 0,
      listLoading: false,
      
      // 查询参数
      listQuery: {
        page: 1,
        limit: 20,
        odd_name: ''
      },
      
      // 表格多选记录
      multipleSelection: [],
      
      // 弹窗相关
      dialogVisible: false,
      dialogTitle: '设置玩法',
      submitLoading: false,
      form: {
        id: null,
        maxinum: 0,
        mininum: 0
      },
      rules: {
        mininum: [{ required: true, message: '请设置最小值', trigger: 'blur' }],
        maxinum: [{ required: true, message: '请设置最大值', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      sportOddsList(this.listQuery).then(response => {
        if (response.code === 0) {
          this.list = response.data.data
          this.total = response.data.total
        } else {
          this.$message.error(response.msg || '获取数据失败')
        }
      }).catch(err => {
        console.error(err)
        this.$message.error('网络请求异常')
      }).finally(() => {
        this.listLoading = false
      })
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    // 切换每页条数
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },

    // 翻页
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },

    // 记录多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 重置表单
    resetForm() {
      this.form = {
        id: null,
        maxinum: 0,
        mininum: 0
      }
      if (this.$refs.dataForm) {
        this.$refs.dataForm.clearValidate()
      }
    },

    // 单个设置
    handleSet(row) {
      this.resetForm()
      this.form.id = row.id // 传递单个数字
      this.form.maxinum = row.maxinum
      this.form.mininum = row.mininum
      this.dialogTitle = `设置 - ${row.odd_name || row.odd_name_en}`
      this.dialogVisible = true
    },

    // 批量设置
    handleBatchSet() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请先勾选需要设置的玩法')
        return
      }
      this.resetForm()
      // 提取所有选中的id组合成数组
      this.form.id = this.multipleSelection.map(item => item.id) 
      this.dialogTitle = `批量设置 (${this.multipleSelection.length}个玩法)`
      this.dialogVisible = true
    },

    // 提交设置数据
    submitSet() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.submitLoading = true
          sportSetOdds(this.form).then(response => {
            if (response.code === 0) {
              this.$message.success('设置成功')
              this.dialogVisible = false
              this.getList() // 重新刷新列表获取最新数值
            } else {
              this.$message.error(response.msg || '设置失败')
            }
          }).catch(err => {
            console.error(err)
            this.$message.error('提交异常')
          }).finally(() => {
            this.submitLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.app-container {
    width: calc(100% - 40px);
    height: calc(100vh - 90px);
  margin: 20px;
  padding: 10px;
  border-radius: 6px;
  box-sizing: border-box;
  background: #fff;
}
.filter-container {
  padding-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>