<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="游戏" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="游戏期号" prop="gameDate">
        <el-input
          v-model="queryParams.gameDate"
          placeholder="请输入游戏期号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主播ID" prop="liveUserId">
        <el-input
          v-model="queryParams.liveUserId"
          placeholder="请输入主播ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直播房间号" prop="liveRoomId">
        <el-input
          v-model="queryParams.liveRoomId"
          placeholder="请输入直播房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否直播间投注" prop="isLive">
        <el-select v-model="queryParams.isLive" placeholder="请选择是否直播间投注" clearable>
          <el-option
            v-for="dict in dict.type.app_betting_is_live"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="投注类型" prop="bettingType">
        <el-select v-model="queryParams.bettingType" placeholder="请选择投注类型" clearable>
          <el-option
            v-for="dict in dict.type.app_game_betting_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否中奖" prop="isWinning">
        <el-select v-model="queryParams.isWinning" placeholder="请选择是否中奖" clearable>
          <el-option
            v-for="dict in dict.type.app_game_betting_winning"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="投注时间" prop="daterange">
        <el-date-picker
          v-model="daterange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChage"
        ></el-date-picker>
      </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" style="padding-left: 2rem;font-size: 15px">
      <el-col :span="1.5">
        <div>今日余额投注: {{staticsWeb.toDayAmount}}</div>
        <div>今日金币投注: {{staticsWeb.toDayCoinAmount}}</div>
        <div>余额总投注: {{staticsWeb.totalAmount}}</div>
        <div>金币总投注: {{staticsWeb.totalCoinAmount}}</div>
      </el-col>
    </el-row>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:betting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:betting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:betting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:betting:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

<!--    <el-table v-loading="loading" :data="bettingList" @selection-change="handleSelectionChange">-->
    <el-table v-loading="loading" :data="bettingList" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="用户ID" align="center" prop="userId" width="100"/>
      <el-table-column label="游戏" align="center" prop="gameName" width="100">
<!--        <template slot-scope="scope">
          <span>{{ getTypeName(scope.row.gameName) }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="投注详情（选项:金额）" align="center"  width="200">
        <template slot-scope="scope">
            {{getBettingInfo(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="本期总投注金额" align="center"  width="120">
        <template slot-scope="scope">
            {{getBettingAmount(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="开奖获得" align="center"  width="120">
        <template slot-scope="scope">
            {{getBettingWinAmount(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="本局盈亏" align="center"  width="120">
        <template slot-scope="scope">
            {{getBettingWinLoseAmount(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="是否开奖" align="center"  width="120">
        <template slot-scope="scope">
            {{scope.row.isLottery==0?'否':'是'}}
        </template>
      </el-table-column>
      <el-table-column label="游戏期号" align="center" prop="gameDate" width="130"/>
      <el-table-column label="开奖项" align="center" prop="gameLotterySucc" width="130">
        <template slot-scope="scope">
          {{getItemName(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="投注类型" align="center" prop="bettingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_game_betting_type" :value="scope.row.bettingType"/>
        </template>
      </el-table-column>
      <el-table-column label="开奖时间" align="center" prop="gameRecordDate" width="130"/>
      <el-table-column label="投注时间" align="center" prop="bettingDate" width="100"/>
<!--      <el-table-column label="是否直播间投注 " align="center" prop="isLive">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_betting_is_live" :value="scope.row.isLive"/>
        </template>
      </el-table-column>
      <el-table-column label="投注类型" align="center" prop="bettingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_game_betting_type" :value="scope.row.bettingType"/>
        </template>
      </el-table-column>
      <el-table-column label="投注选项" align="center" prop="bettingItem">
        <template slot-scope="scope">
          {{getItemName(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="投注选项倍数" align="center" prop="bettingMultiple" />
      <el-table-column label="投注金额" align="center" prop="bettingAmount" />
      <el-table-column label="投注佣金" align="center" prop="bettingCommission" />
      <el-table-column label="是否中奖" align="center" prop="isWinning">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_game_betting_winning" :value="scope.row.isWinning"/>
        </template>
      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:betting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:betting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

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

    <!-- 添加或修改用户下注记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游戏类型ID" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入游戏类型ID" />
        </el-form-item>
        <el-form-item label="游戏ID" prop="gameId">
          <el-input v-model="form.gameId" placeholder="请输入游戏ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="游戏期号" prop="gameDate">
          <el-input v-model="form.gameDate" placeholder="请输入游戏期号" />
        </el-form-item>
        <el-form-item label="主播ID" prop="liveUserId">
          <el-input v-model="form.liveUserId" placeholder="请输入主播ID" />
        </el-form-item>
        <el-form-item label="直播房间号" prop="liveRoomId">
          <el-input v-model="form.liveRoomId" placeholder="请输入直播房间号" />
        </el-form-item>
        <el-form-item label="投注金额ID" prop="moneyId">
          <el-input v-model="form.moneyId" placeholder="请输入投注金额ID" />
        </el-form-item>
        <el-form-item label="是否直播间投注 0：否 1：是" prop="isLive">
          <el-select v-model="form.isLive" placeholder="请选择是否直播间投注 0：否 1：是">
            <el-option
              v-for="dict in dict.type.app_betting_is_live"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="投注类型：0：余额 1：金币" prop="bettingType">
          <el-select v-model="form.bettingType" placeholder="请选择投注类型：0：余额 1：金币">
            <el-option
              v-for="dict in dict.type.app_game_betting_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="投注选项倍数" prop="bettingMultiple">
          <el-input v-model="form.bettingMultiple" placeholder="请输入投注选项倍数" />
        </el-form-item>
        <el-form-item label="投注金额" prop="bettingAmount">
          <el-input v-model="form.bettingAmount" placeholder="请输入投注金额" />
        </el-form-item>
        <el-form-item label="投注佣金" prop="bettingCommission">
          <el-input v-model="form.bettingCommission" placeholder="请输入投注佣金" />
        </el-form-item>
        <el-form-item label="是否中奖 0 未开奖 1 是 2 否" prop="isWinning">
          <el-select v-model="form.isWinning" placeholder="请选择是否中奖 0 未开奖 1 是 2 否">
            <el-option
              v-for="dict in dict.type.app_game_betting_winning"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBetting, getBetting, delBetting, addBetting, updateBetting,staticsWeb } from "@/api/business/betting";
import {allGameList } from "@/api/business/game_item";
import {allList} from "@/api/business/lottery";
import {listHistoryWeb} from "../../../api/business/betting";

export default {
  name: "Betting",
  dicts: ['app_betting_is_live', 'app_game_betting_winning', 'app_game_betting_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户下注记录表格数据
      bettingList: [],
      staticsWeb:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      typeMap:{},
      typeList:[],
      gameItemMap:{},
      gameItemList:[],
      gameItemShowList:[],
      daterange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: null,
        gameId: null,
        userId: null,
        gameDate: null,
        liveUserId: null,
        liveRoomId: null,
        moneyId: null,
        isLive: null,
        bettingType: null,
        bettingGameType: null,
        bettingItem: null,
        bettingMultiple: null,
        bettingAmount: null,
        bettingCommission: null,
        isWinning: null,
        beginTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "游戏类型ID不能为空", trigger: "blur" }
        ],
        gameId: [
          { required: true, message: "游戏ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    var that = this;
    allList().then(response => {
      if(response.data){
        for(var i in response.data){
          var item = response.data[i];
          that.gameItemMap[item.itemLocation.toString() + "_" + item.gameId] = item.itemName;
          that.gameItemList.push({
            value:item.itemLocation+"_"+item.gameId,
            label:item.itemName,
            gameId:item.gameId
          })
        }
      }
    });
    allGameList().then(response => {
      if(response.data){
        for(var i in response.data){
          var item = response.data[i];
          that.typeMap[item.id.toString()] = item.name;
          that.typeList.push({
            value:item.id,
            label:item.name
          })
        }
      }
      that.getList();

    });
  },
  methods: {
    getBettingInfo(row){
      let detailsCountVOList = row.detailsCountVOList;
      if(!detailsCountVOList){
        return "-";
      }
      let res = "";
      let map = {};
      for(var i in detailsCountVOList){
        let item = detailsCountVOList[i];
        let amount = map[item.bettingTtem];
        if(!amount){
          amount = 0;
        }
        amount += item.orderAmount;
        map[item.bettingTtem] = amount;
      }
      for(var i in map){
        if(res.length > 0){
          res += " | "
        }
        // res += this.gameItemMap[i+"_"+row.gameId] + ":" + map[i];
        res += (i*1)+1 + ":" + map[i];
      }
      return res;
    },
    getBettingAmount(row){
      let detailsCountVOList = row.detailsCountVOList;
      if(!detailsCountVOList){
        return "-";
      }
      let amount = 0;
      for(var i in detailsCountVOList){
        let item = detailsCountVOList[i];
        amount += parseInt(item.orderAmount);
      }
      return amount;
    },
    getBettingWinAmount(row){
      let detailsCountVOList = row.detailsCountVOList;
      if(!detailsCountVOList){
        return "-";
      }
      let amount = 0;
      for(var i in detailsCountVOList){
        let item = detailsCountVOList[i];
        amount += parseInt(item.winAmount);
      }
      return amount;
    },
    getBettingWinLoseAmount(row){
      let detailsCountVOList = row.detailsCountVOList;
      if(!detailsCountVOList){
        return "-";
      }
      let amount = 0;
      for(var i in detailsCountVOList){
        let item = detailsCountVOList[i];
        amount += (parseInt(item.winAmount) - (parseInt(item.orderAmount) - parseInt(item.loseAmount))) - parseInt(item.loseAmount);
      }
      return amount;
    },
    dateChage(val){
      if(undefined != val && null != val && val.length > 1){
        this.queryParams.beginTime = val[0];
        this.queryParams.endTime = val[1];
      }else{
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
    },
    getItemName(row){
      let that = this;
      let bettingGameType = row.bettingGameType;
      let bettingItem = row.gameLotterySucc;
      if(!bettingItem){
        return "";
      }
      let gameId = row.gameId;
      /*if("0" == bettingGameType){
        // return that.gameItemMap[bettingItem+"_"+gameId];
        return bettingItem;
      }else{*/
        let name = "";
        let arr = bettingItem.split(",")
        for(var i in arr){
          let item = arr[i];
          if("" != name){
            name = name + "|";
          }
          // name = name + that.gameItemMap[item+"_"+gameId];
          name = name + (item*1 +1);
        }
        return name;
      // }
    },
    getTypeName(id) {
      return this.typeMap[id.toString()];
    },
    /** 查询用户下注记录列表 */
    getList() {
      this.loading = true;
      listHistoryWeb(this.queryParams).then(response => {
        this.bettingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      staticsWeb(this.queryParams).then(response => {
        this.staticsWeb = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classId: null,
        gameId: null,
        userId: null,
        gameDate: null,
        liveUserId: null,
        liveRoomId: null,
        moneyId: null,
        isLive: null,
        bettingType: null,
        bettingGameType: null,
        bettingItem: null,
        bettingMultiple: null,
        bettingAmount: null,
        bettingCommission: null,
        isWinning: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.daterange = [];
      this.queryParams.beginTime = null;
      this.queryParams.endTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户下注记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBetting(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户下注记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBetting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBetting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户下注记录编号为"' + ids + '"的数据项？').then(function() {
        return delBetting(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/betting/export', {
        ...this.queryParams
      }, `betting_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
