<template>
  <div class="ma-content-block">
    <sa-table
      ref="crudRef"
      :options="options"
      :columns="columns"
      :searchForm="searchForm"
    >
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="6" :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="6" :xs="24">
          <a-form-item label="日期" field="pay_time">
            <a-range-picker
              v-model="searchForm.pay_time"
              :show-time="false"
              mode="date"
              style="width: 100%"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :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="6" :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="6" :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="6" :xs="24">
          <a-form-item label="交易订单号" field="trade_id">
            <a-input
              v-model="searchForm.trade_id"
              placeholder="请输入交易订单号"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="支付状态" field="sync_status">
            <a-select
              clearable
              v-model="searchForm.sync_status"
              placeholder="请选择支付状态"
            >
              <a-option value="1">已支付</a-option>
              <a-option value="2">异常回调</a-option>
              <a-option value="0">未支付</a-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="发货状态" field="send_status">
            <a-select
              clearable
              v-model="searchForm.send_status"
              placeholder="请选择发货状态"
            >
              <a-option value="1">已发货</a-option>
              <a-option value="0">未发货</a-option>
            </a-select>
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #game_id="{ record }">
        [{{ record.game_id }}] {{ record.game_name }}
      </template>
      <template #sync_status="{ record }">
        <!-- 支付回调状态 0未支付, 1已支付, 2异常回调 -->
        <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 }">
        <!-- 发货状态 0未发货, 1已发货 -->
        <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_time: [],
});

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  showSummary: true,
  summary: [
    {
      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: 120 },
  { title: "充值IP", dataIndex: "ip", width: 130 },
  { title: "订单号", dataIndex: "order_id", width: 180 },
  { title: "交易订单号", dataIndex: "trade_id", width: 180 },
  { title: "研发订单号", dataIndex: "cp_order_id", width: 180 },
  { title: "支付时间", dataIndex: "pay_date", width: 140 },
  {
    title: "金额",
    dataIndex: "money",
    width: 100,
  },
  { title: "充值游戏", dataIndex: "game_id", width: 120 },

  { title: "充值区服", dataIndex: "server_name", width: 120 },
  { title: "角色ID", dataIndex: "role_id", width: 60 },
  { title: "角色名", dataIndex: "role_name", width: 100 },

  { title: "支付状态", dataIndex: "sync_status", width: 80 },
  { title: "发货状态", dataIndex: "send_status", width: 80 },

  { title: "充值方式", dataIndex: "pay_channel_name", width: 120 },

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

  { title: "SDK版本", dataIndex: "sdk_version", width: 80 },
]);

// 页面数据初始化
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();
});
</script>

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