<template>
  <div class="new-app-container layout-vertical">
    <YMTableSearch :loading="loading" :search-form="searchForm" @onValueChange="onValueChange" @submit="onSubmit"/>
    <YMTable
      ref="table"
      :table-key="1"
      :index="true"
      :selection="false"
      :table-options="tableOptions"
      :edit-form="editForm"
      :edit-config="editConfig"
      :columns="columns"
      :rows-actions="rowsActions"
      :show-summary="false"
      :apis="apis"
      :loading="loading"
      :custom-style="{ height: 'calc(100vh - 150px)' }"
    />
    <!-- 自定义弹窗 -->
    <Dialog ref="dialog" :dialogActions="{width:'480px'}" @confirm="confirm">
      <template slot="content">
        <el-form label-width="80px">
          <el-form-item label="预开奖" :rules="[{ required: true, message: '请输入预开奖' }]">
            <el-input placeholder='开奖号码取值范围：0-27' v-model="form.he" autocomplete="off" style="width: 280px"></el-input>
          </el-form-item>
        </el-form>
      </template>
    </Dialog>
  </div>
</template>
<script>
import Dialog from '@/components/Dialog/index.vue'
import { pcIssuepreDraw } from '@/api/data'

const options = [
  {
    value: 0,
    label: '草稿'
  },
  {
    value: 1,
    label: '投注中'
  },
  {
    value: 2,
    label: '封盘'
  },
  {
    value: 3,
    label: '开奖'
  }
]
export default {
  components: {
    Dialog
  },

  data() {
    return {
      loading: false,
      visibleBasicDrawer: false,
      renderContent: '',
      queryData: {},
      form: {},
      searchForm: [
        {
          label: '期号',
          type: 'input',
          prop: 'issue_no',
          clearable: false
        }
      ],
      columns: [
        {
          label: '期号',
          prop: 'issue_no',
          link: true,
          click: (row) => {
            this.$router.push({
              name: 'order',
              query: {
                issue_no: row.issue_no // 假设row有id字段，传递订单id
              }
            })
          }
        },
        {
          label: '状态',
          prop: 'status',
          method: (row) => options.find(item => item.value === row.status)?.label
        },
        {
          label: '组合',
          prop: 'combo'
        },
        {
          label: '结果',
          method: (row) => {
            const arr = row?.winning_numbers?.split(',') ?? [];
            if (!arr.length) return '--';
            const sum = arr.reduce((a, b) => Number(a) + Number(b), 0);
            const expression = arr.join('+');

            return `${expression}=${sum}`;
          }
        },
        {
          label: '预开奖号码',
          prop: 'advance_code',
          method:(row)=> row.advance_code===0?'0':row.advance_code||'--'
        },
        {
          label: '开奖时间',
          prop: 'end_time'
        }
      ],
      rowsActions: [
        {
          key: 'open',
          label: '预开奖',
          vIF: (row) => row.status === 1,
          method: (row) => {
            this.form = {
              issue_no: row.issue_no,
              he: ''
            }
            this.$refs.dialog.open()
          }
        }
      ],
      apis: {
        list: {
          url: 'pcIssue',
          dataParam: 'data.data',
          resultParam: 'data',
          init: false
        }
      },
      tableOptions: {
        add: {
          show: false
        }
      },
      editForm: [
        {
          body: []
        }
      ],
      editConfig: {
        size: 'small'
      }
    }
  },
  created() {
  },
  mounted() {
    this.onSubmit()
  },
  methods: {
    handleSubmit(formData) {
      console.log('提交数据:', formData)
      // 调用API提交数据
    },
    confirm() {
      pcIssuepreDraw(this.form)
        .then((res) => {
          if (res.code === 0) {
            this.$message.success('设置成功')
            this.onSubmit(this.queryData)
            this.$refs.dialog.close()
          }
        }).catch(() => {
        this.$message.error('设置失败')
      })
    },
    onValueChange(val) {

    },
    onSubmit(params) {
      this.queryData = params
      this.$refs.table.getList(params)
    }
  }
}
</script>
