<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 v-model="form.winning_numbers" autocomplete="off" style="width: 280px"></el-input>
          </el-form-item>
          <el-form-item label="图片" :rules="[{ required: true, message: '请选择图片' }]">
            <UploadOneImage v-model="form.image"></UploadOneImage>
          </el-form-item>
          <el-form-item label="组合" :rules="[{ required: true, message: '请输入组合' }]">
            <el-input v-model="form.combo" 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 { issuefailure, issuelotteryDraw } from '@/api/data'
import UploadOneImage from '@/components/UploadOneImage/index.vue'

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

  data() {
    return {
      loading: false,
      visibleBasicDrawer: false,
      renderContent: '',
      queryData: {},
      form: {},
      searchForm: [
        {
          label: '期号',
          type: 'input',
          prop: 'issue_no',
          clearable: false
        }
        // {
        //   label: '状态',
        //   type: 'select',
        //   prop: 'status',
        //   clearable: false,
        //   options: options
        // }
      ],
      columns: [
        {
          label: '期号',
          prop: 'issue_no'
        },
        {
          label: '组合',
          prop: 'combo'
        },
        {
          label: '状态',
          prop: 'status',
          method: (row) => options.find(item => item.value === row.status)?.label
        },
        {
          label: '开奖号吗',
          prop: 'winning_numbers'
        }
      ],
      rowsActions: [
        {
          key: 'open',
          label: '开奖',
          method: (row) => {
            this.form = {
              id: row.id,
              image: '',
              winning_numbers: '',
              combo: ''
            }
            this.$refs.dialog.open()
          }
        },
        {
          key: 'err',
          label: '开奖失败',
          method: (row) => {
            this.$confirm('此操作将设为开奖失败, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              issuefailure({ id: row.id })
                .then((res) => {
                  this.$message({
                    type: 'success',
                    message: '设置成功!'
                  })
                  this.onSubmit(this.queryData)
                })

            })
          }
        }
      ],
      apis: {
        list: {
          url: 'issue/abnormal',
          dataParam: 'data.data',
          resultParam: 'data',
          init: false
        },
        add: {
          url: 'issue/submit'
        },
        edit: {
          url: 'issue/submit'
        },
        delete: {
          url: 'issue/failure',
          method: 'post'
        }
      },
      tableOptions: {
        add: {
          show: false
        }
      },
      editForm: [
        {
          body: [
            {
              label: '期号',
              type: 'input',
              prop: 'issue_no',
              clearable: false,
              rules: [
                {
                  required: true,
                  message: '期号',
                  trigger: 'change'
                }
              ]
            }
          ]
        }
      ],
      editConfig: {
        size: 'small'
      }
    }
  },
  created() {
  },
  mounted() {
    this.onSubmit()
  },
  methods: {
    handleSubmit(formData) {
      console.log('提交数据:', formData)
      // 调用API提交数据
    },
    confirm() {
      issuelotteryDraw(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>
