<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item label="游戏" field="game_id">
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="日期" field="pay_date">
            <a-range-picker v-model="searchForm.pay_date" :show-time="false" mode="date" style="width: 100%" />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="账号" field="user_name">
            <a-input v-model="searchForm.user_name" placeholder="请输入账号" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="角色ID" field="role_id">
            <a-input v-model="searchForm.role_id" placeholder="请输入角色ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="订单号" field="order_id">
            <a-input v-model="searchForm.order_id" placeholder="请输入订单号" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="交易订单号" field="trade_id">
            <a-input v-model="searchForm.trade_id" placeholder="请输入交易订单号" allow-clear />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #game_id="{ record }"> [{{ record.game_id }}] {{ record.game_name }} </template>
      <template #sync_status="{ record }">
        <!-- 1成功 2失败 3处理中 -->
        <a-tag :color="record.sync_status === 1 ? 'green' : record.sync_status === 2 ? 'red' : 'blue'">
          {{ record.sync_status === 1 ? '成功' : record.sync_status === 2 ? '失败' : '异常回调' }}
        </a-tag>
      </template>
      <template #send_status="{ record }">
        <a-tag :color="record.send_status === 1 ? 'green' : 'red'">
          {{ record.send_status === 1 ? '成功' : '失败' }}
        </a-tag>
      </template>
      <template #operationCell="{ record }">
        <a-button
          type="primary"
          v-if="record?.send_status === 0 && record?.sync_status === 1"
          @click="handleSend(record)"
          >补发</a-button
        >
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../../api/customer/sdkOrder'
import GameSelect from '@/components/game-select/index.vue'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  order_id: '',
  trade_id: '',
  game_id: '',
  user_name: '',
  role_id: '',
  pay_date: [],
})

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  showSummary: true,
  summary: [
    {
      text: '总金额',
      suffixText: '元',

      action: 'totalRow',
      dataIndex: 'money',
    },
  ],

  add: {
    show: false,
    auth: ['/v1/customer/SdkOrder/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: false,
    auth: ['/v1/customer/SdkOrder/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: false,
    auth: ['/v1/customer/SdkOrder/destroy'],
    func: async (params) => {
      const resp = await api.destroy(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '充值账号', dataIndex: 'user_name', width: 180 },
  { title: '充值IP', dataIndex: 'user_ip', width: 180 },
  { title: '订单号', dataIndex: 'order_id', width: 180 },
  { title: '交易订单号', dataIndex: 'trade_id', width: 180 },
  { title: '研发订单号', dataIndex: 'cp_order_id', width: 180 },
  { title: '支付时间', dataIndex: 'pay_date', width: 180 },
  {
    title: '金额',
    dataIndex: 'money',
    width: 180,
  },
  { title: '充值游戏', dataIndex: 'game_id', width: 180 },

  { title: '充值区服', dataIndex: 'server_name', width: 180 },
  { title: '角色ID', dataIndex: 'role_id', width: 180 },
  { title: '角色名', dataIndex: 'role_name', width: 180 },

  { title: '支付状态', dataIndex: 'sync_status', width: 180 },
  { title: '发货状态', dataIndex: 'send_status', width: 180 },

  { title: '充值方式', dataIndex: 'pay_channel_name', width: 180 },

  { title: '支付返回值', dataIndex: 'sync_result', width: 180 },
  { title: '回调信息', dataIndex: 'sync_data', width: 180 },

  { title: 'SDK版本', dataIndex: 'sdk_version', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// 补发
const handleSend = async (record) => {
  console.log(record)
  Modal.info({
    title: '补发',
    content: '确定要补发吗？',
    okText: '确定',
    cancelText: '取消',
    onOk: async () => {
      const resp = await api.send(record.order_id)
      if (resp.code === 200) {
        Message.success(`操作成功，请稍后刷新`)
        crudRef.value?.refresh()
      }
    },
  })
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>

<script>
export default { name: 'v1/customer/sdkOrder' }
</script>
