<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户手机号" prop="userPhone">
        <el-input v-model="queryParams.userPhone" placeholder="请输入用户手机号" maxlength="11" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="执行时间">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="属性" prop="behavior">
        <el-select v-model="queryParams.behavior" placeholder="请选择属性" clearable size="small" style="width: 240px">
          <el-option v-for="(v,i) in behaviorList" :key="i" :label="v.label" :value="v.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="结算订单号" prop="relatedOrderNo">
        <el-input v-model="queryParams.relatedOrderNo" placeholder="请输入结算订单号" maxlength="30" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="3">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">操作金额</el-button>
      </el-col>
      <el-col :span="4">
        累计充值金额：{{totalInfo.totalRecharge||0}}元
      </el-col>
      <el-col :span="4">
        累计发放金额：{{totalInfo.totalIssued||0}}元
      </el-col>
      <el-col :span="4">
        剩余可发放金额：{{totalInfo.remainingIssuable||0}}元
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="流水号" align="center" prop="serialNumber" />
      <el-table-column label="游戏流水号" align="center" prop="gameSerialNumber">
        <template slot-scope="{row}">
          <div>
            {{row.gameSerialNumber||'--'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center" prop="userPhone">
        <template slot-scope="{row}">
          <div>
            {{row.userPhone||'--'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="属性" prop="behavior" align="center">
        <template slot-scope="{row}">
          <div>
            <div v-for="(v,i) in behaviorList" :key="i" :class="{color_red:row.behavior==0||row.behavior==1}" v-if="row.behavior==v.value">{{v.label}}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="转账状态" align="center" prop="transferState">
        <template slot-scope="{row}">
          <div v-for="(v,i) in transferStateList" :key="i" v-if="row.transferState==v.value">{{v.label}}</div>
        </template>
      </el-table-column>
      <el-table-column label="结算订单号" prop="relatedOrderNo" align="center">
        <template slot-scope="{row}">
          <div>
            {{row.relatedOrderNo||'--'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改测试单表对话框 -->
    <el-drawer title="操作金额" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="操作类型" prop="behavior">
          <el-radio-group v-model="form.behavior">
            <el-radio :label="1">增加</el-radio>
            <el-radio :label="0">减少</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="操作金额" prop="amount">
          <el-input-number v-model="form.amount" controls-position="right" :min="0" :precision="2"></el-input-number>
        </el-form-item>
        <span class="hint">注意：需要先在微信商户平台“运营账户”完成充值后操作，否则充值无效</span>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { rechargeRedPacketAdd, rechargeRedPacketPage, rechargeTotalInfo } from "@/api/game.js";
import { getToken } from "@/utils/auth";

export default {
  dicts: ['ad_location'],
  components: {
  },
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "添加广告",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/demo/demo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userPhone: undefined,
        behavior: undefined,
        relatedOrderNo: undefined
      },
      // 表单参数
      form: {
        behavior: 1,
        amount: undefined
      },
      // 表单校验
      rules: {
        behavior: [
          { required: true, message: "请选择操作类型", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "请输入操作金额", trigger: "blur" }
        ],
      },
      behaviorList: [
        { label: "减少", value: 0 },
        { label: "增加", value: 1 },
        // { label: "利润结算", value: 2 },
        // { label: "翻倍奖励", value: 3 },
        { label: "红包奖励", value: 4 },
      ],
      totalInfo: {},
      transferStateList: [
        { label: "转账已受理", value: 'ACCEPTED' },
        { label: "转账锁定资金中", value: 'PROCESSING' },
        { label: "待用户确认", value: 'WAIT_USER_CONFIRM' },
        { label: "转账中", value: 'TRANSFERING' },
        { label: "转账成功", value: 'SUCCESS' },
        { label: "转账失败", value: 'FAIL' },
        { label: "商户撤销请求受理成功", value: 'CANCELING' },
        { label: "转账撤销完成", value: 'CANCELLED' },
      ]
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList () {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      rechargeRedPacketPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.getRechargeTotalInfo()
        this.loading = false;
      });
    },
    getRechargeTotalInfo () {
      rechargeTotalInfo().then(response => {
        this.totalInfo = response.data || {};
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        behavior: 1,
        amount: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handlePage () {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.adId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          rechargeRedPacketAdd(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
  }
};
</script>
<style lang="scss">
.el-drawer__body {
  padding: 0 20px !important;
}
</style>
<style lang="scss" scoped>
.flex {
  display: flex;
}
.demo-drawer__footer {
  margin-left: 120px;
}
.color_red {
  color: #ff1313;
}
.hint {
  display: inline-block;
  margin-left: 10px;
  color: #808080;
  font-size: 14px;
  display: inline-block;
  margin-bottom: 30px;
}
</style>
