<template>
  <section class="content">
    <div class="box box-primary">
      <div class="box-body">
        <section class="content-header">
          <h1 id="itemTitle"></h1>
        </section>
        <form id="form2" action="" method="post" target="_blank">
          <input type="hidden" name="erbanNo" id="exportErbanNo" />
          <input type="hidden" name="channel" id="exportChannel" />
          <input type="hidden" name="startTime" id="exportStartTime" />
          <input type="hidden" name="endTime" id="exportEndTime" />
          <input type="hidden" name="status" id="exportStatus" />
          <input type="hidden" name="newUser" id="exportNewUser" />
          <input type="hidden" name="appChannel" id="exportAppChannel" />
        </form>
        <section class="content">
          <div id="toolbar">
            <form class="form-inline">
              <div class="form-group">
                <label for="qErbanNo">平台号：</label>
                <input
                  type="text"
                  class="form-control"
                  name="erbanNo"
                  id="qErbanNo"
                />
              </div>
              <div class="form-group">
                <label for="qChannel">渠道:</label>
                <input
                  type="text"
                  class="form-control"
                  name="channel"
                  id="qChannel"
                />
              </div>
              <div class="form-group">
                <label for="qChannel">时间:</label>
                <input
                  type="text"
                  class="form-control datetime"
                  name="startTime"
                  id="qStartTime"
                />
                <label for="qChannel">-</label>
                <input
                  type="text"
                  class="form-control datetime"
                  name="endTime"
                  id="qEndTime"
                />
              </div>
              <div class="form-group">
                <label for="qStatus">状态:</label>
                <select class="form-control" name="status" id="qStatus">
                  <option value="">全部</option>
                  <option value="1">发起充值</option>
                  <option value="2">充值成功</option>
                </select>
              </div>
              <div class="form-group">
                <label for="qNewUser">新用户:</label>
                <select class="form-control" name="newUser" id="qNewUser">
                  <option value="">全部</option>
                  <option value="1">是</option>
                  <option value="0">否</option>
                </select>
              </div>
              <div class="form-group">
                <label for="qChannel">app渠道</label>
                <input
                  type="text"
                  class="form-control"
                  name="appChannel"
                  id="qAppChannel"
                />
              </div>
            </form>
            <div id="mapObjHtml"></div>
            <button id="btnSearch" class="btn btn-sm btn-primary">查询</button>
            <button id="btnExport" class="btn btn-sm btn-primary">导出</button>
          </div>
          <!-- .content -->
          <div class="content" id="table"></div>
        </section>
      </div>
    </div>
  </section>
</template>

<script>
import TableHelper from "@/utils/bootstrap-table-helper";
export default {
  name: "ChargeRecordView",
  created() {
    this.$nextTick(function () {
      this.initData();
    });
  },
  methods: {
    initData() {
      $(".datetime").datetimepicker({
        format: "yyyy-mm-dd hh:ii:00",
        autoclose: true,
      });

      // 初始化表格，但不设置 `ajax` 方法，也不绑定 `url`，只提供列配置
      $("#table").bootstrapTable({
        columns: [
          { field: "erbanNo", title: "平台号", align: "center", width: "5%" },
          { field: "uid", title: "uid", align: "center", width: "5%" },
          { field: "nick", title: "昵称", align: "center", width: "5%" },
          {
            field: "partitionName",
            title: "分区",
            align: "center",
            width: "5%",
          },
          {
            field: "createTime",
            title: "创建时间",
            align: "center",
            valign: "middle",
            width: "10%",
          },
          { field: "channel", title: "渠道", align: "center", width: "5%" },
          {
            field: "localCurrencyCode",
            title: "当地货币代码",
            align: "center",
            width: "5%",
          },
          {
            field: "localAmount",
            title: "当地金额",
            align: "center",
            width: "5%",
          },
          {
            field: "chargeRecordId",
            title: "商户订单号",
            align: "center",
            width: "5%",
          },
          {
            field: "pingxxChargeId",
            title: "第三方订单号",
            align: "center",
            width: "5%",
          },
          {
            field: "status",
            title: "状态",
            align: "center",
            width: "5%",
            formatter: function (val, row, index) {
              return val == 2 ? "充值成功" : "发起充值";
            },
          },
          {
            field: "newUser",
            title: "是否新用户",
            align: "center",
            width: "5%",
            formatter: function (val, row, index) {
              return val ? "是" : "否";
            },
          },
          {
            field: "appChannel",
            title: "app渠道",
            align: "center",
            width: "5%",
          },
        ],
        undefinedText: "-",
        cache: false,
        striped: true,
        showRefresh: false,
        pageSize: 20,
        pagination: true,
        pageList: [20, 50, 100, 200, 300, 500],
        search: false,
        sidePagination: "server",
        queryParamsType: "undefined",
        data: [], // 初始化为空数据
      });

      // 查询刷新
      $("#btnSearch").on("click", function () {
        const params = {
          pageNumber: 1,
          pageSize: 20,
          erbanNo: $("#qErbanNo").val(),
          channel: $("#qChannel").val(),
          startTime: $("#qStartTime").val(),
          endTime: $("#qEndTime").val(),
          status: $("#qStatus").val(),
          newUser: $("#qNewUser").val(),
          appChannel: $("#qAppChannel").val(),
        };

        // 手动加载数据
        $.ajax({
          type: "GET",
          url: "/admin/chargeRecord/list",
          contentType: "application/json;charset=utf-8",
          dataType: "json",
          data: params,
          success: function (res) {
            if (apiResult(res)) {
              $("#table").bootstrapTable("load", {
                rows: res.data.rows,
                total: res.data.total,
              });

              $("#mapObjHtml span").remove();
              const totalMap = res.data.totalMap;
              let str = "";
              const sortedKeys = Object.keys(totalMap).sort((keyA, keyB) => {
                return keyA === "总充值{USD}"
                  ? -1
                  : keyB === "总充值{USD}"
                  ? 1
                  : 0;
              });
              for (const key of sortedKeys) {
                str += `
              <span style="margin-right:10px;display: inline-block;font-size: 18px;color: #2049a9;" type="text">${key}: ${totalMap[key]}, </span>
            `;
              }
              $("#mapObjHtml").append(str);
            }
          },
          error: function (req) {
            serverError(req);
          },
        });
      });

      function serverError(req) {
        $("#tipMsg").text(req.responseJSON.message);
        $("#tipModal").modal("show");
      }

      function apiResult(json) {
        if (json.code == 200 && json.message == "success") {
          return true;
        }
        $("#tipMsg").text("请求失败，错误信息：" + json.message);
        $("#tipModal").modal("show");
        return false;
      }

      // 导出excel
      $("#btnExport").on("click", function () {
        const form = $("#form2");
        form.attr("action", "/admin/chargeRecord/exportChargeDetail");
        $("#exportErbanNo").val($("#qErbanNo").val());
        $("#exportChannel").val($("#qChannel").val());
        $("#exportStartTime").val($("#qStartTime").val());
        $("#exportEndTime").val($("#qEndTime").val());
        $("#exportStatus").val($("#qStatus").val());
        $("#exportNewUser").val($("#qNewUser").val());
        $("#exportAppChannel").val($("#qAppChannel").val());

        form.submit();
      });
    },
  },
};
</script>

<style scoped></style>